title: How to call a Javascript method in iOS?
body: <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;html&gt;
  &lt;head&gt;
  &lt;script type="text/javascript" src="js/jquery.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="js/quiz-1.js"&gt;&lt;/script&gt;
  &lt;link rel="stylesheet" type="text/css" href="style.css" /&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;p class="question"&gt;1. What is the answer to this question?&lt;/p&gt;        
  
  &lt;ul class="answers"&gt;            
  &lt;input type="radio" name="q1" value="a" id="q1a"&gt;&lt;label for="q1a"&gt;Answer 1&lt;/label&gt;&lt;br/&gt;          
  &lt;input type="radio" name="q1" value="b" id="q1b"&gt;&lt;label for="q1b"&gt;Answer 2&lt;/label&gt;&lt;br/&gt;            
  &lt;input type="radio" name="q1" value="c" id="q1c"&gt;&lt;label for="q1c"&gt;Answer 3&lt;/label&gt;&lt;br/&gt;            
  &lt;input type="radio" name="q1" value="d" id="q1d"&gt;&lt;label for="q1d"&gt;Answer 4&lt;/label&gt;&lt;br/&gt;       
  &lt;/ul&gt;        
  
  
  &lt;p class="question"&gt;2. What is the answer to this question?&lt;/p&gt;        
  
  &lt;ul class="answers"&gt;            
  &lt;input type="radio" name="q2" value="a" id="q2a"&gt;&lt;label for="q2a"&gt;Answer 1&lt;/label&gt;&lt;br/&gt;           
  &lt;input type="radio" name="q2" value="b" id="q2b"&gt;&lt;label for="q2b"&gt;Answer 2&lt;/label&gt;&lt;br/&gt;            
  &lt;input type="radio" name="q2" value="c" id="q2c"&gt;&lt;label for="q2c"&gt;Answer 3&lt;/label&gt;&lt;br/&gt;           
  &lt;input type="radio" name="q2" value="d" id="q2d"&gt;&lt;label for="q2d"&gt;Answer 4&lt;/label&gt;&lt;br/&gt;       
  &lt;/ul&gt;        
  
  &lt;p class="question"&gt;3. What is the answer to this question?&lt;/p&gt;        
  
  &lt;ul class="answers"&gt;            
  &lt;input type="radio" name="q3" value="a" id="q3a"&gt;&lt;label for="q3a"&gt;Answer 1&lt;/label&gt;&lt;br/&gt;            
  &lt;input type="radio" name="q3" value="b" id="q3b"&gt;&lt;label for="q3b"&gt;Answer 2&lt;/label&gt;&lt;br/&gt;            
  &lt;input type="radio" name="q3" value="c" id="q3c"&gt;&lt;label for="q3c"&gt;Answer 3&lt;/label&gt;&lt;br/&gt;           
  &lt;input type="radio" name="q3" value="d" id="q3d"&gt;&lt;label for="q3d"&gt;Answer 4&lt;/label&gt;&lt;br/&gt;       
  &lt;/ul&gt;        
  
  &lt;p class="question"&gt;4. What is the answer to this question?&lt;/p&gt;        
  
  &lt;ul class="answers"&gt;           
  &lt;input type="radio" name="q4" value="a" id="q4a"&gt;&lt;label for="q4a"&gt;Answer 1&lt;/label&gt;&lt;br/&gt;            
  &lt;input type="radio" name="q4" value="b" id="q4b"&gt;&lt;label for="q4b"&gt;Answer 2&lt;/label&gt;&lt;br/&gt;            
  &lt;input type="radio" name="q4" value="c" id="q4c"&gt;&lt;label for="q4c"&gt;Answer 3&lt;/label&gt;&lt;br/&gt;            
  &lt;input type="radio" name="q4" value="d" id="q4d"&gt;&lt;label for="q4d"&gt;Answer 4&lt;/label&gt;&lt;br/&gt;        
  &lt;/ul&gt;        
  
  &lt;p class="question"&gt;5. What is the answer to this question?&lt;/p&gt;        
  
  &lt;ul class="answers"&gt;            
  &lt;input type="radio" name="q5" value="a" id="q5a"&gt;&lt;label for="q5a"&gt;Answer 1&lt;/label&gt;&lt;br/&gt;            
  &lt;input type="radio" name="q5" value="b" id="q5b"&gt;&lt;label for="q5b"&gt;Answer 2&lt;/label&gt;&lt;br/&gt;            
  &lt;input type="radio" name="q5" value="c" id="q5c"&gt;&lt;label for="q5c"&gt;Answer 3&lt;/label&gt;&lt;br/&gt;           
  &lt;input type="radio" name="q5" value="d" id="q5d"&gt;&lt;label for="q5d"&gt;Answer 4&lt;/label&gt;&lt;br/&gt;        
  &lt;/ul&gt;        
  
  &lt;p class="question"&gt;6. What is the answer to this question?&lt;/p&gt;        
  
  &lt;ul class="answers"&gt;            
  &lt;input type="radio" name="q6" value="a" id="q6a"&gt;&lt;label for="q6a"&gt;Answer 1&lt;/label&gt;&lt;br/&gt;            
  &lt;input type="radio" name="q6" value="b" id="q6b"&gt;&lt;label for="q6b"&gt;Answer 2&lt;/label&gt;&lt;br/&gt;            
  &lt;input type="radio" name="q6" value="c" id="q6c"&gt;&lt;label for="q6c"&gt;Answer 3&lt;/label&gt;&lt;br/&gt;            
  &lt;input type="radio" name="q6" value="d" id="q6d"&gt;&lt;label for="q6d"&gt;Answer 4&lt;/label&gt;&lt;br/&gt;        
  &lt;/ul&gt;       
  
  &lt;p class="question"&gt;7. What is the answer to this question?&lt;/p&gt;        
  
  &lt;ul class="answers"&gt;            
  &lt;input type="radio" name="q7" value="a" id="q7a"&gt;&lt;label for="q7a"&gt;Answer 1&lt;/label&gt;&lt;br/&gt;            
  &lt;input type="radio" name="q7" value="b" id="q7b"&gt;&lt;label for="q7b"&gt;Answer 2&lt;/label&gt;&lt;br/&gt;            
  &lt;input type="radio" name="q7" value="c" id="q7c"&gt;&lt;label for="q7c"&gt;Answer 3&lt;/label&gt;&lt;br/&gt;            
  &lt;input type="radio" name="q7" value="d" id="q7d"&gt;&lt;label for="q7d"&gt;Answer 4&lt;/label&gt;&lt;br/&gt;        
  &lt;/ul&gt;        
  
  &lt;p class="question"&gt;8. What is the answer to this question?&lt;/p&gt;        
  
  &lt;ul class="answers"&gt;            
  &lt;input type="radio" name="q8" value="a" id="q8a"&gt;&lt;label for="q8a"&gt;Answer 1&lt;/label&gt;&lt;br/&gt;            
  &lt;input type="radio" name="q8" value="b" id="q8b"&gt;&lt;label for="q8b"&gt;Answer 2&lt;/label&gt;&lt;br/&gt;            
  &lt;input type="radio" name="q8" value="c" id="q8c"&gt;&lt;label for="q8c"&gt;Answer 3&lt;/label&gt;&lt;br/&gt;            
  &lt;input type="radio" name="q8" value="d" id="q8d"&gt;&lt;label for="q8d"&gt;Answer 4&lt;/label&gt;&lt;br/&gt;       
  &lt;/ul&gt;        
  
  &lt;p class="question"&gt;9. What is the answer to this question?&lt;/p&gt;        
  
  &lt;ul class="answers"&gt;            
  &lt;input type="radio" name="q9" value="a" id="q9a"&gt;&lt;label for="q9a"&gt;Answer 1&lt;/label&gt;&lt;br/&gt;            
  &lt;input type="radio" name="q9" value="b" id="q9b"&gt;&lt;label for="q9b"&gt;Answer 2&lt;/label&gt;&lt;br/&gt;            
  &lt;input type="radio" name="q9" value="c" id="q9c"&gt;&lt;label for="q9c"&gt;Answer 3&lt;/label&gt;&lt;br/&gt;            
  &lt;input type="radio" name="q9" value="d" id="q9d"&gt;&lt;label for="q9d"&gt;Answer 4&lt;/label&gt;&lt;br/&gt;        
  &lt;/ul&gt;        
  
  &lt;p class="question"&gt;10. What is the answer to this question?&lt;/p&gt;        
  
  &lt;ul class="answers"&gt;           
  &lt;input type="radio" name="q10" value="a" id="q10a"&gt;&lt;label for="q10a"&gt;Answer 1&lt;/label&gt;&lt;br/&gt;            
  &lt;input type="radio" name="q10" value="b" id="q10b"&gt;&lt;label for="q10b"&gt;Answer 2&lt;/label&gt;&lt;br/&gt;            
  &lt;input type="radio" name="q10" value="c" id="q10c"&gt;&lt;label for="q10c"&gt;Answer 3&lt;/label&gt;&lt;br/&gt;            
  &lt;input type="radio" name="q10" value="d" id="q10d"&gt;&lt;label for="q10d"&gt;Answer 4&lt;/label&gt;&lt;br/&gt;        
  &lt;/ul&gt;        
  
  &lt;br/&gt;
  &lt;div id="results"&gt;            
  Show me the answers!       
  &lt;/div&gt;                
  
  &lt;div id="category1"&gt;            
  &lt;p&gt;              
  &lt;strong&gt;Question 1:&lt;/strong&gt; The correct answer is the &lt;strong&gt;Answer 1&lt;/strong&gt;.&lt;/p&gt;        
  &lt;/div&gt;        
  
  &lt;div id="category2"&gt;            
  &lt;p&gt;              
  &lt;strong&gt;Question 2:&lt;/strong&gt; The correct answer is &lt;strong&gt;Answer 2&lt;/strong&gt;.&lt;/p&gt;        
  &lt;/div&gt;        
  
  &lt;div id="category3"&gt;            
  &lt;p&gt;                
  &lt;strong&gt;Question 3:&lt;/strong&gt; The correct answer is &lt;strong&gt;Answer 3&lt;/strong&gt;.&lt;/p&gt;        
  &lt;/div&gt;        
  
  &lt;div id="category4"&gt;            
  &lt;p&gt;               
  &lt;strong&gt;Question 4:&lt;/strong&gt; The correct answer is &lt;strong&gt;Answer 4&lt;/strong&gt;.&lt;/p&gt;        
  &lt;/div&gt;        
  
  &lt;div id="category5"&gt;            
  &lt;p&gt;                
  &lt;strong&gt;Question 5:&lt;/strong&gt; The correct answer is &lt;strong&gt;Answer 1&lt;/strong&gt;.&lt;/p&gt;        
  &lt;/div&gt;        
  
  &lt;div id="category6"&gt;            
  &lt;p&gt;                
  &lt;strong&gt;Question 6:&lt;/strong&gt; The correct answer is &lt;strong&gt;Answer 2&lt;/strong&gt;.&lt;/p&gt;        
  &lt;/div&gt;        
  
  &lt;div id="category7"&gt;            
  &lt;p&gt;                
  &lt;strong&gt;Question 7:&lt;/strong&gt; The correct answer is &lt;strong&gt;Answer 3&lt;/strong&gt;.&lt;/p&gt;        
  &lt;/div&gt;        
  
  &lt;div id="category8"&gt;            
  &lt;p&gt;               
  &lt;strong&gt;Question 8:&lt;/strong&gt; The correct answer is &lt;strong&gt;Answer 4&lt;/strong&gt;.&lt;/p&gt;        
  &lt;/div&gt;        
  
  &lt;div id="category9"&gt;           
  &lt;p&gt;               
  &lt;strong&gt;Question 9:&lt;/strong&gt; The correct answer is &lt;strong&gt;Answer 1&lt;/strong&gt;.&lt;/p&gt;        
  &lt;/div&gt;        
  
  &lt;div id="category10"&gt;            
  &lt;p&gt;                
  &lt;strong&gt;Question 10:&lt;/strong&gt; The correct answer is &lt;strong&gt;Answer 2&lt;/strong&gt;.&lt;/p&gt;        
  &lt;/div&gt;        
  
  &lt;div id="category11"&gt;            
  &lt;p&gt;                
  You answered them all right!&lt;/p&gt;        
  &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>I faced a big problem on my iOS Coding. My requirement is I have a <strong>HTML</strong> file. That file contain some multiple choice Q&amp;A. I Load that html file in a <code>UIWebView</code>. And when I click that answer button I have to retrieve which button is clicked on webView. </p>
  
  <p>I have to retrieve that method from html file.</p>
  
  <p>How is it possible?</p>
  
  <p>Its not a duplicate Q, I already googled on this and whatever i got, its not satisfying my requirement. </p>
  
  <p>My code is like below : </p>
  
  <pre><code>- (void)viewDidLoad {
      [super viewDidLoad];
  
      NSString *path;
      NSBundle *thisBundle = [NSBundle mainBundle];
      path = [thisBundle pathForResource:@"Untitled" ofType:@"html"];
      NSURL *instructionsURL = [NSURL fileURLWithPath:path];
     [_webview loadRequest:[NSURLRequest requestWithURL:instructionsURL]];
    }
    - (void)webViewDidFinishLoad:(UIWebView*)theWebView
    {
      NSString* returnValue =[theWebView stringByEvaluatingJavaScriptFromString:@"myFunction()"];
      NSLog(@"returnValue = %@ ",returnValue);
    }
    - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
    {
       NSLog(@" didFailLoadWithError");
    }
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/kbIuB.png" alt="enter image description here"></p>
  
  <p>But my output is : <strong>Sample_JS[2218:44719] returnValue =</strong></p>
  
  <p>Please help me out ....</p>