http://www.codingexplorer.com/add-sharing-to-your-app-via-uiactivityviewcontroller/
April 4, 2014
Ever wonder what is various apps like Photos or Safari with when do I click on the Share button? So do I until a few days ago. It is apparently uiactivityviewcontroller. I just learned a bit about what to use it, so I thought I would pass it along.
Update November, 2014:want to what do Uiactivityviewcontroller in Swift? Check out of 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 are a box with an arrow pointing up from the C Enter). To get it from the Interface Builder, you must use a uibarbuttonitem, so the This button can is in a uinavigationbar (part of Uinavigationcontroller) or Uitoolbar. You simply add a uibarbuttonitem, and change It "Identifier" in the Attributes Inspector to "Action, as shown in the Imag E.
The Identifier is set to Action for the sharing icon.
Now the Code
This was actually surprisingly easy to do. So-wire, a ibaction, here's the gist of how mine was set up.
| 123456789101112131415161718192021 |
- (ibaction)Sharebutton:(uibarbuttonitem *)sender { nsstring *texttoshare = @ "Look in this awesome website for aspiring IOS Develo Pers! " ; 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. Nsarray of objects to share, and basically pass that along to the Uiactivityviewcontroller when you instantiat E it. For my app, I just wanted a nsstring, and an nsurl to be gkfx, so I excluded things where that would make less sense Li Ke 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 apps, I left it able to post to Facebook, Twitter, Weibo, Tencent Weibo, Message, Mail, and copy to the Pasteboar D.
Anyway, you then set the to The excludedactivitytypes property, and then tell the your current view controller to pre Sent the Uiactivityviewcontroller. i is very surprised at how easy this is. so when you click on Facebook or Twitter in the Uiactivityviewcontroller (as long as your is set up to use Twitter o R Facebook), it'll show your text, and attach the URL in whatever to be appropriate for that social media choice. if you use a Message or copy it to the pasteboard, you should get something similar to "Look in this awesome website f or aspiring IOS developers! http://www.codingexplorer.com/".
I'll probably update this post as I learn more about Uiactivityviewcontroller. I hope you found this article helpful. If you do, 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 had any questions, don ' t hesitate to the contact me on Twitter @CodingExplorer, and I'll see what I can do. thanks!
Sources:
This post is heavily influenced by a great tutorial for using AirDrop from the Appcoda Blog at Http://www.appcoda.com/ios 7-airdrop-programming-tutorial/written by Simon Ng. Thank for the Great tutorial.
Filed Under:class Reference
Add sharing to your app via Uiactivityviewcontroller