How to run javascript functions from UIWebView

Discussion in 'iOS Programming' started by tdog09, Aug 13, 2009.

  1. tdog09 macrumors member

    Joined:
    Jul 13, 2008
    #1
    Hi All-

    So I've been bashing my head in with this the past few days. What I have is a UIWebView that has loaded a webpage, say www.mypage.com.

    The html of mypage.com contains javascript that computes certain things for the page to display.

    How would I be able to call the javascript functions of this page from objective c, and return those values?

    Say the webpage has a function called foo(), that when called spits out the phrase,"foo was called". I would like from object C to be able to call this function, and print what it returns in NSLOG. Is this possible?

    Any help is greatly appreciated. Thanks!
     
  2. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #2
    Read the UIWebview documentation and look up the stringByEvaluatingJavaScriptFromString method.
     
  3. tdog09 thread starter macrumors member

    Joined:
    Jul 13, 2008
    #3

    Thanks but i am working with this, and threw my million tries have yet to get it to work. I do read documentation, and have read tons, but still to no avail.

    Thanks!
     
  4. Guiyon macrumors 6502a

    Joined:
    Mar 19, 2008
    Location:
    North Shore, MA
    #4
    What exactly are you having problems with? Do you have a code snippet that isn't working?
     
  5. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #5
    Have you googled for working examples on the web?
     
  6. ghayenga macrumors regular

    Joined:
    Jun 18, 2008
    #6
    NSLog(@"js result -", [webView stringByEvaluatingJavaScriptFromString:mad:"foo();"]);

    works for me.
     
  7. anmldr macrumors member

    Joined:
    Jul 14, 2008
    #7
    Also try something simple first...make sure that JavaScript is turned on in the device's preferences.

    Linda
     
  8. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #8
    Are you sure you've loaded the entire web page into the UIWebView?

    Some devs have forgotten to put all the parts into the app bundle the correct way.
     

Share This Page