2016-02-10 146 views
1

這個問題讓我有好幾天的時間。應用內購買和恢復按鈕:單個產品 - 非消耗品

我有一個簡單的應用程序,顯示橫幅和插頁式廣告。

我使用的是單一視圖的應用程序,有主視圖控制器(ViewController.swift),並建立了另一個視圖控制器(InAppViewController.swift)來處理一個彈出頁面:

  1. 允許用戶作出通過應用內購買來移除所有廣告(AdBanners & InterstitialAds);或
  2. 恢復購買。

當我運行它時,我的代碼沒有錯誤。

應用內購買運行正常,但偶爾我會向iTunes發送兩次登錄請求。

但我的恢復按鈕和關聯的功能似乎是問題。

我已經設置了許多沙箱測試賬戶來測試,而一位沒有購買該應用的新用戶能夠成功恢復購買。這不應該是可能的,所以我在這裏肯定做錯了什麼。

這裏是我的代碼:

主視圖控制器:

// ViewController.swift 

import UIKit 
import MessageUI 
import Social 
import iAd 
import StoreKit 

class ViewController: UIViewController, MFMailComposeViewControllerDelegate, MFMessageComposeViewControllerDelegate, ADBannerViewDelegate, ADInterstitialAdDelegate 
{ 

let defaults = NSUserDefaults.standardUserDefaults() 
var product_id: NSString?; 

override func viewDidLoad() { 
    product_id = "some.product.id"; 
    super.viewDidLoad() 

    //Check if product is purchased 
    if (defaults.boolForKey("purchased")){ 
     print("already purchased") 

     // Hide or show banner ads is purchased/not purchased. 
     // Advertising Banner: 
     self.canDisplayBannerAds = false 
     } 

    else if (!defaults.boolForKey("stonerPurchased")){ 
     print("not yet purchased") 

     // Advertising Banner: 
     self.canDisplayBannerAds = true 
     } 

此代碼似乎很好地工作。當應用加載時,它可以確定誰支付了刪除廣告和未支付廣告的費用,並且適當地顯示了廣告條幅。

它在第二個視圖控制器(InAppPViewController.swift)我有問題。

這裏是我的代碼:

二 - 視圖 - 控制器 - InAppViewController.swift:

// InAppPViewController.swift 

import UIKit 
import StoreKit 
import iAd 

class InAppPViewController: UIViewController, SKProductsRequestDelegate, SKPaymentTransactionObserver { 

let defaults = NSUserDefaults.standardUserDefaults() 
var product_id: NSString?; 


@IBOutlet weak var unlockAction: UIButton! 
@IBOutlet var adBannerView: ADBannerView? 

override func viewDidLoad() { 
    super.viewDidLoad() 

    // Do any additional setup after loading the view. 
    } 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

    @IBAction func restorePurchases(sender: UIButton) { 
    SKPaymentQueue.defaultQueue().addTransactionObserver(self) 
    SKPaymentQueue.defaultQueue().restoreCompletedTransactions() 

} 
    func paymentQueueRestoreCompletedTransactionsFinished(queue: SKPaymentQueue) { 

    print("Transactions Restored") 
    let alert = UIAlertView(title: "Thank You", message: "Your purchase(s) were restored.", delegate: nil, cancelButtonTitle: "OK") 
    alert.show() 
} 


@IBAction func unlockAction(sender: AnyObject) { 

product_id = "some.product.id"; 

    SKPaymentQueue.defaultQueue().addTransactionObserver(self) 

    //Check if product is purchased 

    if (defaults.boolForKey("purchased")){ 

    } 
    else if (!defaults.boolForKey("stonerPurchased")){ 
     print("false")  
    } 


    print("About to fetch the products"); 
    // We check that we are allowed to make the purchase. 

    if (SKPaymentQueue.canMakePayments()) 
    { 
     let productID:NSSet = NSSet(object: self.product_id!); 
     let productsRequest:SKProductsRequest = SKProductsRequest(productIdentifiers: productID as! Set<String>); 
     productsRequest.delegate = self; 
     productsRequest.start(); 
     print("Fething Products"); 
    }else{ 
     print("can't make purchases"); 
    } 

} 

func buyProduct(product: SKProduct){ 
    print("Sending the Payment Request to Apple"); 
    let payment = SKPayment(product: product) 
    SKPaymentQueue.defaultQueue().addPayment(payment); 

} 
//Delegate Methods for IAP 

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

    let count : Int = response.products.count 
    if (count>0) { 

     let validProduct: SKProduct = response.products[0] as SKProduct 
     if (validProduct.productIdentifier == self.product_id) { 
      print(validProduct.localizedTitle) 
      print(validProduct.localizedDescription) 
      print(validProduct.price) 
      buyProduct(validProduct); 
     } else { 
      print(validProduct.productIdentifier) 
     } 
    } else { 
     print("nothing") 
    } 
} 


func request(request: SKRequest, didFailWithError error: NSError) { 
    print("Error Fetching product information"); 
} 

func paymentQueue(queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) { 
    print("Received Payment Transaction Response from Apple"); 

    for transaction:AnyObject in transactions { 
     if let trans:SKPaymentTransaction = transaction as? SKPaymentTransaction{ 
      switch trans.transactionState { 

      case .Purchased: 
       print("Product Purchased"); 
       SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction) 
       defaults.setBool(true , forKey: "purchased") 
       break; 

      case .Failed: 
       print("Purchased Failed"); 
       SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction) 
       break; 

      case .Restored: 
       print("Already Purchased"); 
       SKPaymentQueue.defaultQueue().restoreCompletedTransactions() 


      default: 
       break; 
      } 
     } 
    } 

} 

}

