在JavaScript的幫助下,您可以獲得屬性「webview」,然後可以將該屬性及其值發送到本機Objective C代碼。
這段JavaScript代碼添加到您的HTML頁面內腳本標籤:
function reportBackToObjectiveC(string)
{
var iframe = document.createElement("iframe");
iframe.setAttribute("src", "callback://" + string);
document.documentElement.appendChild(iframe);
iframe.parentNode.removeChild(iframe);
iframe = null;
}
var links = document.getElementsByTagName("a");
for (var i=0; i<links.length; i++) {
links[i].addEventListener("click", function() {
var attributeValue=links[i].webview; //this will give you your attribute(webview) value.
reportBackToObjectiveC(attributeValue);
}, true);
}
這是你的webViewDelegate方法後,會調用:
- (BOOL)webView:(UIWebView *)wView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
{
if (navigationType == UIWebViewNavigationTypeLinkClicked)
{
NSURL *URL = [request URL];
if ([[URL scheme] isEqualToString:@"callback"])
{
//You can get here your attribute's value.
}
}
檢查這個** HTTP: //stackoverflow.com/questions/5775679/how-can-i-get-name-from-link** –