非消耗性产品一般需要一个Restore按钮,用户重新安装的时候用来恢复购买。

流程:
1.获取 products
2.添加到 payment 队列
3.根据 updatedTransactions 判断是否购买成功
实现代理:SKPaymentTransactionObserver,SKProductsRequestDelegate

swift
1
2
var payment : SKPayment?
var product : SKProduct?
swift
1
2
3
4
5
6
7
//load request
SKPaymentQueue.defaultQueue().addTransactionObserver(self)
productRequest = SKProductsRequest(productIdentifiers: ["me.zhangxi.xxxx"])
productRequest?.delegate = self
productRequest?.start()
//restore
SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
SKProductsRequestDelegate
1
2
3
4
5
6
7
func productsRequest(request: SKProductsRequest, didReceiveResponse response: SKProductsResponse) {
product = response.products.first
if product != nil
{
payment = SKPayment(product: product!)
}
}
SKPaymentTransactionObserver
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
func paymentQueue(queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
for transaction in transactions
{
switch transaction.transactionState {
case .Purchasing: // Transaction is being added to the server queue.
break;
case .Purchased: // Transaction is in queue, user has been charged. Client should complete the transaction.
self.buyOK()
SKPaymentQueue.defaultQueue().finishTransaction(transaction)
case .Failed: // Transaction was cancelled or failed before being added to the server queue.
break;
case .Restored: // Transaction was restored from user's purchase history. Client should complete the transaction.
self.buyOK()
self.alert("Restore Success.")
SKPaymentQueue.defaultQueue().finishTransaction(transaction)
case .Deferred: // The transaction is in the queue, but its final status is pending external action.
break;
}
}
}