how to store json data from url in ios device and to use local it

前端 未结 1 933
醉话见心
醉话见心 2021-01-03 15:19

I want to use json in my application. I can parse json from url with this code :

#import \"ViewController.h\"
@implementation ViewController
{
    NSDictiona         


        
相关标签:
1条回答
  • 2021-01-03 15:49

    The way parsing should be same for Online and Offline json data, both are same, just you need to write a file using Unparsed data say data.json in Documents Directory, if you want to use the same process to use json, but if you want to use parsed json, then write it as plist..

    You can try to use the following methods

    -(void)saveJsonWithData:(NSData *)data{
    
         NSString *jsonPath=[[NSSearchPathForDirectoriesInDomains(NSUserDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingFormat:@"/data.json"];
    
         [data writeToFile:jsonPath atomically:YES];
    
    }
    
    -(NSData *)getSavedJsonData{
        NSString *jsonPath=[[NSSearchPathForDirectoriesInDomains(NSUserDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingFormat:@"/data.json"];
    
        return [NSData dataWithContentsOfFile:jsonPath]
    }
    

    Then call the function as

    - (void)connectionDidFinishLoading:(NSURLConnection *)connection
    {
    
        [self saveJsonWithData:data];
    }
    
    0 讨论(0)
提交回复
热议问题