我正在iOS 8中测试新的UIDocumentPickerViewController
API。我想在iCloud中打开一个文件,看看交互是如何工作的。
这是我从UIViewController
运行的代码:
- (IBAction)openDocument:(id)sender {
[self showDocumentPickerInMode:UIDocumentPickerModeOpen];
}
- (void)showDocumentPickerInMode:(UIDocumentPickerMode)mode {
UIDocumentPickerViewController *picker = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:@[(NSString *)kUTTypeData] inMode:mode];
picker.delegate = self;
[self presentViewController:picker animated:YES completion:nil];
}
openDocument
与IB中的按钮相关联。当我点击它时,它会打开iCloud浏览器,但它中的每个文件夹都显示为灰色(我创建了一些测试Keynote,Numbers和Pages文件),所以我无法访问这些文件:
我检查了一些文档并执行了以下操作(没有运气):
在我的Info.plist中添加了public.data
的UTI,如下所示:
<key>CFBundleDocumentTypes</key>
CFBundleTypeIconFile 的icon.png CFBundleTypeName 迈德特 CFBundleTypeRole 查看器 LSItemContentTypes public.data LSTypeIsPackage NSDocumentClass 文献 NSPersistentStoreTypeKey 二进制
在我的Info.plist中添加了值为NSUbiquitousContainerIsDocumentScopePublic
的{{1}}密钥。
知道什么可能是错的或遗失的吗?
答案 0 :(得分:16)
iWork文档不符合kUTTypeData
,符合kUTTypePackage
。
但是,在iOS 8 beta 3中,我必须使用确切的UTI:
UIDocumentPickerViewController *picker = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:@[@"com.apple.iwork.pages.pages", @"com.apple.iwork.numbers.numbers", @"com.apple.iwork.keynote.key"] inMode:mode];
答案 1 :(得分:14)
**Swift 3+ Solution**
将打开并提供对驱动器上所有项目的访问权限。
//open document picker controller
func openImportDocumentPicker() {
let documentPicker = UIDocumentPickerViewController(documentTypes: ["public.item"], in: .import)
documentPicker.delegate = self
documentPicker.modalPresentationStyle = .formSheet
self.present(documentPicker, animated: true, completion: { _ in })
}
/*
*
* Handle Incoming File
*
*/
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
if controller.documentPickerMode == .import {
let alertMessage: String = "Successfully imported \(url.absoluteURL)"
}
}
/*
*
* Cancelled
*
*/
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
print("Cancelled")
}