3

We're trying to get the user's profile image URL. We are experiencing random behavior. Sometimes the permission dialog is opened in the browser as expected but sometimes the browser opens with the users homepage. any ideas why?

NSArray *permissions = [[NSArray alloc] initWithObjects:@"email",@"publish_actions", nil];
FBSession* session;

session = [[FBSession alloc] initWithAppID:@"xxx" permissions:permissions defaultAudience:FBSessionDefaultAudienceEveryone urlSchemeSuffix:nil tokenCacheStrategy:nil];

[FBSession setActiveSession:session];


// get image

NSString *path = [[NSString alloc] initWithFormat:@"%@/picture?redirect=false&width=170&height=170",fuid];
[FBRequestConnection startWithGraphPath:path completionHandler:^]
Guy
  • 12,488
  • 16
  • 79
  • 119

1 Answers1

1

This is how I would collect the User Profile picture, although there are plenty of ways to do this:

I would use a notification (or any other method) to notify once the user is signed in, then call a method to collect the Facebook Profile picture. Once they are logged in you can pass their facebook Id to a value. (kUserFacebookIDKey)

NSLog(@"Downloading user's profile picture");
// Download user's profile picture
NSURL *profilePictureURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=large", self.kUserFacebookIDKey];
NSURLRequest *profilePictureURLRequest = [NSURLRequest requestWithURL:profilePictureURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0f]; // Facebook profile picture cache policy: Expires in 2 weeks
[NSURLConnection connectionWithRequest:profilePictureURLRequest delegate:self];

Note: You would need to update your header to <NSURLConnectionDataDelegate> and add a property for NSMutableData *_data;

Then handle the response:

#pragma mark - NSURLConnectionDataDelegate
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    _data = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [_data appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    [TESTUtility processFacebookProfilePictureData:_data];
}

Then you can do what you wish with the file. Typically I cache the file and then check if the cached file matches the new and so on...

StuartM
  • 6,743
  • 18
  • 84
  • 160