Fetching Photos from google photos Objective c [iOS]

不问归期 提交于 2019-12-02 08:42:55

Ok finally i fetched google photos with Google Drive, hopefully this answer will be useful for someone in future.Many one suggested Picasa but i achieved it with google drive.I will let you know the steps

1)Install Pod

pod 'GoogleAPIClientForREST/Drive', '~> 1.2.1'

ViewController.h

@property (nonatomic, strong) GTLRDriveService *service;

ViewDidLoad

    GIDSignIn* signIn = [GIDSignIn sharedInstance];
    signIn.delegate = self;
    signIn.uiDelegate = self;
    signIn.scopes = [NSArray arrayWithObjects:kGTLRAuthScopeDrivePhotosReadonly, nil];
    [signIn signInSilently];

// Initialize the service object.
    self.service = [[GTLRDriveService alloc] init];
    self.service.shouldFetchNextPages = YES;

After making Signin and getting permission it will redirect to this method

- (void)signIn:(GIDSignIn *)signIn
didSignInForUser:(GIDGoogleUser *)user
     withError:(NSError *)error
{
    if (error != nil)
    {
        self.service.authorizer = nil;
    }
    else
    {
        self.service.authorizer = user.authentication.fetcherAuthorizer;

        [self listFiles];
    }
}

Fetching Photos and storing in NsmutableArray

//Google photos is Nsmutablearray

- (void)listFiles
{

    GTLRDriveQuery_FilesList *query = [GTLRDriveQuery_FilesList query];
    query.q = @"mimeType='image/jpeg'";
    query.spaces = @"photos";
    query.pageSize = 200;
    query.fields = @"nextPageToken,files(id,name,mimeType,thumbnailLink,originalFilename)";
    [self.service executeQuery:query completionHandler:^(GTLRServiceTicket *ticket,
                                                         GTLRDrive_FileList *result,
                                                         NSError *error)
     {
         if (error == nil)
         {
             [GooglePhotos removeAllObjects];


             for (GTLRDrive_File *fileInput in result.files)
             {
                 [SVProgressHUD show];

                 NSMutableDictionary*Mydiction=[[NSMutableDictionary alloc]init];

                 [Mydiction setObject:fileInput.thumbnailLink forKey:@"IMAGEURL"];
                 [Mydiction setObject:fileInput.identifier forKey:@"UNIQUEKEY"];
                 [Mydiction setObject:fileInput.originalFilename forKey:@"ORIGINALFILENAME"];
                 [Mydiction setObject:fileInput.name forKey:@"NAME"];
                 [Mydiction setObject:fileInput.mimeType forKey:@"MIMETYPE"];


                 [GooglePhotos removeObject:Mydiction];
                 [GooglePhotos addObject:Mydiction];

                // NSLog(@"PrintDetails %@",GooglePhotos);
             }

             [SVProgressHUD dismiss];

         } else
         {
             NSLog(@"An error occurred: %@", error);
         }
     }];
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!