2013-02-21 18 views
13

Tôi có một ứng dụng mà tôi chụp ảnh bằng máy ảnh và lưu hình ảnh đó vào thư viện gốc. Nhưng nếu ứng dụng không có quyền cho điều đó, tôi muốn người dùng biết điều đó. Vậy làm cách nào để kiểm tra?Làm cách nào để kiểm tra xem ứng dụng của tôi có quyền truy cập vào thư viện điện thoại

Bằng cách này: tôi lưu trữ hình ảnh vào thư viện với:

UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil); 

Trả lời

29

Bạn cần phải kiểm tra tình trạng của ALAssetLibrary chắc chắn rằng bạn có AssetsLibrary/AssetsLibrary.h bao gồm trong tập tin của bạn

ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus]; 

// kiểm tra tình trạng cho ALAuthorizationStatusAuthorized hoặc ALAuthorizationStatusDenied ví dụ

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

    } 
+0

Tính năng này có hoạt động trên tất cả iOS không? Cảm ơn btw. – gabrjan

+0

nó sẽ hoạt động trên ios5 +, nếu bạn thấy ALAssetLibrary thì bạn sẽ nhận thấy rằng nó có hỗ trợ cho ios5 + – nsgulliver

+0

một câu hỏi nữa mà tôi cần cho khung làm việc đó? – gabrjan

3

Lưu ý: iOS 6 Chỉ

Đây có phải là những gì bạn đang tìm kiếm

[ALAssetsLibrary authorizationStatus] == ALAuthorizationStatusAuthorized; 

giá trị khác authorizationStatus là

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

tại sao chỉ iOS6 này? Tôi cần cho tất cả joss, nhưng đó là những gì tôi cần – gabrjan

+0

authorizationStatus phương pháp có sẵn trong iOS 6.0 và sau đó. – msk

+0

Vì vậy, tôi phải làm gì với các phiên bản trước? – gabrjan

3

Nếu bạn đang sử dụng hình ảnh khuôn khổ vì các thư viện ALAsset không được chấp nhận từ ios 9, bạn có thể sử dụng PHAuthorizationStatus để kiểm tra truy cập thư viện. Bạn cũng cần nhập khung ảnh.

#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 
    } 
}