我正在开发一个应用程序,该应用程序的概念是从不同的iPhone设备在iCloud中存储.csv文件。如果我在设备上使用与创建证书相同的iCloud帐户登录,则文件存储功能可以正常工作。我的应用程序要求是我是否必须将.csv文件存储到不同设备中的iCloud存储中,这可能是使用不同苹果帐户登录的信息。
下面是我用于文件存储的代码。
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
}
}}
如果我登录到为该应用程序创建了证书和iCloud容器的同一个iCloud帐户,对我来说很好用。但是我不会将文件存储在其他iCloud帐户上的iCloud中。我的问题是
我希望我能阐明我的观点。请给我您宝贵的建议,我该如何解决这个问题。
提前感谢。
我正在开发一个应用程序,该应用程序的概念是从不同的iPhone设备在iCloud中存储.csv文件。如果我使用相同的iCloud在设备上登录,则文件存储功能可以正常工作...
[如果要在多个设备上的多个iCloud帐户之间共享文件,建议使用CloudKit存储文件。