Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

AppFusion - 6 in 1!
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 03-28-2009, 07:03 AM   #1 (permalink)
Registered Member
 
chuck's Avatar
 
Join Date: Aug 2008
Location: Berlin, Germany
Posts: 87
Default JavaScript interaction from UIWebView with app

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!
__________________
German Course: Learn German anywhere! Chess Player: Study Chess anywhere!
chuck is offline   Reply With Quote
Old 03-28-2009, 12:45 PM   #2 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 4
Default

Quote:
Originally Posted by chuck View Post
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)webViewUIWebView*)p_webView shouldStartLoadWithRequestNSURLRequest*)p_reques t
navigationTypeUIWebViewNavigationType)p_navigati onType
ghayenga is offline   Reply With Quote
Old 04-23-2009, 05:48 AM   #3 (permalink)
Registered Member
 
chuck's Avatar
 
Join Date: Aug 2008
Location: Berlin, Germany
Posts: 87
Default

Quote:
Originally Posted by ghayenga View Post
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)webViewUIWebView*)p_webView shouldStartLoadWithRequestNSURLRequest*)p_reques t
navigationTypeUIWebViewNavigationType)p_navigati onType
Hi, sorry, got sidetracked by another project for a month and now I'm back to this one. Here is my JavaScript code:

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!
chuck is offline   Reply With Quote
Old 04-24-2009, 05:51 AM   #4 (permalink)
Registered Member
 
chuck's Avatar
 
Join Date: Aug 2008
Location: Berlin, Germany
Posts: 87
Default

I finally figured it out! Here's the relevant JavaScript code:

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:
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!

Last edited by chuck; 04-24-2009 at 07:33 AM. Reason: blog link to tutorial
chuck is offline   Reply With Quote
Reply

Bookmarks

Tags
javascript, uiwebview

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 624
21 members and 603 guests
ADY, bandley, BrianSlick, Creativ, dacapo, Dattee, djbrooks111, gbenna, HDshot, IphoneSdk, iseff, jakerocheleau, jbro, joeallenpro, kampftrinker, linkmx, mer10, nimesh_158, Reyna, yurikus
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 160,619
Threads: 89,833
Posts: 383,490
Top Poster: BrianSlick (7,244)
Welcome to our newest member, yurikus
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 04:44 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0