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)
}
Nguồn
2017-05-03 10:41:54
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
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ó. –
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