iOS导入应用外文件

66 篇文章 0 订阅

之前需求提出想要像安卓一样从手机的文件夹获取文档

安卓那种肯定做不到,只能用像QQ、微信之类的外部app传入文档。在网上看了一些资料,总结如下:

 

1、拷贝文档方式,如图

这是从QQ上打开的word文档,然后点击用其他应用打开,图中第二行,可以找到很多其他app,问题是如何让自己的app处于其中。

方法如下:

其实只需要在info.plist注册文件类型,就可以在其他app中找到了

需要在info.plist文件中,添加一个新的属性Document type,用source code方式把下面的内容粘贴进去就行,可以删除自己不需要的文件类型

 
  1. <key>CFBundleDocumentTypes</key>

  2. <array>

  3. <dict>

  4. <key>CFBundleTypeName</key>

  5. <string>PDF</string>

  6. <key>LSHandlerRank</key>

  7. <string>Owner</string>

  8. <key>LSItemContentTypes</key>

  9. <array>

  10. <string>com.adobe.pdf</string>

  11. </array>

  12. </dict>

  13. <dict>

  14. <key>CFBundleTypeName</key>

  15. <string>Microsoft Word</string>

  16. <key>LSHandlerRank</key>

  17. <string>Alternate</string>

  18. <key>LSItemContentTypes</key>

  19. <array>

  20. <string>com.microsoft.word.doc</string>

  21. <string>com.microsoft.word.wordml</string>

  22. <string>org.openxmlformats.wordprocessingml.document</string>

  23. </array>

  24. </dict>

  25. <dict>

  26. <key>CFBundleTypeName</key>

  27. <string>Microsoft Excel</string>

  28. <key>LSHandlerRank</key>

  29. <string>Alternate</string>

  30. <key>LSItemContentTypes</key>

  31. <array>

  32. <string>com.microsoft.excel.xls</string>

  33. <string>org.openxmlformats.spreadsheetml.sheet</string>

  34. </array>

  35. </dict>

  36. <dict>

  37. <key>CFBundleTypeIconFiles</key>

  38. <array/>

  39. <key>CFBundleTypeName</key>

  40. <string>Microsoft PowerPoint</string>

  41. <key>LSHandlerRank</key>

  42. <string>Alternate</string>

  43. <key>LSItemContentTypes</key>

  44. <array>

  45. <string>com.microsoft.powerpoint.​ppt</string>

  46. <string>org.openxmlformats.presentationml.presentation</string>

  47. <string>public.presentation</string>

  48. </array>

  49. </dict>

  50. <dict>

  51. <key>CFBundleTypeName</key>

  52. <string>Text</string>

  53. <key>LSHandlerRank</key>

  54. <string>Alternate</string>

  55. <key>LSItemContentTypes</key>

  56. <array>

  57. <string>public.text</string>

  58. <string>public.plain-text</string>

  59. <string>public.utf8-plain-text</string>

  60. <string>public.utf16-external-plain-​text</string>

  61. <string>public.utf16-plain-text</string>

  62. <string>com.apple.traditional-mac-​plain-text</string>

  63. <string>public.source-code</string>

  64. <string>public.c-source</string>

  65. <string>public.objective-c-source</string>

  66. <string>public.c-plus-plus-source</string>

  67. <string>public.objective-c-plus-​plus-source</string>

  68. <string>public.c-header</string>

  69. <string>public.c-plus-plus-header</string>

  70. <string>com.sun.java-source</string>

  71. <string>public.script</string>

  72. <string>public.shell-script</string>

  73. </array>

  74. </dict>

  75. <dict>

  76. <key>CFBundleTypeName</key>

  77. <string>Rich Text</string>

  78. <key>LSHandlerRank</key>

  79. <string>Alternate</string>

  80. <key>LSItemContentTypes</key>

  81. <array>

  82. <string>public.rtf</string>

  83. <string>com.apple.rtfd</string>

  84. <string>com.apple.flat-rtfd</string>

  85. </array>

  86. </dict>

  87. <dict>

  88. <key>CFBundleTypeName</key>

  89. <string>HTML</string>

  90. <key>LSHandlerRank</key>

  91. <string>Alternate</string>

  92. <key>LSItemContentTypes</key>

  93. <array>

  94. <string>public.html</string>

  95. <string>public.xhtml</string>

  96. </array>

  97. </dict>

  98. <dict>

  99. <key>CFBundleTypeName</key>

  100. <string>Image</string>

  101. <key>LSHandlerRank</key>

  102. <string>Alternate</string>

  103. <key>LSItemContentTypes</key>

  104. <array>

  105. <string>public.image</string>

  106. </array>

  107. </dict>

  108. </array>

然后再Appdelegate中添加以下方法

 
  1. - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options{

  2. // 判断传过来的url是否为文件类型

  3. if ([url.scheme isEqualToString:@"file"]) {

  4.  
  5. //todo 操作文档

  6.  
  7.  
  8. }

  9. return YES;

  10. }

这样就可以获取并操作外部传过来的文档了。

 

 

2.存储到“文件”方式(iOS11之后可用),如图

在外部app存储文档到自己app的“文件夹”

 

 

在自己app内部可以打开如上图方式的文件夹,然后获取到文档

 

方法如下:

也是在info.plist中添加一组键值,Supports Document Browser设置为YES

 
  1. <key>UISupportsDocumentBrowser</key>

  2. <true/>

这样就可以在打开微信或qq等外部app中将他们的文档存储到我们app的文件夹中,再从我们的app中打开文件夹界面,选择存储好的文档,代码如下(随便写在一个控制器中的):

 
  1. - (IBAction)presentDocumentPicker:(id)sender {

  2. //类型可以根据需求自己加

  3. NSArray *documentTypes = @[@"com.microsoft.word.doc",@"com.microsoft.excel.xls"];

  4. UIDocumentPickerViewController *pickerVC = [[UIDocumentPickerViewController alloc]initWithDocumentTypes:documentTypes inMode:UIDocumentPickerModeOpen];

  5. pickerVC.delegate = self;

  6. [self presentViewController:pickerVC animated:YES completion:nil];

  7.  
  8. }

  9.  
  10. - (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray <NSURL *>*)urls{

  11.  
  12. //获取文档url,操作文档

  13. }

以上就是两种导入外部文档的方式,将就用着

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值