如何检查Facebook是否已登录设备?目标C.

时间:2013-04-16 18:34:28

标签: iphone ios objective-c facebook

我正在将Facebook SDK应用到我的应用中。我正在使用FBLoginView登录Facebook。我有UIButton,我用它在用户的Facebook墙上分享。现在我不想使用FBLoginView登录,我想检查是否有Facebook应用程序,以及用户是否已登录。

- (IBAction)pickFriendsList:(UIButton *)sender
{
    FBFriendPickerViewController *friendPickerController = [[FBFriendPickerViewController alloc] init];
    friendPickerController.title = @"Pick Friends";
    [friendPickerController loadData];

    // Use the modal wrapper method to display the picker.
    [friendPickerController presentModallyFromViewController:self animated:YES handler:
     ^(FBViewController *sender, BOOL donePressed) {

         if (!donePressed) {
             return;
         }
         NSString* fid;
         NSString* fbUserName;

         for (id<FBGraphUser> user in friendPickerController.selection)
         {
             NSLog(@"\nuser=%@\n", user);
             fid = user.id;

             fbUserName = user.name;

             NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"test", @"message", @"http://webecoist.momtastic.com/wp-content/uploads/2009/01/nature-wonders.jpg", @"picture", @"Sample App", @"name",fid,@"tags",fid,@"to",@"106377336067638",@"place", nil];


                 [FBRequestConnection startWithGraphPath:[NSString stringWithFormat:@"%@/feed",fid] parameters:params               HTTPMethod:@"POST" completionHandler:^(FBRequestConnection *connection,id result,NSError *error)
                                          {

             [FBWebDialogs presentFeedDialogModallyWithSession:nil                                                     parameters:params  handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error)

              {
                  if (error)
                  {
                      // Error launching the dialog or publishing a story.

                      NSLog(@"Error publishing story.");
                  }
                  else
                  {
                      if (result == FBWebDialogResultDialogNotCompleted) {
                          // User clicked the "x" icon
                          NSLog(@"User canceled story publishing.");
                      }
                      else
                      {

                          // Handle the publish feed callback

                               //Tell the user that it worked.

                           }


                  }
              }];
                                              }];

         }

     }];

}

2 个答案:

答案 0 :(得分:3)

我假设你使用的是iOS 6 sdk。在这种情况下,请使用以下代码(适用于设备)

if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) 
{

//user is already logged in using iOS integrated FB a/c

}
else
{

//user is not logged in

}

注意:FBSession无法检查此条件。因此,使用FBSession进行会话检查与上面的代码具有不同的含义。

此致

答案 1 :(得分:0)

检查URL Scheme fb://

如果安装了Facebook App,

[[UIApplication sharedApplication] canOpenURL:@"fb://"]将返回YES。这篇文章列出了Facebook的可用URL方案:https://stackoverflow.com/a/5707825/1511668

也许fb:// online就是你想要的。