我剛剛更新到最新的SDK FB大約2個星期前。下面是我用同樣的方式:
//FB recommends to put these two in the app delegate in their sample apps but you can place them other places
- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI {
NSArray *permissions = [NSArray arrayWithObjects:@"publish_actions", nil];
return [FBSession openActiveSessionWithPermissions:permissions
allowLoginUI:allowLoginUI
completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {
NSLog((@"session.state is %d",session.state));
if(session.state==513){
[AppPrefererences sharedAppPrefererences].faceBookLoggedIn=YES;
NSLog((@"facebookLogin pref has been set to yes from inside appDelegate"));
}
else{
[AppPrefererences sharedAppPrefererences].faceBookLoggedIn=NO;
}
}];
}
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
// FBSample logic
// We need to handle URLs by passing them to FBSession in order for SSO authentication
// to work.
return [FBSession.activeSession handleOpenURL:url];
}
//Then whereever you want to initiate the log in (i use mine in a tableView)
AppDelegate *appDelegate =(AppDelegate*) [[UIApplication sharedApplication]delegate];
if (![appDelegate openSessionWithAllowLoginUI:NO]) {
[appDelegate openSessionWithAllowLoginUI:YES];
[email protected]"Facebook Log Out";
[AppPrefererences sharedAppPrefererences].faceBookEnabled=YES;
}
else{
[email protected]"Facebook Log In";
[AppPrefererences sharedAppPrefererences].faceBookLoggedIn=NO;
[FBSession.activeSession closeAndClearTokenInformation];
}
然後whererever您想發表你這樣做:
[FBRequestConnection startForPostStatusUpdate:@"any thing you want to post"
completionHandler:^(FBRequestConnection *connection, id result, NSError *error){}];
而且,也不要忘記設置FBLoginViewDelegate
設我知道你是否有任何問題!希望能幫助到你!
什麼是AppPreferences? – Stackie
哦,我的不好,那只是我用來實現nsuserdefaults的自定義類。只是忽略這一部分 –