I've created a function which is suppose to handle the permission with location, so that the app will close if it does not have permission to location. However when you press open settings and press "back to app" in the status bar the determeinePermission method is not being executed again. I've tried to add it to viewDidLoad, viewDidAppear and viewWillAppear. what can I do?
func determinePermission() {
switch CLLocationManager.authorizationStatus() {
case .Authorized:
if CLLocationManager.locationServicesEnabled() {
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
manager.startUpdatingLocation()
}
case .NotDetermined:
manager.requestWhenInUseAuthorization()
case .AuthorizedWhenInUse, .Restricted, .Denied:
let alertController = UIAlertController(
title: "Background Location Access Disabled",
message: "In order to be notified about adorable kittens near you, please open this app's settings and set location access to 'Always'.",
preferredStyle: .Alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in
exit(0)
}
alertController.addAction(cancelAction)
let openAction = UIAlertAction(title: "Open Settings", style: .Default) { (action) in
if let url = NSURL(string:UIApplicationOpenSettingsURLString) {
UIApplication.sharedApplication().openURL(url)
}
}
alertController.addAction(openAction)
self.presentViewController(alertController, animated: true, completion: nil)
}
}
Try adding it to UIApplicationDelegate.applicationDidBecomeActive.
Related
If user dennied the camera access, i am showing one alert with cancel and setting button to show it. But the code is not working.
#IBAction func ProfileImageButton(_ sender: UIButton) {
print("profile image Button is pressed")
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
profileimgbool = true
let actionSheet = UIAlertController(title: "Photo Source", message: "choose a Source", preferredStyle: .actionSheet)
actionSheet.addAction(UIAlertAction(title: "Camera", style: .default, handler: {(action:UIAlertAction) in imagePickerController.sourceType = .camera
self.present(imagePickerController, animated: true, completion: nil)
}))
actionSheet.addAction(UIAlertAction(title: "Photo Library", style: .default, handler: {(action:UIAlertAction) in imagePickerController.sourceType = .photoLibrary
self.present(imagePickerController, animated: true, completion: nil)}))
actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
self.present(actionSheet, animated: true, completion: nil)
}
func checkCameraPermission() {
let cameraMediaType = AVMediaType.video
AVCaptureDevice.requestAccess(for: cameraMediaType) { granted in
if granted {
//Do operation
print("Granted access for camera")
// self.setCamera()
} else {
self.noCameraFound()
print("Denied access for camera ")
}
}
}
func noCameraFound(){
let alert = UIAlertController(title: "CallDoc", message: "Please allow camera access in phone settings", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Back", style: UIAlertActionStyle.cancel, handler: {(action:UIAlertAction) in
}));
alert.addAction(UIAlertAction(title: "Open setting", style: UIAlertActionStyle.default, handler: {(action:UIAlertAction) in
UIApplication.shared.open(NSURL(string:UIApplicationOpenSettingsURLString)! as URL, options: [:], completionHandler: nil)
}));
self.present(alert, animated: true, completion: nil)
}
in my above code my method was checkCameraPermission where i will call this to show alert. I needs to show when user click camera, and when if user denied that black screen will show instead of camera. There i need to show that alert pop up.
Where i can call this checkCameraPermission method to show my popup ?.
Any idea ?
for reference purpose I taken the answer from this tutorial.
step 1
add the avfoundation framework in your project
import AVFoundation
step 2
dont forget to Set Camera Usage Description in Info.plist
When you request permission to use the device’s camera, a short message will appear in the default iOS system dialog. You customize this message by adding the Privacy - Camera Usage Description key to your Info.plist file.
step 3
on your image profile change button action verify the permission, etc.
#IBAction func ProfileImageButton(_ sender: UIButton) {
let cameraAuthorizationStatus = AVCaptureDevice.authorizationStatus(for: .video)
switch cameraAuthorizationStatus {
case .notDetermined: requestCameraPermission()
case .authorized: presentCamera()
case .restricted, .denied: alertCameraAccessNeeded()
}
}
based on the above action the condition will satisfy,
If the user has never responded to a request to access his/her camera, you need to prompt with the iOS system alert to request permission:
func requestCameraPermission() {
AVCaptureDevice.requestAccess(for: .video, completionHandler: {accessGranted in
guard accessGranted == true else { return }
self.presentCamera()
})
}
there after the camera access will continue
func presentCamera() {
let photoPicker = UIImagePickerController()
photoPicker.sourceType = .camera
photoPicker.delegate = self as? UIImagePickerControllerDelegate & UINavigationControllerDelegate
self.present(photoPicker, animated: true, completion: nil)
}
To use the image that the camera captured, you need to set up your view controller to adhere to and implement couple of delegate protocols:
class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
// ...
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let photo = info[UIImagePickerControllerOriginalImage] as! UIImage
// do something with the photo... set to UIImageView, save it, etc.
dismiss(animated: true, completion: nil)
}
If camera access has been denied or restricted, you can alert the user and direct them to the Settings app to make the appropriate permissions adjustment:
func alertCameraAccessNeeded() {
let settingsAppURL = URL(string: UIApplicationOpenSettingsURLString)!
let alert = UIAlertController(
title: "Need Camera Access",
message: "Camera access is required to make full use of this app.",
preferredStyle: UIAlertControllerStyle.alert
)
alert.addAction(UIAlertAction(title: "Cancel", style: .default, handler: nil))
alert.addAction(UIAlertAction(title: "Allow Camera", style: .cancel, handler: { (alert) -> Void in
UIApplication.shared.open(settingsAppURL, options: [:], completionHandler: nil)
}))
present(alert, animated: true, completion: nil)
}
My iOS app has been rejected by Apple as the app crashes if a user selected "Dont allow" location access. And the proceeds to tap on my Map button.
How can I wrap this button in check to see if the user has given permission, And if not how can I ask for permission again?
//Map Button Action - Opens Maps - Gives choice of Google or Apple maps
#IBAction func googleMapBtn(_ sender: UIButton) {
UIDevice.current.isBatteryMonitoringEnabled = true
let state = UIDevice.current.batteryState
//If user is in Loop - Cant open maps
if state == .charging {
print("In Loop - Cant open maps")
}
//Present Map Options
else {
let alertController = UIAlertController.init(title: "Open Map", message: "", preferredStyle: .alert)
alertController.addAction(UIAlertAction.init(title: "Google Maps", style: .default, handler: { (action) in
self.googleMapsOpen()
}))
alertController.addAction(UIAlertAction.init(title: "Apple Maps", style: .default, handler: { (action) in
self.appleMapsOpen()
}))
alertController.addAction(UIAlertAction.init(title: "Back", style: .default, handler: { (action) in
self.dismiss(animated: true, completion: nil)
}))
self.present(alertController, animated: true) {
}
}
}
The code crashes whenever a user selects a map type Google/Apple and the self.googleMapsOpen() or self.appleMapsOpen() are executed. Specifically is crashed on the let scheme=
func googleMapsOpen(){
print("Google Maps Pressed")
let scheme = "comgooglemaps://?center=\(LocationManager.sharedInstance.location.coordinate.latitude),\(LocationManager.sharedInstance.location.coordinate.longitude)&zoom=15"
self.open(scheme: scheme)
}
func appleMapsOpen(){
print("Apple Maps Pressed")
let scheme = "http://maps.apple.com/?ll=\(LocationManager.sharedInstance.location.coordinate.latitude),\(LocationManager.sharedInstance.location.coordinate.longitude)&zoom=15"
self.open(scheme: scheme)
}
You can do something like this :
func checkLocationPermissionEnabled()
{
if CLLocationManager.locationServicesEnabled()
{
switch(CLLocationManager.authorizationStatus())
{
case .notDetermined, .restricted, .denied:
self.openDeviceLocationSetting()
return
case .authorizedAlways, .authorizedWhenInUse:
//do whatever you want to do with location
return
}
}
else
{
self.openDeviceLocationSetting()
return
}
}
func openDeviceLocationSetting()
{
let alertController = UIAlertController(title: "", message:"For best results, let your device turn on location using Google's location service.", preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
UIAlertAction in
self.isAlertShowing = false
let settingsUrl = NSURL(string: UIApplicationOpenSettingsURLString)
if let url = settingsUrl {
UIApplication.shared.openURL(url as URL)
}
}
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.default) {
UIAlertAction in
}
alertController.addAction(okAction)
alertController.addAction(cancelAction)
self.present(alertController, animated: true, completion: nil)
}
var locMgr = INTULocationManager.sharedInstance()
locMgr.requestLocation(withDesiredAccuracy: .city, timeout: 30, delayUntilAuthorized: true,block: {(currentLoc: CLLocation!, achievedAccuracy: INTULocationAccuracy, status: INTULocationStatus) -> Void in
if status == INTULocationStatus.success {
}
else{
}
Used INTULocationManager , Swift 4.1 , iOS 11.1
if first time run this code pop up Location Permission Request
but If I denied, this is not pop up next time.
how to open Permission Pop up?
i create Button
run this code
let locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
but not worked
There isn't any default functionality which will popup the location permission once the user has denied the permission. You need to show an alert to the user that permission is required, and then redirect the user to Settings screen.
Here's the complete code you can use.
Define a function which will check the location permission.
func hasLocationPermission() -> Bool {
var hasPermission = false
if CLLocationManager.locationServicesEnabled() {
switch CLLocationManager.authorizationStatus() {
case .notDetermined, .restricted, .denied:
hasPermission = false
case .authorizedAlways, .authorizedWhenInUse:
hasPermission = true
}
} else {
hasPermission = false
}
return hasPermission
}
Now check location permission through this function and show alert if needed.
if !hasLocationPermission() {
let alertController = UIAlertController(title: "Location Permission Required", message: "Please enable location permissions in settings.", preferredStyle: UIAlertControllerStyle.alert)
let okAction = UIAlertAction(title: "Settings", style: .default, handler: {(cAlertAction) in
//Redirect to Settings app
UIApplication.shared.open(URL(string:UIApplicationOpenSettingsURLString)!)
})
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel)
alertController.addAction(cancelAction)
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
}
Also don't forget to import CoreLocation.
Swift 5.3 and iOS 14 version
func hasLocationPermission() -> Bool {
var hasPermission = false
let manager = CLLocationManager()
if CLLocationManager.locationServicesEnabled() {
switch manager.authorizationStatus {
case .notDetermined, .restricted, .denied:
hasPermission = false
case .authorizedAlways, .authorizedWhenInUse:
hasPermission = true
#unknown default:
break
}
} else {
hasPermission = false
}
return hasPermission
}
if !hasLocationPermission() {
let alertController = UIAlertController(title: "Location Permission Required", message: "Please enable location permissions in settings.", preferredStyle: .alert)
let okAction = UIAlertAction(title: "Settings", style: .default, handler: {(cAlertAction) in
//Redirect to Settings app
UIApplication.shared.open(URL(string:UIApplication.openSettingsURLString)!)
})
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
alertController.addAction(cancelAction)
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
}
Swift 5
Once the user denies the permission then the alert is disabled for your app and will not show again.
You can show popup to the user that permission is required.
Here's the complete code you can use
if CLLocationManager.locationServicesEnabled() {
switch CLLocationManager.authorizationStatus() {
case .notDetermined, .restricted, .denied:
showPermissionAlert()
case .authorizedAlways, .authorizedWhenInUse:
locationManager.startUpdatingLocation()
}
} else {
locationManager.startUpdatingLocation()
}
Now check location permission through this function and show alert if needed.
func showPermissionAlert(){
let alertController = UIAlertController(title: "Location Permission Required", message: "Please enable location permissions in settings.", preferredStyle: UIAlertController.Style.alert)
let okAction = UIAlertAction(title: "Settings", style: .default, handler: {(cAlertAction) in
//Redirect to Settings app
UIApplication.shared.open(URL(string:UIApplication.openSettingsURLString)!)
})
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertAction.Style.cancel)
alertController.addAction(cancelAction)
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
}
if Permission deny by the user then open Permission PopUp
/* func checkLocation() {
if CLLocationManager.authorizationStatus() != .authorizedWhenInUse
{
print("requesting autorization")
locationManager.requestWhenInUseAuthorization()
} else {
print("start updating location")
}
}*/
func askEnableLocationService() ->String {
var showAlertSetting = false
var showInitLocation = false
if CLLocationManager.locationServicesEnabled() {
switch CLLocationManager.authorizationStatus() {
case .denied:
showAlertSetting = true
print("HH: kCLAuthorizationStatusDenied")
case .restricted:
showAlertSetting = true
print("HH: kCLAuthorizationStatusRestricted")
case .authorizedAlways:
showInitLocation = true
print("HH: kCLAuthorizationStatusAuthorizedAlways")
case .authorizedWhenInUse:
showInitLocation = true
print("HH: kCLAuthorizationStatusAuthorizedWhenInUse")
case .notDetermined:
showInitLocation = true
print("HH: kCLAuthorizationStatusNotDetermined")
default:
break
}
}else{
showAlertSetting = true
print("HH: locationServicesDisabled")
}
if showAlertSetting {
let alertController = UIAlertController(title: "xxxxxx", message: "Please enable location service in the settings", preferredStyle: .alert)
let OKAction = UIAlertAction(title: "OK", style: .default) { (action:UIAlertAction!) in
if let url = URL(string: UIApplicationOpenSettingsURLString) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}
alertController.addAction(OKAction)
self.window?.rootViewController?.present(alertController, animated: true, completion:nil)
}
if showInitLocation {
return "YES"
}
return "NO"
}
That is default behavior. Once the popup is shown for the first time. The subsequent request will be treated as declined or whatever is selected on first select. However you can implement your own alert and send user directly to setting app to grant location access like below:
//check if user has denied the access on first popup
if !permissionGranted {
let permissionAlert = UIAlertController(title: "Location Access", message: "Requires location access to take advantage of this feature. Please provide location access from settings", preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil)
let settingAction = UIAlertAction(title: "Settings", style: .default) { (action) in
guard let appSettingURl = URL(string: UIApplicationOpenSettingsURLString) else { return }
if UIApplication.shared.canOpenURL(appSettingURl) {
UIApplication.shared.open(appSettingURl, options: [:], completionHandler: nil)
}
}
permissionAlert.addAction(cancelAction)
permissionAlert.addAction(settingAction)
present(permissionAlert, animated: true, completion: nil)
}
Complete Solution : (iOS 14+ and also for prior versions)
1)first get authorization status:-
func locationAuthorizationStatus() -> CLAuthorizationStatus {
let locationManager = CLLocationManager()
var locationAuthorizationStatus : CLAuthorizationStatus
if #available(iOS 14.0, *) {
locationAuthorizationStatus = locationManager.authorizationStatus
} else {
// Fallback on earlier versions
locationAuthorizationStatus = CLLocationManager.authorizationStatus()
}
return locationAuthorizationStatus
}
2)Then check for location permission:-
func hasLocationPermission() -> Bool {
var hasPermission = false
let manager = self.locationAuthorizationStatus()
if CLLocationManager.locationServicesEnabled() {
switch manager {
case .notDetermined, .restricted, .denied:
hasPermission = false
case .authorizedAlways, .authorizedWhenInUse:
hasPermission = true
#unknown default:
break
}
} else {
hasPermission = false
}
return hasPermission
}
3)Then show alert
if !hasLocationPermission() {
let alertController = UIAlertController(title: "Location Permission Required", message: "Please enable location permissions in settings.", preferredStyle: .alert)
let okAction = UIAlertAction(title: "Settings", style: .default, handler: {(cAlertAction) in
//Redirect to Settings app
UIApplication.shared.open(URL(string:UIApplication.openSettingsURLString)!)
})
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
alertController.addAction(cancelAction)
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
}
CHEERS :) :)
What is the most effective way to prompt a User to provide access to the Camera (or other feature), while ensuring the best experience?
When accessing the Camera, iOS must ask the Customer permission to allow access. As we all know, if the Customer says "No" but then changes their mind, there is no way to reverse this decision from within your App. They must go to Settings and follow a number of steps to re-enable access, namely:
Settings -> Privacy -> Camera -> [Your App] -> turn switch on
Permission Priming is an effective way to avoid a situation where your Customer might deny access to a key feature of your app.
On iOS an App is only allowed to trigger the default system permission once per feature. Permission priming is when an app "primes" the Customer with an alert that mimics a system permission.
The benefit to doing this is so that if the Customer opts-out (selects Cancel), the App is still able to ask again in future, until they say yes — at which time the actual system permission is displayed and the Customer is statistically less likely to then change their mind and enter into the negative work flow.
Furthermore, since cameraSelected() performs this workflow, if the user declines, but then at some future point does change their settings, the App will immediately reflect the new permissions without further input (ie. the User could switch to Settings, change permissions, and then switch back to the App).
Here is some Swift 3 code to implement this feature:
[UPDATE: Included is a solution to open a deep-link to Settings where the User can enable camera access, if they have previously denied it.]
[UPDATE 2: Added sample lines for Analytics implementation.]
func cameraSelected() {
// First we check if the device has a camera (otherwise will crash in Simulator - also, some iPod touch models do not have a camera).
if let deviceHasCamera = UIImagePickerController.isSourceTypeAvailable(.camera) {
let authStatus = AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeVideo)
switch authStatus {
case .authorized:
showCameraPicker()
case .denied:
alertPromptToAllowCameraAccessViaSettings()
case .notDetermined:
permissionPrimeCameraAccess()
default:
permissionPrimeCameraAccess()
}
} else {
let alertController = UIAlertController(title: "Error", message: "Device has no camera", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .default, handler: { (alert) in
Analytics.track(event: .permissionsPrimeCameraNoCamera)
})
alertController.addAction(defaultAction)
present(alertController, animated: true, completion: nil)
}
}
func alertPromptToAllowCameraAccessViaSettings() {
let alert = UIAlertController(title: "\"<Your App>\" Would Like To Access the Camera", message: "Please grant permission to use the Camera so that you can <customer benefit>.", preferredStyle: .alert )
alert.addAction(UIAlertAction(title: "Open Settings", style: .cancel) { alert in
Analytics.track(event: .permissionsPrimeCameraOpenSettings)
if let appSettingsURL = NSURL(string: UIApplicationOpenSettingsURLString) {
UIApplication.shared.openURL(appSettingsURL)
}
})
present(alert, animated: true, completion: nil)
}
func permissionPrimeCameraAccess() {
let alert = UIAlertController( title: "\"<Your App>\" Would Like To Access the Camera", message: "<Your App> would like to access your Camera so that you can <customer benefit>.", preferredStyle: .alert )
let allowAction = UIAlertAction(title: "Allow", style: .default, handler: { (alert) -> Void in
Analytics.track(event: .permissionsPrimeCameraAccepted)
if AVCaptureDevice.devices(withMediaType: AVMediaTypeVideo).count > 0 {
AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo, completionHandler: { [weak self] granted in
DispatchQueue.main.async {
self?.cameraSelected() // try again
}
})
}
})
alert.addAction(allowAction)
let declineAction = UIAlertAction(title: "Not Now", style: .cancel) { (alert) in
Analytics.track(event: .permissionsPrimeCameraCancelled)
}
alert.addAction(declineAction)
present(alert, animated: true, completion: nil)
}
func showCameraPicker() {
let picker = UIImagePickerController()
picker.delegate = self
picker.modalPresentationStyle = UIModalPresentationStyle.currentContext
picker.allowsEditing = false
picker.sourceType = UIImagePickerControllerSourceType.camera
present(picker, animated: true, completion: nil)
}
Suppose we have two buttons (one for picking picture from library another from camera) with tags 1,2 that are linked to action:
import UIKit
import AVFoundation
#IBAction func changeImage(sender: UIButton) {
let picker = UIImagePickerController()
if sender.tag == 2 { // tag = 2 for camera button. tag = 1 for image picker
guard UIImagePickerController.isSourceTypeAvailable(.camera) else { return }
let cameraAuthorizationStatus = AVCaptureDevice.authorizationStatus(for: .video)
switch cameraAuthorizationStatus {
case .notDetermined:
requestCameraPermission()
return
case .authorized:
break
case .restricted, .denied:
alertCameraAccessNeeded()
return
#unknown default:
return
}
picker.sourceType = .camera
}
picker.allowsEditing = true
picker.delegate = self
present(picker, animated: true)
}
private func requestCameraPermission() {
AVCaptureDevice.requestAccess(for: .video) { [weak self] accessGranted in
if !accessGranted {
DispatchQueue.main.async {
self?.alertCameraAccessNeeded()
}
}
}
}
private func alertCameraAccessNeeded() {
guard let settingsAppURL = URL(string: UIApplication.openSettingsURLString),
UIApplication.shared.canOpenURL(settingsAppURL) else { return } // This should never happen
let alert = UIAlertController(
title: "Need Camera Access",
message: "Camera access is required to take pictures of item.",
preferredStyle: .alert
)
alert.addAction(UIAlertAction(title: "Cancel", style: .default))
alert.addAction(UIAlertAction(title: "Allow Camera", style: .cancel) { _ in
UIApplication.shared.open(settingsAppURL, options: [:])
})
present(alert, animated: true)
}
If the user did not allow access to photo album at the start, I will prompt with a pop up with Cancel and Settings to choose from. If he chooses settings, it will bring him to settings page where he can enable camera and photo Library for the app. However, as soon as the user toggles the camera or photo library switch in settings, my app crashes with "Message from debugger: Terminated due to signal 9" printout. Below is the code for my popup
#IBAction func cameraBarBtnPress(sender: AnyObject) {
let photoAuthStatus = PHPhotoLibrary.authorizationStatus()
switch photoAuthStatus {
case .Authorized:
presentFusumaCameraVC()
case .Denied, .Restricted :
showNeedPhotoAlbumAccessPopup()
case .NotDetermined:
PHPhotoLibrary.requestAuthorization({ (authStatus: PHAuthorizationStatus) in
switch authStatus {
case .Authorized:
self.presentFusumaCameraVC()
case .Denied, .Restricted :
self.showNeedPhotoAlbumAccessPopup()
case .NotDetermined:
print("Shouldnt get to here")
}
})
}
}
func showNeedPhotoAlbumAccessPopup() {
let alertController = UIAlertController(title: "Enable Photo Album Access", message: "", preferredStyle: .Alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .Default, handler: nil)
let settingsAction = UIAlertAction(title: "Settings", style: .Default, handler: { (action: UIAlertAction) in
let settingsUrl = NSURL(string: UIApplicationOpenSettingsURLString)
if let url = settingsUrl {
UIApplication.sharedApplication().openURL(url)
}
})
alertController.addAction(settingsAction)
alertController.addAction(cancelAction)
self.presentViewController(alertController, animated: true, completion: nil)
}
What would be the correct way to handle this so that the user can go back to the app and begin selecting the photos after toggling the switch?
Apple's documentation says the following:
If permissions changes, app is quit
Background task expiration handler is called, if registered
iOS then kills the application
Haven't seen a way around so far.