{"id":6549,"date":"2024-09-15T21:39:18","date_gmt":"2024-09-15T13:39:18","guid":{"rendered":"https:\/\/t.n-years.com\/?p=6549"},"modified":"2024-09-15T21:39:18","modified_gmt":"2024-09-15T13:39:18","slug":"swiftystorekit-%e4%bb%8b%e7%bb%8d","status":"publish","type":"post","link":"https:\/\/t.n-years.com\/?p=6549","title":{"rendered":"SwiftyStoreKit \u4ecb\u7ecd"},"content":{"rendered":"<p><strong>SwiftyStoreKit<\/strong> \u662f\u4e00\u4e2a\u4e13\u4e3a iOS \u548c macOS \u5f00\u53d1\u7684\u8f7b\u91cf\u7ea7\u5e94\u7528\u5185\u8d2d\u4e70\uff08IAP\uff09\u5e93\uff0c\u65e8\u5728\u7b80\u5316\u4e0e Apple \u7684 <code>StoreKit<\/code> \u6846\u67b6\u7684\u4ea4\u4e92\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u7ec4\u6613\u7528\u7684 API\uff0c\u4f7f\u5f00\u53d1\u8005\u80fd\u591f\u8f7b\u677e\u7ba1\u7406\u5e94\u7528\u5185\u7684\u8d2d\u4e70\u3001\u8ba2\u9605\u3001\u6536\u636e\u9a8c\u8bc1\u4ee5\u53ca\u8d2d\u4e70\u6062\u590d\u64cd\u4f5c\u3002<\/p>\n<h3>\u6838\u5fc3\u529f\u80fd<\/h3>\n<ol>\n<li>\n<p><strong>\u4ea7\u54c1\u4fe1\u606f\u67e5\u8be2<\/strong>\uff1a<\/p>\n<ul>\n<li>\u53ef\u4ee5\u901a\u8fc7 <code>retrieveProductsInfo<\/code> \u65b9\u6cd5\u67e5\u8be2\u5728 App Store Connect \u4e0a\u914d\u7f6e\u7684\u4ea7\u54c1\uff08\u5305\u62ec\u8ba2\u9605\u548c\u4e00\u6b21\u6027\u8d2d\u4e70\uff09\u4fe1\u606f\uff0c\u5982\u4ef7\u683c\u3001\u4ea7\u54c1\u63cf\u8ff0\u7b49\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u8d2d\u4e70\u4ea7\u54c1<\/strong>\uff1a<\/p>\n<ul>\n<li><code>SwiftyStoreKit<\/code> \u63d0\u4f9b\u4e86\u7b80\u5355\u7684 API \u6765\u6267\u884c\u8d2d\u4e70\u6d41\u7a0b\u3002\u5f00\u53d1\u8005\u53ef\u4ee5\u901a\u8fc7 <code>purchaseProduct<\/code> \u65b9\u6cd5\u89e6\u53d1\u8d2d\u4e70\u884c\u4e3a\uff0c\u5904\u7406\u6210\u529f\u6216\u5931\u8d25\u7684\u56de\u8c03\u3002<\/li>\n<li>\u652f\u6301\u6d88\u8017\u578b\u4ea7\u54c1\u3001\u975e\u6d88\u8017\u578b\u4ea7\u54c1\uff08\u5982\u6c38\u4e45\u89e3\u9501\uff09\u4ee5\u53ca\u81ea\u52a8\u7eed\u8ba2\u8ba2\u9605\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u6536\u636e\u9a8c\u8bc1<\/strong>\uff1a<\/p>\n<ul>\n<li>\u652f\u6301\u672c\u5730\u548c\u8fdc\u7a0b\u6536\u636e\u9a8c\u8bc1\u3002<\/li>\n<li>\u53ef\u4ee5\u9009\u62e9\u4f7f\u7528 Apple \u63d0\u4f9b\u7684\u9a8c\u8bc1\u670d\u52a1\u5668\u8fdb\u884c\u8fdc\u7a0b\u6536\u636e\u9a8c\u8bc1\uff0c\u6216\u8005\u5728\u672c\u5730\u89e3\u6790\u6536\u636e\u4ee5\u83b7\u53d6\u8d2d\u4e70\u8be6\u60c5\u3002<\/li>\n<li>\u652f\u6301\u9a8c\u8bc1\u8ba2\u9605\u7684\u6709\u6548\u671f\u4ee5\u53ca\u8fc7\u671f\u65e5\u671f\uff0c\u9002\u7528\u4e8e\u5e94\u7528\u5185\u8ba2\u9605\u7684\u7ba1\u7406\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u6062\u590d\u8d2d\u4e70<\/strong>\uff1a<\/p>\n<ul>\n<li>\u901a\u8fc7 <code>restorePurchases<\/code> \u65b9\u6cd5\uff0c\u53ef\u4ee5\u8ba9\u7528\u6237\u6062\u590d\u4ed6\u4eec\u4e4b\u524d\u7684\u8d2d\u4e70\u8bb0\u5f55\uff0c\u9002\u7528\u4e8e\u975e\u6d88\u8017\u578b\u4ea7\u54c1\u6216\u8ba2\u9605\u6062\u590d\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u8d2d\u4e70\u72b6\u6001\u76d1\u542c<\/strong>\uff1a<\/p>\n<ul>\n<li>\u53ef\u4ee5\u76d1\u542c\u672a\u5b8c\u6210\u7684\u4ea4\u6613\u5e76\u8fdb\u884c\u5904\u7406\uff0c\u786e\u4fdd\u5e94\u7528\u4e0d\u4f1a\u4e22\u5931\u4efb\u4f55\u8d2d\u4e70\u8bb0\u5f55\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u652f\u6301\u8ba2\u9605\u7684\u72b6\u6001\u68c0\u67e5<\/strong>\uff1a<\/p>\n<ul>\n<li>\u652f\u6301\u901a\u8fc7\u6536\u636e\u9a8c\u8bc1\u6765\u68c0\u67e5\u7528\u6237\u8ba2\u9605\u7684\u72b6\u6001\uff0c\u5305\u62ec\u8ba2\u9605\u662f\u5426\u5df2\u7ecf\u5230\u671f\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h3>SwiftyStoreKit \u7684\u7279\u70b9<\/h3>\n<ol>\n<li>\n<p><strong>\u7b80\u6d01\u6613\u7528<\/strong>\uff1a<\/p>\n<ul>\n<li><code>SwiftyStoreKit<\/code> \u8bbe\u8ba1\u5f97\u975e\u5e38\u7b80\u5355\u76f4\u89c2\uff0c\u5b83\u5c01\u88c5\u4e86\u7e41\u7410\u7684 <code>StoreKit<\/code> API\uff0c\u4f7f\u5e94\u7528\u5185\u652f\u4ed8\u6d41\u7a0b\u53d8\u5f97\u66f4\u5bb9\u6613\u7ba1\u7406\u3002\u901a\u8fc7\u7b80\u5355\u7684\u4ee3\u7801\uff0c\u4f60\u53ef\u4ee5\u67e5\u8be2\u4ea7\u54c1\u3001\u53d1\u8d77\u8d2d\u4e70\u3001\u5904\u7406\u4ea4\u6613\uff0c\u4ee5\u53ca\u8fdb\u884c\u6536\u636e\u9a8c\u8bc1\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u652f\u6301 Swift \u8bed\u8a00<\/strong>\uff1a<\/p>\n<ul>\n<li>\u4f5c\u4e3a\u7eaf Swift \u5e93\uff0c<code>SwiftyStoreKit<\/code> \u975e\u5e38\u9002\u5408 Swift \u9879\u76ee\uff0cAPI \u98ce\u683c\u4e5f\u4e0e Swift \u5f00\u53d1\u4e60\u60ef\u76f8\u7b26\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u7075\u6d3b\u7684\u6536\u636e\u9a8c\u8bc1<\/strong>\uff1a<\/p>\n<ul>\n<li>\u65e2\u53ef\u4ee5\u5728\u672c\u5730\u8fdb\u884c\u9a8c\u8bc1\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u8fdc\u7a0b\u7684 Apple \u670d\u52a1\u5668\u8fdb\u884c\u9a8c\u8bc1\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u7075\u6d3b\u5904\u7406\u4ed8\u8d39\u903b\u8f91\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u826f\u597d\u7684\u793e\u533a\u652f\u6301\u548c\u7ef4\u62a4<\/strong>\uff1a<\/p>\n<ul>\n<li><code>SwiftyStoreKit<\/code> \u62e5\u6709\u8f83\u4e3a\u6d3b\u8dc3\u7684 GitHub \u793e\u533a\uff0c\u95ee\u9898\u4fee\u590d\u548c\u65b0\u529f\u80fd\u7684\u5f00\u53d1\u54cd\u5e94\u6bd4\u8f83\u53ca\u65f6\uff0c\u6587\u6863\u4e5f\u76f8\u5bf9\u5b8c\u5584\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h3>\u4f7f\u7528\u573a\u666f<\/h3>\n<ul>\n<li><strong>\u5e94\u7528\u5185\u5355\u6b21\u8d2d\u4e70<\/strong>\uff1a\u9002\u7528\u4e8e\u9500\u552e\u4e00\u6b21\u6027\u865a\u62df\u7269\u54c1\u6216\u6c38\u4e45\u89e3\u9501\u7684\u529f\u80fd\u3002<\/li>\n<li><strong>\u8ba2\u9605\u7ba1\u7406<\/strong>\uff1a\u975e\u5e38\u9002\u5408\u7528\u4e8e\u5904\u7406\u81ea\u52a8\u7eed\u8ba2\u548c\u975e\u81ea\u52a8\u7eed\u8ba2\u7684\u8ba2\u9605\u670d\u52a1\u3002<\/li>\n<li><strong>\u8d2d\u4e70\u6062\u590d<\/strong>\uff1a\u63d0\u4f9b\u6062\u590d\u4e4b\u524d\u8d2d\u4e70\u7684\u529f\u80fd\uff0c\u6bd4\u5982\u7528\u6237\u5728\u66f4\u6362\u8bbe\u5907\u540e\u91cd\u65b0\u4e0b\u8f7d\u5e94\u7528\u3002<\/li>\n<\/ul>\n<h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<pre><code class=\"language-swift\">import SwiftyStoreKit\n\n\/\/ \u67e5\u8be2\u4ea7\u54c1\u4fe1\u606f\nSwiftyStoreKit.retrieveProductsInfo([&quot;com.example.productid&quot;]) { result in\n    for product in result.retrievedProducts {\n        print(&quot;Product: \\(product.localizedDescription), price: \\(product.localizedPrice)&quot;)\n    }\n    for invalidProduct in result.invalidProductIDs {\n        print(&quot;Invalid product identifier: \\(invalidProduct)&quot;)\n    }\n    print(&quot;Error: \\(String(describing: result.error))&quot;)\n}\n\n\/\/ \u8d2d\u4e70\u4ea7\u54c1\nSwiftyStoreKit.purchaseProduct(&quot;com.example.productid&quot;) { result in\n    switch result {\n    case .success(let purchase):\n        print(&quot;Purchase Success: \\(purchase.productId)&quot;)\n    case .error(let error):\n        print(&quot;Purchase Failed: \\(error.localizedDescription)&quot;)\n    }\n}\n\n\/\/ \u6062\u590d\u8d2d\u4e70\nSwiftyStoreKit.restorePurchases { result in\n    for purchase in result.restoredPurchases {\n        print(&quot;Restored purchase: \\(purchase.productId)&quot;)\n    }\n    print(&quot;Restore failed: \\(String(describing: result.error))&quot;)\n}\n\n\/\/ \u672c\u5730\u6536\u636e\u9a8c\u8bc1\nSwiftyStoreKit.verifyReceipt(using: AppleReceiptValidator(service: .production)) { result in\n    switch result {\n    case .success(let receipt):\n        print(&quot;Receipt verified: \\(receipt)&quot;)\n    case .error(let error):\n        print(&quot;Receipt verification failed: \\(error.localizedDescription)&quot;)\n    }\n}<\/code><\/pre>\n<h3>SwiftyStoreKit \u7684\u5c40\u9650\u6027<\/h3>\n<ol>\n<li>\n<p><strong>\u53ea\u652f\u6301 Apple \u5e73\u53f0<\/strong>\uff1a<code>SwiftyStoreKit<\/code> \u53ea\u9002\u7528\u4e8e iOS \u548c macOS\uff0c\u4e0d\u652f\u6301 Android \u5e73\u53f0\u3002\u5982\u679c\u4f60\u7684\u5e94\u7528\u9700\u8981\u8de8\u5e73\u53f0\u652f\u6301\uff0c\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u50cf RevenueCat \u8fd9\u6837\u7684\u670d\u52a1\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u6536\u636e\u9a8c\u8bc1\u9700\u8981\u81ea\u5df1\u5904\u7406<\/strong>\uff1a\u867d\u7136 <code>SwiftyStoreKit<\/code> \u652f\u6301\u6536\u636e\u9a8c\u8bc1\uff0c\u4f46\u4f60\u9700\u8981\u81ea\u5df1\u7f16\u5199\u4ee3\u7801\u8fdb\u884c\u5904\u7406\uff0c\u7279\u522b\u662f\u5f53\u9700\u8981\u8fdb\u884c\u590d\u6742\u7684\u670d\u52a1\u5668\u7aef\u9a8c\u8bc1\u65f6\u3002<\/p>\n<\/li>\n<\/ol>\n<h3>\u7ed3\u8bba<\/h3>\n<p><code>SwiftyStoreKit<\/code> \u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u9002\u5408\u4e0d\u60f3\u624b\u52a8\u5904\u7406\u590d\u6742 <code>StoreKit<\/code> API \u7684\u5f00\u53d1\u8005\u3002\u5bf9\u4e8e\u60f3\u8981\u5feb\u901f\u5b9e\u73b0\u5e94\u7528\u5185\u8d2d\u4e70\u548c\u8ba2\u9605\u529f\u80fd\u7684\u5c0f\u578b\u5e94\u7528\uff0c\u5b83\u63d0\u4f9b\u4e86\u975e\u5e38\u7b80\u6d01\u548c\u76f4\u89c2\u7684\u89e3\u51b3\u65b9\u6848\u3002\u7279\u522b\u662f\u5bf9\u4e8e\u4f7f\u7528 Swift \u7684\u5f00\u53d1\u8005\uff0c\u5b83\u7684\u8bbe\u8ba1\u4f7f\u96c6\u6210\u66f4\u52a0\u987a\u7545\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>SwiftyStoreKit \u662f\u4e00\u4e2a\u4e13\u4e3a iOS \u548c macOS \u5f00\u53d1\u7684\u8f7b\u91cf\u7ea7\u5e94\u7528\u5185\u8d2d\u4e70\uff08IAP\uff09\u5e93\uff0c\u65e8\u5728\u7b80\u5316&#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/t.n-years.com\/?p=6549\">\u9605\u8bfb\u66f4\u591a<i class=\"fa fa-angle-double-right\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[135],"tags":[156],"class_list":["post-6549","post","type-post","status-publish","format-standard","hentry","category-135","tag-storekit"],"_links":{"self":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6549","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6549"}],"version-history":[{"count":1,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6549\/revisions"}],"predecessor-version":[{"id":6550,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6549\/revisions\/6550"}],"wp:attachment":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6549"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6549"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6549"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}