2

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

这个问题似乎是这个问题的答案。
模态应用商店不会解雇
然而,这个问题仍然存在于我的情况中。

显示功能

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

3 回答 3

7

问题是您必须实施

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

在委托类内部。如果您在其委托呈现 的类中实现它SKStoreProductViewController,它将无法工作,因为SKStoreProductViewController将尝试productViewControllerDidFinish:在它的委托中调用,而该委托并未实现该方法。

让我举个例子:

@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.

希望这可以帮助!

于 2014-02-24T21:54:28.487 回答
0

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

[self dismissViewControllerAnimated:YES completion:nil];
于 2013-10-09T03:30:03.817 回答
0

这段代码在哪里?您将委托设置为编写此代码的实例,但您将 SKStoreProductViewController 添加到根视图。
您是否尝试将商店添加到自己?

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

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

于 2013-12-16T16:44:12.007 回答