【问题标题】:Create Plist from content of URL从 URL 的内容创建 Plist
【发布时间】:2012-08-15 12:33:24
【问题描述】:

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

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

我获取 plist 的代码:

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

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

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

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

希望你明白我的意思:)

提前致谢 迈克

【问题讨论】:

    标签: iphone objective-c xml xcode plist


    【解决方案1】:

    保存文件的方法是使用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];
       }
    }
    

    它可能会解决你的问题!

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

        猜你喜欢
        • 2013-03-17
        • 2016-07-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多