Presenting camera permission dialog in iOS 8
Here is the approach we ended up using:
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.
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.
}
}];
} else {
// We are on iOS <= 6. Just do what we need to do.
[self doStuff];
}
How to know that application have camera access or not programmatically in iOS8
Check AVAuthorizationStatus for camera availability and then handle cases accordingly
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");
}
}];
}
Detect permission of camera in iOS
Check the AVAuthorizationStatus
and handle the cases properly.
NSString *mediaType = AVMediaTypeVideo;
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
if(authStatus == AVAuthorizationStatusAuthorized) {
// do your logic
} else if(authStatus == AVAuthorizationStatusDenied){
// denied
} else if(authStatus == AVAuthorizationStatusRestricted){
// restricted, normally won't happen
} else if(authStatus == AVAuthorizationStatusNotDetermined){
// not determined?!
[AVCaptureDevice requestAccessForMediaType:mediaType completionHandler:^(BOOL granted) {
if(granted){
NSLog(@"Granted access to %@", mediaType);
} else {
NSLog(@"Not granted access to %@", mediaType);
}
}];
} else {
// impossible, unknown authorization status
}
Is the camera permission automatically handled by iOs?
Once you open the gallery, the app asks the user for permission for one time, however many times you you open it, then if you uninstall and install back it will ask you one time again.
1- the iOS handles the permission dialogues.
2- if the user uses a features that requires a permission the OS handles the permission request with a dialogue.
Watch App get killed while allowing/disallowing camera permission on iPhone App
Your app is not crashing its just forced to restart by WatchOS with new privacy settings. So when you change the any permission in iPhone then it means privacy policy changed, so app will be killed if its attached to debugger else it will relaunch. its happing on both Apple Watch and iPhone. if you change the permission in Apple Watch iOS app will restart. if you change the permission in iOS app Apple Watch app will be restart.
Also, Not only camera permission If the user at some point changes the Address Book, Calendars, Reminders, Camera, or Photos permissions, WatchOS app will SIGKILL the app when change App permission in iPhone. (it's default behaviour of iOS and WatchOS)
Please refer the below links
App killed by SIGKILL when changing privacy settings ,
https://developer.apple.com/forums/thread/64740,
App crashes in background while changing permission - swift,
App crashed in iOS 6 when user changes Contacts access permissions
How to check if the user gave permission to use the camera?
You can use the following code for doing the same:
if AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo) == AVAuthorizationStatus.Authorized {
// Already Authorized
} else {
AVCaptureDevice.requestAccessForMediaType(AVMediaTypeVideo, completionHandler: { (granted: Bool) -> Void in
if granted == true {
// User granted
} else {
// User rejected
}
})
}
NOTE:
- Make sure that you add the
AVFoundation
Framework in the Link Binary section of build phases - You should write
import AVFoundation
on your class for importingAVFoundation
SWIFT 3
if AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeVideo) == AVAuthorizationStatus.authorized {
// Already Authorized
} else {
AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo, completionHandler: { (granted: Bool) -> Void in
if granted == true {
// User granted
} else {
// User Rejected
}
})
}
Swift 4
if AVCaptureDevice.authorizationStatus(for: .video) == .authorized {
//already authorized
} else {
AVCaptureDevice.requestAccess(for: .video, completionHandler: { (granted: Bool) in
if granted {
//access allowed
} else {
//access denied
}
})
}
Related Topics
Uitableview Inside Uiscrollview Not Receiving First Tap After Scrollling
Preferredinterfaceorientationforpresentation Must Return a Supported Interface Orientation
How to Apply a Vignette Cifilter to a Live Camera Feed in iOS
Add a Uiview Above All, Even the Navigation Bar
Add Lefthand Margin to Uitextfield
How to Authenticate the Gklocalplayer on My 'Third Party Server'
Uicollectionview - Diddeselectitematindexpath Not Called If Cell Is Selected
Getting Client Certificate to Work for Mutual Authentication Using Swift 3 and Alamofire 4
Xcode 6.3 Adding Margins to Tableviewcell
Move a Node to Finger Using Swift + Spritekit
iOS 7 Uitextview Vertical Alignment
Error: Error Domain=Nsurlerrordomain Code=-1001 "The Request Timed Out."
Core Data in Swift: Only Saving Last Object in a for Loop
How to Use Presentmodalviewcontroller to Create a Transparent View
iOS Touch Event Notifications (Private API)
How to Embed a Youtube Video into My App