简体   繁体   中英

Adding “Open In…” option to iOS app

On iOS devices, the Mail app offers "Open In..." option for attachments. The apps listed have registered their CFBundleDocumentTypes with the OS. What I am wondering is how my app might allow users to open files generated by my app in other apps. Is Mail the only app that provides this feature?

Take a look at the Document Interaction Programming Topics for iOS: Registering the File Types Your App Supports .

As long as you provide your document types in your Info.plist, other apps that recognize that document type will list your app in their "open in" choices. Of course, that presumes that your app creates documents that other apps can open.

This is a great tutorial, that helped me.

I have added support for *.xdxf files in my app. In short, you have to do two things. First - add entries like this to your app's Plist file:

<key>CFBundleDocumentTypes</key>
<array>
    <dict>
        <key>CFBundleTypeName</key>
        <string>XDXF Document</string>
        <key>LSHandlerRank</key>
        <string>Owner</string>
        <key>CFBundleTypeRole</key>
        <string>Editor</string>
        <key>LSItemContentTypes</key>
        <array>
            <string>com.alwawee.xdxf</string>
        </array>
    </dict>
</array>
<key>UTExportedTypeDeclarations</key>
<array>
    <dict>
        <key>UTTypeDescription</key>
        <string>XDXF - XML Dictionary eXchange Format</string>
        <key>UTTypeConformsTo</key>
        <array>
            <string>public.text</string>
        </array>
        <key>UTTypeIdentifier</key>
        <string>com.alwawee.xdxf</string>
        <key>UTTypeTagSpecification</key>
        <dict>
            <key>public.filename-extension</key>
            <string>xdxf</string>
            <key>public.mime-type</key>
            <string>text/xml</string>
        </dict>
    </dict>
</array>

Here, you should add UTExportedTypeDeclarations only if your file type is unique. Or by other words is not here .

Second - handle delegate method in AppDelegate :

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{

    if (url != nil && [url isFileURL]) {

        //  xdxf file type handling

        if ([[url pathExtension] isEqualToString:@"xdxf"]) {

            NSLog(@"URL:%@", [url absoluteString]);

        }

    }

    return YES;
}

In order to be visible in the list of "open in..." for all files, you need to add this to your plist

<key>CFBundleDocumentTypes</key>
<array>
    <dict>
        <key>CFBundleTypeName</key>
        <string>Open All Files</string>
        <key>LSHandlerRank</key>
        <string>Owner</string>
        <key>CFBundleTypeRole</key>
        <string>Editor</string>
        <key>LSItemContentTypes</key>
        <array>
           <string>public.content</string>
           <string>public.data</string>
        </array>
    </dict>
</array>

Once your app shows in "open in...", you need to load that file. Most website shows to implement this function:

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String, annotation: AnyObject?) -> Bool
{
   println("Open URL "+url.path!)
}

But this function that worked fine in IOS 7 crashes in IOS 8. I had to implement the following function instead to get it to work.

func application(application: UIApplication, handleOpenURL url: NSURL) -> Bool 
{
   println("Open URL "+url.path!)
}

I add my app in "open in" list successfully as follows,

在YourAppName.target中选择信息 Add a new document type filter, which Name is anything you want and the type is defined in https://developer.apple.com/library/ios/documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html#//apple_ref/doc/uid/TP40009259-SW1

Hope you can be success too!!

However, the feature I want to implement is "Share" like Facebook or Slack do, I can not make it still...anyone can give me a big hand :(

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM