2011-04-10 94 views
2

我有一个属性列表文件,当我构建我的程序时,它将获取到该包。现在,我想要用我的Mac修改它,并在程序运行时更新它。我想捆绑文件不是正确的方法,因为我似乎没有任何访问捆绑的内容后,它已经建成。iOS - 我可以修改iPhone中的属性列表文件吗?

我应该怎么办?至少在iPhone模拟器上工作会很好,但也可以使用该设备。

回答

1

整理。您对该软件包拥有只读访问权限,因此您需要做的是将软件包中的plist复制到应用程序的文档文件夹中。

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

有一个教程有人在网上写道:基本上回答您的具体问题,因此,而不是试图做我自己的解释这里是一个更好的一个!

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

5

你的应用程序包进行签名,因此在创建后签字不能修改/。 为了修改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; 
    }