PhoneGap / Cordova 1.5 iOS“不备份”文件属性

根据苹果公司网站上的开发者文档: https : //developer.apple.com/library/ios/#qa/qa1719/_index.html

从iOS 5.0.1开始,引入了一个新的“不备份”文件属性,允许开发者清楚地指定应该备份哪些文件。 (com.apple.MobileBackup)

我想知道这是否在PhoneGap / Cordova支持,因为我希望能够存储一些离线数据(可下载或重新创build的数据,但用户希望离线时可靠可用),这是不支持的在iCloud上。

PhoneGap网站上的持久性logging清楚(LocalFileSystem.PERSISTENT – http://docs.phonegap.com/en/1.5.0/phonegap_file_file.md.html#LocalFileSystem ),但似乎没有办法确保保存的文件不备份到iCloud。

这里是一个运行的JS代码示例,利用Cordova框架,我相信解决了苹果正在寻找的东西。

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); } 

由于Phonegap 1.9,你可以在你的config.xml中设置:

  <preference name="BackupWebStorage" value="none" /> 

BackupWebStorage(string,默认为云):有效值为无,云和本地。 设置为允许networking存储数据备份到iCloud,并设置为本地只允许本地备份(iTunes同步)。 设置为无,不允许任何Web存储备份。

为了检查它是否有效,Applebuild议用这种方法来检查您在iCloud主持下已经放置了多less数据:

  • 安装并启动您的应用程序
  • 进入设置> iCloud>存储和备份>pipe理存储
  • 如有必要,请点按“显示所有应用”
  • 检查你的应用程序的存储

请注意,这不能在模拟器中完成。 你需要一个真正的设备。

我仍然支持PhoneGap / Cordova的解决scheme,但作为一个临时的工作…

在我的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); 

别忘了#import“sys / xattr.h”

这将在文档下创build一个新文件夹并设置no backup属性。

然后,您可以使用持久化本地文件存储选项将文件保存在PhoneGap中,并且不会备份保存在新子目录中的文件。