Swift-Determines if there is a feature access, no prompt, and automatically jumps to the settings page

Source: Internet
Author: User


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


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.