Reputation: 14123
I am using UIWebView
to render web content in my application. I observed that the initial request when the app launches i.e. loadRequest
, takes a long time to render the contents. However the subsequent requests which I don't track of, are much faster.
To confirm this, I created a standalone application which just has a UIWebView
. This is the single line of code which I have added :
[wkBrowser loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.yahoo.com"]]];
The result is same. It takes around 15-20 seconds to load the page. However on tapping any link on web page, it takes 3-5 seconds to load the next page. I did put the UIWebView delegate function didFailLoadWithError, but there is never an error.
Question:
Upvotes: 16
Views: 14222
Reputation: 1589
Just load UIWebView
with a dummy page off-screen, during your app's launch.This will work well on real devices.
In AppDelegates didFinishLaunchingWithOptions method.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
self.loadInitialWebView()
return true
}
func loadInitialWebView() {
var dummyWebView : UIWebView? = UIWebView(frame: UIScreen.main.bounds)
let htmlString = "<!doctype html><html lang=en><head><meta charset=utf-8><title>dummy</title></head><body><p>dummy content</p></body></html>"
dummyWebView?.loadHTMLString(htmlString, baseURL: nil)
dummyWebView = nil;
}
Upvotes: 0
Reputation:
The answer of your first question: When you're loading request for the first time, the webview downloads and caches all the files like css, images, etc... After, when you click on a url that brings you to another page of the same website, it's basically loads it from the cache instead of downloading again, and it saves a lot of time.
Upvotes: 1
Reputation: 2028
There are multiple reasons the first load might be slower than subsequent loads.
First of all, your application is linking to dynamic frameworks. Dynamic link resolution is performed lazily. So whenever you use the UIWebView
for the first time, there is a very small overhead involved with resolving the symbols you have used from the dynamic library. The UIWebView
itself could depend on some more internal libraries that need to be dinamically linked, adding to that overhead.
Then there is also the implementation of the UIWebView
itself. The UIWebView
's internal most likely involve data structures that are initialized lazily.
Then there is the memory allocation aspect. Whenever you make your first page load, the memory usage of your application rises much more than what it does upon the second page load. Allocating sufficient memory to process a page load adds to the overhead, but once that is done, iOS will entrust this memory to your application for some period of time that exceeds the point where the memory was deallocated in order to make your application faster when it hits peaks in its memory usage.
Someone suggested that there might be cached resources between your two pages. Even if both pages are different and are being loaded for the first time, they might share a common css file or a common javascript file such as jQuery. Then the browser is able to reuse those cached resources.
Overall, there are a lot of factors like these that explain why the first page load might be slower than subsequent page loads. You could get to know more about what the UIWebView
is actually doing by profiling the application during both page loads.
Upvotes: 3
Reputation: 5667
The main reason is because, when you invoke the first time URL request, the server on the receiving end tries to display the information as per the requested device browser, ( like mobile, desktop). This is where you see a bit of delay.
Hope this solves the doubt.
Upvotes: 5
Reputation: 417
One thing you can do here to see what is going on is to implement an NSURLProtocol similar to the following link: http://www.raywenderlich.com/59982/nsurlprotocol-tutorial
and intercept your requests in the canInitWithRequest method.
This will only show you what is going on with your requests at least.
Upvotes: 0
Reputation: 11039
The answer of your first question is following:
When you're loading request for the first time, the webview downloads and caches all the files like css, images, etc... After, when you click on a url that brings you to another page of the same website, it's basically loads it from the cache instead of downloading again, and it saves a lot of time.
Upvotes: 3