2017-07-05 8 views
0

今、アプリストアにアプリがあります。私はそれを遅く更新することを知っていたが、私はそれをする前にそれを置くことを確認したかった。今ではアプリ内購入を実装したいと考えています。しかし、それは動作していない何らかの理由:アプリストア内のアプリをアップデートしてアプリの購入を含むようにする方法

これは、のviewDidLoad()

let productIdentifiers: Set<String> = ["1000Coins"] 

    let productsRequest = SKProductsRequest(productIdentifiers: productIdentifiers) 

    productsRequest.delegate = self 

    productsRequest.start() 

であり、これは他の関数です。何らかの理由で、iTunesが接続して、これが

print("Product: \(product.productIdentifier), \(product.localizedTitle),\(product.price.floatValue)") 

func productsRequest(_ request: SKProductsRequest, didReceive 
response: SKProductsResponse) { 

    print("Loaded Products") 

    for product in response.products { 

     print("Product: \(product.productIdentifier), \(product.localizedTitle),\(product.price.floatValue)") 

    } 

} 

私はアプリの購入に作成、印刷されていないが、何らかの理由でそれが機能していません。助言がありますか??ありがとう!

答えて

0

ViewDidLoadを不必要に乱雑にしないでください。

import StoreKit 
protocol IAPManagerDelegate { 
func managerDidRestorePurchases() 
} 

class IAPManager: NSObject, SKProductsRequestDelegate, SKPaymentTransactionObserver, SKRequestDelegate { 

static let sharedInstance = IAPManager() 

var request:SKProductsRequest! 
var products:NSArray! 

var delegate:IAPManagerDelegate? 

//Load product identifiers for store usage 
func setupInAppPurchases(){ 
    self.validateProductIdentifiers(self.getProductIdentifiersFromMainBundle()) 

    SKPaymentQueue.default().add(self) 
} 

//Get product identifiers 
func getProductIdentifiersFromMainBundle() -> NSArray { 
    var identifiers = NSArray() 
    if let url = Bundle.main.url(forResource: "iap_products", withExtension: "plist"){ 
     identifiers = NSArray(contentsOf: url)! 
    } 

    return identifiers 
} 

//Retrieve product information 
func validateProductIdentifiers(_ identifiers:NSArray) { 
    let productIdentifiers = NSSet(array: identifiers as [AnyObject]) 
    let productRequest = SKProductsRequest(productIdentifiers: productIdentifiers as! Set<String>) 
    self.request = productRequest 
    productRequest.delegate = self 
    productRequest.start() 

} 

func createPaymentRequestForProduct(_ product:SKProduct){ 

    let payment = SKMutablePayment(product: product) 
    payment.quantity = 1 

    SKPaymentQueue.default().add(payment) 

} 

func verifyReceipt(_ transaction:SKPaymentTransaction?){ 
    let receiptURL = Bundle.main.appStoreReceiptURL! 
    if let receipt = try? Data(contentsOf: receiptURL){ 
     //Receipt exists 
     let requestContents = ["receipt-data" : receipt.base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0))] 

     //Perform request 
     do { 
      let requestData = try JSONSerialization.data(withJSONObject: requestContents, options: JSONSerialization.WritingOptions(rawValue: 0)) 

      //Build URL Request 

      //change the storeURL before you submit to the app store 
      let storeURL = URL(string: "https:/sandbox.itunes.apple.com/verifyReceipt") 
      var request = URLRequest(url: storeURL!) 
      request.httpMethod = "Post" 
      request.httpBody = requestData 

      let session = URLSession.shared 
      let task = session.dataTask(with: request , completionHandler: { (responseData, response , error) -> Void in 

       do { 
        let json = try JSONSerialization.jsonObject(with: responseData! as Data, options: .mutableLeaves) as! NSDictionary 

        print(json) 

        if (json.object(forKey: "status") as! NSNumber) == 0 { 
         // 
         let receipt_dict = json["receipt"] as! NSDictionary 

         if let purchases = receipt_dict["in_app"] as? NSArray { 
          self.validatePurchaseArray(purchases) 
         } 

         // you can add more statements to check different parts of the receipt 

         if transaction != nil { 
          SKPaymentQueue.default().finishTransaction(transaction!) 
         } 
         DispatchQueue.main.sync(execute: {() -> Void in self.delegate?.managerDidRestorePurchases() })      } 
        else { 
         //Debug the receipt 
         print(json.object(forKey: "status") as! NSNumber) 
        } 

       } 
       catch { 
        print(error) 
       } 
      }) 


      task.resume() 

     } 
     catch { 
      print(error) 
     } 

    } 
    else { 
     //Receipt does not exist 
     print("No Receipt") 
    } 
} 
func validatePurchaseArray(_ purchases:NSArray){ 

    for purchase in purchases as! [NSDictionary]{ 


     self.unlockPurchasedFunctionalityForProductIdentifier(purchase["product_id"] as! String) 



    } 
} 


func unlockPurchasedFunctionalityForProductIdentifier(_ productIdentifier:String){ 

    ///here you put the functionality that you want to unlock for the user 
    UIApplication.shared.isNetworkActivityIndicatorVisible = false 
} 



func isDateExpired(_ expires_date:Double) -> Bool{ 
    var isExpired:Bool = false 
    let currentDate = (Date().timeIntervalSince1970 * 1000) as TimeInterval 

    if currentDate > expires_date{ 
     isExpired = true 
    } 

    return isExpired 
} 

//MARK: SKProductsRequestDelegate 
func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) { 
    // 
    self.products = response.products as NSArray! 
    print(self.products) 

} 

//MARK: SKPaymentTransactionObserver Delegate Protocol 
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) { 
    // 
    for transaction in transactions as [SKPaymentTransaction]{ 
     switch transaction.transactionState{ 
     case .purchasing: 
      print("Purchasing") 
      UIApplication.shared.isNetworkActivityIndicatorVisible = true 
     case .deferred: 
      print("Deferrred") 
      UIApplication.shared.isNetworkActivityIndicatorVisible = false 
     case .failed: 
      print("Failed") 
      print(transaction.error?.localizedDescription) 
      UIApplication.shared.isNetworkActivityIndicatorVisible = false 
      SKPaymentQueue.default().finishTransaction(transaction) 
     case.purchased: 
      print("Purchased") 
      // 
      self.verifyReceipt(transaction) 
     case .restored: 
      print("Restored") 

     } 
    } 
} 

func restorePurchases(){ 
    let request = SKReceiptRefreshRequest() 
    request.delegate = self 
    request.start() 
} 

func requestDidFinish(_ request: SKRequest) { 
    self.verifyReceipt(nil) 
} 

} 

追加:領収書を確認し、製品識別子を取得するなどのIAPに関連するすべての機能を処理するために、アプリ内購入のヘルパーを作成し、ロンBuencaminoによって書かれたなど

良いヘルパーを復元することはこれですiap_products.plistという名前のplistファイルをXcodeに追加し、製品IDを含めます。前記ファイルには、

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"  "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
<plist version="1.0"> 
<array> 
<string>product1Id</string> 
<string>product2Id</string> 
    so on.... 
</array> 
</plist> 
関連する問題