Foreign letters in NSURL

Discussion in 'iOS Programming' started by Ralle, Feb 9, 2009.

  1. Ralle macrumors newbie

    Joined:
    Jan 6, 2009
    #1
    I live in Denmark and here we have 3 additional letters (æ,ø,å) and I am making GET requests for XML content but NSURL fails whenever I put in any of the characters. They have to be escaped somehow but I am not sure how. This is where I call NSURL:
    NSURL *xmlURL = [NSURL URLWithString:URL];
     
  2. Aea macrumors 6502a

    Aea

    Joined:
    May 23, 2007
    Location:
    Denver, Colorado
  3. Ralle thread starter macrumors newbie

    Joined:
    Jan 6, 2009
    #3
    It is taken from a UISearchBar.

    NSString* search = [searchBox text];
    NSString* purl = @"http://www.google.com/search?query=";
    NSString* url = [purl stringByAppendingString:search ];
     
  4. xsmasher macrumors regular

    xsmasher

    Joined:
    Jul 18, 2008
    #4
    Same way that you encode spaces and other special chars - stringByAddingPercentEscapesUsingEncoding

    This code works for me:

    Code:
    	NSString *url = @"http://www.google.com/search?q=æøå";
    	url = [url stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding ];
    	xmlURL = [NSURL URLWithString:url];
    
     

Share This Page