0

Is it possible to listen for a notification posted by Javascript in a UIWebView? Or similar? I have thought about repeating a function in Objective-C that monitors a JS variable but surely there is a better way?

I want to listen for when a particular image loads in the UIWebView (which I can do with .load method in jQuery), when the image does load I would like to hide a native UIActivityIndicator. I don't want to use a gif in the web view!

i.e.

$('img.someImage').load(function(){
//fire something for obj-c to listen to!
});

1 Answer 1

2

The only way to pass event from your html page to the iPhone app, is by using url,

When you want to pass a special even, make your js change ur navigation to a new url, say for example, "MyCustomURl://DoSomeThing"

Now in your iPhone application, set the delegate of the webView to be self

And add the following function

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSString *absolute = [request.URL absoluteString];

    if ([absolute isEqualToString:@"MyCustomURl://DoSomeThing"]) {
        //Do what you want here
        return NO;//this will not load the url
    }

    return YES;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.