【问题标题】:iOS UIWebView Javascript - insert data -receive callbacks?iOS UIWebView Javascript - 插入数据 - 接收回调?
【发布时间】:2011-10-07 00:18:57
【问题描述】:

我有一个网页。我有一个 javascript 文件,它可以做很多事情。在应用程序中,我有一个 NSString ,其中包含一些需要在 javascript 文件中处理的关键数据。如何将我的字符串值放入我的 html 中的某个位置,以便我的 javascript 可以读取它?

此外,当执行操作(如按下按钮)时,javascript 会产生某些数据,我如何从 webview/javascript 中检索数据以便将其存储在应用程序中?

【问题讨论】:

    标签: javascript iphone ios


    【解决方案1】:

    你可以调用一个javascript方法并使用this传递值

    NSString *selectedValue = [webViewInstance stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"getAndReturnRadioValue(%d)", questionCounterIntValue]];
    

    但要从 javascript 中触发,您需要使用解决方法,

    实现以下 webViewDelegate 方法

    - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    
        NSLog(@"passed data from web view : %@",[[request URL] query]);
    
        if([[[request URL] query] isEqualToString:@"clickedOnLineNo"])
        {
            //Do your action here    
        }
    }
    

    你必须通过你的 javascript 方法导航到一个虚假的 URL,例如,

    window.location = "someLink://yourApp/form_Submitted:param1:param2:param3";
    

    点击按钮或您需要的操作。

    你可以看看我对这个问题的回答 Submit a form in UIWebView

    【讨论】:

    • 您能否进一步详细说明如何从 javascript 获取数据?例如,如果您单击 html 中的按钮,它将触发 javascript 函数。该函数具有我需要的返回值。我如何获得该返回值?
    • 正如我在答案中提到的,从您的 JS 函数导航到 URL - 它会触发委托方法。
    【解决方案2】:
    - (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script
    

    听起来正是您需要的方法。例如,如果需要与 JavaScript 交互的输入和输出可以通过单个方法完成,则可以将值传递给 JavaScript 并使用以下代码获取结果:

    NSString* jsCode = [NSString stringWithFormat:@"doSomething('%@')", dataToPass];
    NSString* resultFromJavaScript = [webView stringByEvaluatingJavaScriptFromString:jsCode];
    

    【讨论】:

    • 我错过了 ' ' 周围的 %@... Uff,1 小时后!谢谢
    【解决方案3】:

    您好,我们可以将目标 c 的数据发送到 javascript,并且可以轻松地从 javascript 回调

    Objective-C --> javaScript

    在 Objective-C 类中

    NSData *locationCountData = [NSJSONSerialization dataWithJSONObject:locationCount   options:NSJSONWritingPrettyPrinted error:nil];
    NSString *locationCountString = [[NSString alloc] initWithData:locationCountData encoding:NSUTF8StringEncoding];
    
    NSData *locationCountData = [NSJSONSerialization dataWithJSONObject:locationCount options:NSJSONWritingPrettyPrinted error:nil];
    NSString *locationCountString = [[NSString alloc] initWithData:locationCountData encoding:NSUTF8StringEncoding];
    
    NSString *str = [NSString   stringWithFormat:@"testFunction(%@,%@)",locationCountString, locationsListString];
    [self.myWebView stringByEvaluatingJavaScriptFromString: str];
    

    在 JavaScript 文件中

    <script type="text/javascript">
    function testFunction(locationCount,locationList)
    {
        alert(locationCount + locationList);
    }
    </Script>
    

    【讨论】:

      猜你喜欢
      • 2020-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-24
      • 1970-01-01
      • 1970-01-01
      • 2022-11-18
      相关资源
      最近更新 更多