Content sharing between iOS app implementations

Source: Internet
Author: User

Preface

There are several common ways we want to share content between different apps on the iOS platform:

    1. A passage AirDrop in English realize the sharing of documents and data between applications of different devices;
    2. The second is to define a URL scheme for each application that accesses an app directly by accessing a URL that specifies a URL scheme;
    3. Third Kind is through UIDocumentInteractionController or a UIActivityViewController The packaged classes in the two iOS SDKs send data between apps, share data, and manipulate data;
    4. the fourth type is through App Extension , the extended new features available in the SDK for iOS 8 enable data manipulation and sharing across applications;
    5. There is also a data sharing for a limited application that integrates with third-party SDK implementations, such as the official SDK provided to us by social platforms (QQ, Sina Weibo, etc.), or the SHARESDK components and allies sharing components that integrate multiple social platforms.

For the integration of the use of the third-party SDK, the major platform official website has detailed documentation, so our series of articles is mainly to talk about Apple native iOS SDK-based sharing technology, and recommend two Apple Developer Center documentation: Inter- Application communication and The document interactive programming theme is iOS. Our first article talks about how to get our app to support sharing through UTI.

principle

in my "detailed description of the UTI (uniform type identifier) provided by Apple", in this article, a UTI(Uniform Type Identifier) set of apples gives us a specification for identifying entity content types in cocoa and cocoa touch applications, The technology for implementing content correlation is based on this set of specifications. In iOS and Mac OS development, Apple has provided us with an interface for registering document types, which are global in type and can be detected by all applications and services in the system. So we can use this bottom-up detection to 第三方App preview The 我们的App documents that don't get itunes immediately, and we can also 我们的App open and process them through this interface. 第三方App the document.

if our application can handle certain types of entity content, then we can do so in our project Info.plist registered in the file. for which type and UTI to use, refer to my explanation in the article "explaining the UTI (uniform type identifier) provided by Apple" . When a third-party application checks to see which applications can handle the content type specified by Apple's underlying detection technology, if our application has registered this type, then our application icon will be displayed in it. And as an entrance to our own application.

Main technology

The technology that is primarily applied to this underlying detection is available UIDocumentInteractionController to us in the iOS SDK , UIActivityViewController and the Quick Look 框架 . In addition, in iOS 8, Apple has also provided developers with a App Extension much larger way to share content between apps. Regarding UIDocumentInteractionController , UIActivityViewController Quick Look 框架 as well as App Extension The details, I plan to explain this in detail later in the article . Articles, mainly our English to talk 如何注册我们App可用的文档类型 as well 简单使用我们的App来处理第三方App分享的内容 .

Registering available Types

need us in info.plist file, add a new property CFBundleDocumentTypes (actually the input is "Document types" ), which is a property of an array type, meaning that we can register multiple types at the same time. For each element in the array, there are many properties that can be specified, with a detailed list of properties we can find in the official documentation:Core Foundation Keys----cfbundledocumenttypes. Here's a list of the properties we used to do iOS development:

    • Cfbundletypename ( "Icon File Name" )
      String type, which specifies a type of alias, which is used to refer to the name of the type we specify, generally to maintain uniqueness, we use UTI to identify.
    • Cfbundletypeiconfiles
      An array type that contains the file name of the specified PNG icon, which specifies a type of icon, and the icon has a specific size identification:
devices dimensions
ipad x 64 pixels, 320 pixels
iphone and ipod touch x 29 pixels, 58 pixels (high resolution)
    • Lsitemcontenttypes ( "Document Content Type UTIs" )
      Array type, containing the UTI string, specifying all of the recognized types of our application collection
    • Lshandlerrank ( "Handler rank" )
      A string type that contains Owner , Default , Alternate , None Four optional values that specify the priority level for some type, and Launcher Service The order in which the applications are displayed is ranked according to this priority . Priority levels from high to low are Owner , Alternate inturn, Default . None Indicates that this type is not accepted.

To understand these basic attributes, we need to specify these properties when registering the app's available types, and the property values vary depending on the requirements of each project. For specific registration please refer to my GitHub project:serazheng---zsutitest. Example as a reference:


adding document types to Information

and when we've added all the properties, we'll start running our program and then go back to our information interface and see Document types This list has changed, which proves that we have successfully registered the type of application available.


successful registration, running the program display resultsOpen a third-party app

we have registered the types that our application can recognize in the steps above, and now we can open a UIDocumentInteractionController or a Quick Look framework to showcase the content of the third-party app, here is an example of the QQ program on the iphone.

we have registered LSItemContentTypes in the above registration steps, all games only. public.image this UTI. So we first from the application of QQ program 我的文件 , open different types of files for comparison, you can see the product 我的文件 List of all the game two types of files, a kind of English .jpg An extension of the picture file, in English an .pdf extension of the document file.


List of my files

When I open a picture file for preview , click 其他应用打开 , you can see our application icon in the list of applications. A brief introduction to this page, the first line is Apple in iOS 7 after we provide the use of AirDrop iPhone , iPad or iPod Touch equipment through the room iCloud a way to share content. The second line is a list of applications identified by the Document Type Association technology . Line third in English the list that is identified by the document Association technique Action Action represents the actions that can be performed on the document, such as copying, printing, and so on.


open picture, show Zsutitestdemo app icon

And if I open a PDF file, I can't see our app icon.


Open PDF file, can't see Zsutitestdemo app iconProgram Callback

when we passed the above steps, we succeeded in showing ZSUTITestDemo icon, click on the icon and we can jump to ZSUTITestDemo app, and Apple's approach to receiving callbacks in the iOS SDK has changed since iOS 9, so we need to make changes to different device versions :

#if __iphone_os_version_max_allowed < __iphone_9_0-(BOOL) Application: (UIApplication *) application OpenURL: (nsurl *) URL sourceapplication: (Nullable NSString *) sourceapplication annotation: (ID) annotation{Uinavigationcontroller *navigation = (Uinavigationcontroller *) Application.keyWindow.rootViewController; Viewcontroller *Displaycontroller = (Viewcontroller *) Navigation.topviewcontroller; [Displaycontroller.imageview setimage:[uiimage Imagewithdata:[nsdata Datawithcontentsofurl:url]]; [Displaycontroller.label Settext:sourceapplication]; Return YES #else-(bool) Application: (UIApplication *) application OpenURL: (nsurl *) URL options: (nonnull nsdictionary<nsstring *,id> *) options{Uinavigationcontroller *navigation = (Uinavigationcontroller *) Application.keywindow.rootviewcontroller; Viewcontroller *displaycontroller = (Viewcontroller *) Navigation.topviewcontrollerdisplaycontroller.imageview setimage:[uiimage imagewithdata:[nsdata Datawithcontentsofurl:url]]]displaycontroller.label settext:[options Objectforkey: Uiapplicationopenurloptionssourceapplicationkey]] return YES  #endif            

The Demo sample can refer to code from the GitHub project :serazheng---zsutitest. When you click on the ZSUTITestDemo program icon back to the calling code, we can do a variety of things we want to do here, such as uploading pictures, previewing pictures, manipulating pictures and so on. I only made a simple preview of the picture to display , and then show the file's source program Bundle Identifier , such as:

Content sharing between iOS app implementations

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.