Created
June 2, 2015 11:34
-
-
Save anonymous/6ff056d4bf29f0866866 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// PHPhotoLibrary_class will only be non-nil on iOS 8.x.x | |
Class PHPhotoLibrary_class = NSClassFromString(@"PHPhotoLibrary"); | |
if (PHPhotoLibrary_class) { | |
/** | |
* | |
iOS 8..x. . code that has to be called dynamically at runtime and will not link on iOS 7.x.x ... | |
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ | |
[PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:title]; | |
} completionHandler:^(BOOL success, NSError *error) { | |
if (!success) { | |
NSLog(@"Error creating album: %@", error); | |
} | |
}]; | |
*/ | |
// dynamic runtime code for code chunk listed above | |
id sharedPhotoLibrary = [PHPhotoLibrary_class performSelector:NSSelectorFromString(@"sharedPhotoLibrary")]; | |
SEL performChanges = NSSelectorFromString(@"performChanges:completionHandler:"); | |
NSMethodSignature *methodSig = [sharedPhotoLibrary methodSignatureForSelector:performChanges]; | |
NSInvocation* inv = [NSInvocation invocationWithMethodSignature:methodSig]; | |
[inv setTarget:sharedPhotoLibrary]; | |
[inv setSelector:performChanges]; | |
void(^firstBlock)() = ^void() { | |
Class PHAssetCollectionChangeRequest_class = NSClassFromString(@"PHAssetCollectionChangeRequest"); | |
SEL creationRequestForAssetCollectionWithTitle = NSSelectorFromString(@"creationRequestForAssetCollectionWithTitle:"); | |
[PHAssetCollectionChangeRequest_class performSelector:creationRequestForAssetCollectionWithTitle withObject:albumName]; | |
}; | |
void (^secondBlock)(BOOL success, NSError *error) = ^void(BOOL success, NSError *error) { | |
if (success) { | |
[assetsLib enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:^(ALAssetsGroup *group, BOOL *stop) { | |
if (group) { | |
NSString *name = [group valueForProperty:ALAssetsGroupPropertyName]; | |
if ([albumName isEqualToString:name]) { | |
groupFound = true; | |
handler(group, nil); | |
} | |
} | |
} failureBlock:^(NSError *error) { | |
handler(nil, error); | |
}]; | |
} | |
if (error) { | |
NSLog(@"Error creating album: %@", error); | |
handler(nil, error); | |
} | |
}; | |
// Set the success and failure blocks. | |
[inv setArgument:&firstBlock atIndex:2]; | |
[inv setArgument:&secondBlock atIndex:3]; | |
[inv invoke]; | |
} | |
else { | |
// code that always creates an album on iOS 7.x.x but fails | |
// in certain situations such as if album has been deleted | |
// previously on iOS 8...x. . | |
[assetsLib addAssetsGroupAlbumWithName:albumName | |
resultBlock:^(ALAssetsGroup *group) { | |
handler(group, nil); | |
} failureBlock:^(NSError *error) { | |
NSLog( @"Failed to create album: %@", albumName); | |
handler(nil, error); | |
}]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment