Можно ли программно хранить файл CSV в разных учетных записях iCloud или Apple? - PullRequest
0 голосов
/ 17 июня 2020

Я работаю над приложением, которое представляет собой концепцию хранения файлов .csv в iCloud с разных iPhone устройств. Функция сохранения файлов работает нормально, если у меня есть логин на устройстве с той же учетной записью iCloud, для которой я создал сертификаты. Мое требование к приложению заключалось в том, могу ли я хранить файл .csv в хранилище iCloud на разных устройствах, которые могут быть логином с другой учетной записью Apple.

Ниже приведены коды, которые я использую для хранения файлов.

extension FarmerTVC{

private func createCSV() -> Void {
    let fileName = getDocumentsDirectory().appendingPathComponent(importFileName)
    var csvOutputText = "Name, Country, Latitude, Longitude\n"

    guard self.allFarmer?.count != 0 else{
        print("There have no active farmer list")
        return
    }

    print("........................Exporting the details....................")

    for farmerDetails in self.allFarmer! {
        let fullName = "\(farmerDetails.firstName) \(farmerDetails.middleName) \(farmerDetails.lastName)"

        let newDetails = "\(fullName),\(farmerDetails.country), \(farmerDetails.latitude), \(farmerDetails.longitude)\n"
        print("\(newDetails)")
        csvOutputText.append(newDetails)
    }

    self.removeExistFileFromDirectory()



    do {
        try csvOutputText.write(to: fileName, atomically: true, encoding: String.Encoding.utf8)
        print("........................Coplete Exporting the File....................\n \(fileName)")
        self.createDirectory()
        //CloudDataManager.sharedInstance.copyFileToCloud()
    } catch {
        print(".........................Failed to create file........................")
        print("\(error)")
    }
    //let activity = UIActivityViewController(activityItems: ["your results", fileName], applicationActivities: nil)
    //present(activity, animated: true)
}

private func getDocumentsDirectory() -> URL {
    let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
    return paths[0]
}

private func removeExistFileFromDirectory(){
    let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
    let url = NSURL(fileURLWithPath: path)
    if let pathComponent = url.appendingPathComponent(importFileName) {
        let filePath = pathComponent.path
        let fileManager = FileManager.default
        if fileManager.fileExists(atPath: filePath) {
            print("File Alreday exist in the directory")
            do {
                try fileManager.removeItem(atPath: filePath)
                print("File removed from the directory")
            }catch{
                print("\(error)")
            }
        } else {
            print("FILE NOT AVAILABLE")
        }
    } else {
        print("FILE PATH NOT AVAILABLE")
    }
}}



extension FarmerTVC{
//Create iCloud Drive directory
func createDirectory(){
    if isICloudContainerAvailable() {
        if let iCloudDocumentsURL = FileManager.default.url(forUbiquityContainerIdentifier: nil)?.appendingPathComponent("Documents") {
            if (!FileManager.default.fileExists(atPath: iCloudDocumentsURL.path, isDirectory: nil)) {
                do {
                    try FileManager.default.createDirectory(at: iCloudDocumentsURL, withIntermediateDirectories: true, attributes: nil)
                    print("File path not avaliable in the iCloud Drive")
                    self.copyDocumentsToiCloudDirectory()
                }
                catch {
                    //Error handling
                    print("Error in creating doc")
                }
            }else{
                print("Alreday Have the document path in the icloud.")
                self.copyDocumentsToiCloudDirectory()
            }
        }
    }
    else {
        self.ShowAlertMsg(title: "Oops!", msg: "Not logged into iCloud")

    }
}

//Copy files from local directory to iCloud Directory
func copyDocumentsToiCloudDirectory() {
    if isICloudContainerAvailable() {
        guard let localDocumentsURL = FileManager.default.urls(for: FileManager.SearchPathDirectory.documentDirectory, in: .userDomainMask).last else { return }

        let fileURL = localDocumentsURL.appendingPathComponent(importFileName)

        guard let iCloudDocumentsURL = FileManager.default.url(forUbiquityContainerIdentifier: nil)?.appendingPathComponent("Documents").appendingPathComponent(importFileName) else { return }

        var isDir:ObjCBool = false

        if FileManager.default.fileExists(atPath: iCloudDocumentsURL.path, isDirectory: &isDir) {
            do {
                try FileManager.default.removeItem(at: iCloudDocumentsURL)
                print("Remove the old existing file from the iCloud drive")
            }
            catch {
                //Error handling
                print("Error in remove item")
            }
        }

        do {
            try FileManager.default.copyItem(at: fileURL, to: iCloudDocumentsURL)
            self.ShowAlertMsg(title: "Saved!", msg: "File saved into your iCloud drive")
        }
        catch {
            //Error handling
            print("Error in copy item")
        }
    }
    else {
        self.ShowAlertMsg(title: "Oops!", msg: "Not logged into iCloud")

    }
}

func ShowAlertMsg(title: String, msg: String) {
    let alert = UIAlertController(title: title, message: msg, preferredStyle: UIAlertController.Style.alert)

    alert.addAction(UIAlertAction(title: "OK",
                                  style: UIAlertAction.Style.default,
                                  handler: {(_: UIAlertAction!) in
    }))
    self.present(alert, animated: true, completion: nil)
}

//To check user logged in to iCloud
func isICloudContainerAvailable() -> Bool {
    if FileManager.default.ubiquityIdentityToken != nil {
        return true
    }
    else {
        return false
    }
}}

info.plist file codes Siniging & Capabilities

У меня все работает нормально, если я вошел в ту же учетную запись iCloud, для которой я создал сертификаты и контейнер iCloud для приложения. Но я не буду хранить файл в iCloud в разных учетных записях iCloud. Мой вопрос:

  1. Можно ли хранить файл в разных учетных записях iCloud? Если да, то как?
  2. Можем ли мы использовать хранилище iCloud для распространения приложения с другим идентификатором Apple?
  3. Не могли бы вы дать предложение, как я могу сохранить файл .csv в хранилище iCloud в разные устройства, которые могут входить в систему с другой учетной записью Apple?

Я надеюсь, что у меня есть ясность по моим пунктам. Пожалуйста, дайте мне ваше ценное предложение. Как я могу решить эту проблему.

Заранее спасибо.

1 Ответ

0 голосов
/ 17 июня 2020

Если вы хотите обмениваться файлами между несколькими учетными записями iCloud на нескольких устройствах, я предлагаю хранить файлы с помощью CloudKit .

Вы должны сохранить файлы как часть одного или нескольких CKRecord s в базе данных Publi c вашего приложения (не частной). Вы также можете использовать CKShare для обмена между определенными c людьми, но это требует немного больше работы.

Apple только недавно включила общие файлы через iCloud Drive (iOS 13 и macOS 10.15) , но, насколько мне известно, для этого нет API. Пользователи должны делать это самостоятельно. Таким образом, ваше приложение не сможет управлять файлами, если вы не сделаете это с помощью CloudKit.

Надеюсь, это поможет.

...