【问题标题】:iOS - Can I modify a property list file live in iPhone?iOS - 我可以修改 iPhone 中的属性列表文件吗?
【发布时间】:2011-04-10 17:02:05
【问题描述】:

我有一个属性列表文件,当我构建我的程序时它会进入捆绑包。现在,我想用我的 Mac 对其进行修改,并在我的程序运行时对其进行更新。我想捆绑文件不是正确的方法,因为我似乎在构建捆绑包后无法访问它的内容。

我应该如何处理?至少与 iPhone 模拟器一起工作会很好,但与设备一起工作也会非常好。

【问题讨论】:

    标签: iphone properties bundle


    【解决方案1】:

    您的应用程序包已签名,因此在创建/签名后无法修改。 为了修改 plist,您需要先将其复制到应用程序的 Documents 目录中。然后,您可以修改副本。这是我在我的一个应用程序中使用的一种方法,它在应用程序启动期间将名为 FavoriteUsers.plist 的文件从包复制到文档目录。

        /* Copies the FavoritesUsers.plist file to the Documents directory
         * if the file hasn't already been copied there
         */
        + (void)moveFavoritesToDocumentsDir
        {
            /* get the path to save the favorites */
            NSString *favoritesPath = [self favoritesPath];
    
            /* check to see if there is already a file saved at the favoritesPath
             * if not, copy the default FavoriteUsers.plist to the favoritesPath
             */
            NSFileManager *fileManager = [NSFileManager defaultManager];
            if(![fileManager fileExistsAtPath:favoritesPath])
            {
            NSString *path = [[NSBundle mainBundle] pathForResource:@"FavoriteUsers" ofType:@"plist"];
            NSArray *favoriteUsersArray = [NSArray arrayWithContentsOfFile:path];
            [favoriteUsersArray writeToFile:favoritesPath atomically:YES];
            }
        }
    
        /* Returns the string representation of the path to
         * the FavoriteUsers.plist file
         */
        + (NSString *)favoritesPath
        {
            /* get the path for the Documents directory */
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *documentsPath = [paths objectAtIndex:0];
    
            /* append the path component for the FavoriteUsers.plist */
            NSString *favoritesPath = [documentsPath stringByAppendingPathComponent:@"FavoriteUsers.plist"];
            return favoritesPath;
        }
    

    【讨论】:

      【解决方案2】:

      有点。您对捆绑包具有只读访问权限,因此您需要将捆绑包中的 plist 复制到应用的文档文件夹中。

      文档文件夹是应用程序沙箱的一个区域,您可以在其中读取和写入文件。因此,如果您在应用首次启动时将 plist 复制到那里,您将能够根据自己的喜好对其进行编辑和修改。

      有人在网上写了一个教程,基本上回答了你的确切问题,所以与其尝试做我自己的解释,这里有一个更好的解释!

      http://iphonebyradix.blogspot.com/2011/03/read-and-write-data-from-plist-file.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-08
        • 2021-05-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多