2014-06-30 84 views
6

我正在測試iOS 8中新的UIDocumentPickerViewController API。我想在iCloud中打開一個文件,以查看交互如何工作。UIDocumentPickerViewController - 所有文件灰顯

這是我從我的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文件),因此我無法訪問文件:

Document Picker with files grayed out

我查了一些資料,做了以下(沒有運氣):

  • 在我的應用程序啓用iCloud中(在功能部分,兩個鍵 - 值存儲和iCloud的文件)。
  • 添加了UTI在我的Info.plist public.data如下:

    <key>CFBundleDocumentTypes</key> 
    

    CFBundleTypeIconFile 的icon.png CFBundleTypeName 邁德特 CFBundleTypeRole 瀏覽器 LSItemContentTypes public.data LSTypeIsPackage 個NSDocumentClass 文件 NSPersistentStoreTypeKey 二進制

  • 添加了NSUbiquitousContainerIsDocumentScopePublic鍵與YES我Info.plist中的值。

任何想法可能是錯誤或失蹤?

+0

您的圖像不顯示變灰的文件。如果你打開文件夾會發生什麼? –

+0

該文件夾是灰色的,我無法找到文件。剛剛添加了一個澄清。 – pgb

+0

@pgb您是否使用beta 3發佈過測試? – karan

回答

15

iWork文檔不符合kUTTypeData,它們符合kUTTypePackage

然而,在iOS 8的測試版3,我不得不用精確的尿路感染:

UIDocumentPickerViewController *picker = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:@[@"com.apple.iwork.pages.pages", @"com.apple.iwork.numbers.numbers", @"com.apple.iwork.keynote.key"] inMode:mode]; 
+0

似乎也發生在beta 4以及 – Weaverfish

+0

它發生在beta 5以及 – arcticmatt

+0

感謝您的答案!你是怎麼知道蘋果自己應用的UTI的?是在哪裏發佈的?再次感謝! – Tarek

7
**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") 
} 
+0

正確。謝謝! –

+0

很高興!可以幫助你。 –

相關問題