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.

2
  • What is the error you are getting on XCODE? May 5, 2012 at 13:26
  • Get rid of one of the %@ symbols. May 5, 2012 at 13:27

1 Answer 1

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];
}
3
  • This helped, but when self.countryName.text is something like "San Marino" (two words instead of one) the link wouldn't open :( May 5, 2012 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 5, 2012 at 13:32
  • Take a look at this question: stackoverflow.com/questions/8088473/url-encode-a-nsstring
    – MByD
    May 5, 2012 at 13:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.