在iOS应用中添加"Open In ..."选项

wes*_*der 44 iphone ipad ios

在iOS设备上,Mail应用程序为附件提供"Open In ..."选项.列出的应用程序已在操作系统中注册了CFBundleDocumentTypes.我想知道的是我的应用程序如何允许用户在其他应用程序中打开我的应用程序生成的文件.Mail是唯一提供此功能的应用程序吗?

Jay*_*nor 37

看看iOS文档交互编程主题:注册您的应用程序支持的文件类型.

只要您在Info.plist中提供文档类型,其他识别该文档类型的应用程序就会在"打开"选项中列出您的应用程序.当然,这假设您的应用创建了其他应用可以打开的文档.


wzb*_*zon 20

是一个很棒的教程,对我有所帮助.

*.xdxf在我的应用中添加了对文件的支持.简而言之,你必须做两件事.首先 - 将这样的条目添加到您的应用程序Plist文件中:

<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>
Run Code Online (Sandbox Code Playgroud)

在这里,UTExportedTypeDeclarations只有在文件类型唯一时才应添加.或者换句话就是不在这里.

第二 - 处理委托方法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;
}
Run Code Online (Sandbox Code Playgroud)


use*_*499 6

为了在所有文件的“打开方式...”列表中可见,您需要将其添加到您的 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>
Run Code Online (Sandbox Code Playgroud)

一旦您的应用程序显示在“打开方式...”中,您就需要加载该文件。大多数网站显示实现此功能:

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

但是这个在 IOS 7 中运行良好的函数在 IOS 8 中崩溃了。我必须实现以下函数才能让它工作。

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