1. 程式人生 > >iOS相簿、相機許可權判斷 (Swift 4)

iOS相簿、相機許可權判斷 (Swift 4)

AppDelegate中的方法

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    
        //MARK: APP啟動時候,判斷使用者是否授權使用相機
        if (AVCaptureDevice.authorizationStatus(for: AVMediaType.video) == .notDetermined) {
            AVCaptureDevice.requestAccess(for: .video, completionHandler: { (statusFirst) in
                if statusFirst {
                    //使用者首次允許
                    print("允許APP訪問相機")
                } else {
                    //使用者首次拒接
                    print("拒絕APP訪問相機")
                }
            })
        }
       
        //MARK: APP啟動時候,判斷使用者是否授權使用相簿
        if (PHPhotoLibrary.authorizationStatus() == .notDetermined) {
            PHPhotoLibrary.requestAuthorization({ (firstStatus) in
                let result = (firstStatus == .authorized)
                if result {
                    print("允許APP訪問相簿")
                } else {
                    print("拒絕APP訪問相簿")
                }
            })
        }
        
        return true
    }

相機、相簿許可權判斷

/****************************APP許可權**************************************/
/* .restricted     ---> 受限制,系統原因,無法訪問
 * .notDetermined  ---> 系統還未知是否訪問,第一次開啟時
 * .authorized     ---> 允許、已授權
 * .denied         ---> 受限制,系統原因,無法訪問
 */
//MARK: 判斷是否可訪問相簿
//UIDevice.current.systemVersion >= 8.0
///相簿許可權---> 直接使用 <Photo方法 @available(iOS 8.0, *)>
func photoEnable() -> Bool {
    func photoResult() {
        let status = PHPhotoLibrary.authorizationStatus()
        if (status == .authorized) {
            savePhoto(value: "1")
        }
        else if (status == .restricted || status == .denied) {
            let alertV = UIAlertView.init(title: "提示", message: "請去-> [設定 - 隱私 - 相簿] 開啟訪問開關", delegate: nil, cancelButtonTitle: nil, otherButtonTitles: "確定")
            alertV.show()
            savePhoto(value: "0")
        }
        else if (status == .notDetermined) {//首次使用
            PHPhotoLibrary.requestAuthorization({ (firstStatus) in
                let isTrue = (firstStatus == .authorized)
                if isTrue {
                    print("首次允許")
                    savePhoto(value: "1")
                } else {
                    print("首次不允許")
                    savePhoto(value: "0")
                }
            })
        }
    }
    func savePhoto(value: String) {
        UserDefaults.standard.setValue(value, forKey: "photoEnablebs")
    }
    let result = (UserDefaults.standard.object(forKey: "photoEnablebs") as! String) == "1"
    return result
}

///相簿許可權 --> 需要AppleDelegate中讓使用者先選擇授權
func photoEnableDelegate() -> Bool {
    let status = PHPhotoLibrary.authorizationStatus()
    if status == .authorized {
        return true
    }
    return false
}

//UIDevice.current.systemVersion < 8.0
//depressed
///相簿許可權---> 需要AppleDelegate中讓使用者先選擇授權, 此方法將被廢棄 <AssetsLibrary方法 @available(iOS 7.0, *)>
func photoEnableDelegate2() -> Bool {
    let author = ALAssetsLibrary.authorizationStatus()
    print("--\(author.rawValue)--")
    if (author == .restricted || author == .denied) {
        let alertV = UIAlertView.init(title: "提示", message: "請去-> [設定 - 隱私 - 相簿] 開啟訪問開關", delegate: nil, cancelButtonTitle: nil, otherButtonTitles: "確定")
        alertV.show()
        return false
    }
    return true
}

//MARK: 判斷是否可訪問相機
///相機許可權 ---> 直接呼叫
func cameraEnable() -> Bool {
    func cameraResult() {
        let authStatus = AVCaptureDevice.authorizationStatus(for: AVMediaType.video)
        
        if (authStatus == .authorized) { /****已授權,可以開啟相機****/
            saveCamera(value: "1")
        }
            
        else if (authStatus == .denied) {
            saveCamera(value: "0")
            let alertV = UIAlertView.init(title: "提示", message: "請去-> [設定 - 隱私 - 相機] 開啟訪問開關", delegate: nil, cancelButtonTitle: nil, otherButtonTitles: "確定")
            alertV.show()
        }
            
        else if (authStatus == .restricted) {//相機許可權受限
            saveCamera(value: "0")
            let alertV = UIAlertView.init(title: "提示", message: "相機許可權受限", delegate: nil, cancelButtonTitle: nil, otherButtonTitles: "確定")
            alertV.show()
        }
            
        else if (authStatus == .notDetermined) {//首次 使用
            AVCaptureDevice.requestAccess(for: .video, completionHandler: { (statusFirst) in
                if statusFirst {
                    //使用者首次允許
                    saveCamera(value: "1")
                } else {
                    //使用者首次拒接
                    saveCamera(value: "0")
                }
            })
        }
    }
    func saveCamera(value: String) {
        UserDefaults.standard.setValue(value, forKey: "cameraEnablebs")
    }
    cameraResult()
    let result = (UserDefaults.standard.value(forKey: "cameraEnablebs") as! String) == "1"
    return result
}

///相機許可權2 --> 需要AppleDelegate中讓使用者先選擇授權
func cameraEnableDelegate() -> Bool {
    let status = AVCaptureDevice.authorizationStatus(for: AVMediaType.video)
    if status == .authorized {
        return true
    }
    return false
}

/******************************************************************/