我制作任何 webView 并且我想从 javascript 调用任何返回任何参数的目标 c 方法。我尝试了很多方法但不允许。
Objective C 方法在这里:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *URL = [request URL];
if ([[URL scheme] isEqualToString:@"donordialog"])
{
// now we need to figure out the function part
NSString *functionString = [URL resourceSpecifier];
if ([functionString hasPrefix:@"bloodTypeChanged"])
{
// the blood type has changed, now do something about it.
NSString *parameter = [functionString stringByReplacingOccurrencesOfString:@"bloodTypeChanged" withString:@""];
// remove the '(' and then the ')'
parameter = [parameter stringByReplacingOccurrencesOfString:@"(" withString:@""];
parameter = [parameter stringByReplacingOccurrencesOfString:@")" withString:@""];
// log the paramter, as I don't know what to do with it right now
UIAlertView *alert=[[ UIAlertView alloc] initWithTitle:@"iosdan javascripti"
message:@"ddddddddd"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
//NSLog(@"%@", parameter);
}
return NO;
}
return YES;
}
javascript:
function myFunction() {
url="http://example.com";
window.location="donordialog:blooTypeChanged("+url+")";
}
html:
<button onclick="myFunction()">click me</button>
评论:我需要:例如,它是 obj c 方法。啊啊{}。我的 aaa 方法必须返回任何参数。我有 wevView。我将任何网址加载到此 webView:例如 www.example.com/forios 。我需要从 javascript 调用这个 (aaa) 方法,它位于 www.example.com/forios 上的 html 中,并提醒 aaa 函数的结果。理解?。如果明白,就不要看我的代码。无论如何,请帮助自己取悦自己。
我的问题的android版本:通过 Android WebView 从 JavaScript 调用 Java 函数我想提醒一些从方法返回的参数。请帮助。谢谢。
这是一个快速简单的解决方案withoutJavaScriptCore 向后兼容(可能会考虑升级到 Javascriptcore )。这是一个简单的实现。
注意:此解决方案仅适用于 UIwebview(不适用于 UIscrollview)
//视图控制器.h
@interface ViewController : UIViewController<UIWebViewDelegate>
@property (strong, nonatomic) IBOutlet UIWebView *viewWeb;
@end
//视图控制器.m
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *fullURL = @"http:player.fusion.fm";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_viewWeb loadRequest:requestObj];
_viewWeb.delegate = self;
}
-(BOOL)webView:(UIWebView *)_viewWeb shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSURL *url = request.URL;
if ([[url scheme] isEqualToString:@"ios"])
{
[self mute]; // <-- YOUR OBJ-C FUNCTION HERE
NSLog(@"test");
return YES;
}else
{
return YES;
}
}
// Javascript(在 webview 中)
try
{
window.location="ios://null"
}
catch(err)
{
}
Source: http://adoptioncurve.net/archives/2012/09/calling-objective-c-methods-from-javascript-in-a-uiwebview/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)