0

I'm using this code to assign the link of my button to the wiki page, while capturing the countryName.text in the UILabel to be a part of the URL, but Xcode gives me an error when I press it. Here's the code:

- (IBAction)openWiki:(id)sender {
NSString *sampleUrl = [[NSString alloc] initWithFormat:@"http://en.wikipedia.org/wiki/%@%@",self.countryName.text];
NSURL *wikiUrl = [[NSURL alloc] initWithString:sampleUrl];
[[UIApplication sharedApplication] openURL:wikiUrl];}

Thanks in advance.

Sergey Grischyov
  • 11,995
  • 20
  • 81
  • 120

1 Answers1

3

In your format you expect two parameters, but give only one:

@"http://en.wikipedia.org/wiki/%@%@",self.countryName.text
//                             ^^

Remove one specifier:

- (IBAction)openWiki:(id)sender {
    NSString *sampleUrl = [[NSString alloc] 
        initWithFormat:@"http://en.wikipedia.org/wiki/%@",self.countryName.text];
    //                                                ^^
    NSURL *wikiUrl = [[NSURL alloc] initWithString:sampleUrl];
    [[UIApplication sharedApplication] openURL:wikiUrl];
}
MByD
  • 135,866
  • 28
  • 264
  • 277
  • This helped, but when self.countryName.text is something like "San Marino" (two words instead of one) the link wouldn't open :( – Sergey Grischyov May 05 '12 at 13:31
  • That doesn't matter, it handles a full NSString. You might what to encode it before, but that's something else. – MByD May 05 '12 at 13:32
  • Take a look at this question: http://stackoverflow.com/questions/8088473/url-encode-a-nsstring – MByD May 05 '12 at 13:35