Because IOS system security restrictions,if you need to access the device's address book, microphone, album, camera, geographic location, etc., you need to ask the user whether to allow access.
Sometimes the user is careless. "not allowed ", you may not know where to go before you open this permission. This requires us to obtain the relevant authorization status each time the relevant function is invoked, and to eject the notification box if the authorization request is not yet authorized. If you have previously been rejected, then pop-up related prompts to make it easy for users to automatically jump to the settings page to modify permissions.
1, example (1) Here is an example of a photo's access rights. To facilitate the presentation, I requested permission after the page was initialized. (2) At the time of the first request we chose "not allowed. " When you start the program again, you will be prompted to restrict photo access and require authorization. (3) Click "Settings" button to automatically jump to the system settings page.
2, sample code
|
import UIKit
import Photos
class ViewController: UIViewController {
override func viewDidLoad () {
_ = authorize ()
}
func authorize ()-> Bool {
let status = PHPhotoLibrary.authorizationStatus ()
switch status {
case .authorized:
return true
case .notDetermined:
// request authorization
PHPhotoLibrary.requestAuthorization ({(status)-> Void in
DispatchQueue.main.async (execute: {()-> Void in
_ = self.authorize ()
})
})
default: ()
DispatchQueue.main.async (execute: {()-> Void in
let alertController = UIAlertController (title: "Restricted access to photos",
message: "Tap" Settings "to allow access to your photos",
preferredStyle: .alert)
let cancelAction = UIAlertAction (title: "Cancel", style: .cancel, handler: nil)
let settingsAction = UIAlertAction (title: "Settings", style: .default, handler: {
(action)-> Void in
let url = URL (string: UIApplicationOpenSettingsURLString)
if let url = url, UIApplication.shared.canOpenURL (url) {
if #available (iOS 10, *) {
UIApplication.shared.open (url, options: [:],
completionHandler: {
(success) in
})
} else {
UIApplication.shared.openURL (url)
}
}
})
alertController.addAction (cancelAction)
alertController.addAction (settingsAction)
self.present (alertController, animated: true, completion: nil)
})
}
return false
}
override func didReceiveMemoryWarning () {
super.didReceiveMemoryWarning ()
}
} |
Original from: www.hangge.com reprint please keep the original link: http://www.hangge.com/blog/cache/detail_1517.html
Swift-Determines if there is a feature access, no prompt, and automatically jumps to the settings page