{"id":6658,"date":"2024-12-17T10:06:15","date_gmt":"2024-12-17T02:06:15","guid":{"rendered":"https:\/\/t.n-years.com\/?p=6658"},"modified":"2024-12-17T10:06:15","modified_gmt":"2024-12-17T02:06:15","slug":"iosstorekit-2-%e5%85%b3%e9%94%ae%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/t.n-years.com\/?p=6658","title":{"rendered":"[iOS]StoreKit 2 \u5173\u952e\u529f\u80fd"},"content":{"rendered":"<p><strong>StoreKit 2<\/strong> \u662f Apple \u5728 <strong>iOS 15 \/ macOS 12<\/strong> \u53ca\u66f4\u9ad8\u7248\u672c\u4e2d\u5f15\u5165\u7684\u5168\u65b0\u6846\u67b6\uff0c\u76f8\u8f83\u4e8e\u65e7\u7248 StoreKit\uff0c\u5b83\u63d0\u4f9b\u4e86\u66f4\u73b0\u4ee3\u5316\u3001\u7b80\u6d01\u4e14\u529f\u80fd\u66f4\u5f3a\u5927\u7684 API\uff0c\u6781\u5927\u5730\u7b80\u5316\u4e86\u5e94\u7528\u5185\u652f\u4ed8\uff08IAP\uff09\u548c\u8ba2\u9605\u7684\u5b9e\u73b0\u6d41\u7a0b\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f <strong>StoreKit 2<\/strong> \u7684\u5173\u952e\u529f\u80fd\uff1a<\/p>\n<hr \/>\n<h3>1. <strong>\u57fa\u4e8e Swift \u7684\u73b0\u4ee3\u5316 API<\/strong><\/h3>\n<p>StoreKit 2 \u4f7f\u7528 <strong><code>async\/await<\/code><\/strong> \u5f02\u6b65\u7f16\u7a0b\u6a21\u578b\uff0c\u7b80\u5316\u4e86\u4ee3\u7801\u7ed3\u6784\uff0c\u63d0\u9ad8\u4e86\u53ef\u8bfb\u6027\uff0c\u907f\u514d\u4e86\u7e41\u6742\u7684\u56de\u8c03\u5904\u7406\u3002<br \/>\n\u4f8b\u5982\uff0c\u83b7\u53d6\u5546\u54c1\u5217\u8868\u3001\u53d1\u8d77\u8d2d\u4e70\u90fd\u53ef\u4ee5\u901a\u8fc7\u7b80\u6d01\u7684 <code>async<\/code> \u65b9\u6cd5\u5b9e\u73b0\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a\u83b7\u53d6\u5546\u54c1\u4fe1\u606f<\/strong><\/p>\n<pre><code class=\"language-swift\">let productIds = [&quot;com.app.product1&quot;, &quot;com.app.subscription1&quot;]\ndo {\n    let products = try await Product.products(for: productIds)\n    for product in products {\n        print(&quot;\u5546\u54c1\u540d\u79f0: \\(product.displayName), \u4ef7\u683c: \\(product.price)&quot;)\n    }\n} catch {\n    print(&quot;\u83b7\u53d6\u5546\u54c1\u5931\u8d25: \\(error)&quot;)\n}<\/code><\/pre>\n<hr \/>\n<h3>2. <strong>\u7edf\u4e00\u7684 <code>Product<\/code> \u548c <code>Transaction<\/code> \u7c7b\u578b<\/strong><\/h3>\n<p>StoreKit 2 \u5f15\u5165\u4e86\u66f4\u6e05\u6670\u7684\u6570\u636e\u6a21\u578b\uff0c\u7edf\u4e00\u7ba1\u7406 <strong>\u5546\u54c1<\/strong> \u548c <strong>\u4ea4\u6613<\/strong>\u3002<\/p>\n<ul>\n<li><strong><code>Product<\/code><\/strong>\uff1a\u8868\u793a\u6240\u6709\u7c7b\u578b\u7684\u53ef\u8d2d\u4e70\u5546\u54c1\uff08\u5982\u4e00\u6b21\u6027\u8d2d\u4e70\u548c\u8ba2\u9605\uff09\u3002<\/li>\n<li><strong><code>Transaction<\/code><\/strong>\uff1a\u8868\u793a\u652f\u4ed8\u7ed3\u679c\u548c\u4ea4\u6613\u6570\u636e\uff0c\u5305\u542b\u9a8c\u8bc1\u4fe1\u606f\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a\u89e6\u53d1\u8d2d\u4e70<\/strong><\/p>\n<pre><code class=\"language-swift\">Task {\n    let result = try await product.purchase()\n    switch result {\n    case .success(let verification):\n        if case .verified(let transaction) = verification {\n            print(&quot;\u8d2d\u4e70\u6210\u529f: \\(transaction.productID)&quot;)\n            await transaction.finish()\n        }\n    case .userCancelled:\n        print(&quot;\u7528\u6237\u53d6\u6d88\u8d2d\u4e70&quot;)\n    default:\n        print(&quot;\u8d2d\u4e70\u5931\u8d25\u6216\u5f85\u5904\u7406&quot;)\n    }\n}<\/code><\/pre>\n<hr \/>\n<h3>3. <strong>Transaction \u4e8b\u52a1\u5904\u7406\u548c\u9a8c\u8bc1<\/strong><\/h3>\n<p>StoreKit 2 \u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684 <strong>\u4e8b\u52a1\u9a8c\u8bc1\u673a\u5236<\/strong> \u548c <strong>\u4e8b\u52a1\u76d1\u542c\u529f\u80fd<\/strong>\uff0c\u5e2e\u52a9\u4f60\u786e\u4fdd\u652f\u4ed8\u7684\u5b89\u5168\u6027\u548c\u6b63\u786e\u6027\u3002<\/p>\n<ul>\n<li><strong>\u5f02\u6b65\u76d1\u542c\u672a\u5b8c\u6210\u7684\u4ea4\u6613\uff1a<\/strong><br \/>\n\u4f7f\u7528 <code>Transaction.updates<\/code> \u76d1\u542c\u652f\u4ed8\u72b6\u6001\uff0c\u5305\u62ec\u672a\u5b8c\u6210\u7684\u4ea4\u6613\uff08\u5982\u5d29\u6e83\u6216\u65ad\u7f51\u5bfc\u81f4\uff09\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a\u76d1\u542c\u4ea4\u6613\u66f4\u65b0<\/strong><\/p>\n<pre><code class=\"language-swift\">for await verification in Transaction.updates {\n    if case .verified(let transaction) = verification {\n        print(&quot;\u76d1\u542c\u5230\u65b0\u4ea4\u6613: \\(transaction.productID)&quot;)\n        await transaction.finish()\n    }\n}<\/code><\/pre>\n<ul>\n<li><strong>\u6536\u636e\u9a8c\u8bc1<\/strong>\uff1a<br \/>\n<code>Transaction<\/code> \u672c\u8eab\u652f\u6301\u9a8c\u8bc1\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u672c\u5730\u9a8c\u8bc1\u4ea4\u6613\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7 Apple \u670d\u52a1\u5668\u9a8c\u8bc1\u3002<\/li>\n<\/ul>\n<hr \/>\n<h3>4. <strong>\u8ba2\u9605\u7ba1\u7406\u548c\u5f53\u524d\u6743\u76ca\u67e5\u8be2<\/strong><\/h3>\n<p>StoreKit 2 \u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684\u63a5\u53e3\uff0c\u5e2e\u52a9\u4f60\u83b7\u53d6\u5f53\u524d\u7528\u6237\u7684\u8ba2\u9605\u72b6\u6001\u548c\u6709\u6548\u6743\u76ca\u3002<\/p>\n<ul>\n<li><strong><code>Transaction.currentEntitlements<\/code><\/strong>\uff1a\u8fd4\u56de\u7528\u6237\u5f53\u524d\u7684\u6709\u6548\u8d2d\u4e70\u4fe1\u606f\uff08\u5305\u62ec\u8ba2\u9605\u72b6\u6001\uff09\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a\u67e5\u8be2\u6709\u6548\u8ba2\u9605<\/strong><\/p>\n<pre><code class=\"language-swift\">for await verification in Transaction.currentEntitlements {\n    if case .verified(let transaction) = verification {\n        print(&quot;\u6709\u6548\u8ba2\u9605: \\(transaction.productID)&quot;)\n    }\n}<\/code><\/pre>\n<ul>\n<li><strong><code>Transaction.renewalInfo<\/code><\/strong>\uff1a\u53ef\u4ee5\u83b7\u53d6\u8ba2\u9605\u7eed\u8ba2\u4fe1\u606f\uff0c\u5305\u62ec\u4e0b\u4e00\u4e2a\u7eed\u8d39\u65e5\u671f\u3001\u8ba2\u9605\u662f\u5426\u4f1a\u81ea\u52a8\u7eed\u8ba2\u7b49\u3002<\/li>\n<\/ul>\n<hr \/>\n<h3>5. <strong>\u652f\u6301 App Store Server API \u4e0e\u901a\u77e5<\/strong><\/h3>\n<p>StoreKit 2 \u652f\u6301\u4e0e <strong>App Store Server API<\/strong> \u96c6\u6210\uff0c\u7528\u4e8e\u5904\u7406\u8ba2\u9605\u72b6\u6001\u3001\u9000\u6b3e\u3001\u8fc7\u671f\u63d0\u9192\u7b49\u64cd\u4f5c\uff0c\u786e\u4fdd\u8ba2\u9605\u7ba1\u7406\u66f4\u52a0\u53ef\u9760\u3002<\/p>\n<ul>\n<li>\u5f00\u53d1\u8005\u53ef\u4ee5\u76d1\u542c <strong>App Store Server Notifications<\/strong> \u6765\u83b7\u53d6\u8ba2\u9605\u72b6\u6001\u7684\u5b9e\u65f6\u66f4\u65b0\uff08\u4f8b\u5982\u8ba2\u9605\u7eed\u8ba2\u3001\u53d6\u6d88\u3001\u9000\u6b3e\u7b49\uff09\u3002<\/li>\n<\/ul>\n<hr \/>\n<h3>6. <strong>\u6d4b\u8bd5\u548c\u8c03\u8bd5\u5de5\u5177\u589e\u5f3a<\/strong><\/h3>\n<p>Apple \u4e3a StoreKit 2 \u63d0\u4f9b\u4e86\u66f4\u597d\u7684\u5f00\u53d1\u8005\u5de5\u5177\uff0c\u652f\u6301\u5728 <strong>Xcode<\/strong> \u4e2d\u4f7f\u7528 StoreKit \u6d4b\u8bd5\u73af\u5883\u8fdb\u884c\u8c03\u8bd5\u3002<\/p>\n<ul>\n<li><strong>StoreKit \u914d\u7f6e\u6587\u4ef6<\/strong>\uff1a\u5141\u8bb8\u5f00\u53d1\u8005\u5728\u672c\u5730\u6a21\u62df\u5e94\u7528\u5185\u8d2d\u4e70\u5546\u54c1\u548c\u8ba2\u9605\uff0c\u4fbf\u4e8e\u6d4b\u8bd5\u652f\u4ed8\u6d41\u7a0b\u3002<\/li>\n<li><strong>Xcode StoreKit \u6d4b\u8bd5\u6846\u67b6<\/strong>\uff1a\u65e0\u9700\u5b9e\u9645\u63d0\u4ea4 App\uff0c\u5373\u53ef\u6d4b\u8bd5\u8d2d\u4e70\u903b\u8f91\u3002<\/li>\n<\/ul>\n<hr \/>\n<h3>7. <strong>\u9519\u8bef\u5904\u7406\u548c\u7528\u6237\u53cb\u597d\u63d0\u793a<\/strong><\/h3>\n<p>StoreKit 2 \u63d0\u4f9b\u4e86\u66f4\u6e05\u6670\u7684\u9519\u8bef\u5904\u7406\uff0c\u5141\u8bb8\u4f60\u6839\u636e\u9519\u8bef\u7c7b\u578b\u63d0\u4f9b\u66f4\u5408\u9002\u7684\u7528\u6237\u63d0\u793a\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a\u5904\u7406\u8d2d\u4e70\u9519\u8bef<\/strong><\/p>\n<pre><code class=\"language-swift\">do {\n    let result = try await product.purchase()\n} catch StoreKitError.userCancelled {\n    print(&quot;\u7528\u6237\u53d6\u6d88\u4e86\u652f\u4ed8&quot;)\n} catch {\n    print(&quot;\u8d2d\u4e70\u5931\u8d25: \\(error.localizedDescription)&quot;)\n}<\/code><\/pre>\n<hr \/>\n<h3>8. <strong>\u7b80\u5316\u7684\u9000\u8ba2\u548c\u9000\u6b3e\u6d41\u7a0b<\/strong><\/h3>\n<p>\u7528\u6237\u53ef\u4ee5\u5728 App \u5185\u76f4\u63a5\u901a\u8fc7 <strong>Manage Subscriptions<\/strong> \u63a5\u53e3\u8df3\u8f6c\u5230 App Store \u7ba1\u7406\u8ba2\u9605\u9875\u9762\uff0c\u63d0\u4f9b\u66f4\u53cb\u597d\u7684\u9000\u6b3e\u548c\u7ba1\u7406\u4f53\u9a8c\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a\u6253\u5f00\u8ba2\u9605\u7ba1\u7406\u9875\u9762<\/strong><\/p>\n<pre><code class=\"language-swift\">try? await AppStore.showManageSubscriptions()<\/code><\/pre>\n<hr \/>\n<h3>\u603b\u7ed3<\/h3>\n<p><strong>StoreKit 2<\/strong> \u5e26\u6765\u7684\u5173\u952e\u529f\u80fd\u5305\u62ec\uff1a<\/p>\n<ol>\n<li>\u73b0\u4ee3\u5316\u7684 <strong><code>async\/await<\/code><\/strong> \u5f02\u6b65 API\uff0c\u7b80\u5316\u5f00\u53d1\u6d41\u7a0b\u3002<\/li>\n<li>\u7edf\u4e00\u7684 <strong><code>Product<\/code><\/strong> \u548c <strong><code>Transaction<\/code><\/strong> \u7c7b\u578b\uff0c\u9002\u7528\u4e8e\u6240\u6709\u5546\u54c1\u3002<\/li>\n<li>\u652f\u6301\u4e8b\u52a1\u9a8c\u8bc1\u548c\u76d1\u542c\uff0c\u4fdd\u8bc1\u652f\u4ed8\u5b89\u5168\u3002<\/li>\n<li>\u63d0\u4f9b\u8ba2\u9605\u7ba1\u7406\u548c\u5f53\u524d\u6743\u76ca\u67e5\u8be2\uff0c\u7b80\u5316\u8ba2\u9605\u903b\u8f91\u3002<\/li>\n<li>\u5f3a\u5927\u7684\u6d4b\u8bd5\u5de5\u5177\u548c App Store Server \u96c6\u6210\uff0c\u65b9\u4fbf\u8c03\u8bd5\u548c\u7ba1\u7406\u3002<\/li>\n<\/ol>\n<p>StoreKit 2 \u63d0\u4f9b\u4e86\u529f\u80fd\u66f4\u5f3a\u5927\u4e14\u66f4\u6613\u7528\u7684 API\uff0c\u662f\u672a\u6765\u5e94\u7528\u5185\u652f\u4ed8\u5f00\u53d1\u7684\u63a8\u8350\u65b9\u6848\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>StoreKit 2 \u662f Apple \u5728 iOS 15 \/ macOS 12 \u53ca\u66f4\u9ad8\u7248\u672c\u4e2d\u5f15\u5165\u7684\u5168\u65b0\u6846\u67b6\uff0c\u76f8\u8f83&#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/t.n-years.com\/?p=6658\">\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":[],"class_list":["post-6658","post","type-post","status-publish","format-standard","hentry","category-135"],"_links":{"self":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6658","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=6658"}],"version-history":[{"count":1,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6658\/revisions"}],"predecessor-version":[{"id":6659,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6658\/revisions\/6659"}],"wp:attachment":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6658"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6658"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6658"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}