简体   繁体   中英

Listen for notifications in Objective-C from UIWebView Javascript

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!

ie

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

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM