[Swift通天遁地]四、网络和线程-(15)程序内购功能

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10229880.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

目录:[Swift]通天遁地Swift

本文将演示使用开源类库往项目中添加内购功能。内购功能需要在真机上进行测试。

内购是苹果市场上的一种常见的盈利模式。

首先确保在项目中已经安装了所需的第三方库。

点击【Podfile】,查看安装配置文件。

1 platform :ios, '12.0'
2 use_frameworks!
3 
4 target 'DemoApp' do
5     source 'https://github.com/CocoaPods/Specs.git'
6     pod 'SwiftyStoreKit'
7 end

根据配置文件中的相关配置,安装第三方库。

然后点击打开【DemoApp.xcworkspace】项目文件。

在项目导航区,打开视图控制器的代码文件【ViewController.swift】

现在开始编写代码,实现程序内购的功能。

  1 import UIKit
  2 //在当前的类文件中,引入已经安装的第三方类库。
  3 import SwiftyStoreKit
  4 
  5 class ViewController: UIViewController {
  6     
  7     //添加一个字符串属性,作为内购项目的唯一标识符。
  8     let productId = "com.strengthen.DemoIAPC8"
  9     //添加一个字符串属性,作为在生成内购项目之后,所生成的安全码。
 10     let secretCode = "806a9bd7aa7f46338902a7d81b9cea6b"
 11     
 12     override func viewDidLoad() {
 13         super.viewDidLoad()
 14         // Do any additional setup after loading the view, typically from a nib.
 15         //测试应用程序是否拥有指定的内购项目
 16         getInfo()
 17 
 18         //测试购买一个内购项目
 19         purchase()
 20 
 21         //测试恢复内购
 22         restorePurchases()
 23     }
 24     
 25     //添加一个方法,用来获得当前应用程序的所有内购项目。
 26     func getInfo()
 27     {
 28         //根据内购项目的唯一标识符,
 29         //通过调用第三方类库的获取产品信息的方法,获得该项目详细的信息。
 30         SwiftyStoreKit.retrieveProductsInfo([productId])
 31         { 
 32             result in
 33             //获得结果列表中第一个元素
 34             if let product = result.retrievedProducts.first
 35             {
 36                 //项目的价格
 37                 let priceString = product.localizedPrice!
 38                 //在控制台输出项目的唯一标识符和项目的价格
 39                 print("Available purchases: (product.productIdentifier), price: (priceString)")
 40             }
 41             //处理无法获得项目的情况
 42             else if let invalidProductId = result.invalidProductIDs.first
 43             {
 44                 //在控制台输出错误的提示信息
 45                 print("Could not retrieve product info, Invalid product identifier: (invalidProductId)")
 46             }
 47             else
 48             {
 49                 //处理由于网络请求失败等情况,所造成的内购查询失败的问题
 50                 print("Error: (result.error)")
 51             }
 52         }
 53     }
 54     
 55     //添加另一个方法,用来执行内购功能
 56     func purchase()
 57     {
 58         //通过调用第三方类库的购买产品方法,购买指定唯一标识符的内购项目。
 59         SwiftyStoreKit.purchaseProduct(productId)
 60         { 
 61             result in
 62             //处理服务器返回的结果
 63             switch result
 64             {
 65                 //交易成功
 66                 case .success(let productId):
 67                     //在控制台输出交易成功的信息。
 68                     //此时应该进行一些业务操作,
 69                     //比如解锁某个游戏场景,或者将内购存储到服务器。
 70                     print("Purchase Success: (productId)")
 71                 
 72                 //交易出错
 73                 case .error(let error):
 74                     //如果在交易中出现错误,则在控制台输出交易出错的信息。
 75                     print("Could not retrieve product info: (error)")
 76             }
 77         }
 78     }
 79     
 80     //添加另一个方法,用来恢复内购。
 81     //如果用户之前购买过内购的项目,当用户重新安装应用程序时,
 82     //可以通过此方法,恢复用户之前购买过的项目。
 83     func restorePurchases()
 84     {
 85         //通过调用第三方类库的恢复所有内购的方法,获得所有购买过的项目
 86         SwiftyStoreKit.restorePurchases()
 87         { 
 88             results in
 89             //如果内购恢复失败
 90             if results.restoreFailedProducts.count > 0
 91             {
 92                 //则在控制台输出相应的错误信息
 93                 print("Restore Failed: (results.restoreFailedProducts)")
 94             }
 95             //如果返回的可恢复内购的项目的数量大于0,
 96             else if results.restoredProducts.count > 0
 97             {
 98                 //则在控制台输出相应的日志信息
 99                 print("Restore Success: (results.restoredProducts)")
100                 //对内购项目的列表进行遍历
101                 for product in results.restoredProducts
102                 {
103                     //当内购被成功恢复后,
104                     //应该走和内购交易一样的业务流程。
105                     //比如解锁某个游戏场景,增加应用的金币数量等。
106                     print(product)
107                 }
108             }
109             else
110             {
111                 //处理无需恢复购买的情况
112                 print("Nothing to Restore")
113             }
114         }
115     }
116     
117     //添加另一个方法,用来验证收据信息
118     func verifyReceipt()
119     {
120         //通过调用第三方类库的验证收据方法,验证指定的安全码。
121         SwiftyStoreKit.verifyReceipt(password: secretCode)
122         { 
123             result in
124             //处理验证失败的情况
125             if case .error(let error) = result
126             {
127                 //如果当前没有安全码
128                 if case .noReceiptData = error
129                 {
130                     //则调用刷新收据的方法
131                     self.refreshReceipt()
132                 }
133             }
134         }
135     }
136     
137     //添加另一个方法,用来刷新收据
138     func refreshReceipt()
139     {
140         //通过调用第三方类库的刷新收据的方法
141         SwiftyStoreKit.refreshReceipt
142         {
143             result in
144             //对服务器返回的结果进行遍历
145             switch result
146             {
147                 //验证成功
148                 case .success:
149                     print("Receipt refresh success")
150 
151                 //验证失败
152                 case .error(let error):
153                     print("Receipt refresh failed: (error)")
154             }
155         }
156     }
157     
158     //添加另一个方法,用来对购买进行验证
159     func verifyPurchase()
160     {
161         //通过调用第三方类库的验证收据方法
162         SwiftyStoreKit.verifyReceipt(password: secretCode)
163         {
164             result in
165             //对服务器返回的结果进行遍历
166             switch result
167             {
168                 //验证成功
169                 case .success(let receipt):
170                     //当验证成功时,执行第三方类库的验证购买的方法
171                     let purchaseResult = SwiftyStoreKit.verifyPurchase(
172                         productId: self.productId,
173                         inReceipt: receipt
174                     )
175                     //对服务器返回的购买验证的结果进行遍历
176                     switch purchaseResult
177                     {
178                         //当内购项目被购买过时
179                         case .purchased( _):
180                             //在控制台输出内购信息
181                             print("Product is purchased.")
182                         
183                          //当内购项目未被购买过时
184                         case .notPurchased:
185                             //在控制台输出项目未被购买
186                             print("The user has never purchased this product")
187                     }
188 
189                 //验证失败
190                 case .error(let error):
191                     //在控制台输出验证错误信息
192                     print("Receipt verification failed: (error)")
193             }
194         }
195     }
196     
197     override func didReceiveMemoryWarning() {
198         super.didReceiveMemoryWarning()
199         // Dispose of any resources that can be recreated.
200     }
201 }
原文地址:https://www.cnblogs.com/strengthen/p/10229880.html