相册权限问题

前提

info.plist 配置

  • Privacy - Photo Library Additions Usage Description
  • Privacy - Photo Library Usage Description

关于权限请求

 func authorize(_ status: PHAuthorizationStatus = PHPhotoLibrary.authorizationStatus(), completion: @escaping (_ authorized: Bool) -> Void) {
        switch status {
        case .authorized:
            // We are authorized. Run block
            completion(true)
            
            // 重新获取数据源
            self.photos = PhotoCollectionViewController.loadPhotos()
            self.collectionView?.reloadData()
        case .notDetermined:
            // Ask user for permission
            PHPhotoLibrary.requestAuthorization({ (status) -> Void in
                DispatchQueue.main.async(execute: { () -> Void in
                    self.authorize(status,completion: completion)
                })
            })
        default: ()
        DispatchQueue.main.async(execute: { () -> Void in
            completion(false)
        })
        }
    }

说明

ps:
我这里是用 StoryBoard, 点击已经跳转过去了, 所以刷新操作写在了方法里,没想到其他办法

 // 重新获取数据源
            self.photos = PhotoCollectionViewController.loadPhotos()
            self.collectionView?.reloadData()

其实完全可以在点击时判断权限,没权限就不让跳转了
pps:
权限代码来自BSImagePicker,一个优秀的图片获取库

Demo

Demo
.../PhotoCollectionViewController.swift

你可能感兴趣的:(相册权限问题)