Skip to content

Commit

Permalink
Implement iOS PHPhotoLibrary authentications
Browse files Browse the repository at this point in the history
  • Loading branch information
takeo-asai committed Jun 15, 2016
1 parent fa08d34 commit 651f360
Showing 1 changed file with 29 additions and 13 deletions.
42 changes: 29 additions & 13 deletions src/ios/SOSPicker.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,40 @@
@interface SOSPicker () <GMImagePickerControllerDelegate>
@end

@implementation SOSPicker
@implementation SOSPicker

@synthesize callbackId;

- (void) hasReadPermission:(CDVInvokedUrlCommand *)command {
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:YES];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:[PHPhotoLibrary authorizationStatus] == PHAuthorizationStatusAuthorized];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

- (void) requestReadPermission:(CDVInvokedUrlCommand *)command {
// [PHPhotoLibrary requestAuthorization:]
// this method works only when it is a first time, see
// https://developer.apple.com/library/ios/documentation/Photos/Reference/PHPhotoLibrary_Class/

PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
if (status == PHAuthorizationStatusAuthorized) {
NSLog(@"Access has been granted.");
} else if (status == PHAuthorizationStatusDenied) {
NSLog(@"Access has been denied. Change your setting > this app > Photo enable");
} else if (status == PHAuthorizationStatusNotDetermined) {
// Access has not been determined. requestAuthorization: is available
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {}];
} else if (status == PHAuthorizationStatusRestricted) {
NSLog(@"Access has been restricted. Change your setting > Privacy > Photo enable");
}

CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

- (void) getPictures:(CDVInvokedUrlCommand *)command {

NSDictionary *options = [command.arguments objectAtIndex: 0];

self.outputType = [[options objectForKey:@"outputType"] integerValue];
BOOL allow_video = [[options objectForKey:@"allow_video" ] boolValue ];
NSString * title = [options objectForKey:@"title"];
Expand All @@ -65,12 +81,12 @@ - (void)launchGMImagePicker:(bool)allow_video title:(NSString *)title message:(N
picker.colsInLandscape = 6;
picker.minimumInteritemSpacing = 2.0;
picker.modalPresentationStyle = UIModalPresentationPopover;

UIPopoverPresentationController *popPC = picker.popoverPresentationController;
popPC.permittedArrowDirections = UIPopoverArrowDirectionAny;
popPC.sourceView = picker.view;
//popPC.sourceRect = nil;

[self.viewController showViewController:picker sender:nil];
}

Expand Down Expand Up @@ -139,9 +155,9 @@ - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
- (void)assetsPickerController:(GMImagePickerController *)picker didFinishPickingAssets:(NSArray *)fetchArray
{
[picker.presentingViewController dismissViewControllerAnimated:YES completion:nil];

NSLog(@"GMImagePicker: User finished picking assets. Number of selected items is: %lu", (unsigned long)fetchArray.count);

NSMutableArray * result_all = [[NSMutableArray alloc] init];
CGSize targetSize = CGSizeMake(self.width, self.height);
NSFileManager* fileMgr = [[NSFileManager alloc] init];
Expand All @@ -153,11 +169,11 @@ - (void)assetsPickerController:(GMImagePickerController *)picker didFinishPickin
CDVPluginResult* result = nil;

for (GMFetchItem *item in fetchArray) {

if ( !item.image_fullsize ) {
continue;
}

do {
filePath = [NSString stringWithFormat:@"%@/%@%03d.%@", docsPath, CDV_PHOTO_PREFIX, i++, @"jpg"];
} while ([fileMgr fileExistsAtPath:filePath]);
Expand Down Expand Up @@ -202,14 +218,14 @@ - (void)assetsPickerController:(GMImagePickerController *)picker didFinishPickin
}
}
}

if (result == nil) {
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:result_all];
}

[self.viewController dismissViewControllerAnimated:YES completion:nil];
[self.commandDelegate sendPluginResult:result callbackId:self.callbackId];

}

//Optional implementation:
Expand All @@ -219,4 +235,4 @@ -(void)assetsPickerControllerDidCancel:(GMImagePickerController *)picker
}


@end
@end

0 comments on commit 651f360

Please sign in to comment.