SKStoreProductViewController取消按钮崩溃或无法正常工作

时间:2013-10-09 03:20:41

标签: ios

我已经为SKStoreProductViewController实现了一个委托。 我将该视图控制器添加到关键窗口的视图控制器中。 我还在委托函数中实现了一个dismiss视图控制器代码。

这个问题似乎是答案。
Modal App Store won't dismiss
然而,这个问题在我的情况下仍然存在。

显示功能

if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
    NSString *appURL = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/%@/app/id%@",
                        [[NSLocale preferredLanguages] objectAtIndex:0], applicationID];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:appURL]];

} else {
    NSDictionary* dict = [NSDictionary dictionaryWithObject:applicationID forKey:SKStoreProductParameterITunesItemIdentifier];
    SKStoreProductViewController *viewCont = [[SKStoreProductViewController alloc] init];
    viewCont.delegate = self;
    [viewCont loadProductWithParameters:dict completionBlock:^(BOOL result, NSError *error)
    {
        UIViewController* viewController = [UIApplication sharedApplication].keyWindow.rootViewController;
        if (viewController)
        { [viewController presentViewController:viewCont animated:YES completion:nil]; }
    }];
}

代表职能

- (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController
{
    if (viewController)
    { [viewController dismissViewControllerAnimated:YES completion:nil]; }
}

3 个答案:

答案 0 :(得分:7)

问题是你必须实施

- (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController
{
    if (viewController)
    { [self dismissViewControllerAnimated:YES completion:nil]; }
}

在委托类中。如果您在其委托提交SKStoreProductViewController的类中实现它,它将无效,因为SKStoreProductViewController将尝试在其委托中调用productViewControllerDidFinish:,而该委托未实现该方法。< / p>

让我举个例子:

@implementation MainViewController

- (void)presentSecondViewController
{
     SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
     [secondViewController setDelegate:self];
     [self presentViewController:secondViewController animated:YES completion:nil];
}

- (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController
{
    if (viewController)
    { [self dismissViewControllerAnimated:YES completion:nil]; }
}

@end


@implementation SecondViewController {
    id delegate <SKStoreProductViewControllerDelegate>;
}

- (void)setDelegate:(id)delegate
{
    _delegate = delegate;
}

- (void)callStoreProductViewController
{
    SKStoreProductViewController *viewCont = [[SKStoreProductViewController alloc] init];
    viewCont.delegate = _delegate;
    [viewCont loadProductWithParameters:dict completionBlock:^(BOOL result, NSError *error)
    {
        UIViewController* viewController = [UIApplication sharedApplication].keyWindow.rootViewController;
        if (viewController)
            { [_delegate presentViewController:viewCont animated:YES completion:nil]; }
    }];
}
@end

因此,如果我确实理解了您的问题,您必须在productViewControllerDidFinish:课程中实施viewController,因为它是预先SKStoreProductViewController的人。

希望这有帮助!

答案 1 :(得分:0)

请尝试使用委托功能,将所有行替换为:

[self dismissViewControllerAnimated:YES completion:nil];

答案 2 :(得分:0)

此代码位于何处?您将委托设置为写入此代码的实例,但您将SKStoreProductViewController添加到根视图中。
您是否尝试将商店添加到自己?

[self presentViewController:storeController animated:YES completion:nil];

感谢委托功能。这在文档中并不明显。