2013-03-17 113 views
2

我仍在使用XCode 4.3.2和iOS 5.1基本SDK。我想要實現的是整合Facebook API for iOS 5.0+設備。只是基本的功能,如發佈代表和獲取基本的用戶信息。集成舊版Xcode的Facebook SDK以支持iOS 5和iOS 6

我假設哪個Facebook SDK在iOS 5上工作,將在iOS 6上工作,但我不確定這一點。

作爲Facebook整合的新手,任何人都可以在這個問題上找到點子。 (因爲我正在開發項目,所以升級XCode和iOS SDK不是一種選擇)

  • 我應該查看哪個iOS SDK?
  • 我可以同時支持iOS 5和 6,基本SDK 5.1和XCode 4.3?
  • 是否有支持iOS版本的API?
  • Facebook集成的必備條件?

回答

2

首先你讀這篇文章從Facebook開發人員公佈的

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

在步驟1中,也將努力爲Xcode的4.3

你仔細閱讀所有步驟一個接一個n實現

在第6步中,您可以編寫以下代碼:Facebook Btn Pressed

- (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"); 
              } 
             }]; 
    } 
} 
+0

首先,我們沒有社交和AdSupport框架? – Bartu 2013-03-19 22:14:30

+0

如果您將此代碼運行到'xcode'4.3中,則無需添加Social和AdSupport框架。如果使用iOS 6,則將這兩個框架添加爲適用於iOS 5的Optional – 2013-03-20 05:05:03