问题
According to the developer documentation on Apple's website: https://developer.apple.com/library/ios/#qa/qa1719/_index.html
Starting in iOS 5.0.1 a new "do not back up" file attribute has been introduced allowing developers to clearly specify which files should be backed up. (com.apple.MobileBackup)
I'm wondering if this is supported in PhoneGap / Cordova, as I want to be able to store some offline data (data that can be downloaded or otherwise recreated, but that the user expects to be reliably available when offline) that is not backed up on iCloud.
Persistance is clearly documented (LocalFileSystem.PERSISTENT - http://docs.phonegap.com/en/1.5.0/phonegap_file_file.md.html#LocalFileSystem) on the PhoneGap website, but there seems to be no way of ensuring a saved file is not backed up to the iCloud.
回答1:
Since Phonegap 1.9 you can set in your config.xml:
<preference name="BackupWebStorage" value="none" />
BackupWebStorage (string, defaults to cloud): valid values are none, cloud and local. Set to cloud to allow the web storage data to be backed up to iCloud, and set to local to only allow local backups (iTunes sync). Set to none to not allow any backups of web storage.
To check that it works, Apple recommends this way to check how much data you have put under the auspices of iCloud:
- Install and launch your app
- Go to Settings > iCloud > Storage & Backup > Manage Storage
- If necessary, tap "Show all apps"
- Check your app's storage
Do note, that this cannot be done in a simulator. You need a real device.
回答2:
I'm still holding out for a solution within PhoneGap / Cordova but as a temporary work around...
In my AppDelegate init:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// Get documents directory
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *formularyPath = [documentsDirectory stringByAppendingPathComponent:@"OfflineData"];
if (![[NSFileManager defaultManager] fileExistsAtPath:formularyPath])
[[NSFileManager defaultManager] createDirectoryAtPath:formularyPath withIntermediateDirectories:NO attributes:nil error:nil];
// Prevent iCloud backup
u_int8_t b = 1;
setxattr([formularyPath fileSystemRepresentation], "com.apple.MobileBackup", &b, 1, 0, 0);
Don't forget #import "sys/xattr.h"
This creates a new folder under documents and sets the no backup attribute.
You can then save your files in PhoneGap using the persisted local file store option and files saved in your new subdirectory will not be backed up.
回答3:
Here is a functioning JS code sample leveraging the Cordova framework that I believe solves what Apple is looking for.
document.addEventListener("deviceready",onDeviceReady,false);
function onSetMetadataSuccess() {
console.log("success setting metadata - DONE DONE DONE!")
}
function onSetMetadataFail() {
console.log("error setting metadata")
}
function onGetDirectorySuccess(parent) {
console.log("success getting dir");
parent.setMetadata(onSetMetadataSuccess, onSetMetadataFail, { "com.apple.MobileBackup": 1});
}
function onGetDirecotryFail() {
console.log("error getting dir")
}
function onFileSystemSuccess(fileSystem) {
console.log("onFileSystemSuccess()")
var dirEntry = fileSystem.root;
dirEntry.getDirectory('Backups', {create: true, exclusive: false},
onGetDirectorySuccess, onGetDirecotryFail);
}
function onFileSystemFail(evt) {
console.log("!!!!! onFileSystem fail...")
console.log(evt.target.error.code);
}
/* When this function is called, PhoneGap has been initialized and is ready to roll */
function onDeviceReady()
{
// this and subsequent callbacks tells iOS not to store our data in iCloud.
// without it they rejected our app because of the way PG 1.8 does local->tem storage
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, onFileSystemFail);
}
来源:https://stackoverflow.com/questions/10085653/phonegap-cordova-1-5-ios-do-not-back-up-file-attribute