iOS 将网络请求数据转换成plist文件

需求设想:

iOS存储方式有很多,我就不一一说明了,现在我只提到我用到的。

现在有个需求是将部分数据存到本地,并且在没有网络的情况下能够查看数据。

根据这个需求,也就是需要做一个数据本地化存储。

那么我们可以想到有呢么几种方式:

1.NSUserDefaults 

2.CoreData

3.存到Library目录下(以plist方式存储、用数据库方式存储)


思路解决:

NSUserDefaults  简单的存储,不是很实用,只能存点简单的用户名本地密码本地等等。

CoreData也是键值对的存储,但是coreData没用过不是很熟悉,哈哈。

在沙盒创建数据库到是做过,但是感觉操作有点东西,写半天东西虽然看起来高大上,但是呢一样的效果。

那么最后我就直接将请求下来的数据存到plist然后如果有网络的情况下我就更新下,没网的情况下我就调plist的东西。


效果:

添加一个按钮,用来测试

iOS 将网络请求数据转换成plist文件

点击按钮,打印出地址

iOS 将网络请求数据转换成plist文件

桌面用command+ shift+g打开文件目录

iOS 将网络请求数据转换成plist文件


代码:

NSString *soapBody = [NSString stringWithFormat:@"<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:jax=\"http://jaxws.webservices.sdoon.com/\"><soapenv:Header><m:auth xmlns:m=\"http://auth.sayhello.HelloWorld2.webservice.cvicse.com\"  soapenv:actor=\"http://schemas.xmlsoap.org/soap/actor/next\"  soapenv:mustUnderstand=\"0\">aaaaaaaaaaaa</m:auth></soapenv:Header><soapenv:Body><jax:queryByMyItemList><arg0>select distinct ModuleID, ModuleName from DesignInfo_new </arg0></jax:queryByMyItemList></soapenv:Body></soapenv:Envelope>"];
    [self.manager yibuPostWithUrl:@"http://10.185.36.13:8082/ylj_shipin/webservice/shipin?wsdl" parameters:soapBody otherStrings:[NetWorkAddress ggaqZdbhdxListHeadParameters] success:^(id responds) {
        NSString *docPath = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).lastObject;
        NSString *filePath = [docPath stringByAppendingPathComponent:@"/GGAQ"];
        BOOL exist = [[NSFileManager defaultManager] fileExistsAtPath:filePath];
        if (!exist) {
            [[NSFileManager defaultManager] createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
        }
        NSString *destPath = [filePath stringByAppendingPathComponent:@"showBuilding.plist"];
        BOOL save = [responds writeToFile:destPath atomically:YES];
        if (save) {
            NSLog(@"yes");
        }
        NSLog(@"%@",destPath);
    } failure:^(NSError *error) {
        NSLog(@"%@",error.localizedDescription);
    }];

说明:上面这段是请求下来存到plist的代码。我请求下来的是字典,存到plist也是字典,调用的时候再取出来用就行了。

plist文件:

iOS 将网络请求数据转换成plist文件