ios6.0 调用系统api 分享到 twitter facebook weibo
Overview: Social Features
There are three ways you can share to social media sites:
- The
UIActivityViewController
class allows your app to provide several services in context, one of which would be posting content to social media sites. - The
SLComposeViewController
class is targeted more to social media sites, allowing users of your app to compose a post to send to those sites. - The
SLRequest
is the third way that you can integrate with social media sites. This provides you a way to wrapHTTP requests to these sites to perform servers, such as sharing content directly via an API provided by that site. For example, posting to Facebook requires you to send an HTTP POST to the http://graph.facebook.com/[FacebookUserId]/feed endpoint with the data you wish to post. Using this class requires more work than the previous two but provides more functionality.
I’ll walk through how you can use each of these classes to post content to Facebook and Twitter. Here are the topic areas I’ll cover:
- Sharing via the Share Sheet
- Sharing via the Composer
- Sharing via the Social Media Site APIs (Facebook)
- Sharing via the Social Media Site APIs (Twitter)
Sharing via the Share Sheet
Using the UIActivityViewController
is the easiest way to implement sharing. It’s scope is broad and gives uses the option to share content based on the type of content. For example, if you pass in info that you’re sharing text, users will see options to share via e-mail, SMS, Twitter, Facebook, etc. The advantage again, is the ease of implementation.
Step 1: Add a button to your view that will launch the share sheet.
- Go to your .xib file and add a Round Rect Button to your view.
- Add an action for this button to your view controller implementation file.
Step 2: Add the logic behind the button.
- Let’s find an image that you’ll use for sharing. Find your favorite image (PNG format), download it and drag it into your project.
- Find the action method that was pre-populated in the previous step.
- Add the following code:
NSString *message = @"Hello World!"; UIImage *imageToShare = [UIImage imageNamed:@"test.jpg"]; NSArray *postItems = @[message, imageToShare]; UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:postItems applicationActivities:nil]; [self presentViewController:activityVC animated:YES completion:nil];
Step 3: Build and Test
Build and run the project on an iOS6 simulator. If this is the first time using your simulator log in to your Facebook and Twitter accounts to test out sharing to those social networks. You can log in by going to the device’s Setting app and entering your login info for Twitter and Facebook. Head back to the app and re-launch it. Click on the button. You should see something similar to this when you click the button then select Facebook and next Twitter:
This is the simplest form of sharing. Now let’s move on to the next one, using the SLComposeViewController
class to narrow the share site choices to only social networks.
Build and Test
Build and run the project on an iOS6 simulator. Click on the Facebook composer button. You should see something similar to this:
Click on the Twitter composer button. You should see something similar to this:
出处:https://www.cocoacontrols.com/posts/2012/10/15/social-media-site-sharing-with-the-ios6-sdk