OS X - WebView questions - objective-c

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!

Related

How to check CGWindowID still valid

If I am getting a CGWindowID (_windowID) as follows...
NSArray *windowList = (__bridge NSArray *)CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly, kCGNullWindowID);
for (NSDictionary *info in windowList) {
if ([[info objectForKey:(NSString *)kCGWindowOwnerName] isEqualToString:#"window name"] && ![[info objectForKey:(NSString *)kCGWindowName] isEqualToString:#""]) {
_windowID = [[info objectForKey:(NSString *)kCGWindowNumber] unsignedIntValue];
}
}
How do I properly test the window id is still valid and the window has not been closed? Do i just run similar code just checking window id exists?
Thanks in advance
The documentation for the kCGWindowListOptionOnScreenOnly constant says:
List all windows that are currently onscreen. Windows are returned in
order from front to back. When retrieving a list with this option, the
relativeToWindow parameter should be set to kCGNullWindowID.
So the windows would certainly be on screen, since nothing appears to be happening between the call to CGWindowListCopyWindowInfo and your action on it.
Maybe you'd want to test to make sure they are not hidden or minimized?

Can't show the right URL in my iPad simulator

I'm building an iPad app and that's a browser. I have a little problem because in a text field, I can write any web direction, such http://www.google.com/. The problem is that when I change the page, at, for example, http://www.apple.com/ , it stills says google. I have written the code, but I don't know why it doesn't work!
-(void)currentURL{
int i= 0;
while (i == 0) {
url = [request URL];
NSLog(#"%#", url.absoluteString);
textfield.text = url.absoluteString;
}}
Please I need some help!
replace this:
textfield.text = url.absoluteString;
with this:
if(textfield)
{
if(url)
{
textfield.text = [url absoluteString];
} else {
NSLog( #"my request or URL is nil");
}
} else {
NSLog( #"I didn't set my textfield");
}
You need to implement the web view delegate which will tell you when the page inside the web view changes, e.g. when the user clicks a link. You can then use the delegate methods to update your text field to the new URL.
if you've done that and your textfield isn't updating, you've probably forgotten to connect your textfield in your nib file to the textfield outlet of your class, so your self.textfield property is nil and setting its text does nothing.

Get Current URL (UIWebView)

Simply enough, I need to know how to obtain the current URL that the UIWebView is in. That way, I would be able to add such features as bookmarking, sharing, etc.
The crux of the question is, what can I do to obtain the current URL of the webvew?
EDIT: Solution:
NSString *currentURL = myWebView.request.URL.absoluteString;
NSString *currentURL = myWebView.request.URL.absoluteString;
[[[web request] URL] absoluteString]
Will give you the current url as a String.
In Swift if you need to get your URL you need to write this block of code:
func webViewDidFinishLoad(webView: UIWebView) {
let currentURL = webView.request?.mainDocumentURL
print(currentURL)
}

Restrict UIWebview to certain pages

I am trying to create a simple ipad app with a UIWebview that displays a form that a customer can fill in.. what i want to do is restrict the app so that it only allows the user to navigate to certain addresses.. (i.e. either something that allows the user to go to a specific address.. OR something that checks for specific keywords and allows/blocks them as appropriate..)
Could someone please show me how its done..
NB: its basically a googledocs form and i dont want to let the user navigate away from it.. (the user could easily click away and go elsewhere)
Thank you for reading :)
In the class that is your UIWebViewDelegate you can use something like this:
-(BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = request.URL;
NSString *urlString = url.absoluteString;
//Check for your own url. You can use more advanced checking techniques of course :)
NSRange range = [urlString rangeOfString:#"http://www.yourUrl.com"];
if (range.location != NSNotFound)
return YES;
else
return NO;
}
You can use the delegate method
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
to determine whether the UIWebView can load a given web page. Although that would mean knowing exactly which pages are allowed (if there are many this might not be convenient).
Use UIWebViewDelegate method webView:shouldStartLoadWithRequest:navigationType:
Then check what is the URL that the UIWebView tires to load, and act consequently.
UIWebViewDelegate Reference

cocoa webview links disabled

On OSX, I am using XCode to make a desktop app with a webview in it. The webview loads ok, and I can dynamically load content into it - but when I click on links inside the webview, they are not followed. They change color, but no new page is loaded. If I code my links with javascript like this - then they work.
link there
Is there an Objective-C one liner that allows links to be followed inside web-views?
Is there another issue I am not aware of here?
The links at the top of Gmail open in a new window. To make them work you have to implement at least the WebUIDelegate methods webView:createWebViewWithRequest: and webViewShow:. If you simply want to open all links in the same web view, you could return it from webView:createWebViewWithRequest: instead of creating a new one.
Turns out it was because I had code I copied form the web with some custom function to ignore WebPolicyDecisionListener...
Sorry for asking question without giving all the details - all this objective-c is new to me, I don't know which bits do what yet. I do some pointing and clicking, and then some coding - I don't know exactly how that all links up. With other languages, you have the whole program in one place - it takes a bit of a learning curve to get used to... but I digress.
I fixed by adding some comments - see code below...
- (void)webView:(WebView *)aWebView
decidePolicyForNavigationAction:(NSDictionary *)actionInformation
request:(NSURLRequest *)request
frame:(WebFrame *)frame
decisionListener:(id < WebPolicyDecisionListener >)listener
{
if ([self requestIsLinkClick:actionInformation]) {
if ([#"method" isEqual:[[request URL] scheme]]) {
SEL selector = NSSelectorFromString([[request URL] resourceSpecifier]);
if ([prototypeDelegate respondsToSelector:selector]) {
[prototypeDelegate performSelector:selector];
}
}
// [listener ignore];
} // else {
[listener use];
//}
}