简体   繁体   English

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

[英]SKStoreProductViewController cancel button crash or not working

I have implemented a delegate for the SKStoreProductViewController. 我已经为SKStoreProductViewController实现了一个委托。 I add that view controller into the key window's view controller. 我将该视图控制器添加到关键窗口的视图控制器中。 I have also implemented a dismiss view controller code in the delegate function. 我还在委托函数中实现了一个dismiss视图控制器代码。

The question seems to be answer in this question. 这个问题似乎是这个问题的答案。
Modal App Store won't dismiss Modal App Store不会解雇
Yet, this problem still persist in my situation. 但是,在我的情况下,这个问题仍然存在。

Display function 显示功能

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]; }
    }];
}

Delegate Function 委托功能

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

The problem is that you must implement 问题是您必须实施

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

inside the delegate class. 在委托类中。 If you implement it inside the class whose delegate is presenting the SKStoreProductViewController , it will not work because SKStoreProductViewController will try to call productViewControllerDidFinish: within it's delegate, which isn't implementing that method. 如果你在其委托呈现SKStoreProductViewController的类中实现它,它将无法工作,因为SKStoreProductViewController将尝试在其委托中调用productViewControllerDidFinish:它没有实现该方法。

Let me show an example: 让我举个例子:

@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

So, if I did understand your problem well, you must implement productViewControllerDidFinish: inside your viewController class, as it is the one who is presentig the SKStoreProductViewController . 所以,如果我确实理解了你的问题,你必须在你的viewController类中实现productViewControllerDidFinish:因为它是SKStoreProductViewController

Hope this helps! 希望这可以帮助!

请尝试使用Delegate函数,将所有行替换为:

[self dismissViewControllerAnimated:YES completion:nil];

Where does this code reside? 这段代码在哪里? You set the delegate to the instance where this code is written, but you are adding the SKStoreProductViewController to the root view. 您将委托设置为写入此代码的实例,但您将SKStoreProductViewController添加到根视图。
Have you tried adding the store to self? 你有没有尝试将商店添加到自己?

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

Thanks for the delegate function. 感谢委托功能。 It isn't obvious in the documentation. 这在文档中并不明显。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM