簡體   English   中英

如何在iOS 8的iOS App的UIWebview中執行Java腳本?

[英]How to perform a java script in a UIWebview in iOS App with iOS 8?

我得到一個Java腳本作為NSUrlRequest的響應。 我想在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];
}

查看UIWebView的以下方法

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

您可以通過以下方式加載網頁:

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()"];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM