我需要在我的iOS应用程序上制作一个离线浏览器,它可以实现以下功能:
当您有互联网连接(wifi、3g、4g等)时,您可以下载您需要在当天阅读的所有网页,然后在没有互联网连接时也可以浏览内容。我从JSON文件导入数据。
我该如何做到这一点?
当您有互联网连接(wifi、3g、4g等)时,您可以下载您需要在当天阅读的所有网页,然后在没有互联网连接时也可以浏览内容。我从JSON文件导入数据。
我该如何做到这一点?
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = [paths objectAtIndex:0];
NSString *path = [NSString stringWithFormat:@"%@",[paths objectAtIndex:0]];
NSDictionary *dict = [[NSDictionary alloc] init];
[dict writeToFile:[path stringByAppendingPathComponent:@"myOfflineData"] atomically:NO];
获取它的方法:
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:[path stringByAppendingPathComponent:@"myOfflineData"]];
或者将其保存为NSString等。
为了跟上你的示例:
NSData *jsonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http:......."]];
id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil]; jsonResults = [jsonObjects objectForKey:@"nodes"];
[jsonData writeToFile:[path stringByAppendingPathComponent:@"myOfflineData"] atomically:NO];
如果你处于离线状态:
NSData *jsonData = [[NSData alloc] initWithContentsOfFile:[path stringByAppendingPathComponent:@"myOfflineData"]];
NSData *jsonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http:......."]]; id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil]; jsonResults = [jsonObjects objectForKey:@"nodes"];
其中jsonResults是一个可变数组。 - Fatima