Untitled diff
1 removal
127 lines
2 additions
128 lines
//
//
// OLFacebookAlbumRequest.m
// OLFacebookAlbumRequest.m
// FacebookImagePicker
// FacebookImagePicker
//
//
// Created by Deon Botha on 15/12/2013.
// Created by Deon Botha on 15/12/2013.
// Copyright (c) 2013 Deon Botha. All rights reserved.
// Copyright (c) 2013 Deon Botha. All rights reserved.
//
//
#import "OLFacebookAlbumRequest.h"
#import "OLFacebookAlbumRequest.h"
#import "OLFacebookImagePickerConstants.h"
#import "OLFacebookImagePickerConstants.h"
#import "OLFacebookAlbum.h"
#import "OLFacebookAlbum.h"
#import <FacebookSDK/FacebookSDK.h>
#import <FacebookSDK/FacebookSDK.h>
@interface OLFacebookAlbumRequest ()
@interface OLFacebookAlbumRequest ()
@property (nonatomic, assign) BOOL cancelled;
@property (nonatomic, assign) BOOL cancelled;
@property (nonatomic, strong) NSString *after;
@property (nonatomic, strong) NSString *after;
@end
@end
@implementation OLFacebookAlbumRequest
@implementation OLFacebookAlbumRequest
+ (void)handleFacebookError:(NSError *)error completionHandler:(OLFacebookAlbumRequestHandler)handler {
+ (void)handleFacebookError:(NSError *)error completionHandler:(OLFacebookAlbumRequestHandler)handler {
NSString *message;
NSString *message;
if ([FBErrorUtility shouldNotifyUserForError:error]) {
if ([FBErrorUtility shouldNotifyUserForError:error]) {
message = [FBErrorUtility userMessageForError:error];
message = [FBErrorUtility userMessageForError:error];
} else if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryAuthenticationReopenSession) {
} else if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryAuthenticationReopenSession) {
message = @"Your current Facebook session is no longer valid. Please log in again.";
message = @"Your current Facebook session is no longer valid. Please log in again.";
} else if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryUserCancelled) {
} else if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryUserCancelled) {
message = @"The app requires authorization to access your Facebook photos to continue. Please open Settings and provide access.";
message = @"The app requires authorization to access your Facebook photos to continue. Please open Settings and provide access.";
} else {
} else {
message = @"Failed to access your Facebook photos. Please check your internet connectivity and try again.";
message = @"Failed to access your Facebook photos. Please check your internet connectivity and try again.";
}
}
handler(nil, [NSError errorWithDomain:error.domain code:error.code userInfo:@{NSLocalizedDescriptionKey: message}], nil);
handler(nil, [NSError errorWithDomain:error.domain code:error.code userInfo:@{NSLocalizedDescriptionKey: message}], nil);
}
}
- (void)cancel {
- (void)cancel {
self.cancelled = YES;
self.cancelled = YES;
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
}
- (void)getAlbums:(OLFacebookAlbumRequestHandler)handler {
- (void)getAlbums:(OLFacebookAlbumRequestHandler)handler {
__block BOOL runOnce = NO;
__block BOOL runOnce = NO;
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[FBSession openActiveSessionWithReadPermissions:@[@"public_profile", @"user_photos"]
[FBSession openActiveSessionWithReadPermissions:@[@"public_profile", @"user_photos"]
allowLoginUI:YES
allowLoginUI:YES
completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {
completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
if (runOnce || self.cancelled) {
if (runOnce || self.cancelled) {
return;
return;
}
}
runOnce = YES;
runOnce = YES;
if (error) {
if (error) {
[OLFacebookAlbumRequest handleFacebookError:error completionHandler:handler];
[OLFacebookAlbumRequest handleFacebookError:error completionHandler:handler];
} else if (!FB_ISSESSIONOPENWITHSTATE(state)) {
} else if (!FB_ISSESSIONOPENWITHSTATE(state)) {
NSString *message = @"Failed to access your Facebook photos. Please check your internet connectivity and try again.";
NSString *message = @"Failed to access your Facebook photos. Please check your internet connectivity and try again.";
handler(nil, [NSError errorWithDomain:kOLErrorDomainFacebookImagePicker code:kOLErrorCodeFacebookImagePickerNoOpenSession userInfo:@{NSLocalizedDescriptionKey: message}], nil);
handler(nil, [NSError errorWithDomain:kOLErrorDomainFacebookImagePicker code:kOLErrorCodeFacebookImagePickerNoOpenSession userInfo:@{NSLocalizedDescriptionKey: message}], nil);
} else {
} else {
// connection is open, perform the request
// connection is open, perform the request
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSString *graphPath = @"me/albums?limit=100&fields=id,name,count,cover_photo";
NSString *graphPath = @"me/albums?limit=100&fields=id,name,count,cover_photo";
if (self.after) {
if (self.after) {
graphPath = [graphPath stringByAppendingFormat:@"&after=%@", self.after];
graphPath = [graphPath stringByAppendingFormat:@"&after=%@", self.after];
}
}
[FBRequestConnection startWithGraphPath:graphPath completionHandler:^(FBRequestConnection *connection,
[FBRequestConnection startWithGraphPath:graphPath completionHandler:^(FBRequestConnection *connection,
id result,
id result,
NSError *error) {
NSError *error) {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
if (self.cancelled) {
if (self.cancelled) {
return;
return;
}
}
NSString *parsingErrorMessage = @"Failed to parse Facebook Response. Please check your internet connectivity and try again.";
NSString *parsingErrorMessage = @"Failed to parse Facebook Response. Please check your internet connectivity and try again.";
NSError *parsingError = [NSError errorWithDomain:kOLErrorDomainFacebookImagePicker code:kOLErrorCodeFacebookImagePickerBadResponse userInfo:@{NSLocalizedDescriptionKey: parsingErrorMessage}];
NSError *parsingError = [NSError errorWithDomain:kOLErrorDomainFacebookImagePicker code:kOLErrorCodeFacebookImagePickerBadResponse userInfo:@{NSLocalizedDescriptionKey: parsingErrorMessage}];
id data = [result objectForKey:@"data"];
id data = [result objectForKey:@"data"];
if (![data isKindOfClass:[NSArray class]]) {
if (![data isKindOfClass:[NSArray class]]) {
handler(nil, parsingError, nil);
handler(nil, parsingError, nil);
return;
return;
}
}
NSMutableArray *albums = [[NSMutableArray alloc] init];
NSMutableArray *albums = [[NSMutableArray alloc] init];
for (id album in data) {
for (id album in data) {
if (![album isKindOfClass:[NSDictionary class]]) {
if (![album isKindOfClass:[NSDictionary class]]) {
continue;
continue;
}
}
id albumId = [album objectForKey:@"id"];
id albumId = [album objectForKey:@"id"];
id photoCount = [album objectForKey:@"count"];
id photoCount = [album objectForKey:@"count"];
id coverPhoto = [album objectForKey:@"cover_photo"];
id name = [album objectForKey:@"name"];
id name = [album objectForKey:@"name"];
if (!([albumId isKindOfClass:[NSString class]] && [photoCount isKindOfClass:[NSNumber class]]
if (!([albumId isKindOfClass:[NSString class]] && [photoCount isKindOfClass:[NSNumber class]]
&& [name isKindOfClass:[NSString class]])) {
&& [coverPhoto isKindOfClass:[NSString class]] && [name isKindOfClass:[NSString class]])) {
continue;
continue;
}
}
OLFacebookAlbum *album = [[OLFacebookAlbum alloc] init];
OLFacebookAlbum *album = [[OLFacebookAlbum alloc] init];
album.albumId = albumId;
album.albumId = albumId;
album.photoCount = [photoCount unsignedIntegerValue];
album.photoCount = [photoCount unsignedIntegerValue];
album.name = name;
album.name = name;
album.coverPhotoURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=small&access_token=%@", album.albumId, session.accessTokenData.accessToken]];
album.coverPhotoURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=small&access_token=%@", album.albumId, session.accessTokenData.accessToken]];
[albums addObject:album];
[albums addObject:album];
}
}
// get next page cursor
// get next page cursor
OLFacebookAlbumRequest *nextPageRequest = nil;
OLFacebookAlbumRequest *nextPageRequest = nil;
id paging = [result objectForKey:@"paging"];
id paging = [result objectForKey:@"paging"];
if ([paging isKindOfClass:[NSDictionary class]]) {
if ([paging isKindOfClass:[NSDictionary class]]) {
id cursors = [paging objectForKey:@"cursors"];
id cursors = [paging objectForKey:@"cursors"];
id next = [paging objectForKey:@"next"]; // next will be non nil if a next page exists
id next = [paging objectForKey:@"next"]; // next will be non nil if a next page exists
if (next && [cursors isKindOfClass:[NSDictionary class]]) {
if (next && [cursors isKindOfClass:[NSDictionary class]]) {
id after = [cursors objectForKey:@"after"];
id after = [cursors objectForKey:@"after"];
if ([after isKindOfClass:[NSString class]]) {
if ([after isKindOfClass:[NSString class]]) {
nextPageRequest = [[OLFacebookAlbumRequest alloc] init];
nextPageRequest = [[OLFacebookAlbumRequest alloc] init];
nextPageRequest.after = after;
nextPageRequest.after = after;
}
}
}
}
}
}
handler(albums, nil, nextPageRequest);
handler(albums, nil, nextPageRequest);
}];
}];
}
}
}];
}];
}
}
@end
@end