String with URL

Discussion in 'iOS Programming' started by guyddor, Sep 21, 2009.

  1. guyddor macrumors member

    Joined:
    Jan 7, 2009
    #1
    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
     
  2. Darkroom Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #2
    the documentation knows best... check out "Combining Strings" under tasks of NSString Class Reference.
     
  3. Kingbombs macrumors member

    Joined:
    Jun 24, 2009
    #3
    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:mad:"http://ww.google.com/"]];

    something like that
     
  4. Darkroom Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #4
    silly me. i suppose stringWithFormat would be easier :eek:

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

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #5
    Or

    Code:
    - (IBAction)gotoURLAction
    	{
    	[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"www.%@.com", textField.text]]];
    	}
    
    Which is even easier :)
     
  6. Darkroom Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #6
    keep in mind that not all website addresses start with www. or end in .com

    indeed :)
     
  7. guyddor thread starter macrumors member

    Joined:
    Jan 7, 2009
    #7
    Thanks a lot!!!
    And if I want more then one word?
     
  8. drf1229 macrumors regular

    drf1229

    Joined:
    Jun 22, 2009
    #8
    Simple:

    Code:
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"www.%@%@.com", textField.text, textField2.text]]];
    
     
  9. guyddor thread starter macrumors member

    Joined:
    Jan 7, 2009
    #9
    Thanks you but still I can't write more then one word
     
  10. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #10
    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"?
     
  11. guyddor thread starter macrumors member

    Joined:
    Jan 7, 2009
    #11
    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
     
  12. drf1229 macrumors regular

    drf1229

    Joined:
    Jun 22, 2009
    #12
    It should open the exact text in the field, but I don't know what kind of domains have spaces in them.
     
  13. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #13
    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

    Code:
    NSURL* url = [[NSURL alloc] initWithString:[inURLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    In which case the space and any other special characters will be % encoded.
     
  14. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #14
    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?
     
  15. guyddor thread starter macrumors member

    Joined:
    Jan 7, 2009
    #15
    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
     
  16. KoolStar macrumors demi-god

    KoolStar

    Joined:
    Oct 16, 2006
    Location:
    Kentucky
    #16
    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.
     
  17. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #17
    UITextField text property does not care how many words are contained in the field. So, the previously given solutions should suffice.
     

Share This Page