0

我在为 IOS 集成 Facebook SDK 时遇到了一个奇怪的问题,而且它并没有在我的所有设备上发生。在我的 iPhone 上它运行良好,但在我的 ipad 上它给了我问题。我的应用程序中有一个共享按钮,当我按下它时,我的 iPhone 中会出现一个请求许可的屏幕,然后发布。

在我的 iPad 上,它会打开一个屏幕,我应该在其中输入用户名和密码,但它不会从已经登录的 Facebook 帐户中获取它们。

这是我的代码:

- (void)sharetoMyWall{
    if (!FBSession.activeSession.state == FBSessionStateCreatedTokenLoaded) {
        NSLog(@" first time access");
        [self openSession];
     } else if (!FBSession.activeSession.isOpen) {
         NSLog(@"active session is not open");
         [self openSession];
     }

    if (FBSession.activeSession.isOpen) {
     NSArray *permissions =
     [NSArray arrayWithObjects:@"publish_stream", nil];

        if ([[[FBSession activeSession]permissions]indexOfObject:@"publish_actions"] == NSNotFound) {
            [[FBSession activeSession] requestNewPublishPermissions:permissions
                                             defaultAudience:FBSessionDefaultAudienceFriends
                                             completionHandler:^(FBSession *session, NSError *error) {
                                                 //handle success + failure in block
                                                 [self postToMyWall];
                                             }];
            } else {
                [self postToMyWall];
            }
     }

- (void)openSession
{



    [FBSession openActiveSessionWithReadPermissions:nil
                                       allowLoginUI:YES
                                  completionHandler:
     ^(FBSession *session,
       FBSessionState state, NSError *error) {
         [self sessionStateChanged:session state:state error:error];
     }];
}

我错过了什么?

还有一个问题:Facebook 应该从“设置”>“Facebook”以“myname”身份登录,还是只需要登录。因为这是我的 iPhone 和 iPad 之间的唯一区别。在我的 iPhone 中,我可以看到我从设置中登录,而在 iPad 中我从设置中注销(尽管我在 Facebook 应用程序本身中登录)

4

1 回答 1

3

试试这个代码

- (IBAction)facebookShareBtn_TouchUpInside:(UIButton *)sender
{
   //Ask for publish_actions permissions in context
        if ([FBSession.activeSession.permissions
             indexOfObject:@"publish_actions"] == NSNotFound) {
            // Permission hasn't been granted, so ask for publish_actions
            [FBSession openActiveSessionWithPublishPermissions:@[@"publish_actions"]
                                               defaultAudience:FBSessionDefaultAudienceFriends
                                                  allowLoginUI:YES
                                             completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {
                                                 if (FBSession.activeSession.isOpen && !error) {
                                                     // Publish the story if permission was granted
                                                     [self publishStory];
                                                 }
                                             }];
        } else {
            // If permissions present, publish the story
            [self publishStory];
        }
}


    - (void)publishStory
    {
        NSMutableDictionary *params = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"Me Something", @"name",@"Think of it as your personal.So what are you waiting for.....!", @"description", @"http://support24hour.com/workplace2/askie/ic_launcher.png", @"picture", nil];

        [FBRequestConnection startWithGraphPath:@"me/feed" parameters:params HTTPMethod:@"POST" completionHandler:^(FBRequestConnection *connection, id result, NSError *error)
        {
             NSString *alertText;
             if (error) {
                 alertText = [NSString stringWithFormat:
                              @"error: domain = %@, code = %d",
                              error.domain, error.code];
             } else {
                 alertText = [NSString stringWithFormat:
                              @"Posted action, id: %@",
                              result[@"id"]];
             }
             // Show the result in an alert
             [[[UIAlertView alloc] initWithTitle:@"Result" message:alertText delegate:self cancelButtonTitle:@"OK!" otherButtonTitles:nil]show];
         }];
    }
于 2013-08-06T12:10:16.297 回答