0

Forgive me for being rather new (that would be very new) to iOS development.

I have just implemented native Facebook authorization, and now I want to pass my access_token to a local html file in a UIWebView. For that sake, I have created a Javascript object in the html file, with an init function that receives the access_token as parameter. Now I just need to externally call that function. Some research got me to [UIWebView stringByEvaluatingJavaScriptFromString:(NSString *)] - but this should not be called until the page has been loaded.

In my ViewController.m I have defined a - (void)updateView in which I do a request on webView which is a UIWebView. I would now like to execute a JavaScript string as soon as this particular requests completes. How do I do that?

- (void)updateView {
    AppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
    if (appDelegate.session.isOpen) {
        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://mydomainwuhuu/mobile?o=Auth&accessToken=%@", appDelegate.session.accessToken]];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        [webView loadRequest:request];
        [webView stringByEvaluatingJavaScriptFromString:@"window.alert('test')"];
    }
}

This code doesn't do it, and I think it's because I call the JavaScript before the page is finished loading? How do I fix?

casparjespersen
  • 3,460
  • 5
  • 38
  • 63

2 Answers2

1

UIWebView has delegate methods see this.For your situation make use of – webViewDidFinishLoad: .

kaar3k
  • 994
  • 7
  • 15
  • How? Right now I have something like: `- (void)webViewDidFinishLoad:(UIWebView *)thisWebView {[thisWebView stringByEvaluatingJavaScriptFromString:@"wuff.init()"]; }` But it does not execute the function.. – casparjespersen Feb 19 '13 at 19:29
  • add a alert inside that function.Can you paste the JS function code – kaar3k Feb 19 '13 at 19:37
  • I included a window.alert("test") inside the wuff.init() function. The problem is not with the JavaScript. If I open the html file in my web browser and execute wuff.init() the popup is shown.. – casparjespersen Feb 19 '13 at 19:39
  • See [this](http://stackoverflow.com/questions/807878/javascript-that-executes-after-page-load) .Try to achieve using JS. – kaar3k Feb 19 '13 at 19:47
  • That is not an option because I need the variable delivered from AppDelegate. See the code I posted on Pastebin in previous comment :) – casparjespersen Feb 19 '13 at 19:50
  • Try passing a plain text inside `wuff.init()`.Example `wuff.init('test')` .If its working fine then you need to find solution for how to pass a escaped string. – kaar3k Feb 19 '13 at 19:56
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/24783/discussion-between-caspar-aleksander-bang-jespers-and-kaar3k) – casparjespersen Feb 19 '13 at 20:03
0

I've had the same issue before. I only ended up including a "Done" button and including the stringbyevaluatingjavascript function inside the IBAction button call.

It worked then.

shyamsundar1988
  • 529
  • 4
  • 14