I have the following code which I got from this forum, and works perfectly. However the first time when it creates the album, requests the user for permission to access his photos. That causes the album doesn't be create until the second time the application is run.
How can I control that?, in order that the album can be created the first time that is requested. Sorry if my english is not good, I only know a little bit. Hope somebody can help me!
Finally here is the code:
import Photos
var assetCollection: PHAssetCollection!
var albumFound : Bool = false
var photosAsset: PHFetchResult!
var assetThumbnailSize:CGSize!
var collection: PHAssetCollection!
var assetCollectionPlaceholder: PHObjectPlaceholder!
func createAlbum() {
let fetchOptions = PHFetchOptions()
fetchOptions.predicate = NSPredicate(format: "title = %@", "MyAlbum")
let collection : PHFetchResult = PHAssetCollection.fetchAssetCollectionsWithType(.Album, subtype: .Any, options: fetchOptions)
if let first_obj: AnyObject = collection.firstObject {
self.albumFound = true
assetCollection = collection.firstObject as PHAssetCollection
} else {
PHPhotoLibrary.sharedPhotoLibrary().performChanges({
var createAlbumRequest : PHAssetCollectionChangeRequest = PHAssetCollectionChangeRequest.creationRequestForAssetCollectionWithTitle("MyAlbum")
self.assetCollectionPlaceholder = createAlbumRequest.placeholderForCreatedAssetCollection
}, completionHandler: { success, error in
self.albumFound = (success ? true: false)
if (success) {
var collectionFetchResult = PHAssetCollection.fetchAssetCollectionsWithLocalIdentifiers([self.assetCollectionPlaceholder.localIdentifier], options: nil)
print(collectionFetchResult)
assetCollection = collectionFetchResult?.firstObject as PHAssetCollection
}
})
}}