2013-05-31 39 views
15

Tôi đang làm một ứng dụng ios có nút khởi chạy máy ảnh.Làm cách nào để phát hiện xem máy ảnh có bị hạn chế bởi người dùng

Tôi muốn bật/tắt nút này nếu thiết bị có sẵn máy ảnh hay không.

Tôi muốn phát hiện xem thiết bị có máy ảnh hay không và thiết bị có máy ảnh nhưng bị hạn chế (với this) để bạn không thể sử dụng.

Làm cách nào để phát hiện hai tùy chọn này?

Cảm ơn

+0

Tại sao bạn không tự mình kiểm tra điều này? Tại sao đăng câu hỏi? – rmaddy

+0

Tôi đã thử nghiệm nó và tôi đã có thể phát hiện nếu thiết bị có máy ảnh hay không nhưng tôi không thể phát hiện khi thiết bị có một máy ảnh nhưng nó bị hạn chế, do đó bạn không thể sử dụng nó. –

+2

Xin lỗi, tôi nghĩ tôi đã hiểu sai những gì bạn đã làm. Tôi nghĩ bạn đã hỏi liệu mã đó có hoạt động không nếu máy ảnh bị hạn chế. Đó là lý do tại sao tôi đề nghị bạn chỉ cần kiểm tra nó. Hết sức tò mò, nếu bạn thiết lập một thiết bị có máy ảnh bị hạn chế, giá trị 'isSourceTypeAvailable' sẽ trả về là gì? – rmaddy

Trả lời

-6

Như đã trình bày ở những nơi khác, kiểm tra AVAuthorizationStatus sẽ không thực sự cho bạn biết nếu nó hạn chế, mặc dù sự hiện diện của một giá trị "bị hạn chế" trong enum. Thay vào đó, tôi đã tìm thấy rằng kiểm tra nếu nguồn được kích hoạt để thể hữu ích:

BOOL isCameraAvailable = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]; 

Nếu isCameraAvailableNO sau đó người dùng có camera khuyết tật có khả năng nhất trong Restrictions.Xem Detect existence of camera in iPhone app?

3

Lần đầu tiên người dùng cố gắng sử dụng máy ảnh trên ios 6 anh/cô ấy được tự động yêu cầu cho phép. Bạn không cần phải thêm mã phụ (trước đó là authorisationstatus là ALAuthorizationStatusNotDetermined).

Vì vậy, nếu người dùng từ chối lần đầu tiên bạn không thể hỏi lại.

Bạn có thể sử dụng ALAssetsLibrary để kiểm tra điều này. Kiểm tra câu trả lời cho giải pháp này: ask-permission-to-access-camera

Hy vọng nó sẽ giúp bạn.

+0

Cảm ơn, tôi sẽ thử nó –

+0

@ A.Vila đánh dấu câu trả lời này nếu nó giải quyết được vấn đề của bạn :-) –

+4

Dường như ALAssetsLibrary chỉ cho bạn biết nếu bạn có quyền truy cập vào ảnh, chứ không phải máy ảnh. –

24

Để kiểm tra trạng thái cho phép máy ảnh trong sử dụng ứng dụng đoạn mã sau.

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) 
{ 
    AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]; 

    if(status == AVAuthorizationStatusAuthorized) { 
    // authorized 
    } else if(status == AVAuthorizationStatusDenied){ 
    // denied 
    } else if(status == AVAuthorizationStatusRestricted){ 
    // restricted 
    } else if(status == AVAuthorizationStatusNotDetermined){ 
     // not determined 
     [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) { 
      if(granted){ 
      NSLog(@"Granted access"); 
      } else { 
      NSLog(@"Not granted access"); 
      } 
     }]; 
    } 
} 
+2

Đảm bảo nhập AVFoundation '@import AVFoundation;' – Antoine

+0

Điều này không hoạt động (ít nhất là trên iOS 8.4 mà tôi đã thử nghiệm) cho kịch bản được đề cập. Nếu bạn đã bật máy ảnh cho ứng dụng nhưng bị giới hạn tổng thể, nó sẽ trả về 'AVAuthorizationStatusAuthorized' không chính xác. Tôi thành thật không chắc tại sao 'AVAuthorizationStatusRestricted' tồn tại! – nickjm

+0

@nickjm cũng có thể là trường hợp. Tôi havent thử nghiệm nó cho trường hợp u đã đề cập. – pankaj

4

Để kiểm tra xem máy ảnh bị hạn chế AVAuthorizationStatus chưa đủ. Như đã nói trong tài liệu:

Trạng thái này thường không hiển thị — phương thức lớp AVCaptureDevice để khám phá thiết bị không trả lại thiết bị mà người dùng bị hạn chế truy cập.

Vì vậy, để kiểm tra thích hợp, bạn cần phải tạo ra một số thiết bị chụp, ví dụ, như tôi đã làm:

AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]; 
if (authStatus == AVAuthorizationStatusAuthorized) { 
    BOOL atLeastOne = NO; 
    NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]; 
    for (AVCaptureDevice *device in devices) { 
     if (device) { 
      atLeastOne = YES; 
     } 
    } 
    if (!atLeastOne) { 
     authStatus = AVAuthorizationStatusRestricted; 
    } 
} 
0

SWIFT

Để quyết định xem nút camera thậm chí nên được kích hoạt (hoặc ẩn) Bạn nên kiểm tra:

if UIImagePickerController.isSourceTypeAvailable(.Camera){ } 

Nhưng sau đó tôi sẽ kiểm tra để xem nếu người dùng đã cho phép truy cập vào máy ảnh như apple gợi ý trong ví dụ PhotoPicker của họ (PhotoPicker example Objective-C):

* xin lưu ý rằng bạn phải nhập AVFoundation

let authStatus = AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeVideo) 

if authStatus == AVAuthorizationStatus.denied { 
    // Denied access to camera 
    // Explain that we need camera access and how to change it. 
    let dialog = UIAlertController(title: "Unable to access the Camera", message: "To enable access, go to Settings > Privacy > Camera and turn on Camera access for this app.", preferredStyle: UIAlertControllerStyle.alert) 

    let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil) 

    dialog.addAction(okAction) 
    self.present(dialog, animated:true, completion:nil) 

} else if authStatus == AVAuthorizationStatus.notDetermined {  // The user has not yet been presented with the option to grant access to the camera hardware. 
    // Ask for it. 
    AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo, completionHandler: { (grantd) in 
    // If access was denied, we do not set the setup error message since access was just denied. 
     if grantd { 
     // Allowed access to camera, go ahead and present the UIImagePickerController. 
      self.showImagePickerForSourceType(sourceType: UIImagePickerControllerSourceType.camera) 
     } 
    }) 
} else { 

    // Allowed access to camera, go ahead and present the UIImagePickerController. 
    self.showImagePickerForSourceType(sourceType: UIImagePickerControllerSourceType.camera) 

} 

func showImagePickerForSourceType(sourceType: UIImagePickerControllerSourceType) { 

    let myPickerController = UIImagePickerController() 
    myPickerController.delegate = self; 
    myPickerController.sourceType = sourceType 
    self.present(myPickerController, animated: true, completion: nil) 
} 
Các vấn đề liên quan