PDA

View Full Version : String with URL




guyddor
Sep 21, 2009, 01:55 PM
Who can explain me how to use it?

I try to build an app that the user will enter one word in UITextField and click a button and a safari window will open with the following address:


http://www.[the word in UITextField].com



Darkroom
Sep 21, 2009, 02:22 PM
the documentation knows best... check out "Combining Strings (http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/stringByAppendingFormat:)" under tasks of NSString Class Reference.

Kingbombs
Sep 21, 2009, 03:41 PM
the documentation knows best... check out "Combining Strings (http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/stringByAppendingFormat:)" under tasks of NSString Class Reference.

i think hes more going to want StringWithFormat

but i think hes actually looking for the call to open a webpage in safari
think its:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://ww.google.com/"]];

something like that

Darkroom
Sep 21, 2009, 04:29 PM
silly me. i suppose stringWithFormat would be easier :o


- (IBAction)gotoURLAction
{
NSString *preURL = @"http://www.";
NSString *postURL = @".com";
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@%@", preURL, textField.text, postURL]]];
}

robbieduncan
Sep 21, 2009, 04:31 PM
silly me. i suppose stringWithFormat would be easier :o


- (IBAction)gotoURLAction
{
NSString *preURL = @"http://www.";
NSString *postURL = @".com";
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@%@", preURL, textField.text, postURL]]];
}


Or


- (IBAction)gotoURLAction
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"www.%@.com", textField.text]]];
}


Which is even easier :)

Darkroom
Sep 21, 2009, 04:48 PM
http://www.[the word in UITextField].com

keep in mind that not all website addresses start with www. or end in .com


Which is even easier :)

indeed :)

guyddor
Sep 26, 2009, 03:24 AM
Or


- (IBAction)gotoURLAction
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"www.%@.com", textField.text]]];
}


Which is even easier :)

Thanks a lot!!!
And if I want more then one word?

drf1229
Sep 26, 2009, 07:30 AM
Simple:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"www.%@%@.com", textField.text, textField2.text]]];

guyddor
Sep 27, 2009, 05:59 AM
Simple:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"www.%@%@.com", textField.text, textField2.text]]];


Thanks you but still I can't write more then one word

dejo
Sep 27, 2009, 12:06 PM
What do you mean by "more than one word"? Are you talking about taking "word1 word2" and using that in a URL, like "www.word1word2.com"?

guyddor
Sep 27, 2009, 12:25 PM
What do you mean by "more than one word"? Are you talking about taking "word1 word2" and using that in a URL, like "www.word1word2.com"?

No, I mean that I will be able to write in the UITextField:

MacBook Pro

Then I will click a button and it will open the url:

http://www.MacBook Pro.com

(I know that there no urls with earnings but I want to know how to use it

drf1229
Sep 27, 2009, 02:28 PM
No, I mean that I will be able to write in the UITextField:

MacBook Pro

Then I will click a button and it will open the url:

http://www.MacBook Pro.com

(I know that there no urls with earnings but I want to know how to use it

It should open the exact text in the field, but I don't know what kind of domains have spaces in them.

PhoneyDeveloper
Sep 27, 2009, 03:06 PM
I don't know about domains having spaces in them but files in a URL can have a space in them. Oddly enough the URL keyboard doesn't have a space character in it. In general when building a URL that might have some special characters in it you use something like

NSURL* url = [[NSURL alloc] initWithString:[inURLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

In which case the space and any other special characters will be % encoded.

dejo
Sep 27, 2009, 07:54 PM
(I know that there no urls with earnings but I want to know how to use it
As has been said, domains are not allowed to have spaces in them. So, given that, how do you want to handle "MacBook Pro" now?

guyddor
Sep 28, 2009, 02:49 AM
As has been said, domains are not allowed to have spaces in them. So, given that, how do you want to handle "MacBook Pro" now?

I want to know for future use, I know that it impossible to use urls with spaces.
I still want to know:

The user should enter a word or more in the UITextField, then the user will click a UIButton and it will open the url with the words that he wrote in the UITextField, for example:

http://www.macrumors forum.com

KoolStar
Sep 28, 2009, 08:12 AM
You just have to remove the spaces from the text field. Simply split the field on the spaces, and then recombine the array of split text. Therefore all spaces will be gone.

dejo
Sep 28, 2009, 09:32 AM
The user should enter a word or more in the UITextField, then the user will click a UIButton and it will open the url with the words that he wrote in the UITextField, for example:

http://www.macrumors forum.com
UITextField text property does not care how many words are contained in the field. So, the previously given solutions should suffice.