2017-02-14 7 views
0

ファイルをユーザーのiCloudドライブフォルダに保存するアプリケーションを作成しています。 iCloud Driveでアプリのフォルダを削除して何が起こるかを見るまで、すべてがうまくいっています。それ以来、私はアプリケーションを開くたびに、サンプルファイルをiCloudに保存しようとしますが、動作しません(reloadiCloud()関数はViewWillAppearで実行するように設定されています)。 iCloud Driveフォルダを作成するために何か必要なことはありますか?私のコードは以下の通りです。iCloudドライブフォルダを再作成しないアプリケーション

func reloadiCloud() { 
    ubiquityURL = filemgr.url(forUbiquityContainerIdentifier: nil) 

    guard ubiquityURL != nil else { 
     print("Unable to access iCloud Account") 
     return 
    } 

    ubiquityURL = ubiquityURL?.appendingPathComponent("Documents") 

    metaDataQuery = NSMetadataQuery() 

    metaDataQuery?.predicate = NSPredicate(format: "NOT %K.pathExtension = ''", NSMetadataItemFSNameKey) 
    metaDataQuery?.searchScopes = [NSMetadataQueryUbiquitousDocumentsScope] 

    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.metadataQueryDidFinishGathering), name: NSNotification.Name.NSMetadataQueryDidFinishGathering, object: metaDataQuery!) 

    metaDataQuery!.start() 
} 

func metadataQueryDidFinishGathering(notification: NSNotification) -> Void { 
    let query: NSMetadataQuery = notification.object as! NSMetadataQuery 

    query.disableUpdates() 

    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.NSMetadataQueryDidFinishGathering, object: query) 

    query.stop() 

    if query.resultCount > 0 { 
     fileList = [] 
     for var i in (0..<query.resultCount) { 
      let currentFileURL = query.value(ofAttribute: NSMetadataItemURLKey, forResultAt: i) as! URL 
      fileList.append(currentFileURL.lastPathComponent) 
      tableView.reloadData() 
     } 
     tableView.reloadData() 
     print(fileList) 
    } else { 
     let sampleFile = Bundle.main.url(forResource: "Sample", withExtension: "pdf")! 
     movetoiCloud(forURL: sampleFile) 
    } 
} 



func movetoiCloud(forURL: URL) { 
    let fileName = forURL.lastPathComponent 
    ubiquityURL = filemgr.url(forUbiquityContainerIdentifier: nil)?.appendingPathComponent("Documents/\(fileName)") 

    //Check if the filename exists and add "copy" to the new version 
    for var i in (0..<fileList.count) { 
     if fileList[i] == forURL.lastPathComponent { 
      let base = ubiquityURL?.absoluteURL.deletingLastPathComponent() 
      let endIndex = fileName.index(fileName.endIndex, offsetBy: -4) 

      let truncatedExtension = fileName.substring(from: endIndex) 
      let truncatedName = fileName.substring(to: endIndex).appending(" copy").appending(truncatedExtension) 

      ubiquityURL = base?.appendingPathComponent(truncatedName) 
      print("New ubiquity URL: \(ubiquityURL)") 
     } 
    } 

    do { 
     try filemgr.setUbiquitous(true, itemAt: forURL, destinationURL: (ubiquityURL)!) 
     reloadiCloud() 
     print("Move to iCloud OK") 
    } catch let error { 
     print("moveToiCloud failed: \(error.localizedDescription)") 
    } 
} 

答えて

2

は、情報PLISTにバンドル番号とバージョンをアップデートしてみてください、これらの値の増加を有効にするのiCloudの追加のために必要とされます。

+0

バンドル版のバンプはすぐに私のために働いた。ありがとう! – damote

+0

お手伝いします! @ damote –

+0

OMG、私は2日間これを検索していました。しかし、ユーザーが後でドライブからフォルダを削除すると、フォルダが再作成されるかどうか – ChanWarde

関連する問題