2013-02-21 7 views
13

Ich habe eine App, in der ich ein Bild mit der Kamera und speichern Sie das Bild in die native Galerie. Aber wenn die App keine Berechtigung dafür hat, möchte ich, dass der Benutzer das weiß. Wie überprüfe ich es?Wie kann ich überprüfen, ob meine App Zugriff auf Handy-Galerie hat

By the way: Ich speichere das Bild in die Galerie mit:

UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil); 

Antwort

29

Sie müssen den Status ALAssetLibrary stellen Sie sicher, Sie haben AssetsLibrary/AssetsLibrary.h enthalten in der Datei überprüfen

ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus]; 

// den Status prüfen ALAuthorizationStatusAuthorized oder ALAuthorizationStatusDenied zB

if (status != ALAuthorizationStatusAuthorized) { 
     //show alert for asking the user to give permission 

    } 
+0

Funktioniert es auf allen iOS? Danke übrigens. – gabrjan

+0

sollte es auf ios5 + funktionieren, wenn Sie ALAssetLibrary sehen, dann würden Sie bemerken, dass es Unterstützung für ios5 + – nsgulliver

+0

eine weitere Frage, welches Framework brauche ich dafür? – gabrjan

3

Hinweis: iOS 6 Nur

Ist das, was Sie für

[ALAssetsLibrary authorizationStatus] == ALAuthorizationStatusAuthorized; 

Andere Werte suchen von authorizationStatus sind

ALAuthorizationStatusRestricted,  // This application is not authorized to access photo data. 
              // The user cannot change this application’s status, possibly due to active restrictions 
              // such as parental controls being in place. 
    ALAuthorizationStatusDenied,   // User has explicitly denied this application access to photos data. 
    ALAuthorizationStatusAuthorized   // User has authorized this application to access photos data. 
+0

warum ist das iOS6 nur? Ich brauche für alle Joss, aber das ist, was ich brauche – gabrjan

+0

authorizationStatus-Methode ist in iOS 6.0 und höher verfügbar. – msk

+0

Also was soll ich mit früheren Versionen machen? – gabrjan

3

Wenn Sie Fotos Rahmen verwenden Da ALAsset-Bibliotheken ab ios 9 veraltet sind, können Sie PHAuthorizationStatus verwenden, um den Galeriezugriff zu überprüfen. Sie müssen auch das Fotorahmen importieren.

#import <Photos/Photos.h> 

- (BOOL)hasGalleryPermission 
{ 
    BOOL hasGalleryPermission = NO; 
    PHAuthorizationStatus authorizationStatus = [PHPhotoLibrary authorizationStatus]; 

    if (authorizationStatus == PHAuthorizationStatusAuthorized) { 
     hasGalleryPermission = YES; 
    } 
    return hasGalleryPermission; 
} 
0

Swift 3

import photos 

PHPhotoLibrary.requestAuthorization { status in 
    switch status { 
    case .authorized: 
      self.processSnapShotPhotos() 
    case .restricted: 
      print("handle restricted") 
    case .denied: 
      print("handle denied")  
    default: 
     // place for .notDetermined - in this callback status is already determined so should never get here 
      break 
    } 
}