我注意到iphone safari会缓存内容,以便以后加载页面的速度比桌面浏览器快得多.所以以移动gmail网页为例,第一次加载很慢(5-10秒).但如果我关闭标签并再次重新打开页面,则会非常快(1秒).
但是,如果通过应用程序中的UIWebView加载内容,则此行为不一样.我错过了一些设置吗?如何让UIWebView自动缓存内容而无需自己保存内容的麻烦?
关键是: NSURLRequestReturnCacheDataElseLoad
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];