Substituting Javascript function with Objective C in WebView

Discussion in 'iOS Programming' started by Affenbrotbaum, Jun 15, 2010.

  1. Affenbrotbaum macrumors newbie

    Joined:
    Jun 15, 2010
    #1
    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
     
  2. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #2
    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.
     
  3. Affenbrotbaum thread starter macrumors newbie

    Joined:
    Jun 15, 2010
    #3
    Sweet. Thanks for the answer,I"ll try it out.
     
  4. Danneman101 macrumors 6502

    Joined:
    Aug 14, 2008
    #4
    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.
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    You sure self.webView is not nil in awakeFromNib?
     
  6. Danneman101 macrumors 6502

    Joined:
    Aug 14, 2008
    #6
    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?
     
  7. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #7
    webView is not webView.delegate. Inconsistency hilited in red.
     
  8. Danneman101 macrumors 6502

    Joined:
    Aug 14, 2008
    #8
    Sorry, the code is actually:
    Code:
    NSLog(@"self.webView.delegate = %@", self.webView.delegate);
    
    ...and still gives (null).
     
  9. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #9
    Maybe because you aren't calling
    Code:
    [super awakeFromNib];
    ? Just speculating, though. Anyways, I would probably set the delegate in viewDidLoad.
     
  10. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #10
    Which doesn't answer the question: what is the value of webView?
     
  11. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #11
    awakeFromNib isn't guaranteed to be called in any particular order. Use viewDidLoad.

    Can't you set the webview's delegate in IB?
     
  12. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #12
    Even better! :eek:
     
  13. Danneman101 macrumors 6502

    Joined:
    Aug 14, 2008
    #13
    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 :)
     

Share This Page