[英]javascript to pass string to native objective-c
I want to pass string from a button click from html
to ios native objective-c
. 我想从
html
单击按钮将字符串传递给ios native objective-c
。 Here is my html
with simple javascript
: 这是我的带有简单
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>
And this is my function in UIWebViewDelegate
: 这是我在
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;
}
}
What seems to be a problem? 似乎有什么问题?
I actually follow the tutorial from this link and everything work. 实际上,我通过此链接遵循了教程,一切正常。 I just not sure how to change the
js function
call from tapping on the uiwebview
to button. 我只是不确定如何将
js function
调用从uiwebview
上的点击uiwebview
为button。 Here is the javascript
from him: 这是他的
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>
I think problem in those two lines: 我认为这两行存在问题:
[_webShelves loadRequest:requestObj];
_webShelves.delegate = self;
please try set delegate
before loadRequest
请尝试在
loadRequest
之前设置delegate
After reread many times and some other related test. 经过多次重读和其他一些相关测试。 I figure out how to get it to work.
我想出了如何使其工作的方法。 In
objective c
, replace this function: 在
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;
}
Mind the caseInsensitiveCompare:@"vramaWebShelves"
, which is not caseInsensitiveCompare:@"vramaWebShelves://"
, That I believed is my lack of understanding with the function. 注意
caseInsensitiveCompare:@"vramaWebShelves"
,而不是caseInsensitiveCompare:@"vramaWebShelves://"
,我认为这是我对该功能缺乏了解。
And, nothing wrong with my javascript
and html
. 而且,我的
javascript
和html
没错。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.