0

我制作了一个 PHP 文件,它打印出工作 plist 的所有源代码,我可以通过这种方式成功地将数据库中的数据检索到我的应用程序中。

但是我不希望应用程序总是在线获取数据库,所以我希望用户能够指定他/她何时想要更新 plist(通过按下按钮“更新数据库”)。

我获取 plist 的代码:

NSURL *url_ = [[NSURL alloc] initWithString:@"http://localhost/~user/plist.php"];
NSArray *data = [NSArray arrayWithContentsOfURL:url_];

这很好用,但是用户不必在线即可使用数据库...

我想要做的是用本地 plist 替换它,这很简单。

每当单击“更新数据库”按钮时,我想从 url 下载新的 plist 并替换本地保存的 plist。

希望你明白我的意思:)

在此先感谢迈克

4

2 回答 2

2

保存文件的方法是使用writeToFile:atomically返回 BOOL 的方法来告诉您文件是否已保存。

这是一个代码示例:

NSArray* a = [NSArray new]; 
BOOL sucess = [a writeToFile:@"my.plist" atomically:YES];
if (!sucess) {
    NSLog(@"Ups, not saved");
}

之后,您可以从您的文档路径加载文件

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //1  Create a list of paths.
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"my.plist"]; 

NSFileManager *fileManager = [NSFileManager defaultManager];

if ([fileManager fileExistsAtPath: path]) {
    NSarray* a = [NSArray arrayWithContentsOfFile:path];
   }
}

它可能会解决你的问题!

于 2012-08-15T13:06:01.457 回答
1

这样做将您的 plist 数据写入文档目录,如下所示:

  [arrImages writeToFile:@"yourDocDirPathWithPlist" atomically:YES];

现在 plist 创建并存储在文档目录中

于 2012-08-15T12:46:40.433 回答