如何检查用户是否点击了取消按钮(当他被问及是否要购买smth时,或者如果他已经购买了这个SKProduct是否要下载它)?
目前,我只收到 SKPaymentTransactionStateFailed in paymentQueue:updateTransactions: 方法,无论是在用户点击取消按钮后还是在没有互联网时。有什么方法可以区分这两种情况吗?
这段代码对我有用:
if (transaction.error.code != SKErrorPaymentCancelled) {
NSLog(@"Other error");
} else {
NSLog(@"User canceled");
}
艾伦的回答是完美的。以防万一有人想知道其他情况
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;
}
Swift 版本:
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;
}
}
}
Swift 4.2
if let error = transaction.error,
(error as? SKError)?.code != SKError.paymentCancelled {
// Here it is failed for sure!
}
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;
}
}
}
检查设置的 SKPaymentTransaction error
属性。
@property(非原子,只读)NSError *错误
描述处理事务时发生的错误的对象。(只读)
error 属性是未定义的,除非 transactionState 设置为 SKPaymentTransactionStateFailed。应用程序可以读取 error 属性以确定事务失败的原因。
此外,您可能希望在启动事务之前使用 Apple 的可达性类来确定互联网是否可用。
也许您正在使用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);
}
在失败的事务方法中,现在可以调用
[[NSNotificationCenter defaultCenter] postNotificationName:IAPHelperProductPurchasedNotification object:nil userInfo:nil];
现在,您可以在当前视图中检查通知发送的nil值