Local function call from UIWebview
Hi i have an uiwebview, I have prepare the htmlstring format of data to load in webview.
I am using this line to load htmlstring in uiwebview.
[webView loadHTMLString:htmlstring baseURL:[NSURL URLWithString:[NSString stringWithFormat:@"file:/%@//",imagePath]]];
In the html formated string we have an one image button.When i click the image button i want to call one local function in ViewController.m file.
Is it possible ? Please help me .
I am using
<input type=""image"" onclick=""func()"" src=""icon_blo开发者_如何学编程g.png"" name=""image"" width=""30"" height=""30"">
<script language=""javascript""> function func(){alert(""hee"");}
But alert not shown in iphone . Any one help me ? Thanks in advance.......
There is no straightforward way to call objective-c code from within page loaded in UIWebView.
The only way is to intercept loading of dummy-pages in UIWebView's delegate (– webView:shouldStartLoadWithRequest:navigationType:
). There you can make decisions based on request.URL
.
- (BOOL)webView:(UIWebView*)_webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
NSArray *components = [[[request URL] absoluteString] componentsSeparatedByString:@":"];
if ([components count] > 1 && [[components objectAtIndex:0] isEqualToString:@"objc"]) {
if ([[components objectAtIndex:1] isEqualToString:@"someFunctionality"]) {
// Do something
}
return NO;
}
return YES;
}
You can trigger those requests by clicking a link, submitting a FORM, or from JavaScript by setting the window's location like this: window.location = "objc:someFunctionality";
.
精彩评论