IOS swift creating app folder error

别等时光非礼了梦想. 提交于 2019-12-19 11:51:32

问题


I got problem with creating folder for PhotoLibrary. Does anyone know what is wrong there?

            var albumPlaceholder:PHObjectPlaceholder!
            //create the folder
            NSLog("\nFolder \"%@\" does not exist\nCreating now...", albumName)
            PHPhotoLibrary.sharedPhotoLibrary().performChanges({
                let request = PHAssetCollectionChangeRequest.creationRequestForAssetCollectionWithTitle(albumName)
                albumPlaceholder = request.placeholderForCreatedAssetCollection
                },
                completionHandler: {(success:Bool, error:NSError!)in
                    NSLog("Creation of folder -> %@", (success ? "Success":"Error!"))
                    self.albumFound = (success ? true:false)
                    if(success){
                        let collection = PHAssetCollection.fetchAssetCollectionsWithLocalIdentifiers([albumPlaceholder.localIdentifier], options: nil)
                        self.assetCollection = collection?.firstObject as PHAssetCollection
                    }
            })

This code all the time prints Error! on console. Thanks in advance!

[Update] My error is sth like this.

Creation of folder -> Error! ViewWillAppear Error Domain=NSCocoaErrorDomain Code=2047 "Photos Access not allowed (authorization status 0)" UserInfo=0x7fd1fb664cc0 {NSLocalizedDescription=Photos Access not allowed (authorization status 0)}


回答1:


You don't have permission to access Photo Library. You need to request it first. So use the following code for that:

PHPhotoLibrary.requestAuthorization
{ (PHAuthorizationStatus status) -> Void in
     switch (status)
     {
        case .Authorized:
            // Permission Granted
            println("Write your code here")
        case .Denied:
            // Permission Denied
            println("User denied")
        default:
            println("Restricted")
        }
    }

For more info refer : requestAuthorization




回答2:


The NSError tells you what the problem is:

Photos Access not allowed (authorization status 0)

The problem is that you have failed to obtain authorization from the user to use the photos library.

For more info, see the docs on requestAuthorization: - https://developer.apple.com/library/ios/documentation/Photos/Reference/PHPhotoLibrary_Class/index.html#//apple_ref/occ/clm/PHPhotoLibrary/requestAuthorization:



来源:https://stackoverflow.com/questions/27671283/ios-swift-creating-app-folder-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!