javascript将字符串传递给本机objective-c

javascript to pass string to native objective-c

本文关键字:本机 objective-c 字符串 javascript      更新时间:2023-09-26

我想将按钮单击的字符串从html传递到ios native objective-c。这是我的html和简单的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>

这是我在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更改为按钮。这是他的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://",我认为这是我对功能缺乏理解。

而且,我的javascripthtml没有什么问题。