开发者

I dont know how or where to add the correct encoding code to this iPhone code

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.

- (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?WOURLEnco开发者_开发知识库ding=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.


-[NSString stringByAddingPercentEscapesUsingEncoding:]

NSASCIIStringEncoding usually works best for URL encoding.

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


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.


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 "ñ"?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