How to Post to Facebook on iOS 6 in Objective-C using ACAccountStore class

前端 未结 5 2131
无人及你
无人及你 2020-12-07 17:21

I want to know how to post a status message to Facebook on iOS 6 using the new frameworks on Xcode 4.5. Thanks! :)

5条回答
  •  醉话见心
    2020-12-07 17:40

    This is how I actually use it in my app though. Smoother and lets the controller do the hard work.

    - (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];
            }
      }];
    }
    

提交回复
热议问题