2012-09-20 15 views
24

我想知道如何使用Xcode 4.5上的新框架向iOS 6上的Facebook發佈狀態消息。謝謝! :)如何使用ACAccountStore在Objective-C的iOS 6上發佈到iOS上的Facebook

+0

有很好的一套教程這裏:http://developers.facebook.com/docs/getting-started/getting-started-with-the-ios-sdk/ –

+0

不,我的意思使用iOS 6上的新框架。 – jaytrixz

+1

您可以使用社交框架獲取更多詳細信息,請訪問http://kmithi.blogspot.in/2012/10/integrating-facebook-and-twitter-in-ios.html – mithilesh

回答

75

發佈消息相當簡單。這幾乎就像Twitter框架。

首先你要導入的框架:社會和應收

#import <Social/Social.h> 
#import <Accounts/Accounts.h> 

在您的.h文件中:

SLComposeViewController *mySLComposerSheet; 

這些代碼被包含在.m文件的行動中:

if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) //check if Facebook Account is linked 
    { 
     mySLComposerSheet = [[SLComposeViewController alloc] init]; //initiate the Social Controller 
     mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook]; //Tell him with what social platform to use it, e.g. facebook or twitter 
       [mySLComposerSheet setInitialText:[NSString stringWithFormat:@"Test",mySLComposerSheet.serviceType]]; //the message you want to post 
     [mySLComposerSheet addImage:yourimage]; //an image you could post 
     //for more instance methods, go here: https://developer.apple.com/documentation/social/slcomposeviewcontroller#//apple_ref/doc/uid/TP40012205 
     [self presentViewController:mySLComposerSheet animated:YES completion:nil]; 
    } 
    [mySLComposerSheet setCompletionHandler:^(SLComposeViewControllerResult result) { 
     NSString *output; 
     switch (result) { 
      case SLComposeViewControllerResultCancelled: 
       output = @"Action Cancelled"; 
       break; 
      case SLComposeViewControllerResultDone: 
       output = @"Post Successful"; 
       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]; 
    }]; 
+0

@Blade thans。但我想分享沒有像「TWRequest」這樣的動作表的消息,那麼Facebook中的流程是什麼? – Hitarth

+0

@Blade Grt suggetion。它的工作很好,但我有一個查詢,當我發回相同的消息比我得到兩個彈出。 1.發佈成功並且2.由於連接到Facebook失敗,帖子無法發送。我只想要一個彈出窗口,以便用戶獲得想法。從這兩個彈出用戶變得困惑,發佈成功與否。如果你有任何想法,請告訴我。 – Hitarth

+1

@Blade是否可以更改「通過iOS共享鏈接」要「通過MyAppName共享鏈接」? – Hitarth

1

我該怎麼辦,當我只想在郵件成功時收到提醒,以及用戶取消帖子後什麼都沒有?

不幸的是,這對Twitter無法正常工作...它不會消除TweetSheet了。這裏是我的代碼:

if(NSClassFromString(@"SLComposeViewController") != nil) 
{ 

     mySLComposerSheet = [[SLComposeViewController alloc] init]; //initiate the Social Controller 
     mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter]; //Tell him with what social plattform to use it, e.g. facebook or twitter 
     [mySLComposerSheet setInitialText:[NSString stringWithFormat:story.title,mySLComposerSheet.serviceType]]; //the message you want to post 
     [mySLComposerSheet addURL:[NSURL URLWithString:story.link]]; 
     //for more instance methodes, go here:https://developer.apple.com/library/ios/#documentation/NetworkingInternet/Reference/SLComposeViewController_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40012205 
     [mySLComposerSheet setCompletionHandler:^(SLComposeViewControllerResult result) { 
      NSString *output; 
      switch (result) { 
       case SLComposeViewControllerResultCancelled: 
        output = NSLocalizedStringFromTable(@"As it seems you didn't want to post to Twitter", @"ATLocalizable", @""); 
        break; 
       case SLComposeViewControllerResultDone: 
        output = NSLocalizedStringFromTable(@"You successfully posted to Twitter", @"ATLocalizable", @""); 
        break; 
       default: 
        break; 
      } //check if everything worked properly. Give out a message on the state. 
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Twitter" message:output delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; 
      [alert show]; 
     }]; 
     [self presentViewController:mySLComposerSheet animated:YES completion:nil]; 
