代码之家  ›  专栏  ›  技术社区  ›  westsider

在iOS应用程序中添加“打开位置…”选项

  •  44
  • westsider  · 技术社区  · 14 年前

    在iOS设备上,邮件应用程序为附件提供“打开位置…”选项。列出的应用程序已在操作系统中注册了它们的cfbundledocumenttype。我想知道的是,我的应用程序如何允许用户在其他应用程序中打开我的应用程序生成的文件。邮件是唯一提供此功能的应用程序吗?

    4 回复  |  直到 14 年前
        2
  •  21
  •   Denis Kutlubaev    11 年前

    This 是一个很好的教程,对我很有帮助。

    我增加了对 *.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>
    

    这里,你应该加上 UTExportedTypeDeclarations 仅当您的文件类型是唯一的。或者换句话说不是 here .

    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;
    }
    
        3
  •  5
  •   user2962499    10 年前

    <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>
    

    一旦你的应用程序显示在“打开位置…”中,你需要加载该文件。大多数网站显示要实现此功能:

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

    但是这个在IOS 7中运行良好的函数在IOS 8中崩溃了。我不得不实现下面的功能来让它工作。

    func application(application: UIApplication, handleOpenURL url: NSURL) -> Bool 
    {
       println("Open URL "+url.path!)
    }
    
        4
  •  2
  •   Yvonne    9 年前

    我成功地将我的应用程序添加到“打开位置”列表中,如下所示,

    Select info in YourAppName.target 添加一个新的文档类型筛选器,该筛选器的名称是您想要的任何名称,并且该类型在 https://developer.apple.com/library/ios/documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html#//apple_ref/doc/uid/TP40009259-SW1

    希望你也能成功!!