IOS迅速创建应用程序文件夹错误

时间:2014-12-27 20:41:25

标签: ios iphone swift ios8

我在为PhotoLibrary创建文件夹时遇到了问题。 有谁知道那里有什么问题?

            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
                    }
            })

此代码始终在控制台上打印Error!。提前谢谢!

[更新] 我的错误是这样的。

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)}

2 个答案:

答案 0 :(得分:4)

您无权访问照片库。 你需要先申请。因此,请使用以下代码:

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")
        }
    }

有关详细信息,请参阅:requestAuthorization

答案 1 :(得分:1)

NSError告诉您问题所在:

  

照片不允许访问(授权状态0)

问题是您未能获得用户授权使用照片库。

有关详细信息,请参阅requestAuthorization: - https://developer.apple.com/library/ios/documentation/Photos/Reference/PHPhotoLibrary_Class/index.html#//apple_ref/occ/clm/PHPhotoLibrary/requestAuthorization上的文档: