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)

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 07-02-2009, 04:54 PM   #1 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 9
Default shouldStartLoadWithRequest not being fired

I have a view controller with a UIWebView controller. I'm trying to get the javascript inside the html content of the web view to pass some information to my objective c code.

I came across a number of examples online that set window.location in a javascript function and then catch the event generated by setting the view controller to be the web view's delegate and catching it in the shouldStartLoadWithRequest function. Unfortunately I can't get it to work because for me shouldStartLoadWithRequest is never called even though I'm setting the web view's delegate.

My code is as follows:

The interface:

Code:
@interface StoryTextViewController : UIViewController <UIWebViewDelegate>
    {
    	IBOutlet UIWebView *storyWebView;	
    	NSString *information;
    }
    
    @property (nonatomic, retain) IBOutlet UIWebView *storyWebView;
    @property (nonatomic, retain) NSString *information;
    
    @end
In the interface, the *information variable is set by the previous view that calls StoryTextViewController.

The implementation:

Code:
#import "StoryTextViewController.h"
    
    @implementation StoryTextViewController
    
    @synthesize storyWebView;
    @synthesize information;
    
    - (NSString *) contructHTMLText
    {
    	NSString *HTMLText = @"";
    	
    	NSArray *words = [information componentsSeparatedByString:@" "];
    	NSString *header =
    	@"<html>\n"
        "<head>\n"
    	"<script type=\"text/javascript\">\n"
    	
    	"function marktext(theSpanID)\n"
    	"{\n"
    	"	var theSpan=document.getElementById(theSpanID);\n"
    	"	if (theSpan.className == \"noHilite\")\n"
    	"	{\n"
    	"		theSpan.className = \"hilite\";\n"
    	"		window.location = \"true\";\n"
    	"	}\n"
    	"	else\n"
    	"	{\n"
    	"		theSpan.className = \"noHilite\";\n"
    	"		window.location = \"false\";\n"
    	"	}\n"
    	"}\n"
    	
    	"</script>\n"
    	
    	"<style type=\"text/css\">\n"
    	
    	".hilite\n"
    	"{\n"
    	"	background-color:red;\n"
    	"	color:white;\n"
    	"	font: bold 60px arial,sans-serif\n"
    	"}\n"
    	
    	".noHilite\n"
    	"{\n"
    	"	background-color:white;\n"
    	"	color:black;\n"
    	"	font: 60px arial,sans-serif\n"
    	"}\n"
    	
    	"</style>\n"
    	
        "</head>\n"
        "<body>\n"
    	"<div class=\"storyText\">\n";
    	
    	NSString *tailer = 
    	@"</div>\n"
        "</body>\n"
    	"</html>\n";
    	
    	HTMLText = [HTMLText stringByAppendingString:header];
    	for (NSInteger i = 0; i < [words count]; i ++)
    	{
    		NSString *tag = [NSString stringWithFormat:@"	<span id=\"mytext%d\" class=\"noHilite\" onclick=\"marktext(\'mytext%d\')\">%@</span>&nbsp;\n", i, i, (NSString *)[words objectAtIndex:i]];
    		HTMLText = [HTMLText stringByAppendingString:tag];
    	}
    	HTMLText = [HTMLText stringByAppendingString:tailer];
    	NSLog(HTMLText);
    	return HTMLText;
    }
    
    // The designated initializer. Override to perform setup that is required before the view is loaded.
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    	if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
    		storyWebView.delegate = self;
    	}
    	return self;
    }
    
    // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    	NSString *HTMLData = [self contructHTMLText];
    	[storyWebView loadHTMLString:HTMLData baseURL:[NSURL URLWithString: @""]];	
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
        // Release anything that's not essential, such as cached data
    }
    
    - (void)dealloc {
        [super dealloc];
    }
    
    - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 
    {	
    	// handle event here	
    	return true;
    }
    
    @end
Whats happening is that basically, the constructHTML function takes the text inside the *information variable and wraps every word in the text with some html and javascript such that whenever a word is clicked, a css high-lighting is applied on it. What I want to do is to count the number of high-lighted words. I do this by trying to pass something in the function thats called whenever a word is clicked but like i said, the shouldStartLoadWithRequest method thats supposed to be fired is never executed.

I've seen a lot of people do this sort of thing but I cant seem to figure out why its not running for me

I also tried putting storyWebView.delegate = self; inside a lot of other startup methods but nothing seems to work
superfahd is offline   Reply With Quote
Old 04-04-2011, 08:24 AM   #2 (permalink)
Registered Member
 
Join Date: Apr 2011
Posts: 1
Default

Did you find the answer to this problem, I got the same problem...

Thanks!
tiois is offline   Reply With Quote
Old 04-04-2011, 02:06 PM   #3 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 4,999
Default

Check the delegate in IB, it should be connected to the File's Owner.
baja_yu is offline   Reply With Quote
Reply

Bookmarks

Tags
iphone, javascript, objective-c, 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: 272
21 members and 251 guests
ADY, AragornSG, Bertrand21, Dani77, Dattee, fkmtc, HDshot, iDifferent, JasonR, jimbo, macquitzon216, mer10, prchn4christ, Rudy, sacha1996, silverwiz, sneaky, spiderguy84, Sunny46, theone8one
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,885
Threads: 89,230
Posts: 380,767
Top Poster: BrianSlick (7,129)
Welcome to our newest member, bookesp
Powered by vBadvanced CMPS v3.1.0

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