使用 UIDocumentBrowserViewController 将文档导入应用程序的文档浏览器
Importing document to app's Document Browser using DocumentBrowserViewController
我已经在基于文档的简单 iOS 应用程序中声明了自定义导出 UTI 类型。我现在正在尝试提供将相同类型的文件从 Mail 导入应用程序的功能。
当长按消息中的文档图标时,我可以选择 "copy to MyApp",然后调用 AppDelegate.m:
中的方法
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)inputURL options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options {
DocumentBrowserViewController *documentBrowserViewController = (DocumentBrowserViewController *)self.window.rootViewController;
[documentBrowserViewController revealDocumentAtURL:inputURL importIfNeeded:YES completion:^(NSURL * _Nullable revealedDocumentURL, NSError * _Nullable error) {
if (error) {
NSLog(@"Failed to reveal the document at URL %@ with error: '%@'", inputURL, error);
return;
}
NSLog(@"Imported document to %@",[revealedDocumentURL absoluteString]);
[documentBrowserViewController presentDocumentAtURL:revealedDocumentURL];
}];
return YES;
}
然而文档被呈现,但未被导入(revealedDocumentURL
与inputURL
相同,并且未调用委托方法importDocumentAtURL
)。也许我不应该使用 revealDocumentAtURL 方法?我无法使用 importDocumentAtURL: nextToDocumentAtURL: mode: completionHandler:
方法,因为我无法为 nextToDocumentAtURL
参数指定文档。有没有办法将文档导入特定目录?也许我错过了一些很明显的东西?此外,这是我的 info.plist 文件,以防有用。
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeIconFiles</key>
<array/>
<key>CFBundleTypeName</key>
<string>MyApp File</string>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>LSHandlerRank</key>
<string>Owner</string>
<key>LSItemContentTypes</key>
<array>
<string>com.myCompany.MyApp</string>
</array>
<key>LSSupportsOpeningDocumentsInPlace</key>
<false/>
</dict>
</array>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>LSSupportsOpeningDocumentsInPlace</key>
<false/>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>
<string>Main</string>
<key>UIRequiredDeviceCapabilities</key>
<array>
<string>armv7</string>
</array>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UISupportsDocumentBrowser</key>
<true/>
<key>UTExportedTypeDeclarations</key>
<array>
<dict>
<key>UITypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<array>
<string>myext</string>
</array>
</dict>
<key>UTTypeConformsTo</key>
<array>
<string>public.data</string>
<string>public.content</string>
</array>
<key>UTTypeDescription</key>
<string>MyApp File</string>
<key>UTTypeIdentifier</key>
<string>com.myCompany.MyApp</string>
</dict>
</array>
</dict>
</plist>
这似乎是 iOS 11.2 中的一个问题。将 iOS 升级到 11.3 并将 Xcode 升级到 9.3 解决了这些问题。
我已经在基于文档的简单 iOS 应用程序中声明了自定义导出 UTI 类型。我现在正在尝试提供将相同类型的文件从 Mail 导入应用程序的功能。
当长按消息中的文档图标时,我可以选择 "copy to MyApp",然后调用 AppDelegate.m:
中的方法- (BOOL)application:(UIApplication *)app openURL:(NSURL *)inputURL options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options {
DocumentBrowserViewController *documentBrowserViewController = (DocumentBrowserViewController *)self.window.rootViewController;
[documentBrowserViewController revealDocumentAtURL:inputURL importIfNeeded:YES completion:^(NSURL * _Nullable revealedDocumentURL, NSError * _Nullable error) {
if (error) {
NSLog(@"Failed to reveal the document at URL %@ with error: '%@'", inputURL, error);
return;
}
NSLog(@"Imported document to %@",[revealedDocumentURL absoluteString]);
[documentBrowserViewController presentDocumentAtURL:revealedDocumentURL];
}];
return YES;
}
然而文档被呈现,但未被导入(revealedDocumentURL
与inputURL
相同,并且未调用委托方法importDocumentAtURL
)。也许我不应该使用 revealDocumentAtURL 方法?我无法使用 importDocumentAtURL: nextToDocumentAtURL: mode: completionHandler:
方法,因为我无法为 nextToDocumentAtURL
参数指定文档。有没有办法将文档导入特定目录?也许我错过了一些很明显的东西?此外,这是我的 info.plist 文件,以防有用。
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeIconFiles</key>
<array/>
<key>CFBundleTypeName</key>
<string>MyApp File</string>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>LSHandlerRank</key>
<string>Owner</string>
<key>LSItemContentTypes</key>
<array>
<string>com.myCompany.MyApp</string>
</array>
<key>LSSupportsOpeningDocumentsInPlace</key>
<false/>
</dict>
</array>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>LSSupportsOpeningDocumentsInPlace</key>
<false/>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>
<string>Main</string>
<key>UIRequiredDeviceCapabilities</key>
<array>
<string>armv7</string>
</array>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UISupportsDocumentBrowser</key>
<true/>
<key>UTExportedTypeDeclarations</key>
<array>
<dict>
<key>UITypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<array>
<string>myext</string>
</array>
</dict>
<key>UTTypeConformsTo</key>
<array>
<string>public.data</string>
<string>public.content</string>
</array>
<key>UTTypeDescription</key>
<string>MyApp File</string>
<key>UTTypeIdentifier</key>
<string>com.myCompany.MyApp</string>
</dict>
</array>
</dict>
</plist>
这似乎是 iOS 11.2 中的一个问题。将 iOS 升级到 11.3 并将 Xcode 升级到 9.3 解决了这些问题。