iOS - 我可以修改 iPhone 中的实时属性列表文件吗?

发布于 2024-10-31 13:58:47 字数 152 浏览 3 评论 0原文

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

我应该如何接近?至少使用 iPhone 模拟器工作会很不错,但使用设备工作也会非常好。

I have a property list file which gets to the bundle when I build my program. Now, I would like to be to modify it with my Mac and get it updated while my program is running. I guess bundling the file is not the correct approach, as I do not seem to have any access to the contents of the bundle after it has been built.

How should I approach? It would be nice to work at least with the iPhone simulator but it would be veeery nice to work with the device too.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

葬﹪忆之殇 2024-11-07 13:58:47

您的应用程序包已签名,因此在创建/签名后无法对其进行修改。
为了修改 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;
    }

Your application bundle is signed, so it can not be modified after it is created/signed.
In order to modify the plist, you need to copy it to the Documents directory for your application first. You can then modify the copy. Here is a method that I have in one of my apps that copies a file called FavoriteUsers.plist from the bundle to the documents directory during app start up.

    /* 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;
    }
掩于岁月 2024-11-07 13:58:47

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

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

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

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

Sort of. You have read-only access to the bundle, so what you need to do is copy over the plist from your bundle into your app's documents folder.

The documents folder is an area of your application's sandbox where you can read and write to files. So if you copy the plist there upon the very first launch of your app, you'll be able to edit and modify it to your heart's content.

There's a tutorial somebody wrote online that basically answers your exact question, so rather than try to do my own explanation here's a much better one!

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文