Advertise here




Advertise here

Howdy, Stranger!

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

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

JavaScript interaction from UIWebView with app

chuckchuck Posts: 87Registered Users
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
German Course: Learn German anywhere! Chess Player: Study Chess anywhere!

Replies

  • ghayengaghayenga Posts: 4New Users
    chuck;66254 said:
    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
    ghayenga;66310 said:
    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?
    German Course: Learn German anywhere! Chess Player: Study Chess anywhere!
  • chuckchuck Posts: 87Registered Users
    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.
    German Course: Learn German anywhere! Chess Player: Study Chess anywhere!
Sign In or Register to comment.