Add sharing to your app via UIActivityViewController

http://www.codingexplorer.com/add-sharing-to-your-app-via-uiactivityviewcontroller/

April 4, 2014

Ever wonder what various apps like Photos or Safari use when you click on the share button? So did I until a few days ago. It is apparently UIActivityViewController. I just learned a bit about how to use it, so I thought I would pass it along.

Update November 10, 2014:  Want to see how to use UIActivityViewController in Swift?  Check out the my newer post Add sharing to your Swift app via UIActivityViewController

Setting the Icon

While not a HUGE step, I thought I might mention it anyway. For my app, I wanted to use the standard Apple sharing button (which in iOS7 is a box with an arrow pointing up from the center). To get it from Interface Builder, you must use a UIBarButtonItem, so this button can only be in a UINavigationBar (part of UINavigationController) or UIToolbar.  You simply add a UIBarButtonItem, and change its “Identifier” in the Attributes Inspector to “Action, as shown in the image.

The Identifier is set to Action for the sharing icon.

Now the Code

This is actually surprisingly easy to do.  So wire that up to an IBAction, here is the gist of how mine was set up.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

- (IBAction)shareButton:(UIBarButtonItem *)sender

{

NSString *textToShare = @"Look at this awesome website for aspiring iOS Developers!";

NSURL *myWebsite = [NSURL URLWithString:@"http://www.codingexplorer.com/"];

NSArray *objectsToShare = @[textToShare, myWebsite];

UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:objectsToShare applicationActivities:nil];

NSArray *excludeActivities = @[UIActivityTypeAirDrop,

UIActivityTypePrint,

UIActivityTypeAssignToContact,

UIActivityTypeSaveToCameraRoll,

UIActivityTypeAddToReadingList,

UIActivityTypePostToFlickr,

UIActivityTypePostToVimeo];

activityVC.excludedActivityTypes = excludeActivities;

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

}

That’s about it.  You make an NSArray of objects to share, and basically pass that along to the UIActivityViewController when you instantiate it.  For my app, I just wanted an NSString, and an NSURL to be shared, so I excluded things where that would make less sense like Printing, AirDropping, Vimeo, etc.  You can read more about this at the AppleUIActivity Class Reference.  Currently it supports these UIActivityTypes:

  • UIActivityTypePostToFacebook
  • UIActivityTypePostToTwitter
  • UIActivityTypePostToWeibo
  • UIActivityTypeMessage
  • UIActivityTypeMail
  • UIActivityTypePrint
  • UIActivityTypeCopyToPasteboard
  • UIActivityTypeAssignToContact
  • UIActivityTypeSaveToCameraRoll
  • UIActivityTypeAddToReadingList
  • UIActivityTypePostToFlickr
  • UIActivityTypePostToVimeo
  • UIActivityTypePostToTencentWeibo
  • UIActivityTypeAirDrop

So for my app, I left it able to post to Facebook, Twitter, Weibo, Tencent Weibo, Message, Mail, and copy to the Pasteboard.

Anyway, you then set that to the excludedActivityTypes property, and then tell your current view controller to present the UIActivityViewController.  I was very surprised at how easy this was.  So when you click on Facebook or Twitter in the UIActivityViewController (as long as you are set up to use Twitter or Facebook), it will show your text, and attach the URL in whatever way is appropriate for that social media choice.  If you use Message or copy it to the pasteboard, you should get something similar to “Look at this awesome website for aspiring iOS Developers! http://www.codingexplorer.com/ “.

I will probably update this post as I learn more about UIActivityViewController.  I hope you found this article helpful.  If you did, don’t hesitate to share this post on twitter or your social media of choice.  The blog is still pretty new, and every share helps.  Of course, if you have any questions, don’t hesitate to contact me on twitter @CodingExplorer, and I’ll see what I can do.  Thanks!

Sources:

This post was heavily influenced by a great tutorial for using AirDrop from the AppCoda Blog at http://www.appcoda.com/ios7-airdrop-programming-tutorial/ written by Simon Ng.  Thank you for the great tutorial.

Filed Under: Class Reference

时间: 2024-11-08 19:03:48

Add sharing to your app via UIActivityViewController的相关文章

[iOS]The app icon set named "AppIcon" did not have any applicable content.

Develop Tools: xCode 5.1 I write a demo for app settings feature. The tutorial url is here. When I add a icon into app icon and build the code, I got this error "The app icon set named "AppIcon" did not have any applicable content.". I

支付宝app支付服务器签名代码(C#)

1,引入支付宝的sdk(AopSdk) 支付宝接口文档网站可下载,注意下载C#版本: 2,代码写的比较简单 public static string RSASign(string OrderNo,decimal Money,string bodyp,string subjectp) { string publicKeyPem = ;//支付宝公钥  string privateKeyPem = ;//商户私钥 string out_trade_no = OrderNo;//订单号 double

The app icon set named "AppIcon" did not have any applicable content.

Develop Tools: xCode 5.1 I write a demo for app settings feature. The tutorial url is here. When I add a icon into app icon and build the code, I got this error "The app icon set named "AppIcon" did not have any applicable content.". I

App Distribution Guide (一)

This guide contains everything you need to know to distribute an app through the App Store or Mac App Store. 这个guide包含了如何通过app stor或者mac app store来发布一个app. Get step-by-step guidance for enrolling in an Apple Developer Program and building, testing, a

C#读写操作app.config中的数据

<?xml version="1.0" encoding="utf-8" ?> <configuration> <connectionStrings> <add name="sqlserver" connectionString="Data Source=WT01395\sqlexpress;Initial Catalog=mydb;Integrated Security=True"/

Microsoft Azure Tutorial: Build your first movie inventory web app with just a few lines of code

Editor's Note: The following is a guest post from Mustafa Mahmutovi?, a Microsoft Student Partner who attends the Faculty of Electrical Engineering at the University of Sarajevo where he is a Computer Science Major. In this tutorial, you will learn h

很好的iOS学习资料

https://github.com/vsouza/awesome-ios 汇集了很多好的资料 https://github.com/vsouza/awesome-ios Skip to content This repository Pull requests Issues Gist You don’t have any verified emails. We recommend verifying at least one email. Email verification helps ou

Awesome Swift

Awesome Swift A collaborative list of awesome Swift resources,inspired by awesome-python and listed on awesome-awesomeness. Feel free to contribute! Awesome Swift Demo Apps iOS Apple Watch OS X Dependency Managers Guides Editor Support Vim Libs Anima

http://wiki.apache.org/tomcat/HowTo

http://wiki.apache.org/tomcat/HowTo Contents Meta How do I add a question to this page? How do I contribute to Tomcat's documentation? Installation How do I set up and run Tomcat on Macintosh OS X? How do I set up and run Tomcat on Solaris 10? How do