检查用户是否取消了SKPaymentTransaction

时间:2012-11-26 18:32:27

标签: ios in-app-purchase storekit

如何检查用户是否点击了取消按钮(当他被问到是否要购买smth时,或者他是否已购买此SKProduct是否要下载)?

现在我只是在用户点击取消按钮后,例如没有互联网时,在paymentQueue:updatedTransactions:方法中收到SKPaymentTransactionStateFailed。有什么方法可以区分这两种情况吗?

7 个答案:

答案 0 :(得分:17)

此代码适用于我:

if (transaction.error.code != SKErrorPaymentCancelled) {
    NSLog(@"Other error");
} else {
   NSLog(@"User canceled");
}

答案 1 :(得分:12)

艾伦的回答非常完美。以防有人想知道其他案件

switch (transaction.error.code) {
        case SKErrorUnknown:
            //Unknown error
            break;
        case SKErrorClientInvalid:
            // client is not allowed to issue the request, etc.
            break;
        case SKErrorPaymentCancelled:
            // user cancelled the request, etc.
            break;
        case SKErrorPaymentInvalid:
            // purchase identifier was invalid, etc.
            break;
        case SKErrorPaymentNotAllowed:
            // this device is not allowed to make the payment
            break;
        default:
            break;
}

答案 2 :(得分:9)

以下是Swift 3.0上的工作代码:

  if let error = transaction.error as? NSError {

                if error.domain == SKErrorDomain {
                    // handle all possible errors
                    switch (error.code) {
                    case SKError.unknown.rawValue:
                        print("Unknown error")

                    case SKError.clientInvalid.rawValue:
                        print("client is not allowed to issue the request")

                    case SKError.paymentCancelled.rawValue:
                        print("user cancelled the request")

                    case SKError.paymentInvalid.rawValue:
                        print("purchase identifier was invalid")

                    case SKError.paymentNotAllowed.rawValue:
                        print("this device is not allowed to make the payment")

                    default:
                        break;
             }
  }

答案 3 :(得分:2)

Swift 2.2

如果您在.Failed中收到paymentQueue回复,那么最好处理所有错误。

if let error = transaction.error {
    if error.domain == SKErrorDomain {
        // handle all possible errors
        switch (error.code) {
        case SKErrorCode.Unknown.rawValue:
            print("Unknown error")
            break;
        case SKErrorCode.ClientInvalid.rawValue:
            print("client is not allowed to issue the request")
            break;
        case SKErrorCode.PaymentCancelled.rawValue:
            print("user cancelled the request")
            break;
        case SKErrorCode.PaymentInvalid.rawValue:
            print("purchase identifier was invalid")
            break;
        case SKErrorCode.PaymentNotAllowed.rawValue:
            print("this device is not allowed to make the payment")
            break;
        default:
            break;
        }
    }
}

答案 4 :(得分:1)

也许您正在使用Ray Wunderlich的教程代码进行应用购买。守则说:

- (id)initWithProductIdentifiers:(NSSet *)productIdentifiers {

if ((self = [super init])) {

    // Store product identifiers
    _productIdentifiers = productIdentifiers;

    // Check for previously purchased products
    _purchasedProductIdentifiers = [NSMutableSet set];
    for (NSString * productIdentifier in _productIdentifiers) {
        BOOL productPurchased = [[NSUserDefaults standardUserDefaults] boolForKey:productIdentifier];
        if (productPurchased) {
            [[SKPaymentQueue defaultQueue] addTransactionObserver:self]; // CHECK THIS
            [_purchasedProductIdentifiers addObject:productIdentifier];
            NSLog(@"Previously purchased: %@", productIdentifier);
        } else {
            NSLog(@"Not purchased: %@", productIdentifier);
        }
    }

在那里你可以看到,只有在产品已经被购买的情况下才会调用addTransactionObserver。如果您在if查询前面移动此行代码,您将获得所需的结果。

[[SKPaymentQueue defaultQueue] addTransactionObserver:self]; // MOVE HERE
if (productPurchased) {
    [_purchasedProductIdentifiers addObject:productIdentifier];
    NSLog(@"Previously purchased: %@", productIdentifier);
} else {
    NSLog(@"Not purchased: %@", productIdentifier);
}                

现在,您可以在failedTransaction方法中调用

 [[NSNotificationCenter defaultCenter] postNotificationName:IAPHelperProductPurchasedNotification object:nil userInfo:nil];

现在您可以检查当前视图中通知发送的nil值

答案 5 :(得分:1)

Swift 4.2

if let error = transaction.error,
 (error as? SKError)?.code != SKError.paymentCancelled {
 // Here it is failed for sure!
}

答案 6 :(得分:0)

检查设置的SKPaymentTransaction错误属性。此外,您可能希望使用Apple的Reachability类来确定在启动事务之前Internet是否可用。

错误 描述处理事务时发生的错误的对象。 (只读)

@property(非原子,只读)NSError *错误 讨论 除非将transactionState设置为SKPaymentTransactionStateFailed,否则error属性是未定义的。您的应用程序可以读取错误属性以确定事务失败的原因。

状况 适用于iOS 3.0及更高版本。 宣告进入 SKPaymentTransaction.h