【问题标题】:How to perform a java script in a UIWebview in iOS App with iOS 8?如何在 iOS 8 的 iOS App 中的 UIWebview 中执行 java 脚本?
【发布时间】:2015-03-24 08:08:26
【问题描述】:

我从 NSUrlRequest 得到一个 java 脚本作为响应。我想在 UIWebView 中显示它,但是如何让这个 java 脚本开始工作呢?提前致谢。

请注意,这里我想在我的 java 脚本中显示一些按钮,用户可以点击该按钮来执行一些操作。以下是我的一些代码:

// get strContent as java script (respond from NSURLRequest)

// _myWebView is an instance of UIWebView

- (void) showWebContent:(NSString*)strContent {
  [self loadWebViewContent:strContent];
}

- (void) loadWebViewContent:(NSString*)strContent {

NSMutableString *htmlString = [[NSMutableString alloc] init];
[htmlString appendFormat:@"<html> <body>"];
[htmlString appendFormat:@"%@",strContent];
[htmlString appendFormat:@"</body> </html>"];
[_webView loadHTMLString:htmlString baseURL:nil];
}

【问题讨论】:

    标签: javascript ios objective-c uiwebview


    【解决方案1】:

    UIWebView的以下方法

    - (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script;
    

    【讨论】:

      【解决方案2】:

      你可以这样加载网页:

      NSString* htmlString= @"<html><head><script> function func(){alert('Button Tapped');}</script></head><br><br><body align='center' bgcolor='green'><input type='button' onClick='func()' value='TAP ME'/></body></html>";
      
      [self.webview loadHTMLString:htmlString baseURL:nil];
      

      然后你可以像这样调用任何javascript函数:

      [self.webview stringByEvaluatingJavaScriptFromString:@"func()"];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-28
        • 2014-09-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多