PDA

View Full Version : Creating a Cocoa Document Based Web Browser and Need Help Checking for "http://"




macman7002
Oct 22, 2010, 05:39 PM
Hello.

I've created a Cocoa document based web browser containing a web view and textfield and am trying to program it to check to see if the user entered "http://" in the URL textfield. If the user didn't enter http, add it to the address and load that page, otherwise just load the user's URL.

I have an action set up:

-(IBAction)connectURL:(id)sender {

Originally, it contained the following:


[urlString setStringValue:[sender stringValue]];
[[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[sender stringValue]]]];


With urlString being the File's Owner output.

My initial thought was to somehow check urlString to see if "http://" was located in there. Unfortunately, I'm not sure how to accomplish this. I read somewhere that I should use rangeOfString to do this, but I'm not understanding how I would implement that.

Going further, I thought that once I check to see if the input URL contains http I would use a conditional statement to either add "http://" to the URL and then load the revised URL, or load the URL located in the textfield if it already contains "http".

Could someone walk me through this a little bit, just to get me started on the right track and provide some examples I could follow along with?

Thank you very much!



jared_kipe
Oct 22, 2010, 07:19 PM
NSString instance method.
- (BOOL)hasPrefix: (NSString *) aString

macman7002
Oct 23, 2010, 01:11 AM
Okay, so this is what I have now:

-(IBAction)connectURL:(id)sender{

BOOL urlResult;
urlResult = [urlString hasPrefix:@"http://"];

if (urlResult == YES)
{
[urlString setStringValue:[sender stringValue]];
[[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[sender stringValue]]]];
}
else {


}


}

Am I on the right track?

In the event that the "else" is called, how would I take "urlString" add "http://" to it, and then use the modified string as the page to load?

Also, I'm no longer able to load a webpage by entering the address of the page into the textfield in my application even when I type in the "http://" and I'm not sure why. Any ideas?

I really appreciate your help! I'm pretty new to programming in Objective-C and get stuck every now and then as I work through learning this language.

Thank you!

ccamelot
Oct 23, 2010, 10:11 AM
UrlString here:

urlResult = [urlString hasPrefix:@"http://"];

should be:

urlResult = [[sender stringValue] hasPrefix:@"http://"];

best
Les

jared_kipe
Oct 23, 2010, 10:22 AM
You can either use a temporary string, or an NSMutableString. But since you're working with a UI as well I'd probably say you should change your interface first and then load the url.

I'm super confused by your naming, is urlString an instance variable? And if so, is it an NSString? Because if it is, it will not respond to - setStringValue:


