I want to pass string from a button click from html
to ios native objective-c
. Here is my html
with simple javascript
:
<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
:
- (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. Here is the javascript
from him:
<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
After reread many times and some other related test. I figure out how to get it to work. In objective c
, replace this function:
- (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.
And, nothing wrong with my javascript
and html
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.