Implementing content sharing between iOS apps

Time:2019-10-9

Preface

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

  1. The first is throughAirDropTo realize the sharing of documents and data between Apps of different devices;

  2. The second one is to define a URL Scheme for each App, which can directly access an APP by accessing a URL that specifies the URL Scheme.

  3. The third is throughUIDocumentInteractionControllerOrUIActivityViewControllerThese two classes encapsulated in iOS SDK send data, share data and manipulate data between Apps.

  4. The fourth is throughApp ExtensionThe new extended features provided in iOS 8 SDK enable data manipulation and sharing across Apps.

  5. There is also a limited number of App data sharing integrated with third-party SDK implementations, such as the official SDK provided to us by social platforms (QQ, Weixin, Sina Weibo, etc.), or ShareSDK components and alliance sharing components integrated with multiple social platforms.

With regard to the use of integrated third-party SDK, there are detailed documentation on the official websites of major platforms, so this series of articles is mainly about sharing technology based on iOS SDK provided by Apple. At the same time, we recommend two documents of Apple Developer Center: Inter-App Communication and Document Interaction Programming Topics for iOS. Our first article talks about how to share our App support through UTI.

principle

In my article “Explaining the UTI provided by Apple” (Uniform Type Identifier), I explained it in detail.UTI(Uniform Type Identifier)Apple offers us a set of specifications for identifying entity content types in Cocoa and Cocoa Touch-based applications, and the technology for implementing content association is based on this set of specifications. In iOS and MacOS development, Apple provides us with an interface for registering document types, which are global and can be detected by all applications and services in the system. So we can use other alternatives through this underlying detectionThird Party AppCome PreviewOur AppDocuments that are not supported, and we can also use this interface inOur AppOpen and processThird Party AppDocumentation.

If our App can handle certain types of entity content, then we can work on our projectInfo.plistRegister in the file. As to which type and UTI to use, refer to my explanation in this article “Explaining the UTI provided by Apple (Uniform Type Identifier)”. When a third-party App uses Apple’s underlying detection technology to check which Apps can handle the content type it specifies, if our App has registered this type, then our App icon will be displayed in it and act as an entry point for our own App.

Major Technologies

The technology mainly applied to this underlying detection is provided by iOS SDK.UIDocumentInteractionControllerUIActivityViewControllerandQuick Look FrameworkThis is the case. In addition, in iOS 8, Apple offered developersApp ExtensionA higher way to share content between Apps. aboutUIDocumentInteractionControllerUIActivityViewControllerQuick Look Frameworkas well asApp ExtensionI plan to elaborate on the details in a later article. In this article, we are going to talk about it.How to register the document types available to our Appas well asSimply use our App to handle content shared by third-party Apps

Register Available Types

We need to be in the ____________info.plistIn the file, add a new attributeCFBundleDocumentTypes(Actually, the input is"Document types"This is an attribute of an array type, meaning that we can register multiple types at the same time. For each element in the array, there are many attributes to specify. A detailed list of attributes can be found in the official document: Core Foundation Keys – — CFBundle Document Types. Here are some common attributes we use in iOS development:

  • CFBundleTypeName("Icon File Name")
    String type, specifying an alias of some type, that is, the alias used to refer to the type we specify. Usually in order to maintain uniqueness, we use UTI to identify.

  • CFBundleTypeIconFiles
    Array type, which contains the file name of the specified png icon, specifies an icon representing a certain type, and the icon has a specific size identifier:

Device Sizes
iPad 64 x 64 pixels, 320 x 320 pixels
iPhone and iPod touch 22 x 29 pixels, 44 x 58 pixels (high resolution)
  • LSItemContentTypes("Document Content Type UTIs")
    Array type, which contains UTI strings, specifies the set of all recognizable types for our application

  • LSHandlerRank("Handler rank")
    String type, includingOwner,Default,Alternate,NoneFour optional values that specify a priority level for a certain type, andLauncher ServiceThe order of Apps displayed will be arranged according to this priority. Priorities from high to low areOwnerAlternate,DefaultNoneIndicates that this type is not accepted.

Understanding these basic attributes, we need to specify these attributes when registering the available types of App, depending on the requirements of each project, the attribute values are different. For registration, please refer to my GitHub project: Sera Zheng – ZSUTITest. The following illustration serves as a reference:
Implementing content sharing between iOS apps

And when we add all the attributes, start running our program, and then go back to our Info interface, you’ll seeDocument typesThis list has changed, which proves that we have successfully registered the types available for App.
Implementing content sharing between iOS apps

Open third-party applications

We registered the types that our App recognizes in the steps above, and now we can open a useUIDocumentInteractionControllerOr Quick Look Framework to show the content of third-party App, here take the QQ program on the iPhone as an example.

In the registration step above, we registered theLSItemContentTypesContains onlypublic.imageThis UTI. So let’s start with the QQ application.My DocumentsOpen different types of files for comparison, you can see the following figureMy DocumentsThere are two types of files in the list. One is.jpgThe extension of the image file, one is.pdfDocument file with extension.

Implementing content sharing between iOS apps

When I open a picture file for preview, ClickOther applications openYou can see our App icon in the App list. A brief introduction to this page. The first line is the usage provided by Apple after iOS 7.AirDropstayiPhoneiPadoriPod TouchInter-equipment passageiCloudA way to share content. The second line is a list of Apps identified by document type association technology. The third line is identified by document Association TechnologyActionList ofActionRepresents operations that can be performed on documents, such as copying, printing, etc.
Implementing content sharing between iOS apps

If I open the PDF file, I won’t see our App icon.
Implementing content sharing between iOS apps

Program callback

When we go through the above steps, we succeed in showingZSUTITestDemoAfter clicking on the icon, we can jump toZSUTITestDemoIn applications, the way Apple gave us callbacks in iOS SDK has changed since iOS 9, so we need to change for 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.topViewController;
    
    [displayController.imageView setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:url]]];
    [displayController.label setText:[options objectForKey:UIApplicationOpenURLOptionsSourceApplicationKey]];
    
    return YES;
}
#endif

Demo examples can refer to the code from the GitHub project: SeraZheng – – ZSUTITest. When clickingZSUTITestDemoWhen the program icon comes back to the calling code, we can do all kinds of things we want to do here, such as uploading pictures, previewing pictures, manipulating pictures and so on. I just did a simple preview of the picture and then showed the source program of the file.Bundle IdentifierAn example is as follows:

Implementing content sharing between iOS apps