2016-12-02 11 views
8

Próbuję wyeksportować moją podklasę UIDocument za pomocą UIDocumentPickerViewController. Podklasa zapisuje dane do FileWrapper, a jego UTI jest zgodne z com.apple.package.Wyeksportuj dokument UID z niestandardowym pakietem plików UTI

Ale przedstawione narzędzie do selekcji dokumentów pokazuje "Dokumenty w iCloud Drive są niedostępne, ponieważ ustawienie dysku iCloud jest wyłączone."

Dokument został pomyślnie zapisany do pamięci podręcznej, co widzę z eksportowanego pakietu pojemnika.

Cache directory

Kiedy zmienić podklasę dokumentów i niestandardowe UTI stosowania się do pojedynczego pliku (np public.plain-text), selektor dokument działa dobrze i mogę wyeksportować plik. Tak więc problem wydaje się być związany z typem dokumentu lub wyeksportowanym UTI.

Czy robię coś nie tak, czy to błąd?


Info.plist

Document Type and Exported UTI

<key>CFBundleDocumentTypes</key> 
<array> 
    <dict> 
     <key>CFBundleTypeIconFiles</key> 
     <array/> 
     <key>CFBundleTypeName</key> 
     <string>Custom Doc</string> 
     <key>LSHandlerRank</key> 
     <string>Owner</string> 
     <key>LSItemContentTypes</key> 
     <array> 
      <string>com.zxzxlch.documentsandbox.customdoc</string> 
     </array> 
     <key>LSTypeIsPackage</key> 
     <true/> 
    </dict> 
</array> 

... 

<key>UTExportedTypeDeclarations</key> 
<array> 
    <dict> 
     <key>UTTypeConformsTo</key> 
     <array> 
      <string>com.apple.package</string> 
     </array> 
     <key>UTTypeDescription</key> 
     <string>Custom Doc File</string> 
     <key>UTTypeIdentifier</key> 
     <string>com.zxzxlch.documentsandbox.customdoc</string> 
     <key>UTTypeTagSpecification</key> 
     <dict> 
      <key>public.filename-extension</key> 
      <array> 
       <string>zzz</string> 
      </array> 
     </dict> 
    </dict> 
</array> 

CustomDocument.swift

private let textFilename = "contents.txt" 

class CustomDocument: UIDocument { 
    var content = "Test" 

    override func load(fromContents contents: Any, ofType typeName: String?) throws { 
     guard let topFileWrapper = contents as? FileWrapper, 
      let textData = topFileWrapper.fileWrappers?[textFilename]?.regularFileContents else { 
       return 
     } 
     content = String(data: textData, encoding: .utf8)! 
    } 

    override func contents(forType typeName: String) throws -> Any { 
     let textFileWrapper = FileWrapper(regularFileWithContents: content.data(using: .utf8)!) 
     textFileWrapper.preferredFilename = textFilename 

     return FileWrapper(directoryWithFileWrappers: [textFilename: textFileWrapper]) 
    } 

} 

ViewController.swift

func exportDocument() { 
    // Write to cache 
    let cachesDir = FileManager.default.urls(for: FileManager.SearchPathDirectory.cachesDirectory, in: .allDomainsMask).first! 
    let dataDir = cachesDir.appendingPathComponent("export", isDirectory: true) 
    try! FileManager.default.createDirectory(at: dataDir, withIntermediateDirectories: true, attributes: nil) 

    let fileURL = dataDir.appendingPathComponent("cookie").appendingPathExtension("zzz") 

    let archive = CustomDocument(fileURL: fileURL) 
    archive.content = "Cookie cat" 

    archive.save(to: archive.fileURL, for: .forCreating) { success in 
     guard success else { 
      let alertController = UIAlertController.notice(title: "Cannot export data", message: nil) 
      self.present(alertController, animated: true, completion: nil) 
      return 
     } 

     let documentPicker = UIDocumentPickerViewController(url: archive.fileURL, in: .exportToService) 
     documentPicker.delegate = self 
     self.present(documentPicker, animated: true, completion: nil) 
    } 

} 

Odpowiedz

2

To rozwiązuje mój problem: sprawić, że ZUM również zgodne public.composite-content, tj

<key>UTTypeConformsTo</key> 
<array> 
    <string>com.apple.package</string> 
    <string>public.composite-content</string> 
</array> 

Nie jestem pewien, dlaczego mimo.

Powiązane problemy