views:

178

answers:

3

Ok, I understand that using strings that have special characters is an encoding issue. However I am not sure how to adjust my code to allow these characters. Below is the code that works great for text that contains no special characters, but can you show me how and where to change the code to allow for the special characters to be used. Right now those characters crash the app.

enter code here

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 1) {
    //iTunes Audio Search
    NSString *stringURL = [NSString stringWithFormat:@"http://phobos.apple.com/WebObjects/MZSearch.woa/wa/search?WOURLEncoding=ISO8859_1&lang=1&output=lm&term=\"%@\"",currentSong.title];
    stringURL = [stringURL stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
    NSURL *url = [NSURL URLWithString:stringURL];
    [[UIApplication sharedApplication] openURL:url];

        }

}

And this:

-(IBAction)launchLyricsSearch:(id)sender{
WebViewController * webView = [[WebViewController alloc] initWithNibName:@"WebViewController" bundle:[NSBundle mainBundle]];
webView.webURL = [NSString stringWithFormat:@"http://www.google.com/m/search?hl=es&q=\"%@\"+letras",currentSong.title];
webView.webTitle = @"Letras";   
[self.navigationController pushViewController:webView animated:YES];

}

Please show me how and where to do this for these two bits of code.

A: 

-[NSString stringByAddingPercentEscapesUsingEncoding:]

NSASCIIStringEncoding usually works best for URL encoding.

If you only want to escape certain characters, use CFURLCreateStringByAddingPercentEscapes().

Dave DeLong
A: 

Ok Dave's answer works great for Part 1:

Working Code:

NSString *stringURL = [NSString stringWithFormat:@"http://phobos.apple.com/WebObjects/MZSearch.woa/wa/search?WOURLEncoding=ISO8859_1&lang=1&output=lm&term=\"%@\"",currentSong.title]; stringURL = [stringURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

However, Part 2 isn't there yet (I know its me... but could you help?)

webView.webURL = [NSString stringwithFormat:@"http://www.google.com/m/search?hl=es&q=\"%@\"+letras",currentSong.title]; webView.webURL = [webView.webURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

What would be the correct code to get webView.webURL to allow spanish characters like "ñ"? Thanks in advance

BC
See my answer. You need to do the percent-escape before you insert it into the URL, instead of doing it to the entire URL string.
lucius
A: 

You should use the percent-escapes only to the currentSong.title and not to the entire URL. Here's what it should look like:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1) {
        //iTunes Audio Search
        NSString *stringURL = [NSString stringWithFormat: 
            @"http://phobos.apple.com/WebObjects/MZSearch.woa/wa/search?WOURLEncoding=ISO8859_1&lang=1&output=lm&term=\"%@\"", 
            [currentSong.title stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
        NSURL *url = [NSURL URLWithString:stringURL];
        [[UIApplication sharedApplication] openURL:url];
    }
}

-(IBAction)launchLyricsSearch:(id)sender{
    WebViewController * webView = [[WebViewController alloc] initWithNibName:@"WebViewController" bundle:[NSBundle mainBundle]];
    webView.webURL = [NSString stringWithFormat: 
        @"http://www.google.com/m/search?hl=es&q=\"%@\"+letras", 
        [currentSong.title stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    webView.webTitle = @"Letras";   
    [self.navigationController pushViewController:webView animated:YES];
}

For non-ASCII characters such as ñ, the NSUTF8StringEncoding should properly handle them. NSUTF8StringEncoding is the recommended encoding for URLs.

lucius
Mmm - almost there. Lucius I added your code.While the app doesn't crash, the resulting currentSong.title now has tons of percentage escapes.For example - taking part 2: So "Dingo Eres Señor"becomesDingo%2520Eres%2520Se%25C3%25B1or (which of course doesn't work in the browser)Ideally it should be: Dingo%20Eres%20Se%C3%B1orAny ideas?
BC
The `%25` represents the percent character, which means that your string is being run through `stringByAddingPercentEscapesUsingEncoding:` twice. Use the debugger to set a breakpoint before you call that method and to see what `currentSong.title` contains. If it's already escaped, you don't need to do it again.
lucius
Perfect - Thank you Lucius. It was in a separate web view controller which was encoding to NSASCIIStringEncoding. Thanks a ton. Now I can sleep! Awesome.
BC