How can I call an Objective-C method from JavaScript in a UIWebView, passing and alerting a parameter? I have tried various methods but none seem to work.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *URL = [request URL];
if ([[URL scheme] isEqualToString:@"donordialog"])
{
// Extract the function name from the URL
NSString *functionString = [URL resourceSpecifier];
if ([functionString hasPrefix:@"bloodTypeChanged"])
{
// Extract and log the parameter passed in the function call
NSString *parameter = [functionString stringByReplacingOccurrencesOfString:@"bloodTypeChanged" withString:@""];
parameter = [parameter stringByReplacingOccurrencesOfString:@"(" withString:@""];
parameter = [parameter stringByReplacingOccurrencesOfString:@")" withString:@""];
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"iosdan javascripti"
message:@"ddddddddd"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
return NO;
}
return YES;
}
JavaScript function:
function myFunction() {
url = "http://example.com";
window.location = "donordialog:blooTypeChanged(" + url + ")";
}
HTML button:
<button onclick="myFunction()">click me</button>
I am trying to achieve functionality similar to calling a Java method from JavaScript on an Android WebView. Any help would be appreciated. Thank you.