0

I'm letting the user choose their own background image for the app. This image is suppose to sync across devices using iCloud, but I can't get it to work. The image appears to save and is accessible to the device that saved it, but other devices can't access the image. Here's how I have everything setup:

static func setBackgroundImage(image: UIImage?) {
    if let image = image {
        let notification = Notification(name: updatedBackgroundImageNotificationName, object: nil, userInfo: ["image": image])
        NotificationCenter.default.post(notification)
    } else {
        NotificationCenter.default.post(name: updatedBackgroundImageNotificationName, object: nil)
    }

    guard let directoryUrl = CloudSettings.resourcesDirectoryUrl,
        let fileUrl = CloudSettings.backgroundImageUrl else {
        print("Unable to save background")
        return
    }

    var isDirectory = ObjCBool(true)
    if !FileManager.default.fileExists(atPath: directoryUrl.path, isDirectory: &isDirectory) {
        do {
            try FileManager.default.createDirectory(at: directoryUrl, withIntermediateDirectories: true, attributes: nil)
        } catch let error {
            print(error)
            return
        }
    }

    let stop = fileUrl.startAccessingSecurityScopedResource()

    defer {
        if stop {
            fileUrl.stopAccessingSecurityScopedResource()
        }
    }

    isDirectory = ObjCBool(false)
    if FileManager.default.fileExists(atPath: fileUrl.path, isDirectory: &isDirectory) {
        do {
            try FileManager.default.removeItem(at: fileUrl)
        } catch let error {
            print(error)
            return
        }
    }

    if let imageData = image?.pngData() {
        do {
            try imageData.write(to: fileUrl)
        } catch let error {
            print(error)
            return
        }
    }
}

static func getBackgroundImage() -> UIImage? {
    guard let url = CloudSettings.backgroundImageUrl else {
        print("Unable to get background image")
        return nil
    }

    let stop = url.startAccessingSecurityScopedResource()

    defer {
        if stop {
            url.stopAccessingSecurityScopedResource()
        }
    }

    var isDirectory = ObjCBool(false)
    if FileManager.default.fileExists(atPath: url.path, isDirectory: &isDirectory) {
        return UIImage(contentsOfFile: url.path)
    }

    return nil
}

entitlements

info plist

capabilities

Is there something I'm missing or is there something wrong with my code?

Jake
  • 13,097
  • 9
  • 44
  • 73
  • No exp. with iCloud but isn't there any observing mechanism like firebase where you can observer certain change happened from anywhere. Or is there no trigger to make the iCloud sync the data? If it syncs on its own, do you know after how much time, does it not load after that interval? – Kamran Sep 25 '19 at 12:31
  • you have done a too tricky task :) , instead you should have saved this background on a server and then should have check in didFinishLaunch of every app if imageUrl is changed you could have downloaded and used that image :) it could have saved your time and a lot of code also the issue you are facing now :) . – Abu Ul Hassan Sep 25 '19 at 12:43

0 Answers0