Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook iOS Authorise and Post Without Dialog?

Im just starting out with the Facebook SDK for iOS and checked out the documentation and other help thoroughly but can't be successful.

I have started a new view based app and authorised just as recommended in the docs. Everytime I start the app its switches to the Facebook app (which I have installed on my iPhone) and says already authorised, press okay. How can I stop it repeatedly doing this?

I also have tried posting to Facebook without a dialog. The console tells me a request is made but then a error occurs (doesn't crash but the didFailWithError tells me).

Anyway I haven't posted any of my code as it seems relatively simple so if there is someone who knows how to do this I would massively appreciate any help, and possibly even a code sample.

Thanks.

like image 637
Josh Kahane Avatar asked May 26 '11 20:05

Josh Kahane


1 Answers

You're missing a key point in your delegate, you have to save the session data yourself

- (void)fbDidLogin {
    // store the access token and expiration date to the user defaults
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:facebook.accessToken forKey:ACCESS_TOKEN_KEY];
    [defaults setObject:facebook.expirationDate forKey:EXPIRATION_DATE_KEY];
    [defaults synchronize];
}

And then when you initialize facebook you would do the following

facebook = [[Facebook alloc] initWithAppId:kAppId];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
facebook.accessToken = [defaults objectForKey:ACCESS_TOKEN_KEY];
facebook.expirationDate = [defaults objectForKey:EXPIRATION_DATE_KEY];

And finally, if you want to post without a dialog you would do this

NSString *message = @"Visit my blog http://effectivemobility.blogspot.com/";
NSArray *obj = [NSArray arrayWithObjects:message, nil];
NSArray *keys = [NSArray arrayWithObjects:@"message", nil];

// There are many other params you can use, check the API
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjects:obj forKeys:keys];

[facebook requestWithGraphPath:@"me/feed" andParams:params andHttpMethod:@"POST" andDelegate:nil];

I hope that helps

like image 115
arclight Avatar answered Oct 14 '22 16:10

arclight