我不使用登录按钮。相反,在 appdelegate 中有一个 make 函数来获取 FBSession。
- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI andCompletionBlock:(void (^)(void)) action{
BOOL theResult = [FBSession openActiveSessionWithPublishPermissions:[NSArray arrayWithObject:@"publish_actions"] defaultAudience:FBSessionDefaultAudienceFriends allowLoginUI:allowLoginUI
completionHandler:^(FBSession *session,
FBSessionState state,
NSError *error) {
if (state == FBSessionStateOpen) {
action();
}
//action();
[self sessionStateChanged:session
state:state
error:error];
}];
return theResult;
}
然后当我想发布时,我这样做:
if (!FBSession.activeSession.isOpen) {
[theAppDelegate openSessionWithAllowLoginUI:YES andCompletionBlock:^{
//Your Code
[self postToWall];
}];
}
else {
[self postToWall];
}
- (void) performPublishAction:(void (^)(void)) action {
// we defer request for permission to post to the moment of post, then we check for the permission
if ([FBSession.activeSession.permissions indexOfObject:@"publish_actions"] == NSNotFound) {
[FBSession.activeSession requestNewPublishPermissions:[NSArray arrayWithObject:@"publish_actions"]
defaultAudience:FBSessionDefaultAudienceFriends
completionHandler:^(FBSession *session, NSError *error) {
if (!error) {
action();
}
}];
} else {
action();
}
}
-(void)postToWall {
[self performPublishAction:^{
NSMutableDictionary* params1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:
kFacebookAppID, @"api_key",
kLink, @"link",
photo, @"picture",
kAppName, @"name",
kDescription, @"description",
nil];
FBRequestConnection *connection = [[FBRequestConnection alloc] init];
FBRequest *theRequest = [[FBRequest alloc] initWithSession:[FBSession activeSession] graphPath:@"me/photos" parameters:params1 HTTPMethod:@"POST"];
[connection addRequest:theRequest completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
NSLog(@"Result from Facebook request: %@",result);
if (!error) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"UploadComplete" object:nil];
}
else
{
}
}];
[connection start];
}];
}
也添加这些功能。
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
// attempt to extract a token from the url
return [FBSession.activeSession handleOpenURL:url];
}
/*
* Callback for session changes.
*/
- (void)sessionStateChanged:(FBSession *)session state:(FBSessionState) state error:(NSError *)error
{
switch (state) {
case FBSessionStateOpen:
if (!error) {
// We have a valid session
NSLog(@"User session found");
}
break;
case FBSessionStateClosed:
case FBSessionStateClosedLoginFailed:
[FBSession.activeSession closeAndClearTokenInformation];
break;
default:
break;
}
[[NSNotificationCenter defaultCenter]
postNotificationName:FBSessionStateChangedNotification
object:session];
if (error) {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"Error"
message:error.localizedDescription
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
}
}
希望这可以帮助!