SoFunction
Updated on 2025-04-03

Solution to iOS UIWebView not getting web titles

I encountered a problem recently, that isUIWebViewin the proxy method, executeofjsThe code cannot obtain the web page title, the code is as follows:

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
  // Get the title name of the load html file  NSString *title = [webView stringByEvaluatingJavaScriptFromString:@""];
}

When this problem occurs, I first determine whether it is a problem with the code. After analysis, I found that the code has not been changed, but this time I cannot get the web page title, which is very strange. After searching and analysis, the problem is that in this version, the front-end personnel put the title setting of the web page in an asynchronous operation, which causedUIWebViewAfter loading the web page, in the proxy methodwebViewDidFinishLoad: cannot get the title immediately because the method of obtaining the title is asynchronous, and the proxy method will be called after the web page is loaded. At that time, the web pagetitleThere is no value yet, so the value of the title cannot be obtained.

The following is the asynchronous acquisition of the web pagetitleThe code usedjQueryandAjaxTechnology to obtain title asynchronously:

 $.ajax({
    url: remoteur+'/api/innerMessageApi/?callBackFunc=xx',
    type: 'get',
    dataType: 'jsonp',
    jsonpCallback:"xx",
    data: {msgId: msgId},

    success: function(res){
      (res);
      if (  == 'Y' ){
        content = ;
        title  = ;
      }
    },
    complete:function(res){
       = title;
      $('body').append(content);
    }
  })

When this problem arises, it is the night when the project is about to be launched. When I was working overtime in groups, I encountered this problem and felt that I was deeply hurt that night. . .

OK, let’s not talk much, let’s introduce the solution below:

Method 1

If only consideriOSThe solution may be delayed acquisitiontitle, specificallywebViewDidFinishLoad:Execute through delayAlthough it can be solved, it is risky because the web page obtains the title asynchronously, and the time of asynchronousness is uncertain, so the time of delay is not certain. Although it can increase the time of delay, it is not a perfect solution.

  dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)),    

  dispatch_get_main_queue(), ^{
     = [webView stringByEvaluatingJavaScriptFromString:@""];
  });

Method 2

If you consider the web page, you can change the asynchronous operation of obtaining titles in the web page to synchronous operation. According to the above js code, you can add a synchronous field async: false. The modified web page code is as follows:

$.ajax({
    url: remoteur+'/api/innerMessageApi/?callBackFunc=xx',
    type: 'get',
    dataType: 'jsonp',
    jsonpCallback:"xx",
    data: {msgId: msgId},
    // Set up synchronization operations    async: false,

    success: function(res){
      // Synchronously set the title        = ;
    complete:function(res){
      ...
    }
  })

Although this can solve the problem, it is still not a good solution. For example, when a web page is loaded, it uses synchronous means to obtain the web page title. If the synchronization operation is blocked, the web page loading will be blocked, which will cause the web page to be unable to display, so it is still not the optimal solution.

Method 3

If the web page and iOS side are combined, you can obtain the title asynchronously on the web page side. After obtaining the title, you can call the native method to set the title through js. This will not block the web page loading process due to synchronous acquisition of the title, nor will the delay time cannot be determined due to delayed acquisition of the title, so this method can perfectly solve this problem.

js side code:

$.ajax({
    url: remoteur+'/api/innerMessageApi/?callBackFunc=xx',
    type: 'get',
    dataType: 'jsonp',
    jsonpCallback:"xx",
    data: {msgId: msgId},
    // Set up synchronization operations    async: false,

    success: function(res){
      // Synchronously set the title       = ;
      // js calls native methods to set the title      setWebViewTitle(title); 
    complete:function(res){
      ...
    }
  })

iOS side code:

  context[@"setWebViewTitle"] = ^(){
    NSArray *args = [JSContext currentArguments];
    if ( == 1) {
      // Set the title, you only need to pass one parameter       = [args firstObject];
    }
  };

In fact, there are many ways to call js natively. Here is just a relatively simple method. It is OK to use any specific method. If you don’t know much about this, you can refer to other materials.

I found a lot of information on the Internet and found that most of the dynamic modification of web page titles are synchronous operations, and there are fewer introductions to dynamically modifying titles using asynchronous operations. So I summarized the methods of dynamically setting titles as above, hoping to give some help to friends in need.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.