恢复应用内购买 Swift
Restoring In-App Purchase Swift
我在我的应用中添加了非消耗性应用内购买以移除广告。采购工作正常,它的恢复我遇到了麻烦。我只是不知道该怎么办。
这是我的购买代码:
func buyNonConsumable(){
println("About to fetch the products");
// We check that we are allow to make the purchase.
if (SKPaymentQueue.canMakePayments())
{
var productID:NSSet = NSSet(object: self.product_id!);
var productsRequest:SKProductsRequest = SKProductsRequest(productIdentifiers: productID);
productsRequest.delegate = self;
productsRequest.start();
println("Fething Products");
}else{
println("can't make purchases");
}
}
// Helper Methods
func buyProduct(product: SKProduct){
println("Sending the Payment Request to Apple");
var payment = SKPayment(product: product)
SKPaymentQueue.defaultQueue().addPayment(payment);
}
func removeTheAds(){
userDefaults.setBool(true, forKey: "proUser")
userDefaults.synchronize()
print("You are a pro User")
//Code for removing the ads currently
}
// Delegate Methods for IAP
func productsRequest (request: SKProductsRequest, didReceiveResponse response: SKProductsResponse) {
println("got the request from Apple")
var count : Int = response.products.count
if (count>0) {
var validProducts = response.products
var validProduct: SKProduct = response.products[0] as SKProduct
if (validProduct.productIdentifier == self.product_id) {
println(validProduct.localizedTitle)
println(validProduct.localizedDescription)
println(validProduct.price)
buyProduct(validProduct);
} else {
println(validProduct.productIdentifier)
}
} else {
println("no product found")
}
}
func request(request: SKRequest!, didFailWithError error: NSError!) {
println("There was an error");
}
func paymentQueue(queue: SKPaymentQueue!, updatedTransactions transactions: [AnyObject]!) {
println("Received Payment Transaction Response from Apple");
for transaction:AnyObject in transactions {
if let trans:SKPaymentTransaction = transaction as? SKPaymentTransaction{
switch trans.transactionState {
case .Purchased:
println("Product Purchased");
SKPaymentQueue.defaultQueue().finishTransaction(transaction as SKPaymentTransaction)
removeTheAds()
break;
case .Failed:
println("Purchased Failed");
SKPaymentQueue.defaultQueue().finishTransaction(transaction as SKPaymentTransaction)
break;
// case .Restored:
default:
SKPaymentQueue.defaultQueue().finishTransaction(transaction as SKPaymentTransaction)
break;
}
}
}
}
我如何处理恢复?我有一个调用这两个按钮的恢复按钮,但我现在该怎么办?
SKPaymentQueue.defaultQueue().addTransactionObserver(self)
SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
我看到那个案例了。Restored 被注释掉了,但我不知道里面有什么。任何帮助将不胜感激。
恢复付款后的操作由您决定。我在我的应用程序中所做的与我在 .Purchased
情况下所做的相同 - 我打开隐藏功能。看起来在您的应用中,您会关闭广告。事实上,在我的应用程序中,.Purchased
和 .Restored
是相同的情况,您也可以这样做。这是我的一些代码:
switch t.transactionState {
case .Purchased, .Restored:
// ... turn on the magic ...
}
我在我的应用中添加了非消耗性应用内购买以移除广告。采购工作正常,它的恢复我遇到了麻烦。我只是不知道该怎么办。
这是我的购买代码:
func buyNonConsumable(){
println("About to fetch the products");
// We check that we are allow to make the purchase.
if (SKPaymentQueue.canMakePayments())
{
var productID:NSSet = NSSet(object: self.product_id!);
var productsRequest:SKProductsRequest = SKProductsRequest(productIdentifiers: productID);
productsRequest.delegate = self;
productsRequest.start();
println("Fething Products");
}else{
println("can't make purchases");
}
}
// Helper Methods
func buyProduct(product: SKProduct){
println("Sending the Payment Request to Apple");
var payment = SKPayment(product: product)
SKPaymentQueue.defaultQueue().addPayment(payment);
}
func removeTheAds(){
userDefaults.setBool(true, forKey: "proUser")
userDefaults.synchronize()
print("You are a pro User")
//Code for removing the ads currently
}
// Delegate Methods for IAP
func productsRequest (request: SKProductsRequest, didReceiveResponse response: SKProductsResponse) {
println("got the request from Apple")
var count : Int = response.products.count
if (count>0) {
var validProducts = response.products
var validProduct: SKProduct = response.products[0] as SKProduct
if (validProduct.productIdentifier == self.product_id) {
println(validProduct.localizedTitle)
println(validProduct.localizedDescription)
println(validProduct.price)
buyProduct(validProduct);
} else {
println(validProduct.productIdentifier)
}
} else {
println("no product found")
}
}
func request(request: SKRequest!, didFailWithError error: NSError!) {
println("There was an error");
}
func paymentQueue(queue: SKPaymentQueue!, updatedTransactions transactions: [AnyObject]!) {
println("Received Payment Transaction Response from Apple");
for transaction:AnyObject in transactions {
if let trans:SKPaymentTransaction = transaction as? SKPaymentTransaction{
switch trans.transactionState {
case .Purchased:
println("Product Purchased");
SKPaymentQueue.defaultQueue().finishTransaction(transaction as SKPaymentTransaction)
removeTheAds()
break;
case .Failed:
println("Purchased Failed");
SKPaymentQueue.defaultQueue().finishTransaction(transaction as SKPaymentTransaction)
break;
// case .Restored:
default:
SKPaymentQueue.defaultQueue().finishTransaction(transaction as SKPaymentTransaction)
break;
}
}
}
}
我如何处理恢复?我有一个调用这两个按钮的恢复按钮,但我现在该怎么办?
SKPaymentQueue.defaultQueue().addTransactionObserver(self)
SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
我看到那个案例了。Restored 被注释掉了,但我不知道里面有什么。任何帮助将不胜感激。
恢复付款后的操作由您决定。我在我的应用程序中所做的与我在 .Purchased
情况下所做的相同 - 我打开隐藏功能。看起来在您的应用中,您会关闭广告。事实上,在我的应用程序中,.Purchased
和 .Restored
是相同的情况,您也可以这样做。这是我的一些代码:
switch t.transactionState {
case .Purchased, .Restored:
// ... turn on the magic ...
}