2012-09-30 40 views
0

我仍然使用舊的臉譜圖api在我的應用程序的用戶牆上發佈內容。由於iOS6的facebook已經集成到操作系統中。我如何使用它來發布內容?我想用同樣的方式作爲Twitter的框架:如何在iOS6的應用程序中整合Facebook框架?

TWTweetComposeViewController *tweetViewController = [[TWTweetComposeViewController alloc] init]; 
    [tweetViewController setInitialText:@"bla bla bla"]; 
    [self dismissModalViewControllerAnimated:YES]; 
    [self presentModalViewController:tweetViewController animated:YES]; 

回答

7

與iOS 6開始,你可以使用SLComposeViewController對所有支持的業務類型,如Twitter,Facebook和微博。 注意:不要忘記添加Social.framework

if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) { 
    SLComposeViewController *fb = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook]; 
    [fb setInitialText:@"bla bla bla"]; 
    [self presentModalViewController:fb animated:YES]; 
} 
+0

哇,得到了一大堆更容易:)非常感謝你!確實是 – doonot

+1

。如果我想在應用程序牆上發佈信息,該怎麼辦? – Tibi

3

如果你希望你的帖子去「通過我的應用程序名稱」,您可以使用SLRequest:

-(IBAction)postMessage:(id)sender 
{ 
    // Create the URL to the end point 
    NSURL *postURL = [NSURL URLWithString:@"https://graph.facebook.com/me/feed"]; 

    NSString *link = [[NSString alloc] init]; 
    NSString *message = [[NSString alloc] init]; 
    NSString *picture = [[NSString alloc] init]; 
    NSString *name = [[NSString alloc] init]; 
    NSString *caption = [[NSString alloc] init]; 
    NSString *description = [[NSString alloc] init]; 

    link = @"http://developer.apple.com/library/ios/#documentation/Social/Reference/Social_Framework/_index.html%23//apple_ref/doc/uid/TP40012233"; 
    message = @"Testing Social Framework"; 
    picture = @"http://www.stuarticus.com/wp-content/uploads/2012/08/SDKsmall.png"; 
    name = @"Social Framework"; 
    caption = @"Reference Documentation"; 
    description = @"The Social framework lets you integrate your app with supported social networking services. On iOS and OS X, this framework provides a template for creating HTTP requests. On iOS only, the Social framework provides a generalized interface for posting requests on behalf of the user."; 

    NSDictionary *postDict = @{ 
    @"link": link, 
    @"message" : message, 
    @"picture" : picture, 
    @"name" : name, 
    @"caption" : caption, 
    @"description" : description 
    }; 

    SLRequest *postToMyWall = [SLRequest requestForServiceType:SLServiceTypeFacebook requestMethod:SLRequestMethodPOST URL:postURL parameters:postDict]; 

    FacebookAccountManager* sharedManager = [FacebookAccountManager sharedAccount]; 
    [postToMyWall setAccount:sharedManager.facebookAccount]; 

    [postToMyWall performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) 
    { 
     if (error) { 
      // If there is an error we populate the error string with error 
      _errorString = [NSString stringWithFormat:@"%@", [error localizedDescription]]; 

      // We then perform the UI update on the main thread. All UI updates must be completed on the main thread. 
      [self performSelectorOnMainThread:@selector(updateErrorString) withObject:nil waitUntilDone:NO]; 
     } 

     else 
     { 
      NSLog(@"Post successful"); 
      NSString *dataString = [[NSString alloc] initWithData:responseData encoding:NSStringEncodingConversionAllowLossy]; 
      NSLog(@"Response Data: %@", dataString); 
     } 
    }]; 

    // Tidy Up 
    link = nil; 
    message = nil; 
    picture = nil; 
    name = nil; 
    caption = nil; 
    description = nil; 
    postDict = nil; 
    postToMyWall = nil; 
    postURL = nil; 

} 

http://theworkingbear.com/social-framework-reference/爲全演練

相關問題