只是将应用迁移到WKWebView
,并想知道是否有任何可能的方式来“预加载”多个网址,但一次只显示一个?
我有5个网址的列表。我已经知道我会在某个时间点出现,我希望通过预加载这些体验来加速体验,以便在单个WKWebView
中使用。
答案 0 :(得分:3)
我最终只使用NSURLCache并使用NSURLRequest和NSURLConnection预加载所有URL。然后每当我将一个url加载到WKWebView时,它就会根据我的缓存策略使用缓存的请求。
答案 1 :(得分:2)
执行此操作的相对直接的方法是创建五个NSData
对象(异步),每个对象使用已知的URL进行初始化。当您需要显示其中一个时,可以将NSData
转换为字符串,然后调用WKWebView
' loadHTMLString
功能来更改显示的页面。
答案 2 :(得分:0)
您还可以预加载五个不同的WKWebView
实例,并在需要特定网址时将其交换进/出。这取决于您的用户界面和当然的互动。
答案 3 :(得分:0)
使用NSURLCache
这是代码
快速
// Create URLRequest
var request: URLRequest? = nil
if let url = URL(string: "YOUR_URL") {
request = URLRequest(url: url)
}
// Check the cache.
var cachedResponse: CachedURLResponse? = nil
if let request = request {
cachedResponse = URLCache.shared.cachedResponse(for: request)
}
print(cachedResponse != nil ? "Cached response found!" : "No cached response found.")
// Load the cache
do {
if let request = request {
try NSURLConnection.sendSynchronousRequest(request, returning: nil)
}
} catch {
}
Obj-C
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"YOUR_URL"]];
// Check the cache.
NSCachedURLResponse *cachedResponse = [[NSURLCache sharedURLCache] cachedResponseForRequest:request];
NSLog(cachedResponse ? @"Cached response found!" : @"No cached response found.");
//Load cache
[NSURLConnection sendSynchronousRequest:request returningResponse:NULL error:NULL];