4
votes

I'm working on an app that takes pictures automatically during certain events and saves them directly to the photo library. Unfortunately the user might not always be looking at their phone when this is running and the first time a picture is taken it asks for permission to access photos.

Is there a way I can force this request during launch and get it out of the way? Thanks

1

1 Answers

6
votes

Yes. You need to request access to the library.
Simply use this code in AppDelegate's didFinishLaunchingWithOptions

ALAssetsLibraryGroupsEnumerationResultsBlock assetGroupEnumerator =
^(ALAssetsGroup *assetGroup, BOOL *stop) {
    if (assetGroup != nil) {
        // do somthing
     }
};

ALAssetsLibraryAccessFailureBlock assetFailureBlock = ^(NSError *error) {
    LogError(@"Error enumerating photos: %@",[error description]);

};

NSUInteger groupTypes = ALAssetsGroupAll;

[library enumerateGroupsWithTypes:groupTypes usingBlock:assetGroupEnumerator failureBlock:assetFailureBlock];