将Facebook SDK与旧版Xcode集成,以支持iOS 5和iOS 6

时间:2013-03-17 23:28:16

标签: iphone ios facebook facebook-ios-sdk

我仍在使用XCode 4.3.2和iOS 5.1基础SDK。我想要实现的是集成Facebook API for iOS 5.0+设备。只需基本功能,例如代表发布和获取基本用户信息。

我假设哪个Facebook SDK适用于iOS 5,可以在iOS 6上运行,但我不确定。

作为Facebook整合的新手,任何人都可以对这个问题有所了解。 (因为我正在处理该项目,所以不能升级XCode和iOS SDK)

  • 我应该研究哪个iOS SDK?
  • 我可以同时支持iOS 5和 6,基础SDK 5.1和XCode 4.3?
  • 是否有支持iOS版本的API?
  • Facebook整合的必备品吗?

1 个答案:

答案 0 :(得分:2)

首先,您阅读了从Facebook开发者发布的这篇文章

http://developers.facebook.com/docs/getting-started/facebook-sdk-for-ios/

在步骤1中它也适用于xcode 4.3

您仔细阅读了所有步骤

在第6步中,您可以按照Facebook Btn按下的位置编写代码

 - (IBAction)facebookBtnPressed:(id)sender
{


        // if it is available to us, we will post using the native dialog
        BOOL displayedNativeDialog = [FBNativeDialogs presentShareDialogModallyFrom:self
                                                                        initialText:[NSString stringWithFormat:@"Here U write code which u want to post on facebook"]
                                                                              image:[UIImage imageNamed:@"1.jpg"]
                                                                                url:[NSURL URLWithString:@""]
                                                                            handler:nil];

        if (!displayedNativeDialog)
        {
            NSString *shareStr = [NSString stringWithFormat:@"Here U write code which u want to post on facebook"];
            NSMutableDictionary* params = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
                                           shareStr,@"description",
                                           @"sharelink", @"link",
                                           @"ImageName", @"picture",
                                           nil];

            [self performPublishAction:^{

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

                    if (error)
                    {
                        NSLog(@"error in post");
                    }
                    else
                    {
                        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success" message:@"Post Successfully" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
                        [alert show];
                    }

                }];
            }];
        }
    //}

    //===set delagate in Viewcontroller.h of mySLComposerSheet
    [mySLComposerSheet setCompletionHandler:^(SLComposeViewControllerResult result) {
        NSString *output;
        switch (result) {
            case SLComposeViewControllerResultCancelled:
                output = @"Action Cancelled";
                break;
            case SLComposeViewControllerResultDone:
                output = @"Post Successfully";
                break;
            default:
                break;
        } //check if everything worked properly. Give out a message on the state.
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Facebook" message:output delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alert show];
    }];
}

- (void) performPublishAction:(void (^)(void)) action {
    if ([[FBSession activeSession]isOpen]) {
        /*
         * if the current session has no publish permission we need to reauthorize
         */
        if ([[[FBSession activeSession]permissions]indexOfObject:@"publish_actions"] == NSNotFound) {
            [[FBSession activeSession] reauthorizeWithPublishPermissions:[NSArray arrayWithObject:@"publish_actions"]
                                                         defaultAudience:FBSessionDefaultAudienceOnlyMe
                                                       completionHandler:^(FBSession *session, NSError *error) {
                                                           action();
                                                       }];
        }else{
            action();
        }
    }else{
        /*
         * open a new session with publish permission
         */
        [FBSession openActiveSessionWithPublishPermissions:[NSArray arrayWithObject:@"publish_actions"]
                                           defaultAudience:FBSessionDefaultAudienceOnlyMe
                                              allowLoginUI:YES
                                         completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
                                             if (!error && status == FBSessionStateOpen) {
                                                 action();
                                             }else{
                                                 NSLog(@"error");
                                             }
                                         }];
    }
}