Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

JavaScript interaction from UIWebView with app

chuckchuck Posts: 87Registered Users
edited April 2009 in iOS SDK Development
I have an app which has a UIWebView which is a quiz with dropdown boxes. At the bottom there is a button within the HTML which calls a JavaScript function to show which questions were answered correctly. At this point, I need to save the user's score. If all the questions are correct, I'd like to mark that that section is finished. Is there any way I could trigger an action from the UIWebView to the view controller when a user has answered all questions correctly? Any help would be greatly appreciated. Thanks!
Post edited by chuck on
<a href="http://www.itunes.com/app/GermanCourse"; target="_blank">German Course</a>: Learn German anywhere! <a href="http://www.itunes.com/app/ChessPlayer"; target="_blank">Chess Player</a>: Study Chess anywhere!

Replies

  • ghayengaghayenga Posts: 4New Users
    edited March 2009
    chuck wrote: »
    I have an app which has a UIWebView which is a quiz with dropdown boxes. At the bottom there is a button within the HTML which calls a JavaScript function to show which questions were answered correctly. At this point, I need to save the user's score. If all the questions are correct, I'd like to mark that that section is finished. Is there any way I could trigger an action from the UIWebView to the view controller when a user has answered all questions correctly? Any help would be greatly appreciated. Thanks!


    In your JavaScript function call:

    window.location = "MYDUMMYURL?";

    Then in your webView's delegate implement the shouldStartLoadWithRequest method and then do whatever you need to and return NO if it's calling MYDUMMYURL.

    - (BOOL)webView:(UIWebView*)p_webView shouldStartLoadWithRequest:(NSURLRequest*)p_request
    navigationType:(UIWebViewNavigationType)p_navigationType
  • chuckchuck Posts: 87Registered Users
    edited April 2009
    ghayenga wrote: »
    In your JavaScript function call:

    window.location = "MYDUMMYURL?";

    Then in your webView's delegate implement the shouldStartLoadWithRequest method and then do whatever you need to and return NO if it's calling MYDUMMYURL.

    - (BOOL)webView:(UIWebView*)p_webView shouldStartLoadWithRequest:(NSURLRequest*)p_request
    navigationType:(UIWebViewNavigationType)p_navigationType

    Hi, sorry, got sidetracked by another project for a month and now I'm back to this one. Here is my JavaScript code:
    function checkAnswers() {
    	// So far all answers are correct.
    	var allCorrect = true;
    	for ( var selectIndex = 0; selectIndex < document.theForm.elements.length; selectIndex++ ) {
    		
    		curSelect = document.theForm.elements[selectIndex];
    		if ( curSelect.name == "quiz" ) {
    			for ( var optionIndex = 0; optionIndex < curSelect.options.length; optionIndex++ ) {
    				if ( curSelect.options[optionIndex].selected ) {
    					// Display checkmark for correct answers and an red X for wrong answers.
    					var answerResult = curSelect.options[optionIndex].value;
    					document.images[selectIndex].src = answerResult + "25.png";
    					if ( answerResult == "wrong" ) {
    						allCorrect = false;
    					}
    				}
    			}
    		}
    	}
    }
    

    This function basically determines which questions were answered correctly and then places a checkmark or X next to them. When a user finally gets all the questions correct, then I need to mark the lesson as being finished. Unfortunately I don't see a way of getting a variable's contents from the JavaScript back to my Objective-C application. Is this even possible?
    <a href="http://www.itunes.com/app/GermanCourse"; target="_blank">German Course</a>: Learn German anywhere! <a href="http://www.itunes.com/app/ChessPlayer"; target="_blank">Chess Player</a>: Study Chess anywhere!
  • chuckchuck Posts: 87Registered Users
    edited April 2009
    I finally figured it out! Here's the relevant JavaScript code:
    function checkAnswers() {
      // Do stuff to see if all answers were correct
      ...
    	
      // Send all correct status back to Objective-C
      window.location = "/allCorrect/" + allCorrect;
    }
    

    Here's the relevant Objective-C code:
    - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
      if ( [request.mainDocumentURL.relativePath isEqualToString:@/allCorrect/false] ) {
        NSLog( @Nope, that is not right! );
        return false;
      }
    	
      if ( [request.mainDocumentURL.relativePath isEqualToString:@/allCorrect/true] ) {
        NSLog( @You got them all! );
        return false;
      }
    	
      return true;
    }
    

    I also just posted this as a tutorial on my blog: iPhone/Web 2.0 » Send a BOOL value from JavaScript to Objective-C.
    <a href="http://www.itunes.com/app/GermanCourse"; target="_blank">German Course</a>: Learn German anywhere! <a href="http://www.itunes.com/app/ChessPlayer"; target="_blank">Chess Player</a>: Study Chess anywhere!
Sign In or Register to comment.