Determine if the access to photo library is set or not - PHPhotoLibrary

后端 未结 11 841
误落风尘
误落风尘 2020-11-27 10:23

With the new functionality in iOS 8, if you are using a camera in the app, it will ask for permission to access the camera and then when you try to retake the pic, it asks f

11条回答
  •  悲&欢浪女
    2020-11-27 10:48

    I have a simple solution on swift 2.0
    
    //
    //  AppDelegate.swift
    //  HoneyBadger
    //
    //  Created by fingent on 14/08/15.
    //  Copyright (c) 2015 fingent. All rights reserved.
    //
    
    import UIKit
    import Photos
    
    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {
    
        var window: UIWindow?
    
        func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
            self.window?.makeKeyAndVisible()
    
                 self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
                let storyboard = UIStoryboard(name: "Main", bundle: nil)
                let initialViewController = storyboard.instantiateViewControllerWithIdentifier("LoginPageID")
                self.window?.rootViewController = initialViewController
                self.window?.makeKeyAndVisible()
            return true
        }
        func applicationDidEnterBackground(application: UIApplication) {
            print("Application On background", terminator: "")
        }
        func applicationDidBecomeActive(application: UIApplication) {
            cameraAllowsAccessToApplicationCheck()
            photoLibraryAvailabilityCheck()
        }
        //MARK:- CAMERA ACCESS CHECK
        func cameraAllowsAccessToApplicationCheck()
        {
            let authorizationStatus = AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo)
            switch authorizationStatus {
            case .NotDetermined:
                // permission dialog not yet presented, request authorization
                AVCaptureDevice.requestAccessForMediaType(AVMediaTypeVideo,
                    completionHandler: { (granted:Bool) -> Void in
                        if granted {
                            print("access granted", terminator: "")
                        }
                        else {
                            print("access denied", terminator: "")
                        }
                })
            case .Authorized:
                print("Access authorized", terminator: "")
            case .Denied, .Restricted:
                alertToEncourageCameraAccessWhenApplicationStarts()
            default:
                print("DO NOTHING", terminator: "")
            }
        }
        //MARK:- PHOTO LIBRARY ACCESS CHECK
        func photoLibraryAvailabilityCheck()
        {
            if PHPhotoLibrary.authorizationStatus() == PHAuthorizationStatus.Authorized
            {
    
            }
            else
            {
                PHPhotoLibrary.requestAuthorization(requestAuthorizationHandler)
            }
        }
        func requestAuthorizationHandler(status: PHAuthorizationStatus)
        {
            if PHPhotoLibrary.authorizationStatus() == PHAuthorizationStatus.Authorized
            {
    
            }
            else
            {
                alertToEncouragePhotoLibraryAccessWhenApplicationStarts()
            }
        }
    
        //MARK:- CAMERA & GALLERY NOT ALLOWING ACCESS - ALERT
        func alertToEncourageCameraAccessWhenApplicationStarts()
        {
            //Camera not available - Alert
            let internetUnavailableAlertController = UIAlertController (title: "Camera Unavailable", message: "Please check to see if it is disconnected or in use by another application", preferredStyle: .Alert)
    
            let settingsAction = UIAlertAction(title: "Settings", style: .Destructive) { (_) -> Void in
                let settingsUrl = NSURL(string:UIApplicationOpenSettingsURLString)
                if let url = settingsUrl {
                    dispatch_async(dispatch_get_main_queue()) {
                        UIApplication.sharedApplication().openURL(url)
                    }
    
                }
            }
            let cancelAction = UIAlertAction(title: "Okay", style: .Default, handler: nil)
            internetUnavailableAlertController .addAction(settingsAction)
            internetUnavailableAlertController .addAction(cancelAction)
            self.window?.rootViewController!.presentViewController(internetUnavailableAlertController , animated: true, completion: nil)
        }
        func alertToEncouragePhotoLibraryAccessWhenApplicationStarts()
        {
    //Photo Library not available - Alert
            let cameraUnavailableAlertController = UIAlertController (title: "Photo Library Unavailable", message: "Please check to see if device settings doesn't allow photo library access", preferredStyle: .Alert)
    
            let settingsAction = UIAlertAction(title: "Settings", style: .Destructive) { (_) -> Void in
                let settingsUrl = NSURL(string:UIApplicationOpenSettingsURLString)
                if let url = settingsUrl {
                    UIApplication.sharedApplication().openURL(url)
                }
            }
            let cancelAction = UIAlertAction(title: "Okay", style: .Default, handler: nil)
            cameraUnavailableAlertController .addAction(settingsAction)
            cameraUnavailableAlertController .addAction(cancelAction)
            self.window?.rootViewController!.presentViewController(cameraUnavailableAlertController , animated: true, completion: nil)
        }
    }
    

提交回复
热议问题