开发者

Can a UIWebView raise events to the containing view controller?

开发者 https://www.devze.com 2023-01-13 18:05 出处:网络
I have a UIWebController embedded in a UI开发者_如何学PythonViewController. I write the HTML code for the UIViewcontroller so I have complete control over the HTML page.

I have a UIWebController embedded in a UI开发者_如何学PythonViewController.

I write the HTML code for the UIViewcontroller so I have complete control over the HTML page.

When the user clicks a button in the HTML page - can the containing UIViewController be notified about it?

Can the UIWebView send/raise an event to the UIViewControler?


You need to implement the webView:shouldStartLoadRequest:navigationType: delegate method for your web view (Apple Docs).

Here's a quick example:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    NSURL *url = [request URL];
    if ([[url scheme] isEqualToString:@"someLinkURL"]) {
        
        // Display alert
        UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@"Hello" message:@"I'm a message!" delegate:self cancelButtonTitle:@"Okay!" otherButtonTitles:nil];
        [myAlert show];
        [myAlert release];
        return NO;
    }
    return YES;
}

Your VC must be the webViewDelegate for this method to work.

If you want to handle all clicks manually (i.e. don't go to other pages in the web view) return NO on that final line.

0

精彩评论

暂无评论...
验证码 换一张
取 消