Today I decided to figure out how to launch the App Store application from within an iPhone application using the SDK.
It turns out this is pretty simple, but you have to beware of a few issues.
- First and foremost, it doesn't work from the iPhone Simulator, in all probability since the App Store application is not available on the simulator.
- Second you need to provide a valid URL to an application in the App Store. You can access the URL to an application by simply right clicking the Application in iTunes and selecting the "Copy iTunes Store URL" menu item.
Here is the core code needed.
I then chose to store the App Store URL in the info.plist file under a key named TEBAppStoreLink, so that I can use the same code for any future applications.
I ended up with this method for launching the App Store with the URL in the info.plist file.
UIApplication *app = [UIApplication sharedApplication];
// Get the url from info.plist.
NSString *appStoreLink = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"TEBAppStoreLink"];
// Open the url if it was available.
[app openURL:[NSURL URLWithString:appStoreLink]];
What really happens is that Mobile Safari is launched with the provided URL, and upon realizing that the link is an App Store link it in turn launches the App Store application.
Of course, if you provide any other URL it just opens in Safari as usual. I haven't tested what happens if a YouTube URL is used, but I imagine the YouTube app would launch. That, however, is not the problem i set out to solve today.