-(IBAction)connectURL:(id)sender{
// you should probably cast sender as whatever UI element you are using
NSString *urlString = [sender stringValue];

if ( ![urlString hasPrefix: @"http://") {
urlString = [self URLStringFromString: urlString];
[sender setStringValue: urlString];
[self connectURL: sender];
return;
}
else {
//[urlString setStringValue:[sender stringValue]]; //unnecessary
[[webView mainFrame] loadRequest:[NSURLRequest requestWithURL: [NSURL URLWithString: urlString]]];
}
}
- (void)URLStringFromString: (NSString *)string {
// any fancy logic you want to implement for adding or further checking
return [NSString stringWithFormat: @"http://%@", string];
}

macman7002
Oct 23, 2010, 12:24 PM
So the urlResult = [[sender stringValue] hasPrefix:@"http://"]; fixed the first part of the conditional statement. The browser loads the webpage if the entered URL contains "http://".

Still trying to figure out the 'else' part.

I followed along with macdevcenter.com's "Build Your Own Browser, Part 2".
In the header file, I defined IBOutlet id urlString;.

So in the 'else' part, would I need to create a new string that would add "http://" to the front of urlString, and then take this new string and set it as the URL to load and proceed to actually load the new string? If so, how would I tell the webView to load this new string?
Am I understanding this correctly?

Thank you.

jared_kipe
Oct 23, 2010, 12:46 PM
So the urlResult = [[sender stringValue] hasPrefix:@"http://"]; fixed the first part of the conditional statement. The browser loads the webpage if the entered URL contains "http://".

Still trying to figure out the 'else' part.

I followed along with macdevcenter.com's "Build Your Own Browser, Part 2".
In the header file, I defined IBOutlet id urlString;.

So in the 'else' part, would I need to create a new string that would add "http://" to the front of urlString, and then take this new string and set it as the URL to load and proceed to actually load the new string? If so, how would I tell the webView to load this new string?
Am I understanding this correctly?

Thank you.

Did you look at my code? It checks if the "sender" which I assume is a TextField has the prefix, and if not adds http:// to the front of it and resubmits it. Honestly, other than possible naming conflicts (which really shouldn't matter because the local declaration will over right higher scoped variable names) my code should be drop-in for doing everything you're describing.

macman7002
Oct 23, 2010, 12:58 PM
Did you look at my code? It checks if the "sender" which I assume is a TextField has the prefix, and if not adds http:// to the front of it and resubmits it. Honestly, other than possible naming conflicts (which really shouldn't matter because the local declaration will over right higher scoped variable names) my code should be drop-in for doing everything you're describing.

Yes, sir, I did.

I just pasted it in my ".m" and ran the program, everything worked great! However, back in XCode, I get warnings such as "....'urlString' hides in instance variable" "MyDocument may not respond to -URLStringFromString" and "return with a value, in function returning void". Could you perhaps briefly explain what these mean? And could you explain how it resubmits the new address?

I really appreciate your time and help.

jared_kipe
Oct 23, 2010, 01:36 PM
Yes, sir, I did.

I just pasted it in my ".m" and ran the program, everything worked great! However, back in XCode, I get warnings such as "....'urlString' hides in instance variable" "MyDocument may not respond to -URLStringFromString" and "return with a value, in function returning void". Could you perhaps briefly explain what these mean? And could you explain how it resubmits the new address?

I really appreciate your time and help.

#1 I didn't know if urlString was an ivar, so I redeclared it as an NSString, thus my declaration of urlString supersedes your declaration. Either change its name, OR if it is an NSString ivar take out my declaration (but that WILL cause some memory issues as all the NSStrings I was working with are autoreleased, eg. -stringValue returns an autoreleased string). BUT if it isn't declared as an NSString then you'll need to rename my version of urlString (and frankly it is poorly named)

#2
my implementation of -URLStringFromString doesn't include a declaration of it in @interface file, so add it if you don't like the warning.

#3 I was typing off my little function too fast instead of - (void)URLStringFromString: (NSString *) string, SHOULD be - (NSString *)URLStringFromString: (NSString *) string.

#4 it resubmits the address in the first conditional. So basically if it doesn't have http:// then it gets a new string that does have that in the beginning, and then tells the "sender" (which is some kind of textField or view) to set its stringValue to that new string, then it recursively calls the exact same method - connectURL: with "sender" again and returns, stopping that function call.

macman7002
Oct 23, 2010, 01:39 PM
You can either use a temporary string, or an NSMutableString. But since you're working with a UI as well I'd probably say you should change your interface first and then load the url.

I'm super confused by your naming, is urlString an instance variable? And if so, is it an NSString? Because if it is, it will not respond to - setStringValue:


-(IBAction)connectURL:(id)sender{
// you should probably cast sender as whatever UI element you are using
NSString *urlString = [sender stringValue];

if ( ![urlString hasPrefix: @"http://") {
urlString = [self URLStringFromString: urlString];
[sender setStringValue: urlString];
[self connectURL: sender];
return;
}
else {
//[urlString setStringValue:[sender stringValue]]; //unnecessary
[[webView mainFrame] loadRequest:[NSURLRequest requestWithURL: [NSURL URLWithString: urlString]]];
}
}
- (void)URLStringFromString: (NSString *)string {
// any fancy logic you want to implement for adding or further checking
return [NSString stringWithFormat: @"http://%@", string];
}


I was just looking at your code some more to understand it and I think I get most of it. I'm still unsure as to how the revised address is resubmitted. How do I cast a sender, in this case it being a TextField? Also, if I wanted to check for ".com" in the URL would that go in "URLStringFromString:" or "connectURL:" ?

jared_kipe
Oct 23, 2010, 09:52 PM
I was just looking at your code some more to understand it and I think I get most of it. I'm still unsure as to how the revised address is resubmitted. How do I cast a sender, in this case it being a TextField? Also, if I wanted to check for ".com" in the URL would that go in "URLStringFromString:" or "connectURL:" ?

[sender setStringValue: urlString];
[self connectURL: sender];
return;

Is the resubmitting part.

Sure you COULD do some more checking in either method (or more likely change your design around a little more for more robust checking) however checking for .com is not generally a good thing to do since there are so many other ways to end a web address .local .net, it could be .com/ or a folder like ... test.com/testPage/ or testPage.html or .htm, .php or even a number or random ending, the mind just reels thinking of all of them...

So I'd say you should probably never just throw .com onto the end of a web address.