我要去哪裏錯了?

問題:

  1. 是我上面的代碼是否正確?
  2. 我應該修改什麼,爲什麼?

道歉提前,我是新的這個美妙的編碼世界......但愛它的每一分鐘!

+0

不要編輯你的答案到你的問題。發佈它作爲答案。 –

+0

@Daniel Storm但是,如果我不確定我的答案是否正確,並且發佈了我認爲是答案的內容,那麼我會有效地結束我的問題並回答問題?我不會收到什麼可能是更好的答案? –

+2

您可以發佈您的答案並*不接受*,這樣其他用戶仍然可以發佈答案。那麼,如果你接受你的答案,他們也可以發佈信息,但它確實可以發送你找到解決方案的信息。所以,是的:您可以從問題中刪除此問題並將其作爲答案發布。如果有人發佈了一個更好的,你可以隨時刪除你的。 – Moritz

回答

0

我已經修改了我的代碼爲InAppPViewController.swift文件,如下所示:

// InAppPViewController.swift 

import UIKit 
import StoreKit 

class InAppPViewController: UIViewController, SKProductsRequestDelegate, SKPaymentTransactionObserver { 

let defaults = NSUserDefaults.standardUserDefaults() 
var product_id: NSString?; 

override func viewDidLoad() { 
    super.viewDidLoad() 

    // Do any additional setup after loading the view. 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

@IBAction func restorePurchases(sender: UIButton) { 
    // Set up the observer 
    SKPaymentQueue.defaultQueue().addTransactionObserver(self) 


    //Check if user can make payments and then proceed to restore purchase 
    if (SKPaymentQueue.canMakePayments()) { 
     SKPaymentQueue.defaultQueue().restoreCompletedTransactions() 
    } 

} 

@IBAction func unlockAction(sender: AnyObject) { 

    product_id = "some.product.id"; 
    // Adding the observer 
    SKPaymentQueue.defaultQueue().addTransactionObserver(self) 

    //Check if product is purchased 
    if (defaults.boolForKey("purchased")){ 
     print("User already purchased this") 
     // Hide a view or show content depends on your requirement 
    } 

    else if (!defaults.boolForKey("Purchased")){ 
     print("User has not yet pur hased this") 
    } 


    print("About to fetch the products"); 

    // Check if user can make payments and then proceed to make the purchase. 
    if (SKPaymentQueue.canMakePayments()) 
    { 
     let productID:NSSet = NSSet(object: self.product_id!); 
     let productsRequest:SKProductsRequest = SKProductsRequest(productIdentifiers: productID as! Set<String>); 
     productsRequest.delegate = self; 
     productsRequest.start(); 
     print("User can make purchases and will fetch products from Apple Store now"); 
    }else{ 
     print("User can't make purchases"); 
    } 

} 

func buyProduct(product: SKProduct){ 
    print("Sending the Payment Request to Apple"); 
    let payment = SKPayment(product: product) 
    SKPaymentQueue.defaultQueue().addPayment(payment); 

} 

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

    let count : Int = response.products.count 
    if (count>0) { 

     let validProduct: SKProduct = response.products[0] as SKProduct 
     if (validProduct.productIdentifier == self.product_id) { 
      print(validProduct.localizedTitle) 
      print(validProduct.localizedDescription) 
      print(validProduct.price) 
      buyProduct(validProduct); 
     } else { 
      print(validProduct.productIdentifier) 
     } 
    } else { 
     print("nothing") 
    } 
} 


func request(request: SKRequest, didFailWithError error: NSError) { 
    print("Error Fetching product information"); 
} 

// Allowing for all possible outcomes: 
func paymentQueue(queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) { 
    print("Received Payment Transaction Response from Apple"); 

    for transaction:AnyObject in transactions { 
     if let trans:SKPaymentTransaction = transaction as? SKPaymentTransaction { 
      switch trans.transactionState { 

      case .Purchased: 
       print("Product Purchased") 
       let alert = UIAlertView(title: "Thank You", message: "Thank you for your purchase!", delegate: nil, cancelButtonTitle: "OK") 
       alert.show(); 
       SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction) 
       defaults.setBool(true , forKey: "purchased") 
       break; 

      case .Failed: 
       print("Purchased Failed"); 
       SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction) 
       break; 

      case .Restored: 
       print("Already Purchased"); 
       SKPaymentQueue.defaultQueue().restoreCompletedTransactions() 
       break; 

      default: 
       break; 
      } 
     } 
    } 

} 

}

我離開ViewController.swift文件是。

產品的購買似乎現在的工作。

但是關於Restore Purchase,我可以在我的物理設備上運行代碼,但不能測試Restore Purchase功能。

我被之前的所捕獲還原尚未解決且在系統中循環的採購。我無法手動清除我的SKPaymentsQueue。因此,我的代碼拒絕接受新的恢復購買請求。

1
  1. SKPaymentQueue.defaultQueue().addTransactionObserver(self)應該是viewDidLoad()沒有恢復FUNC
  2. 你可以把SKPaymentQueue.defaultQueue().restoreCompletedTransactions()這樣 override func viewWillDisappear(animated: Bool) { SKPaymentQueue.defaultQueue().removeTransactionObserver(self) }
  3. 登錄兩次在沙箱是正常的。

希望我幫助的東西。

相關問題