zoukankan      html  css  js  c++  java
  • Add sharing to your app via UIActivityViewController

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

    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.

    Bar Button Item Attributes Inspector with "Identifier" set to "Action"

    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.

    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.

  • 相关阅读:
    在Repeater中动态添加服务器端(按钮)控件
    两种自定义表单设计方案 [转贴]
    如何解决在.Net中用Sql语句向SqlServer数据库中插入特殊字符失败的问题?
    Visual Studio 2005 IDE 技巧和窍门
    C#的Windows编程中多语言的实现
    NET设计模式(18):迭代器模式(Iterator Pattern)(转载)
    “/”应用程序中的服务器错误。当前标识(NT AUTHORITY\NETWORK SERVICE)没有对“C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files”的写访问权限。
    union的妙用
    数据类型—C++基础篇
    C++,VC++,MCF和SLT的关系
  • 原文地址:https://www.cnblogs.com/xuejinhui/p/4930077.html
Copyright © 2011-2022 走看看