Question

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!

Was it helpful?

Solution

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);
}

OTHER TIPS

Try passing the string animalTitle through CFURLCreateStringByAddingPercentEscapes first.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top