我最近在我的应用程序中添加了 Facebook 集成,除了用户重新启动应用程序外,一切正常。每次重新启动应用程序时,Facebook 都必须返回其授权过程。这涉及将应用程序切换到 Safari/Facebook,然后再切换回我的应用程序。我怎样才能让 Facebook 保存信息,或者能够获得我的应用程序的一揽子权限,这样我就不会经常重新授权?
这是我的applicationDidFinishLaunchingWithOptions:
方法代码:
facebook = [[Facebook alloc] initWithAppId:@"203604286395694" andDelegate:self];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"FBAccessTokenKey"]
&& [defaults objectForKey:@"FBExpirationDateKey"]) {
facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
}
if (![facebook isSessionValid]) {
[facebook authorize:nil];
}
相关 Facebook 委托方法:
- (void)fbDidLogin {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"];
[defaults synchronize];
}
- (void) requestDialogButtonClicked {
NSMutableDictionary* params =
[NSMutableDictionary dictionaryWithObjectsAndKeys:
@"invites you to check out cool stuff", @"message",
@"Check this out", @"notification_text",
nil];
[facebook dialog:@"apprequests"
andParams:params
andDelegate:self];
}
- (void)dialogDidComplete:(FBDialog *)dialog {
NSLog(@"dialog completed successfully");
}