本文介绍了从javascript调用objective-c方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作任何webView我想从javascript调用任何返回任何参数的Objective c方法。我尝试了很多方法,但不允许。
目标c方法:

I make any webView And I want to call from javascript any objective c method which return any parameter. I tried many ways but not allowed .Objective c method here:

- (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:

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版本:
我想提醒一些从方法返回的参数。请帮助。谢谢。

comment: I need that: it is obj c method for example. aaa{} . My aaa method must return any parameter. and I have wevView. I loaded any url to this webView: for example www.example.com/forios . I need call this (aaa) method from javascript which it is in html on www.example.com/forios and alert the result of aaa function. understand?. If understand, dont watch to my code. help please yourself anyway.android version of my question:Call Java function from JavaScript over Android WebViewI want to alert some parameter returning from method. Help please. Thanks.

推荐答案

我用来与obj-c中的javascript进行交互的最常用方法是更改​​哈希。在js中的必需事件中写入
window.location.hash ='#cmd_alertMessage';
之后你的 - ( BOOL)webView:shouldStartLoadWithRequest:navigationType:将被调用:

The most usual approach that I used to interact from javascript within obj-c is to changing hash. On required event in your js writewindow.location.hash = '#cmd_alertMessage';after this your - (BOOL)webView: shouldStartLoadWithRequest: navigationType: will be called:

   - (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";
}

因此,它将分三步进行:

So, it will work in 3 steps:


  1. 调用哈希值更改为从
    js中的obj-c代码请求参数;

  2. 处理散列更改(参数请求)并返回到obj-c中的
    js ;

  3. 再次处理在js中收到的参数

  1. Invoke hash changing to request parameter from obj-c code in js;
  2. Handle hash changed (parameter request) and return in back to js in obj-c;
  3. Handle recieved parameter in js again

这篇关于从javascript调用objective-c方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 07:37