簡體   English   中英

JavaScript將字符串傳遞給本機Objective-C

[英]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://" ,我認為這是我對該功能缺乏了解。

而且,我的javascripthtml沒錯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM