Skip to content

Instantly share code, notes, and snippets.

@VGraupera
Created February 17, 2015 16:20
Show Gist options
  • Save VGraupera/e9f81ecd6548d668bf97 to your computer and use it in GitHub Desktop.
Save VGraupera/e9f81ecd6548d668bf97 to your computer and use it in GitHub Desktop.
Backup and restore to iCloud
def backup
error_ptr = Pointer.new(:object)
fileManager = NSFileManager.defaultManager
# iCloud
icloud = fileManager.URLForUbiquityContainerIdentifier(nil)
Logger.debug "iCloud at #{NSHomeDirectory().stringByAppendingPathComponent( "Documents/")}"
if icloud
icloud_url = icloud.URLByAppendingPathComponent("Documents/cd_backup.sqlite")
a = fileManager.startDownloadingUbiquitousItemAtURL(icloud_url, error:error_ptr)
Logger.debug "iCloud URL at #{icloud_url.path}"
Logger.debug "iCloud downloaded? #{a}"
if fileManager.fileExistsAtPath(icloud_url.path)
BW::UIAlertView.new({
title: 'Found a previous backup',
message: "There is a backup file in iCloud, would like to overwirte this backup?",
buttons: ['Don\'t backup now', 'Backup right now'],
cancel_button_index: 0
}) do |alert|
if alert.clicked_button.cancel?
# puts 'Canceled'
else
# Remove this file in iCloud
fileManager.removeItemAtURL(icloud_url, error:error_ptr)
backup_to(icloud_url)
end
end.show
else
backup_to(icloud_url)
end
else
App.alert("iCloud not woking on your device.")
end
end
def backup_to(path)
error_ptr = Pointer.new(:object)
App.delegate.persistentStoreCoordinator.persistentStores.each do |persistentStore|
SVProgressHUD.show
App.delegate.persistentStoreCoordinator.migratePersistentStore(persistentStore, toURL:path, options:nil, withType:NSSQLiteStoreType, error:error_ptr)
SVProgressHUD.dismiss
App.alert("Backup successful!")
end
end
def restore
error_ptr = Pointer.new(:object)
fileManager = NSFileManager.alloc.init
# iCloud
icloud = NSFileManager.defaultManager.URLForUbiquityContainerIdentifier(nil)
if icloud
icloud_url = icloud.URLByAppendingPathComponent("Documents/cd_backup.sqlite")
a = fileManager.evictUbiquitousItemAtURL(icloud_url, error:error_ptr)
b = fileManager.startDownloadingUbiquitousItemAtURL(icloud_url, error:error_ptr)
Logger.debug "iCloud URL at #{icloud_url.path}"
Logger.debug "iCloud evicted? #{a} and downloaded? #{b}"
if fileManager.fileExistsAtPath(icloud_url.path)
App.delegate.persistentStoreCoordinator.persistentStores.each do |persistentStore|
SVProgressHUD.show
App.delegate.persistentStoreCoordinator.removePersistentStore(persistentStore, error:error_ptr)
if fileManager.fileExistsAtPath(App.delegate.sqlite_path)
storeDirectory = App.delegate.sqlite_url.URLByDeletingLastPathComponent
enumerator = fileManager.enumeratorAtURL(storeDirectory, includingPropertiesForKeys:nil, options:0, errorHandler:nil)
storeName = App.delegate.sqlite_url.lastPathComponent.stringByDeletingPathExtension
enumerator.each do |url|
unless url.lastPathComponent.hasPrefix(storeName)
next
end
fileManager.removeItemAtURL(url, error:error_ptr)
end
end
# Copy the new data file
fileManager.copyItemAtPath(icloud_url.path, toPath:App.delegate.sqlite_path, error:error_ptr)
App.delegate.persistentStoreCoordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: App.delegate.sqlite_url, options: App.delegate.persistent_store_options, error: error_ptr)
SVProgressHUD.dismiss
App.alert("Restroe successful!")
end
else
App.alert("Didn't find any backup file in your iCloud, can't restore now!")
end
else
App.alert("iCloud not woking on your device.")
end
end
@VGraupera
Copy link
Author

Warning. This code does not work correctly. See lines 10 and 56. startDownloadingUbiquitousItemAtURL is asynchronous.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment