向 iOS 应用程序添加“打开方式...”选项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3981199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding "Open In..." option to iOS app
提问by westsider
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?
在 iOS 设备上,邮件应用程序为附件提供“打开方式...”选项。列出的应用程序已向操作系统注册了它们的 CFBundleDocumentTypes。我想知道我的应用程序如何允许用户在其他应用程序中打开我的应用程序生成的文件。邮件是唯一提供此功能的应用程序吗?
采纳答案by Jay O'Conor
Take a look at the Document Interaction Programming Topics for iOS: Registering the File Types Your App Supports.
查看iOS 文档交互编程主题:注册您的应用程序支持的文件类型。
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.
只要您在 Info.plist 中提供您的文档类型,识别该文档类型的其他应用程序就会在其“打开方式”选项中列出您的应用程序。当然,前提是您的应用程序创建了其他应用程序可以打开的文档。
回答by Denis Kutlubaev
Thisis 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:
我*.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>
Here, you should add UTExportedTypeDeclarations
only if your file type is unique. Or by other words is not here.
在这里,您应该UTExportedTypeDeclarations
仅在您的文件类型是唯一的情况下添加。或者换句话说就是不在这里。
Second - handle delegate method in AppDelegate
:
第二个处理委托方法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;
}
回答by user2962499
In order to be visible in the list of "open in..." for all files, you need to add this to your plist
为了在所有文件的“打开方式...”列表中可见,您需要将其添加到您的 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.
但是这个在 IOS 7 中运行良好的函数在 IOS 8 中崩溃了。我必须实现以下函数才能让它工作。
func application(application: UIApplication, handleOpenURL url: NSURL) -> Bool
{
println("Open URL "+url.path!)
}
回答by Yvonne
I add my app in "open in" list successfully as follows,
我成功地将我的应用程序添加到“打开方式”列表中,如下所示,
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
添加一个新的文档类型过滤器,名称是您想要的任何内容,类型定义在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 :(
然而,我想要实现的功能是像 Facebook 或 Slack 那样的“分享”功能,我不能让它静止......任何人都可以给我一个大手:(