Я позволяю пользователю выбирать свое собственное фоновое изображение для приложения.Предполагается, что это изображение синхронизируется между устройствами с помощью iCloud, но я не могу заставить его работать.Изображение отображается для сохранения и доступно для устройства, которое его сохранило, но другие устройства не могут получить к нему доступ.Вот как у меня все настроено:
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
}
Что-то мне не хватает или что-то не так с моим кодом?