Search code examples
phpiosjsonnsurlmediawiki-api

Can't open NSURL from MediaWiki API URL with multiple titles


I am using the MediaWiki API in my iPhone app.

I am trying to load this link (JSON format of all images on the Wikipedia articles for "Spider" and "Cat"):

http://en.wikipedia.org/w/api.php?action=query&prop=images&format=json&imlimit=200&titles=Spider|Cat

My code for doing it is this:

NSString *urlPath = @"http://en.wikipedia.org/w/api.php?action=query&prop=images&format=json&imlimit=200&titles=Spider|Cat";
NSURL *URL = [NSURL URLWithString:urlPath];    // nil

I can go to the link just fine on my computer, but it refuses to load in this code. It does work, however, with other links, like this one (Just images for "Spider"):

http://en.wikipedia.org/w/api.php?action=query&prop=images&format=json&imlimit=200&titles=Spider

NSString *urlPath = @"http://en.wikipedia.org/w/api.php?action=query&prop=images&format=json&imlimit=200&titles=Spider";
NSURL *URL = [NSURL URLWithString:urlPath];    // works just fine

Is this a problem with the | character in an NSString? I don't understand why this would work for the second link, but not the first one.


Solution

  • I realized that the problem was with the vertical pipe symbol, | being added into an NSURL, which makes no sense to me, but the solution is to replace it with %7C, the "URL encoded value for the | character," which I learned from this answer

    So, this works:

    NSString *urlPath = @"http://en.wikipedia.org/w/api.php?action=query&prop=images&format=json&imlimit=200&titles=Spider%7CCat";
    NSURL *URL = [NSURL URLWithString:urlPath];    // nil