0

好吧,所以我調整了原帖,適用於iOS 6/7。更改Facebook的服務類型,警報標題和消息。請享用!

- (IBAction)tweetMessage:(id)sender { 


    if(![SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) //check if Facebook Account is linked 
    { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Unable to Tweet!" message:@"Please login to Twitter in your device settings." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; 
     [alert show]; 
     return; 
    } 
    //self.mySLComposerSheet = [[SLComposeViewController alloc] init]; //initiate the Social Controller 
    self.mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter]; 
    [self.mySLComposerSheet setInitialText:[NSString stringWithFormat:@"I found this Thing, check it out at this Place:\n %@ \n", [self someplace]]]; 
    [self.mySLComposerSheet addImage:self.photos.firstObject]; 

    [self presentViewController:self.mySLComposerSheet animated:YES completion:nil]; 
    //} 

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

這是我在我的應用程序中實際使用它的方式。更平滑,並讓控制器完成艱苦的工作。

- (IBAction)postToFacebook:(id)sender { 
    if(![SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) { 
     NSLog(@"log output of your choice here"); 
    } 
    // Facebook may not be available but the SLComposeViewController will handle the error for us. 
    self.mySLComposerSheet = [[SLComposeViewController alloc] init]; 
    self.mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook]; 
    [self.mySLComposerSheet setInitialText:[NSString stringWithFormat:@"I found this Thing, check it out at SomeWhere:\n %@ \n", [self someURLString]]]; 
    [self.mySLComposerSheet addImage:self.photos.firstObject]; //an image you could post 

    [self presentViewController:self.mySLComposerSheet animated:YES completion:nil]; 

    [self.mySLComposerSheet setCompletionHandler:^(SLComposeViewControllerResult result) { 
     NSString *output; 
     switch (result) { 
       case SLComposeViewControllerResultCancelled: 
        output = @"Action Cancelled"; 
        break; 
       case SLComposeViewControllerResultDone: 
        output = @"Post Successfull"; 
        break; 
       default: 
        break; 
     } 
     if (![output isEqualToString:@"Action Cancelled"]) { 
       // Only alert if the post was a success. Or not! Up to you. 
       UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Facebook" message:output delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; 
       [alert show]; 
     } 
    }]; 
} 
1
- (IBAction)btn_facebook:(id)sender { 

    SLComposeViewController *facebookcomposer = 
     [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook]; 
    [facebookcomposer setInitialText:@"This is just a test"]; 
    [facebookcomposer addURL:[NSURL URLWithString:@"http://www.google.com"]]; 
    [facebookcomposer addImage:[UIImage imageNamed:@"images.jpg"]]; 

    [self presentViewController:facebookcomposer animated:YES completion:nil]; 

    [facebookcomposer setCompletionHandler:^(SLComposeViewControllerResult result) 
    { 
     switch (result) 
     { 
      case SLComposeViewControllerResultDone: 
       NSLog(@"done"); 
       break; 
      case SLComposeViewControllerResultCancelled: 
       NSLog(@"cancelled"); 
       break; 

      default: 
      break; 
     } 

    }]; 

} 

- (IBAction)btn_twitter:(id)sender { 

    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) { 
     SLComposeViewController *twitter = 
      [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter]; 

     [twitter setInitialText:@"this is just a test"]; 
     [twitter addURL:[NSURL URLWithString:@"http://www.google.com"]]; 
     [twitter addImage:[UIImage imageNamed:@"images.jpg"]]; 
     [self presentViewController:twitter animated:YES completion:nil]; 
    } else { 
     NSLog(@"it is not configured"); 
    } 
} 
相關問題