简体   繁体   中英

OS X - WebView questions

I am using webview in one of OS X app and got strange requirement from client that:

1) Need to show address bar in app for webview, I tried if there's any support by webview but couldn't really find. I know I can do that by having a text field and show URL in that but I am not sure if that's the right way so is there any other better way, please suggest.

2) Need to also check whether loaded URL has SSL support and show some icon like padlock (open/close). So again is there any support or feature of Webview that i can use or I just have to check for URL prefix of http or https? Please help.

Thanks in advance. MP

That is exactly what you should do. Create the text field, and insert this bit of code into your application:

- (void)webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame {
    NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:@"window.location"];
    [myTextField setStringValue:currentURL];
}

For the second part of your question, use the currentURL string, and check if https:// exists in the string using the NSNotFound method. By using [myString rangeOfString:@"string_to_search_for"].location != NSNotFound , it will return true if the rangeOfString: is found. ( != means not equal to.) (So != NSNotFound means that your rangeOfString is not equal to not being found... if that makes sense)

 - (void)webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame {

    NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:@"window.location"];
    if ([currentURL rangeOfString:@"https://"].location != NSNotFound) {
        // *https://* exists! Show your closed padlock image!
    } else {
        // *https://* does not exist. Show your open padlock image.
    }
}

Hope this was helpful!

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM