Xcode openURL doesn't read link

2019-08-30 04:48发布

I have an animal list and special button. When I press the button, I would like to go to Wikipedia and read about this animal more. So I wrote this code:

-(IBAction)goWiki:(id)sender
{
    NSString *wikiUrl = "http://ru.wikipedia.org/wiki/";
    NSString *url = [NSString stringWithFormat:@"%@%@",wikiUrl,animalTitle];  
    [[UIApplication sharedApplication]openURL:[NSURL URLWithString:url]];
    NSLog(@"%@",url);
}

NSLog shows that url was written correctly, however, nothing happened. I am 99,9% sure its because of animalTitle. My native language is russian and animalTitle is also an animal name in russian. So if link is like http://ru.wikipedia.org/wiki/Frog its fine and it works but if its like http://ru.wikipedia.org/wiki/Лягушка nothing happens. Any ideas, how can I move to a russian article? Thanks!

2条回答
Melony?
2楼-- · 2019-08-30 05:26

use stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding as follows -

-(IBAction)goWiki:(id)sender
{
    NSString *wikiUrl = @"http://ru.wikipedia.org/wiki/";
    NSString *url = [NSString stringWithFormat:@"%@%@",wikiUrl,animalTitle];  


    url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    [[UIApplication sharedApplication]openURL:[NSURL URLWithString:url]];
    NSLog(@"%@",url);
}
查看更多
劫难
3楼-- · 2019-08-30 05:39

Try passing the string animalTitle through CFURLCreateStringByAddingPercentEscapes first.

查看更多
登录 后发表回答