Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Affenbrotbaum

macrumors newbie
Original poster
Jun 15, 2010
2
0
Hey guys,

I have a webview loading up a website on the ipad which has some javascript functions integrated. However, I need to override some of the javascript functions with objective-c functions for compatibility reasons. In other words, instead of calling the corresponding javascript function I need the uiwebview to intervene and call an objective c function and after that return the values to the website.

I've been stuck on this for a while now. Do you have any pointers??

Thanks for your help already
 

firewood

macrumors G3
Jul 29, 2003
8,108
1,345
Silicon Valley
You can tap into the webview's shouldStartLoadWithRequest: delegate, and check all the URL's for any stuff to pass to a C or ObjC function/method. Then you could pass any result back asynchronously using the webview's stringByEvaluatingJavaScriptFromString: method. This requires encapsulating the javascript call into a pseudo-URL, and string parsing in ObjC land to recover the call and it's parameters from that URL.
 

Danneman101

macrumors 6502
Aug 14, 2008
361
1
shouldStartLoadWithRequest not being called [iPad]

Im developing a Split-View iPad app.

In the DetailView there is a UIWebView that works properly, displaying my webpage (loaded locally) of choice.

Problem is the shouldStartLoadWithRequest, which should enable me to make javascript-to-objc-calls from within my webpages, is never called.

The only difference between the iPad-app and the iPhone-app (on which this code works perfectly) is that the webview has been setup and linked in the DetailView.xib-file in the iPad-app.

DetailViewController.h
Code:
#import <UIKit/UIKit.h>

@interface DetailViewController : UIViewController <UIPopoverControllerDelegate, UISplitViewControllerDelegate, UIWebViewDelegate, AVAudioPlayerDelegate> {
    
    UIPopoverController *popoverController;
    UIToolbar *toolbar;
    
    id detailItem;
    UILabel *detailDescriptionLabel;

    IBOutlet UIWebView *webView;
}
@property (nonatomic, retain) UIWebView *webView;
@end

DetailViewController.m
Code:
...

@synthesize webView;

...

- (void)awakeFromNib{	
	self.webView.delegate = self;
}

...

// IS NEVER CALLED
- (void)webViewDidFinishLoad:(UIWebView *)webView {
	....
}

....

// IS NEVER CALLED
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
	....
}

....

Can't really figure out what I'm doing differently in this setup as to not have the javascript-to-ObjC-functionality in the webpages.
 

Danneman101

macrumors 6502
Aug 14, 2008
361
1
You're right...

Code:
NSLog(@"self.webView.delegate = %@", self.webView.delegate);

..results in (null).

I don't see the reason behind this, though. I've added "UIWebViewDelegate" in the .h-file, made webView a property and synthesized it...it should in my experience be enough, right?
 

chown33

Moderator
Staff member
Aug 9, 2009
10,751
8,425
A sea of green
Code:
NSLog(@"[COLOR="Red"]self.webView.delegate[/COLOR] = %@", [COLOR="Red"]self.webView[/COLOR]);

..results in (null).

I don't see the reason behind this, though. I've added "UIWebViewDelegate" in the .h-file, made webView a property and synthesized it...it should in my experience be enough, right?
webView is not webView.delegate. Inconsistency hilited in red.
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
..results in (null).

I don't see the reason behind this, though. I've added "UIWebViewDelegate" in the .h-file, made webView a property and synthesized it...it should in my experience be enough, right?
Maybe because you aren't calling
Code:
[super awakeFromNib];
? Just speculating, though. Anyways, I would probably set the delegate in viewDidLoad.
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
awakeFromNib isn't guaranteed to be called in any particular order. Use viewDidLoad.

Can't you set the webview's delegate in IB?
 

Danneman101

macrumors 6502
Aug 14, 2008
361
1
You're correct (when are you not, almighty lords of objc? :) ) - it seems viewDidLoad was a better choice for setting the delegate.

And yeah, setting it up in IB would be much simpler - not used to using IB, thats all :p

Thanks a lot guys for the help - I appreciate it :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.