Tôi có một ứng dụng có Trình quét mã QR hoạt động tốt nhưng trên iOS 8 quyền truy cập mặc định vào máy ảnh bị "Từ chối". Vì vậy, tôi phải đi vào cài đặt và tự cấp cho ứng dụng quyền truy cập để sử dụng máy ảnh. Làm thế nào tôi có thể thực hiện lời nhắc nói điều gì đó như "Bạn có muốn cấp cho ứng dụng này quyền truy cập để sử dụng Máy ảnh" không?iOS: Yêu cầu quyền truy cập vào Camera
Đây là mẫu kiểm tra mã của tôi cho quyền đối với máy ảnh và sau đó yêu cầu quyền nếu người dùng chưa cấp cho họ. Tuy nhiên, liên kết để cấp quyền không bao giờ xuất hiện và cuối cùng chỉ hiển thị UIAlertView. Tình trạng thực sự là bị bỏ qua khi tôi kiểm tra, do đó, có một lý do mà nó không phải là yêu cầu quyền truy cập? Cảm ơn!
Ngoài ra tôi có #import AVFoundation/AVFoundation.h do đó không phải là vấn đề.
-(void) checkCameraAuthorization {
AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if(status == AVAuthorizationStatusAuthorized) { // authorized
NSLog(@"camera authorized");
}
else if(status == AVAuthorizationStatusDenied){ // denied
if ([AVCaptureDevice respondsToSelector:@selector(requestAccessForMediaType: completionHandler:)]) {
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
// Will get here on both iOS 7 & 8 even though camera permissions weren't required
// until iOS 8. So for iOS 7 permission will always be granted.
NSLog(@"DENIED");
if (granted) {
// Permission has been granted. Use dispatch_async for any UI updating
// code because this block may be executed in a thread.
dispatch_async(dispatch_get_main_queue(), ^{
//[self doStuff];
});
} else {
// Permission has been denied.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Not Authorized" message:@"Please go to Settings and enable the camera for this app to use this feature." delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alert show];
}
}];
}
}
else if(status == AVAuthorizationStatusRestricted){ // restricted
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Not Authorized" message:@"Please go to Settings and enable the camera for this app to use this feature." delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alert show];
}
else if(status == AVAuthorizationStatusNotDetermined){ // not determined
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
if(granted){ // Access has been granted ..do something
NSLog(@"camera authorized");
} else { // Access denied ..do something
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Not Authorized" message:@"Please go to Settings and enable the camera for this app to use this feature." delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alert show];
}
}];
}
}
trả lời ở đây: http://stackoverflow.com/questions/24229422/accessing-the-settings-app-from-your -app-in-ios-8 –