2015-10-06 92 views
0

我在Swift中創建了一個應用程序,其中包含應用內購買列表。我想要做的是獲取/檢索「已購買」產品標識符列表。獲取在Swift中恢復應用程序內購買的已購買產品ID列表

func productsRequest(request: SKProductsRequest!, didReceiveResponse response: SKProductsResponse!) { 

} 

例如,在上述方法中,我們可以獲得可用產品的列表。我想獲取已購買的產品ID列表,而不是獲取該列表。然後,我會遍歷該列表並啓用這些產品的所有功能。

有什麼方法可以獲得該列表嗎?或者你可以通過其他方式獲得該列表?

回答

0

你的問題和你的代碼中的信息太少。我建議你檢查一下我剛纔回答的問題:My IAP isn't working. Bugs at func Paymentqueue

它包含了工作中的IAP代碼和一些提示。我希望能幫到你!

以下是完整的IAP代碼:

import UIKit 
import StoreKit 


class GameViewController: UIViewController, ADBannerViewDelegate, SKProductsRequestDelegate, SKPaymentTransactionObserver, GKGameCenterControllerDelegate,GADBannerViewDelegate{ 


    @IBOutlet var outRemoveAds: UIButton! 
    @IBOutlet var outRestorePurchases: UIButton! 


    override func viewDidLoad() { 
     super.viewDidLoad() 

    } 

    override func viewWillAppear(animated: Bool) { 
     super.viewWillAppear(animated) 

     if NSUserDefaults.standardUserDefaults().objectForKey("val") != nil { 
      print("Has a value.") 
      banner.removeFromSuperview() 
      bannerGoogle.removeFromSuperview() 
      outRemoveAds.removeFromSuperview() 
      outRestorePurchases.removeFromSuperview() 
      removeInterFrom = 1 
     } 
     else { 
      print("No Value.") 
     } 

     if(SKPaymentQueue.canMakePayments()){ 
      print("IAP is enabled, loading...") 
      let productID:NSSet = NSSet(objects:"IAP id") 
      let request: SKProductsRequest = SKProductsRequest(productIdentifiers: productID as! Set<String>) 
      request.delegate = self 
      request.start() 
     } 
     else{ 
      print("Please enable IAPS") 

     } 

    } 


    //IAP Ads 

    @IBAction func removeAds(sender: UIButton) { 
     for product in list{ 
      let prodID = product.productIdentifier 
      if (prodID == "IAP id"){ 
       p = product 
       buyProduct() 
       break 
      } 
     } 
    } 
    @IBAction func restorePurchases(sender: UIButton) { 
     SKPaymentQueue.defaultQueue().restoreCompletedTransactions() 
    } 


    //IAP Functions 

    var list = [SKProduct]() 
    var p = SKProduct() 

    func removeAds(){ 
     banner.removeFromSuperview() 
     bannerGoogle.removeFromSuperview() 
     outRemoveAds.removeFromSuperview() 
     outRestorePurchases.removeFromSuperview() 
     let theValue = 10 
     NSUserDefaults.standardUserDefaults().setObject(theValue, forKey: "val") 
     NSUserDefaults.standardUserDefaults().synchronize() 
    } 

    func buyProduct(){ 
     print("Buy: "+p.productIdentifier) 
     let pay = SKPayment (product: p) 
     SKPaymentQueue.defaultQueue().addPayment(pay as SKPayment) 
    } 

    func productsRequest(request: SKProductsRequest, didReceiveResponse response: SKProductsResponse) { 
     print("Product Request") 
     let myProduct = response.products 

     for product in myProduct{ 
      print("Product Added") 
      print(product.productIdentifier) 
      print(product.localizedTitle) 
      print(product.localizedDescription) 
      print(product.price) 

      list.append(product as SKProduct) 
     } 
    } 

    func paymentQueue(queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) { 
     print("Add Payment") 

     for transaction:AnyObject in transactions{ 
      let trans = transaction as! SKPaymentTransaction 
      print(trans.error) 
      switch trans.transactionState{ 
      case .Purchased: 
       print("IAP unlocked") 
       print(p.productIdentifier) 

       let prodID = p.productIdentifier as String 
       switch prodID{ 
       case "IAP id": 
        print("Remove Ads") 
        removeAds() 
       default: 
        print("IAP not setup") 
       } 
       queue.finishTransaction(trans) 
       break 
      case .Failed: 
       print ("Buy error") 
       queue.finishTransaction(trans) 
       break 
      default: 
       print("default: Error") 
       break 
      } 
     } 
    } 

    func paymentQueueRestoreCompletedTransactionsFinished(queue: SKPaymentQueue) { 
     print("Purchases Restored") 

     _ = [] 
     for transaction in queue.transactions { 
      let t: SKPaymentTransaction = transaction as SKPaymentTransaction 

      let prodID = t.payment.productIdentifier as String 
      switch prodID{ 
      case "IAP id": 
       print("Remove Ads") 
       removeAds() 
      default: 
       print("IAP not setup") 
      } 


     } 
    } 
    func finishTransaction(trans:SKPaymentTransaction){ 
     print("Finshed Transaction") 
    } 

    func paymentQueue(queue: SKPaymentQueue, removedTransactions transactions: [SKPaymentTransaction]) { 
     print("Remove Transaction") 
    } 
} 

地說:

SKPaymentQueue.defaultQueue().addTransactionObserver(self) 

viewDidLoadviewDidAppear