将文字分享到 Facebook Objective-C

Share text to Facebook Objective-C

我正在尝试通过我的 iOS 应用程序在 Facebook 上分享一些文本。 我已经设法让它在这样的 Swift 书面应用程序中工作:

@IBAction func shareAction(sender: AnyObject) {

if SLComposeViewController.isAvailableForServiceType(SLServiceTypeFacebook){
    var facebookSheet:SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
    facebookSheet.setInitialText("Preadded text")
    self.presentViewController(facebookSheet, animated: true, completion: nil)

} else {

    var alert = UIAlertController(title: "Accounts", message: "Please login to a Facebook account to share.", preferredStyle: UIAlertControllerStyle.Alert)
    alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
    self.presentViewController(alert, animated: true, completion: nil)

} }

但是,我的代码是用Objective-C写的。我的问题是,在 Objective-C 中是否有类似的写法?

您可以通过实施 Social Framework

-(void)fbButton {
    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
        fbSLComposeViewController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
        [fbSLComposeViewController addImage:someImage];
        [fbSLComposeViewController setInitialText:@"Some Text"];
        [self presentViewController:fbSLComposeViewController animated:YES completion:nil];

        fbSLComposeViewController.completionHandler = ^(SLComposeViewControllerResult result) {
            switch(result) {
                case SLComposeViewControllerResultCancelled:
                    NSLog(@"facebook: CANCELLED");
                    break;
                case SLComposeViewControllerResultDone:
                    NSLog(@"facebook: SHARED");
                    break;
            }
        };
    }
    else {
        UIAlertView *fbError = [[UIAlertView alloc] initWithTitle:@"Facebook Unavailable" message:@"Sorry, we're unable to find a Facebook account on your device.\nPlease setup an account in your devices settings and try again." delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil];
        [fbError show];
    }
}

这个怎么样?

- (IBAction)postToFacebook:(id)sender {
    if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
            SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];

        [controller setInitialText:@"First post from my iPhone app"];
        [self presentViewController:controller animated:YES completion:Nil];        
    }
}

Reference