【问题标题】:PhoneGap / Cordova 1.5 iOS "do not back up" file attributePhoneGap / Cordova 1.5 iOS“不备份”文件属性
【发布时间】:2012-04-22 13:27:18
【问题描述】:

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

从 iOS 5.0.1 开始,引入了新的“不备份”文件属性,允许开发人员明确指定应备份哪些文件。 (com.apple.MobileBackup)

我想知道 PhoneGap / Cordova 是否支持这一点,因为我希望能够存储一些离线数据(可以下载或以其他方式重新创建的数据,但用户希望在离线时可靠可用)未在 iCloud 上备份。

PhoneGap 网站上明确记录了持久性 (LocalFileSystem.PERSISTENT - http://docs.phonegap.com/en/1.5.0/phonegap_file_file.md.html#LocalFileSystem),但似乎无法确保保存的文件不会备份到 iCloud。

【问题讨论】:

    标签: ios cordova icloud phonegap-build


    【解决方案1】:

    从 Phonegap 1.9 开始,您可以在 config.xml 中设置:

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

    BackupWebStorage(字符串,默认为云):有效值为无、云和本地。设置为云以允许将网络存储数据备份到 iCloud,设置为本地以仅允许本地备份(iTunes 同步)。设置为 none 以不允许任何网络存储备份。

    为了检查它是否有效,Apple 建议通过这种方式检查您在 iCloud 的支持下放置了多少数据:

    • 安装并启动您的应用
    • 转到设置> iCloud > 存储和备份> 管理存储
    • 如有必要,点按“显示所有应用”
    • 检查应用的存储空间

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

    【讨论】:

    • 这是近 2 年前的一个老问题,与 1.5 版有关,在该功能实施之前。
    • 是的,你是对的,但问题仍然存在,我想帮助其他用户。
    【解决方案2】:

    这是一个利用 Cordova 框架的有效 JS 代码示例,我相信它可以解决 Apple 正在寻找的问题。

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

    【讨论】:

      【解决方案3】:

      我仍在等待 PhoneGap / Cordova 中的解决方案,但作为临时解决方案...

      在我的 AppDelegate 初始化中:

      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"

      这会在文档下创建一个新文件夹并设置无备份属性。

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

      【讨论】:

      • 感谢@LeeCrossley 的回答!!我参与了一个PhoneGap项目,该项目下载的数据应该保留而不是备份,但是,由于我们使用的版本是1.7,我们不能使用setMetadata功能,升级到1.8就可以了现在一团糟。只是一个问题,您是否在应用商店中发布了包含此代码的任何内容?我看不出它可能被拒绝的原因,但是,由于我既没有使用 Objective-c 也没有发布到应用商店的经验,所以我想确定
      • 是的,这个应用程序:itunes.apple.com/gb/app/nice-bnf/id523093958 使用上面的代码,目前运行的是 1.7。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-09
      相关资源
      最近更新 更多