NSString*iTunesLink = @"http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=284417350&mt=8";
NSURL*iTunesURL =[NSURLURLWithString:iTunesLink];
// Produce a phobos.apple.com URL that will open the iTunes or App Store application directly[NSURLURLWithString:[NSStringstringWithFormat:@"http://phobos.apple.com%@?%@",
iTunesURL.path, iTunesURL.query]];
Some iTunes links, including iTunes Affiliate links, result in multiple redirections before reaching the appropriate store application. You can process these redirects silently using NSURLConnection, and open the final URL once the redirects are complete. This allows your application to transition right to the store without launching Safari. Listing 3 demonstrates how to accomplish this.
Note: If you have iTunes links inside a UIWebView,
you can use this technique after intercepting the links with the
-[UIWebViewDelegate webView:shouldStartLoadWithRequest:navigationType:] delegate method.
// Process a LinkShare/TradeDoubler/DGM URL to something iPhone can handle-(void)openReferralURL:(NSURL*)referralURL {NSURLConnection*conn =[[NSURLConnectionalloc]initWithRequest:[NSURLRequestrequestWithURL:referralURL]delegate:selfstartImmediately:YES];
[conn release];
}// Save the most recent URL in case multiple redirects occur// "iTunesURL" is an NSURL property in your class declaration-(NSURLRequest*)connection:(NSURLConnection*)connectionwillSendRequest:(NSURLRequest*)requestredirectResponse:(NSURLResponse*)response{self.iTunesURL =[responseURL];
returnrequest;
}// No more redirects; use the last URL saved-(void)connectionDidFinishLoading:(NSURLConnection*)connection{[[UIApplication sharedApplication]openURL:self.iTunesLink];
}