我制作任何webView我想从javascript调用任何返回任何参数的Objective c方法。我尝试了很多方法,但不允许。 目标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 {}。我的aaa方法必须返回任何参数。我有wevView。我将任何网址加载到此webView:例如www.example.com/forios。我需要从javascript调用这个(aaa)方法,它在www.example.com/forios上的html中,并提醒aaa函数的结果。了解?。如果明白,不要看我的代码。无论如何,请帮助自己。 我的问题的android版本:Call Java function from JavaScript over Android WebView 我想提醒一些从方法返回的参数。请帮忙。感谢。
答案 0 :(得分:3)
这是一个快速简便的解决方案没有 JavaScriptCore以实现向后兼容性(可能会考虑升级到Javascriptcore)。这是一个简单的实现。
注意:此解决方案仅适用于UIwebview(不是UIscrollview)
// viewcontroller.h
@interface ViewController : UIViewController<UIWebViewDelegate>
@property (strong, nonatomic) IBOutlet UIWebView *viewWeb;
@end
// viewcontroller.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)
{
}
答案 1 :(得分:2)
我用来与obj-c中的javascript进行交互的最常用方法是更改哈希。在你的js写的必需事件
window.location.hash = '#cmd_alertMessage';
在此之后,您的- (BOOL)webView: shouldStartLoadWithRequest: navigationType:
将被调用:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSArray * compenents = [request.URL.absoluteString componentsSeparatedByString:@"#"];
if (compenents.count > 1) {
NSString * cmd = compenents[1];
if ([cmd rangeOfString:@"cmd_alertMessage"].location != NSNotFound) {
// Call your obj-c method and get appropriated param
NSString * jsFunction = [NSString stringWithFormat:@"setParameterFromAAAMethod('%@')", [self aaa]];
// Return this param back to js
NSString * alertMessage = [webView stringByEvaluatingJavaScriptFromString:jsFunction];
}
}
return YES;
}
- (NSString *)aaa
{
return @"This is special parameter that you need";
}
因此,它将分三步进行:
答案 2 :(得分:1)
您可以将隐藏的iframe位置设置为特定于您应用的内容,然后在Obj-C中截取该请求。
答案 3 :(得分:0)
好吧,我不确定我的问题是否正确,但是如果你想调用任何javascript方法,那么你可以这样做: -
[yourWebView stringByEvaluatingJavaScriptFromString:@"yourMethodName()"];
< br />
您也可以通过上述方法传递参数。