title: Objective C check if element exists in web view
body: <p>ANSWER: here is how you do it, since I couldn't get any of the solutions below to work. Basically, you got to javascript, check if a document is ready, and return a value to objective c. simple and elegant.
  in objective C, webviewdidfinishload:</p>
  
  <pre><code>        NSLog(@"Didload:Check if webview %d loading or loaded", X);
          // The JS File
          NSString *filePath = [[NSBundle mainBundle] pathForResource:@"UIWebViewSearch" ofType:@"js" inDirectory:@""];
          NSData *fileData = [NSData dataWithContentsOfFile:filePath];
          NSString *jsString = [[NSMutableString alloc] initWithData:fileData encoding:NSUTF8StringEncoding];
          [webView stringByEvaluatingJavaScriptFromString:jsString];
          NSString *result1 = [webView stringByEvaluatingJavaScriptFromString:@"uiWebview_ReturnDocStatus()"];
          NSLog(@"docread= %@", result1);
          int RREsult = [result1 intValue];
          if (RREsult == 0) {
              NSLog(@"Didload: webview %d not yet loaded", X);
          }
          else if (RREsult == 1){
              NSLog(@"Didload: webView %d loaded", X);
          }
  </code></pre>
  
  <p>Then, in javascript, in my UIWebViewSearch.js file:</p>
  
  <pre><code>function uiWebview_ReturnDocStatus() {
  alert ("check");
  if(document.readyState == 'complete'){
      return 1;
  }
  else {
      return 0;
  }
  }
  </code></pre>
  
  <p>Below is my code. I am trying to determine if a certain element exists in my web view. I don't want to extract the entire html text to objective c using document.body etc because that will take up too much memory. I want something quick. But neither option below works. Please help.</p>
  
  <pre><code>NSString *oneString = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementById('moodlez_documentnotyetloaded').value"];
  BOOL test = [webView stringByEvaluatingJavaScriptFromString:@"return !!document.getElementById('moodlez_documentnotyetloaded');"];
  </code></pre>
  
  <p>EDIT1:</p>
  
  <p>Here is my code for objective C, followed by javascript. This code is in didfinishload.</p>
  
  <pre><code>  if (1 ==1 ) {
      //doesn't work
      NSString *exists = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementById('moodlez_documentnotyetloaded');"];
      NSLog(@"exists value=%d", [exists intValue]);
  
  }
  NSString *filePath = [[NSBundle mainBundle] pathForResource:@"UIWebViewSearch" ofType:@"js" inDirectory:@""];
  NSData *fileData = [NSData dataWithContentsOfFile:filePath];
  NSString *jsString = [[NSMutableString alloc] initWithData:fileData encoding:NSUTF8StringEncoding];
  [webView stringByEvaluatingJavaScriptFromString:jsString];
  [webView stringByEvaluatingJavaScriptFromString:@"uiWebview_InjectJSToTellIfLoading()"];
  </code></pre>
  
  <p>.JS code:</p>
  
  <pre><code> function uiWebview_InjectJSToTellIfLoading() {
    if(document.readyState == 'complete'){
  var element = document.createElement('input');
  element.setAttribute('type','hidden');
  element.setAttribute('id','moodlez_documentloaded');
  document.body.appendChild(element);
  
     }
     else {
  if (1 == 1) {
      var element = document.createElement('input');
      element.setAttribute('type','hidden');
      element.setAttribute('id','moodlez_documentnotyetloaded');
      document.body.appendChild(element);
  }
  </code></pre>
  
  <p>}
  }</p>
  
  <p>EDIT 2: OK there are two alternative ways to solve this problem. The first is to use javascript to get the html code from the webview and do a manual search for the moodlez tag. This method is undesirable since it may take up to 1-2 seconds for large webpages, but it works. The second is to make a javascript function that checks if the document is ready and if so modifies a javascript variable that can then be checked from objective c. </p>