iOS 6以降、SKStoreProductViewControllerクラスを使用してそれを行う正しい方法。
Swift 5.x:
func openStoreProductWithiTunesItemIdentifier(_ identifier: String) {
let storeViewController = SKStoreProductViewController()
storeViewController.delegate = self
let parameters = [ SKStoreProductParameterITunesItemIdentifier : identifier]
storeViewController.loadProduct(withParameters: parameters) { [weak self] (loaded, error) -> Void in
if loaded {
// Parent class of self is UIViewContorller
self?.present(storeViewController, animated: true, completion: nil)
}
}
}
private func productViewControllerDidFinish(viewController: SKStoreProductViewController) {
viewController.dismiss(animated: true, completion: nil)
}
// Usage:
openStoreProductWithiTunesItemIdentifier("1234567")
Swift 3.x:
func openStoreProductWithiTunesItemIdentifier(identifier: String) {
let storeViewController = SKStoreProductViewController()
storeViewController.delegate = self
let parameters = [ SKStoreProductParameterITunesItemIdentifier : identifier]
storeViewController.loadProduct(withParameters: parameters) { [weak self] (loaded, error) -> Void in
if loaded {
// Parent class of self is UIViewContorller
self?.present(storeViewController, animated: true, completion: nil)
}
}
}
func productViewControllerDidFinish(_ viewController: SKStoreProductViewController) {
viewController.dismiss(animated: true, completion: nil)
}
// Usage:
openStoreProductWithiTunesItemIdentifier(identifier: "13432")
アプリのitunesアイテムIDは次のように取得できます:(静的なIDではなく)
Swift 3.2
var appID: String = infoDictionary["CFBundleIdentifier"]
var url = URL(string: "http://itunes.apple.com/lookup?bundleId=\(appID)")
var data = Data(contentsOf: url!)
var lookup = try? JSONSerialization.jsonObject(with: data!, options: []) as? [AnyHashable: Any]
var appITunesItemIdentifier = lookup["results"][0]["trackId"] as? String
openStoreProductViewController(withITunesItemIdentifier: Int(appITunesItemIdentifier!) ?? 0)
Swift 2.x:
func openStoreProductWithiTunesItemIdentifier(identifier: String) {
let storeViewController = SKStoreProductViewController()
storeViewController.delegate = self
let parameters = [ SKStoreProductParameterITunesItemIdentifier : identifier]
storeViewController.loadProductWithParameters(parameters) { [weak self] (loaded, error) -> Void in
if loaded {
// Parent class of self is UIViewContorller
self?.presentViewController(storeViewController, animated: true, completion: nil)
}
}
}
func productViewControllerDidFinish(viewController: SKStoreProductViewController) {
viewController.dismissViewControllerAnimated(true, completion: nil)
}
// Usage
openStoreProductWithiTunesItemIdentifier("2321354")
客観的C:
static NSInteger const kAppITunesItemIdentifier = 324684580;
[self openStoreProductViewControllerWithITunesItemIdentifier:kAppITunesItemIdentifier];
- (void)openStoreProductViewControllerWithITunesItemIdentifier:(NSInteger)iTunesItemIdentifier {
SKStoreProductViewController *storeViewController = [[SKStoreProductViewController alloc] init];
storeViewController.delegate = self;
NSNumber *identifier = [NSNumber numberWithInteger:iTunesItemIdentifier];
NSDictionary *parameters = @{ SKStoreProductParameterITunesItemIdentifier:identifier };
UIViewController *viewController = self.window.rootViewController;
[storeViewController loadProductWithParameters:parameters
completionBlock:^(BOOL result, NSError *error) {
if (result)
[viewController presentViewController:storeViewController
animated:YES
completion:nil];
else NSLog(@"SKStoreProductViewController: %@", error);
}];
[storeViewController release];
}
#pragma mark - SKStoreProductViewControllerDelegate
- (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController {
[viewController dismissViewControllerAnimated:YES completion:nil];
}
次のようにkAppITunesItemIdentifier
(アプリのiTunesアイテム識別子)を取得できます:(静的なものの代わりに)
NSDictionary* infoDictionary = [[NSBundle mainBundle] infoDictionary];
NSString* appID = infoDictionary[@"CFBundleIdentifier"];
NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/lookup?bundleId=%@", appID]];
NSData* data = [NSData dataWithContentsOfURL:url];
NSDictionary* lookup = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSString * appITunesItemIdentifier = lookup[@"results"][0][@"trackId"];
[self openStoreProductViewControllerWithITunesItemIdentifier:[appITunesItemIdentifier intValue]];