Appstore as modal view in iOS6

て烟熏妆下的殇ゞ 提交于 2019-11-28 03:12:51

I added this method as a category to UIViewController, but you can repurpose it for your own needs. The app store ID is the big number in the app store URL. Make sure you import the StoreKit framework and header file!

@import StoreKit;

- (void)presentAppStoreForID:(NSNumber *)appStoreID withDelegate:(id<SKStoreProductViewControllerDelegate>)delegate
{
    if(NSClassFromString(@"SKStoreProductViewController")) { // Checks for iOS 6 feature.

        SKStoreProductViewController *storeController = [[SKStoreProductViewController alloc] init];
        storeController.delegate = delegate; // productViewControllerDidFinish

        // Example App Store ID (e.g. for Words With Friends)
        // @322852954

        [storeController loadProductWithParameters:@{ SKStoreProductParameterITunesItemIdentifier: appStoreID }
                                   completionBlock:^(BOOL result, NSError *error) {
            if (result) {
                [self presentViewController:storeController animated:YES completion:nil];
            } else {
                [[[UIAlertView alloc] initWithTitle:@"Uh oh!" message:@"There was a problem opening the app store" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil] show];
            }
        }];

    } else { // Before iOS 6, we can only open the App Store URL
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/app/id%@",appStoreID]]];
    }
}

Looks like this is introduced in IOS 6 as StoreKit, specifically SKITunesProductViewController which allows you to present iTunes contents (apps, music, books etc.) inside your app for user to purchase directly.

There is a plugin for handling affiliate links here: https://github.com/adeven/AEProductController

Or you could roll your own by first opening the affiliate link in-app (just as you would before opening the iTunes Store app directly), following the Apple Technical Q&A QA1629:

https://developer.apple.com/library/ios/#qa/qa2008/qa1629.html

and then opening the SKStoreProductViewController as MaxGabriel noted above.

[Edit] I completed this in my app (a music app) but whether I have an active AVAudioSession or I completely disable all playback (for testing), song samples in the modal iTunes Store play but have no sound. If you don't have this issue or you find a resolution, let me know. It may be a bug that should be reported to https://bugreport.apple.com/ .

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!