我正在使用啓動相機的按鈕執行ios應用程序。如何檢測相機是否受用戶限制
我想啓用/禁用按鈕,如果設備有一個相機可用或不。
我想檢測設備是否有攝像頭,也有設備有攝像頭但受限制(使用this),因此您無法使用它。
如何檢測這兩個選項?
感謝
我正在使用啓動相機的按鈕執行ios應用程序。如何檢測相機是否受用戶限制
我想啓用/禁用按鈕,如果設備有一個相機可用或不。
我想檢測設備是否有攝像頭,也有設備有攝像頭但受限制(使用this),因此您無法使用它。
如何檢測這兩個選項?
感謝
如在別處所述,檢查AVAuthorizationStatus
實際上並不會告訴你,如果它的限制,儘管在枚舉中存在「受限制」的值。 相反,我發現,檢查是否被啓用是有幫助的:
BOOL isCameraAvailable = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
如果isCameraAvailable
是NO
那麼用戶在限制最有可能禁用相機。見Detect existence of camera in iPhone app?
的用戶第一次嘗試使用相機在iOS 6上,他/她會自動請求批准。您不必添加額外的代碼(在authorisationstatus爲ALAuthorizationStatusNotDetermined之前)。
所以如果用戶第一次否認你不能再問。
您可以使用ALAssetsLibrary來檢查這一點。 檢查此解決方案的答案: ask-permission-to-access-camera
希望它可以幫助你。
謝謝,我會嘗試 –
@ A.Vila mark如果它解決了您的問題,則回答此問題:-) –
ALAssetsLibrary似乎告訴您是否可以訪問照片,而不是照相機。 –
檢查應用程序使用以下代碼段中的攝像頭權限狀態。
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");
}
}];
}
}
檢查攝像機是否受限制AVAuthorizationStatus
是不夠的。正如文檔中所述:
此狀態通常不可見 - 用於發現設備的AVCaptureDevice類方法不返回用戶被禁止訪問的設備。
因此,對於合適的檢查,你需要創建一些捕獲裝置,例如,像我一樣:
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;
}
}
SWIFT
,以決定是否相機按鈕甚至應該允許(或隱藏)您應檢查:
if UIImagePickerController.isSourceTypeAvailable(.Camera){ }
但後來我會檢查,看看是否用戶允許訪問攝像機,如蘋果在其PhotoPicker示例中所示(PhotoPicker example Objective-C):
*請注意你必須導入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)
}
你爲什麼不自己測試一下?爲什麼發佈問題? – rmaddy
我已經測試過,並且能夠檢測到設備是否有攝像頭,但是我無法檢測到設備有攝像頭,但受到限制,因此您無法使用它。 –
對不起,我想我誤解了你所做的。我以爲你問如果相機已被限制,該代碼是否有效。這就是爲什麼我建議你簡單地測試它。出於好奇,如果您使用受限攝像頭設置設備,「isSourceTypeAvailable」返回的是什麼? – rmaddy