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

Affenbrotbaum

macrumors newbie
Original poster
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
 
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.
 
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.
 
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?
 
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.
 
..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.
 
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 😛

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