通过保存html页面使加载UIWebView更快

时间:2016-05-12 04:19:21

标签: ios objective-c uiwebview

我正在制作hybrid iOS app。应用的原生部分会重定向到UIWebView动态数据。是否可以在本地缓存此数据以更快地转换到网页?

1 个答案:

答案 0 :(得分:1)

请尝试这个:

NSData *urlData;
NSString *baseURLString =  @"mysite.com";
NSString *urlString = [baseURLString stringByAppendingPathComponent:@"myfile"];

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval: 10.0]; 
NSURLConnection *connection=[[NSURLConnection alloc] initWithRequest:request delegate:nil]; 

if (connection)
{ 
   urlData = [NSURLConnection sendSynchronousRequest: request];

   NSString *htmlString = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
   [webView loadHTMLString:htmlString baseURL:baseURLString];
   [htmlString release];
}

[connection release];