2017-07-04 108 views
0

因此,我正在實施應用程序內購買到我的應用程序,我正在觀看Jared Davidson的教程如何做到這一點(偉大的教程)但是有一個問題,這是6個月大。我還沒有完成它,但是我在設置警報時遇到了一些錯誤。在'SKError.Code'類型中找不到'Enum case'failed''

extension ViewController { 

     func alertWithTitle(title: String, message : String) -> UIAlertController { 

      let alert = UIAlertController(title: title, message: message, preferredStyle: .alert) 
      alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil)) 
      return alert 


    } 

     func showAlert(alert : UIAlertController) { 
      guard let _ = self.presentedViewController else { 
       self.present(alert, animated: true, completion: nil) 
       return 
      } 


     } 
     func alertForProductRetrievalInfo(result : RetrieveResults) -> UIAlertController { 
      if let product = result.retrievedProducts.first { 
       let priceString = product.localizedPrice! 
       return alertWithTitle(title: product.localizedTitle, message: "\(product.localizedDescription) - \(priceString)") 
      } 
      else if let ivalidProductID = result.invalidProductIDs.first { 
       return alertWithTitle(title: "Could not retrieve product info", message: "Invalid product identifier: \(ivalidProductID)") 
      } 
      else { 
       let errorString = result.error?.localizedDescription ?? "Unknown Error. Please Contact Support" 
       return alertWithTitle(title: "Could not retreive product info", message: errorString) 
      } 
     } 
     func alertForPurchaseResult(result : PurchaseResult) -> UIAlertController { 
      switch result { 
      case .success(let product): 
       print("Purchase Succesful: \(product.productId)") 

       ///////////////////////////////////////////////////////////////////////////////////// 
       return alertWithTitle(title: "Thank you for your kind donation!", message: "Purchase completed") 
      case .error(let error): 
       print("Purchase Failed: \(error)") 
       switch error.code { 
       case .failed(let error): 
        if (error as NSError).domain == SKErrorDomain { 
         return alertWithTitle(title: "Purchase Failed", message: "Check your internet connection or try again later.") 
        } 
        else { 
         return alertWithTitle(title: "Purchase Failed", message: "Unkown Error. Please Contact Support") 
        } 
       case .invalidProductID(let productID): 
        return alertWithTitle(title: "Purchase Failed", message: "\(productID) is not a valid product identifier") 
       case .noProductIdentifier: 
        return alertWithTitle(title: "Purchase Failed", message: "Product not found") 
       case .paymentNotAllowed: 
        return alertWithTitle(title: "Purchase Failed", message: "You are not allowed to make payments") 

       } 

     } 

    } 

這是我得到的錯誤:

「枚舉案‘失敗’型找不到‘SKError.Code’
」枚舉案‘invalidProductID’類型不被發現「SKError 。代碼

「枚舉案‘noProductIdentifier’型找不到‘SKError.Code’

我知道我得到這些錯誤,因爲他們更新它或東西,有新的情況,但我不是S如何將它們轉換成像哪一個去哪?

我很抱歉,我現在真的很困惑!所有幫助非常感謝!

回答

0

最近教程使用的框架(SwiftyStoreKit)changed錯誤檢查API的工作原理。更新應該是:

func alertForPurchaseResult(result : PurchaseResult) -> UIAlertController { 
     switch result { 
     case .success(let product): 
      return alertWithTitle(title: "Thank You", message: "Purchase completed") 
     case .error(let error): 
      switch error.code { 
      case .unknown: return alertWithTitle(title: "Purchase Error", message: "Unknown error. Please contact support") 
      case .clientInvalid: return alertWithTitle(title: "Purchase Error", message: "Not allowed to make the payment") 
      case .paymentCancelled: return alertWithTitle(title: "Payment Cancelled", message: "Payment Cancelled") 
      case .paymentInvalid: return alertWithTitle(title: "Purchase Error", message: "The purchase identifier was invalid") 
      case .paymentNotAllowed: return alertWithTitle(title: "Purchase Error", message: "The device is not allowed to make the payment") 
      case .storeProductNotAvailable: return alertWithTitle(title: "Purchase Error", message: "The product is not available in the current storefront") 
      case .cloudServicePermissionDenied: return alertWithTitle(title: "Purchase Error", message: "Access to cloud service information is not allowed") 
      case .cloudServiceNetworkConnectionFailed: return alertWithTitle(title: "Purchase Error", message: "Could not connect to the network") 
      default: return alertWithTitle(title: "Purchase Error", message: "Unknown error") 
      } 
     } 
    }