![](/img/trans.png)
[英]Pass Javascript string (NimbleKit) to Objective-C function?
[英]javascript to pass string to native objective-c
我想從html
單擊按鈕將字符串傳遞給ios native objective-c
。 這是我的帶有簡單javascript
html
:
<html>
<head>
<script language="javascript">
function callVrama(vramaID) {
window.location= "vramaWebShelves://vramaID/"+vramaID;
}
</script>
</head>
<body>
<button onclick="callVrama('asdfqwer1234')">Try it</button>
</body>
</html>
這是我在UIWebViewDelegate
函數:
- (void)viewDidLoad
{
NSURL *url = [[NSBundle mainBundle] URLForResource:@"content"
withExtension:@"html"];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[super viewDidLoad];
[_webShelves loadRequest:requestObj];
_webShelves.delegate = self;
}
/// some other codes
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType {
NSString *url = [[request URL] absoluteString];
static NSString *urlPrefix = @"vramaWebShelves://vramaID/";
if ([url hasPrefix:urlPrefix]) {
NSString *vramaID = [url substringFromIndex:[urlPrefix length]];
NSLog(@"VramasRequest: %@", vramaID);
return NO;
}
else {
return YES;
}
}
似乎有什么問題?
實際上,我通過此鏈接遵循了教程,一切正常。 我只是不確定如何將js function
調用從uiwebview
上的點擊uiwebview
為button。 這是他的javascript
:
<script language="javascript">
function resizeText(multiplier) {
if (document.body.style.fontSize == "") {
document.body.style.fontSize = "1.0em";
}
document.body.style.fontSize =
parseFloat(document.body.style.fontSize) +
(multiplier * 0.2) + "em";
}
function touchStart(event) {
sX = event.touches[0].clientX;
sY = event.touches[0].clientY;
}
function touchEnd(event) {
var parentNode = event.target.parentNode
if ( parentNode != null && parentNode.nodeName.toLowerCase() == "a" )
return;
if (event.changedTouches[0].clientX == sX &&
event.changedTouches[0].clientY == sY) {
window.location = "nativeAction://hideShow";
}
}
document.addEventListener("touchstart", touchStart, false);
document.addEventListener("touchend", touchEnd, false);
</script>
我認為這兩行存在問題:
[_webShelves loadRequest:requestObj];
_webShelves.delegate = self;
請嘗試在loadRequest
之前設置delegate
經過多次重讀和其他一些相關測試。 我想出了如何使其工作的方法。 在objective c
,替換此函數:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType {
if ([request.URL.scheme caseInsensitiveCompare:@"vramaWebShelves"] == NSOrderedSame) {
NSString *url = [[request URL] absoluteString];
static NSString *urlPrefix = @"vramaWebShelves://vramaID/";
NSString *vramaID = [url substringFromIndex:[urlPrefix length]];
NSLog(@"VramasRequest: %@",vramaID);
return NO;
}
return YES;
}
注意caseInsensitiveCompare:@"vramaWebShelves"
,而不是caseInsensitiveCompare:@"vramaWebShelves://"
,我認為這是我對該功能缺乏了解。
而且,我的javascript
和html
沒錯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.