Although Apple has now abandoned UIWebView, a lot of projects still use UIWebView. A common way to get this is to get it in JS in UIWebView’s Delegate webViewDidFinishLoad method

[the webView stringByEvaluatingJavaScriptFromString: @ “document. The title”] but encountered in the project after the page load can’t get to the title, as the access method return value is null. Failed to get the page title correctly.

After investigation, there are several solutions:

  1. Add delay:Dispatch_after (dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ self.navigationItem.title = [self.webView stringByEvaluatingJavaScriptFromString:@"document.title"]; });This scheme is crude, inelegant and uncertain of the actual timing of the title. There may be a time lag when AJAX requests are set up asynchronously. But it solves most cases where you don’t set the title dynamically through AJAX.
  2. Add a timer. Every once in a while to execute the JS method to get the title, of course, because of the cost of resources, should be a lack of set title request, if there is to manually stop the timer.

_intervalTimer = [NSTimer scheduledTimerWithTimeInterval: 0.03 target: self selector: @ the selector (timerCallBack:) userInfo:nil repeats:YES]

self.navigationItem.title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"]; if (self.navigationItem.title) { [_intervalTimer invalidate]; _intervalTimer = nil; }}Copy the code

This way, you need to handle the timer manually, or better yet, you need to set off the timer after a few cycles.

The above two methods do not need to change the H5 page. The third method is to add the H5 callback, which is the most real-time and suitable for the H5 page in the APP. The above two methods can be used for activities or external links of variable URLS.

  1. The third way is a safer and more reasonable way. This is how H5 calls the client after asynchronously changing or setting the title. That is, JS calls the local way, passing the title to the client, the client according to the value to set.