如果用户选择继续,则在ApplicationDidBecomeActive时显示警报并重定向到另一个视图

时间:2014-01-10 17:20:27

标签: ios objective-c cocoa-touch in-app-purchase uialertview

我正在实施应用内购买功能。除Apple Store标准购买流程外,我还从我自己的服务器端进行了另一次收据验证。有时,购买程序在Apple端完成,应用程序在我的服务器验证收据之前就已存在。

所以我将收据存储在NSUserDefaults中。每当applicationDidBecomeActive,我会检查NSUserDefaults中是否有待处理的收据。如果是,我想弹出警报,询问用户是否继续完成购买。如果用户取消,那么我会取消收据取消购买。否则,我会将用户引导到购买视图,并完成剩下的工作。

以前,我在AppDelegate ApplicationDidBecomeActive内进行了检查。这似乎不是一个好习惯。然后我尝试将代码移动到MainViewController中,在init中捕获AppicationDidBecomeActive通知。但我不确定这样做的正确方法是什么?我尝试用selector:@selector(resumePurchase:)捕捉事件,这是我的简历购买代码

- (void)resumePurchase:(NSNotification*)notification {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSData *purchaseDocument = (NSData*)[userDefaults objectForKey:@"lastPurchaseDocument"];
    if (!purchaseDocument) {
        NSString *message = [NSString stringWithFormat:NSLocalizedString(@"You have an incomplete purchase in the app, do you want to continue the payment?",@"resume purchase.")];
        self.resumePurchaseAlert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Resume Payment",@"alter title")
                                                              message:message
                                                             delegate:self
                                                    cancelButtonTitle:NSLocalizedString(@"Cancel",@"Cancel caption")
                                                    otherButtonTitles:NSLocalizedString(@"Continue",@"altert approve button"), nil];
        [self.resumePurchaseAlert show];
    }
}

到目前为止这似乎没问题。我被困在继续按钮处理程序

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView == self.resumePurchaseAlert) {
        if (buttonIndex == 0) { // Cancel
            NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
            [userDefaults removeObjectForKey:kPurchaseDocument];
            [userDefaults synchronize];
        }
        if (buttonIndex == 1) { // Continue
            NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
            NSDictionary *purchaseDocument = (NSDictionary*)[userDefaults objectForKey:kPurchaseDocument];
            NSData *purchasedReceipt = [purchaseDocument objectForKey:kPurchasedReceipt];
            NSString *purchasedFeature = [purchaseDocument objectForKey:kPurchasedFeature];
            if (purchasedFeature && purchasedReceipt) {
                // here I want to redirect the view to PurchaseViewController
            }
        }
    }
}

我不知道如何以优雅的方式将视图重定向到PurchaseViewController ......

1 个答案:

答案 0 :(得分:0)

UIViewController中保存的其他NSUserDefaults检查值,并根据它进行了nabvigation。