我不知道如何或在何处将正确的编码代码添加到此iPhone代码中

时间:2010-03-28 03:29:37

标签: iphone objective-c xcode encoding utf-8

好的,我知道使用具有特殊字符的字符串是编码问题。 但是我不确定如何调整我的代码以允许这些字符。下面的代码适用于不包含特殊字符的文本,但是您可以告诉我如何以及在何处更改代码以允许使用特殊字符。现在这些角色使应用程序崩溃。

- (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];
    }
}

而且:

-(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];
}

请告诉我如何以及在何处为这两位代码执行此操作。

3 个答案:

答案 0 :(得分:0)

-[NSString stringByAddingPercentEscapesUsingEncoding:]

NSASCIIStringEncoding通常最适合网址编码。

如果您只想转义某些字符,请使用CFURLCreateStringByAddingPercentEscapes()

答案 1 :(得分:0)

Ok Dave的回答对第1部分非常有用:

工作代码:

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];

然而,第2部分还没有(我知道它......但你可以帮忙吗?)

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

让webView.webURL允许像“ñ”这样的西班牙语字符的正确代码是什么?

答案 2 :(得分:0)

您应该仅将percent-escapes用于currentSong.title而不是整个URL。这是它应该是什么样子:

- (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];
}

对于非ASCII字符(如ñ),NSUTF8StringEncoding应正确处理它们。 NSUTF8StringEncoding是URL的推荐编码。