{"id":3761,"date":"2021-08-14T16:04:35","date_gmt":"2021-08-14T08:04:35","guid":{"rendered":"http:\/\/123.57.164.21\/?p=3761"},"modified":"2021-08-14T16:04:35","modified_gmt":"2021-08-14T08:04:35","slug":"wcsession%e5%9c%a8iphone-%e5%92%8c-watch%e4%b9%8b%e9%97%b4%e4%bc%a0%e9%80%92%e4%bf%a1%e6%81%af","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=3761","title":{"rendered":"WCSession\u5728iPhone \u548c Watch\u4e4b\u95f4\u4f20\u9012\u4fe1\u606f"},"content":{"rendered":"\n<p>WCSession\uff1a<a href=\"https:\/\/developer.apple.com\/documentation\/watchconnectivity\/wcsession\">https:\/\/developer.apple.com\/documentation\/watchconnectivity\/wcsession<\/a><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>WatchConnectivity\u6846\u67b6\u5b9e\u73b0iOS\u5e94\u7528\u7a0b\u5e8f\u4e0e\u5176\u914d\u5bf9\u7684watchOS\u5e94\u7528\u7a0b\u5e8f\u4e4b\u95f4\u7684\u53cc\u5411\u901a\u4fe1\u3002\u63a5\u4e0b\u6765\u51e0\u7bc7\u6211\u4eec\u5c31\u4e00\u8d77\u770b\u4e00\u4e0b\u8fd9\u4e2a\u6846\u67b6\u3002<\/p><\/blockquote>\n\n\n\n<p>WatchConnectivity\u662fwatchOS2\u91cciPhone\u4e0eAppleWatch\u901a\u4fe1\u7684\u57fa\u7840\u6846\u67b6\uff0c\u4e0b\u9762\u5148\u770b\u4e00\u4e0b\u8be5\u6846\u67b6\u7684\u57fa\u672c\u4fe1\u606f\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/08\/\u56fe\u7247-44-1024x421.png\" alt=\"\" class=\"wp-image-3762\" width=\"534\" height=\"219\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/08\/\u56fe\u7247-44-1024x421.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/08\/\u56fe\u7247-44-300x123.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/08\/\u56fe\u7247-44-768x315.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/08\/\u56fe\u7247-44-830x341.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/08\/\u56fe\u7247-44-230x94.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/08\/\u56fe\u7247-44-350x144.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/08\/\u56fe\u7247-44-480x197.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/08\/\u56fe\u7247-44.png 1154w\" sizes=\"(max-width: 534px) 100vw, 534px\" \/><\/figure><\/div>\n\n\n\n<p>\u4f7f\u7528\u6b64\u6846\u67b6\u5728iOS\u5e94\u7528\u7a0b\u5e8f\u548c\u914d\u5bf9\u7684watchOS\u5e94\u7528\u7a0b\u5e8f\u7684WatchKit\u6269\u5c55\u4e4b\u95f4\u4f20\u8f93\u6570\u636e\u3002 \u60a8\u53ef\u4ee5\u4f20\u9012\u5c11\u91cf\u7684\u6570\u636e\u6216\u6574\u4e2a\u6587\u4ef6\u3002 \u60a8\u4e5f\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2a\u6846\u67b6\u89e6\u53d1\u60a8\u7684watchOS\u5e94\u7528\u7a0b\u5e8f\u7684\u590d\u6742\u5316\u66f4\u65b0\u3002<\/p>\n\n\n\n<p>\u4ece\u60a8\u7684\u5e94\u7528\u7a0b\u5e8f\u542f\u52a8\u4f20\u8f93\u540e\uff0c\u7cfb\u7edf\u5c06\u8d1f\u8d23\u4f20\u8f93\u4efb\u4f55\u6570\u636e\u3002 \u5f53\u63a5\u6536\u5e94\u7528\u7a0b\u5e8f\u5904\u4e8e\u975e\u6d3b\u52a8\u72b6\u6001\u65f6\uff0c\u5927\u591a\u6570\u4f20\u8f93\u90fd\u4f1a\u5728\u540e\u53f0\u8fdb\u884c\u3002\u5f53\u5e94\u7528\u7a0b\u5e8f\u88ab\u5524\u9192\u65f6\uff0c\u5b83\u4f1a\u88ab\u901a\u77e5\u4efb\u4f55\u5728inactive\u72b6\u6001\u4e0b\u5230\u8fbe\u7684\u6570\u636e\u3002 \u4e24\u4e2a\u5e94\u7528\u7a0b\u5e8f\u90fd\u5904\u4e8e\u6d3b\u52a8\u72b6\u6001\u65f6\uff0c\u5b9e\u65f6\u901a\u4fe1\u4e5f\u662f\u53ef\u80fd\u7684\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u521d\u59cb\u5316\u8bbe\u7f6e<\/strong><\/h5>\n\n\n\n<p>WatchConnectivity\u7684\u539f\u7406\u662fiPhone\u4f34\u4fa3\u5e94\u7528\u548cAppleWatch\u4e0a\u8fd0\u884c\u7684WatchKit Extension\u4e4b\u95f4\u901a\u8fc7WCSession\u4f1a\u8bdd\u8fdb\u884c\u901a\u4fe1\u3002\u5f53\u524dController\u82e5\u9700\u8981\u54cd\u5e94WatchConnectivity\u7684\u6d88\u606f\u9700\u8981\u9075\u5faaWCSessionDelegate\u534f\u8bae\u3002<\/p>\n\n\n\n<p>\u521d\u59cb\u5316\u65f6\u9700\u8981\u6fc0\u6d3b\u4f1a\u8bdd\u4ee3\u7406\uff08iPhone\u5e94\u7528\u548cWatchKitExtension\u91cc\u90fd\u8981\u6fc0\u6d3b\uff09\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a\u00a0<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">if WCSession.isSupported() { \n\n   let session=WCSession.defaultSession() \n\n   session.delegate=self \n\n   session.activateSession() \n\n}<\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u4f1a\u8bdd\u72b6\u6001<\/strong><\/h5>\n\n\n\n<p>\u4e3a\u4e86\u4e86\u89e3iPhone\u5e94\u7528\u4e0eWatch\u5e94\u7528\u7684\u4f1a\u8bdd\u72b6\u6001\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e9b\u5c5e\u6027\u53bb\u5224\u65ad\uff0c\u6ce8\u610f\u8fd9\u4e9b\u64cd\u4f5c\u53ea\u80fd\u5728iPhone\u5e94\u7528\u91cc\u8fdb\u884c\u3002<\/p>\n\n\n\n<ul><li>\u68c0\u67e5\u662f\u5426\u914d\u5bf9<\/li><\/ul>\n\n\n\n<p>AppleWatch\u662f\u5426\u4e0eiPhone\u914d\u5bf9\uff0c\u53ef\u4ee5\u901a\u8fc7session.paired\u5c5e\u6027\u7684\u5e03\u5c14\u503c\u8fdb\u884c\u5224\u65ad<\/p>\n\n\n\n<ul><li>\u68c0\u67e5watchapp\u662f\u5426\u5df2\u5b89\u88c5<\/li><\/ul>\n\n\n\n<p>\u5373\u4f7f\u5df2\u914d\u5bf9\uff0cwatchapp\u4e5f\u53ef\u80fd\u672a\u5b89\u88c5\u6210\u529f\uff0c\u901a\u8fc7session.watchAppInstalled\u5c5e\u6027\u7684\u5e03\u5c14\u503c\u53ef\u4ee5\u5f97\u77e5\u6b64\u72b6\u6001\u3002\u53e6\u5916\uff0cwatchapp\u5b89\u88c5\u6210\u529f\u5c31\u4f1a\u5728\u624b\u8868\u4e0a\u5efa\u7acb\u76f8\u5e94app\u7684\u76ee\u5f55\uff0c\u56e0\u6b64session.watchAppInstalled==false\u6548\u679c\u7b49\u540c\u4e8ewatchDirectoryURL != nil\u3002<\/p>\n\n\n\n<ul><li>3.\u68c0\u67e5\u662f\u5426\u5f00\u542f\u8868\u76d8\u7ec4\u4ef6\u529f\u80fd<\/li><\/ul>\n\n\n\n<p>\u82e5watchapp\u652f\u6301complication\u8868\u76d8\u7ec4\u4ef6\uff0c\u90a3\u4e48session.complicationEnabled\u503c\u4e3atrue\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u901a\u4fe1<\/strong><\/h5>\n\n\n\n<p>WatchConnectivity\u6846\u67b6\u7684\u901a\u4fe1\u65b9\u5f0f\u6709\u4e24\u79cd\u6a21\u5f0f\uff0c\u4e00\u79cd\u662f<strong>\u540e\u53f0\u4f20\u8f93<\/strong>\uff0c\u53e6\u4e00\u79cd\u662f<strong>\u4ea4\u4e92\u5f0f\u6d88\u606f<\/strong>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u540e\u53f0\u4f20\u8f93\u6a21\u5f0f<\/strong><\/h5>\n\n\n\n<p>\u540e\u53f0\u4f20\u8f93\u6a21\u5f0f\u662f\u6700\u5e38\u7528\u7684\u901a\u4fe1\u6a21\u5f0f\uff0c\u9762\u5411\u5185\u5bb9\u4e0e\u7528\u6237\u4ea4\u4e92\uff0c\u4e3b\u8981\u7528\u4e8e\u4f20\u8f93\u975e\u5373\u65f6\u7684\u5185\u5bb9\uff0c\u4f53\u73b0\u5728\u5185\u5bb9\u53ef\u7531\u64cd\u4f5c\u7cfb\u7edf\u667a\u80fd\u4f20\u8f93\uff08\u64cd\u4f5c\u7cfb\u7edf\u5141\u8bb8\u53d1\u9001\u65b9\u53ef\u9000\u51fa\uff0c\u9009\u62e9\u4f20\u8f93\u65f6\u673a\uff0c\u652f\u6301\u63a5\u6536\u8005\u4e0b\u6b21\u542f\u52a8\u65f6\u53d1\u9001\uff09\uff0c\u5e76\u5c06\u5185\u5bb9\u4ee5\u961f\u5217\u65b9\u5f0f\u53d1\u9001\u3002\u00a0\u540e\u53f0\u4f20\u8f93\u4e00\u822c\u5206\u4e09\u79cd\u7c7b\u578b\uff1a\u00a0<\/p>\n\n\n\n<ul><li><strong>1. Application context\u00a0<\/strong><\/li><\/ul>\n\n\n\n<p>\u53d1\u9001\u65b9\u4ee3\u7801\u793a\u4f8b<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"> do {\n    let context=\/\/\u6700\u65b0\u5185\u5bb9,\u521d\u59cb\u5316context\n    try WCSession.defaultSession().updateApplicationContext(context)\n\n    } catch {\n\n  } <\/pre>\n\n\n\n<p>\u63a5\u6536\u65b9\u9700\u54cd\u5e94\u4ee5\u4e0b\u4ee3\u7406\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">func session(session:WCSession,didReceiveApplicationContext:applicationContext:[String:AnyObject]){} <\/pre>\n\n\n\n<p>ApplicationContext\u4f20\u8f93\u6570\u636e\u5e38\u89c1\u4e8e\u4f20\u8f93\u5355\u4e2a\u5b57\u5178\u7c7b\u578b\u6570\u636e\u7684\u60c5\u51b5\uff0c\u975e\u5e38\u9002\u5408\u4ec5\u9700\u8981\u4fe1\u606f\u5b50\u96c6\u7684AppleWatch\u5e94\u7528\u3002\u8fd9\u8fc7\u7a0b\u4e0d\u4f1a\u5373\u523b\u53d1\u9001\uff0c\u4f46\u4f1a\u5728\u5bf9\u5e94\u7684app\u5524\u9192\u7684\u65f6\u5019\u53d1\u9001\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<ul><li><strong>2. Userinfo transfer\u00a0<\/strong><\/li><\/ul>\n\n\n\n<p>Userinfo\u65b9\u5f0f\u4e0eApplicationContext\u76f8\u6bd4\u80fd\u591f\u4f20\u8f93\u66f4\u590d\u6742\u7684\u6570\u636e\u3002\u00a0<\/p>\n\n\n\n<p>\u53d1\u9001\u65b9\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">let userInfo= \/\/\u5f85\u4f20\u8f93\u7684\u5185\u5bb9\n\nlet userInfoTransfer = WCSession.defaultSession().transferUserInfo(userInfo)\n\nuserInfoTransfer\u4f20\u8f93\u5668\u5c01\u88c5\u4e86\u5f85\u4f20\u6570\u636e\uff0c\u5e76\u4e14\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee3\u7801\u63a7\u5236\u6765\u53d6\u6d88\u4f20\u8f93\u3002 \n\n\n\u672a\u4f20\u8f93\u7684\u5185\u5bb9\u53ef\u4ee5\u8fd9\u6837\u83b7\u53d6\uff1a\n\nlet transfer=WCSession.defaultSession().outstandingUserInfoTransfer(userInfo)\n\n\n\u63a5\u6536\u65b9\u901a\u8fc7\u4ee5\u4e0b\u56de\u8c03\u65b9\u6cd5\u8fdb\u884c\u5904\u7406\uff1a\n\nfunc session(session:WCSession,didReceiveUserInfo userInfo:[String:AnyObject]) {\/\/\u5904\u7406\u63a5\u6536\u7684userInfo}\n\n<\/pre>\n\n\n\n<p>Userinfo transfer\u9002\u5408\u5185\u5b58\u5185\u5bb9\u7684\u4f20\u8f93\uff0c\u5e76\u4e14\u652f\u6301\u8bbf\u95ee\u961f\u5217\u91cc\u7684\u672a\u4f20\u8f93\u5185\u5bb9\u3002<\/p>\n\n\n\n<ul><li><strong>3. File transfer\u00a0<\/strong><\/li><\/ul>\n\n\n\n<p>File transfer\u9762\u5411\u6587\u4ef6\uff0cAPI\u4f7f\u7528\u4e0a\u548cUserinfo transfer\u5f88\u50cf\uff0c\u652f\u6301\u961f\u5217\uff0c\u652f\u6301\u672a\u5b8c\u6210\u5185\u5bb9\u7684\u8bbf\u95ee\uff0c\u9700\u8981\u9644\u52a0\u5143\u6570\u636e\u4fe1\u606f\u3002\u00a0<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\u53d1\u9001\u65b9\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a\n\nlet url = \/\/\u6587\u4ef6\u5730\u5740\nlet metadata = \/\/\u5b57\u5178\u5f62\u5f0f\u5143\u6570\u636e\nlet fileTransfer = WCSession.defaultSession().transferFile(url,metadata:metadata)\n\n\u63a5\u6536\u65b9\u4ee3\u7801\u5982\u4e0b\uff1a\nfunc session(session:WCSession,didReceiveFile file:WCSessionFile) {\/\/\u5904\u7406\u63a5\u6536\u7684File}\n<\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u4ea4\u4e92\u5f0f\u6d88\u606f<\/strong><\/h5>\n\n\n\n<p>\u4ea4\u4e92\u5f0f\u6d88\u606f\u80fd\u591f\u4e3aiPhone\u548cAppleWatch\u95f4\u63d0\u4f9b\u5b9e\u65f6\u7684\u901a\u8baf\u529f\u80fd\u3002<\/p>\n\n\n\n<p>\u4f7f\u7528\u524d\u63d0\uff1a1.\u8bbe\u5907\u95f4\u80fd\u591f\u65e0\u7ebf\u8054\u901a\uff1b2.\u5e94\u7528\u4e4b\u95f4\u80fd\u591f\u8054\u901a\uff0c\u8fd9\u610f\u5473\u7740AppleWatch\u7aef\u7684\u7a0b\u5e8f\u5fc5\u987b\u524d\u53f0\u8fd0\u884c\uff0c\u5373session\u7684reachable\u503c\u4e3atrue\u3002<\/p>\n\n\n\n<p>\u4e0e\u540e\u53f0\u4f20\u8f93\u6a21\u5f0f\u7684\u4e00\u4e2a\u503c\u5f97\u6ce8\u610f\u7684\u533a\u522b\u662f\uff1a<strong>\u82e5iOS\u5e94\u7528\u672a\u542f\u52a8<\/strong>\uff0c<strong>AppleWatch\u4e0a\u8fd0\u884c\u7684WatchKit\u6269\u5c55\u80fd\u591f\u542f\u52a8iOS\u5e94\u7528<\/strong>\uff01<\/p>\n\n\n\n<p>\u4ea4\u4e92\u5f0f\u6d88\u606f\u65b9\u5f0f\u53ef\u4ee5\u4f20\u8f93\u4e24\u79cd\u6570\u636e\u7c7b\u578b\u7684\u6d88\u606f\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">1.\u5b57\u5178\nfunc sendMessage(message:,replyHandler:,errorHandler:)\n\n2.\u6570\u636e \n\u652f\u6301\u53ef\u5e8f\u5217\u5316\u7684\u81ea\u5b9a\u4e49\u6570\u636e\nfunc sendMessageData(data:,replyHandler:,errorHandler:)\n<\/pre>\n\n\n\n<p>\u7528\u4e8e\u6570\u636e\u5373\u65f6\u4f20\u8f93\u5230\u5bf9\u5e94app\u7684\u65b9\u6cd5\u3002\u8c03\u7528\u8fd9\u4e2a\u65b9\u6cd5\u53d1\u9001\u7684\u6570\u636e\u4f1a\u8fdb\u5165\u4e00\u4e2a\u961f\u5217\uff0c\u6309\u7167\u8fdb\u5165\u961f\u5217\u7684\u5148\u540e\u987a\u5e8f\u6765\u53d1\u9001\u51fa\u53bb\u3002\u5982\u679c\u662f\u4ecewatch\u5411iOSapp\u53d1\u9001\u6570\u636e\u5e76\u4e14\u8be5iOSapp\u6ca1\u5728\u8fd0\u884c\u7684\u8bdd\uff0c\u90a3\u4e48\u63a5\u6536\u6570\u636e\u7684iOSapp\u5c06\u4f1a\u5728\u540e\u53f0\u88ab\u5524\u9192\u3002\u5982\u679c\u4f60\u4eceiOSapp\u53d1\u9001\u6570\u636e\u800cwatchapp\u6ca1\u6709\u8fd0\u884c\u7684\u60c5\u51b5\u4e0b\uff0cerrorHandler\u5c31\u4f1a\u88ab\u8c03\u7528\u3002\u63a5\u6536\u6570\u636e\u7684app\u4f1a\u901a\u8fc7WCSession\u7684\u59d4\u6258\u65b9\u6cd5session(_\uff1adidReceiveMessageData\uff1areplyHandler:)\u65b9\u6cd5\u6765\u63a5\u6536\u3002<\/p>\n\n\n\n<p>\u5728\u4ee5\u4e0b\u65b9\u6cd5\u91cc\u8fdb\u884c\u63a5\u6536\u65f6\u7684\u56de\u8c03\u5904\u7406<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">func session(session:WCSession,didReceiveMessage message:[String:AnyObject],replyHandler:([String:AnyObject]) -> Void){\/\/\u6d88\u606f\u5904\u7406\u5e76\u8fd4\u56de}    <\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h5 class=\"wp-block-heading\">Demo<\/h5>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/08\/\u56fe\u7247-45-1024x753.png\" alt=\"\" class=\"wp-image-3771\" width=\"494\" height=\"362\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/08\/\u56fe\u7247-45-1024x753.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/08\/\u56fe\u7247-45-300x221.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/08\/\u56fe\u7247-45-768x565.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/08\/\u56fe\u7247-45-1536x1130.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2021\/08\/\u56fe\u7247-45-830x610.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/08\/\u56fe\u7247-45-230x169.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/08\/\u56fe\u7247-45-350x257.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/08\/\u56fe\u7247-45-480x353.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/08\/\u56fe\u7247-45.png 1656w\" sizes=\"(max-width: 494px) 100vw, 494px\" \/><\/figure><\/div>\n\n\n\n<p><strong>iPhone\u7aef\u4ee3\u7801<\/strong>\uff1a<\/p>\n\n\n\n<ul><li>AppDelegate.swift<\/li><\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\nimport UIKit\nimport WatchConnectivity\n\n@main\nclass AppDelegate: UIResponder, UIApplicationDelegate {\n\n    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {\n\n        if WCSession.isSupported() {\n            WKSession = WCSession.default\n            WKSession?.delegate = self\n            WKSession?.activate()\n        }\n\n        \/\/ Override point for customization after application launch.\n        return true\n    }\n\n    \/\/ MARK: UISceneSession Lifecycle\n\n    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {\n        \/\/ Called when a new scene session is being created.\n        \/\/ Use this method to select a configuration to create the new scene with.\n        return UISceneConfiguration(name: \"Default Configuration\", sessionRole: connectingSceneSession.role)\n    }\n\n    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set&lt;UISceneSession>) {\n        \/\/ Called when the user discards a scene session.\n        \/\/ If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.\n        \/\/ Use this method to release any resources that were specific to the discarded scenes, as they will not return.\n    }\n\n}\n\nextension AppDelegate: WCSessionDelegate {\n\n    func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {\n        if activationState == WCSessionActivationState.activated {\n            print(\"App Session Activated\")\n        } else if activationState == WCSessionActivationState.inactive {\n            print(\"App Session Inactive\")\n        } else if activationState == WCSessionActivationState.notActivated {\n            print(\"App Session Not Activate\")\n        }\n    }\n\n    func sessionDidDeactivate(_ session: WCSession) {\n\n    }\n\n    func sessionDidBecomeInactive(_ session: WCSession) {\n\n    }\n\n    func session(_ session: WCSession, didReceiveMessage message: [String: Any], replyHandler: @escaping ([String: Any]) -> Void) {\n\n        let queue = DispatchQueue.main\n        queue.sync {\n            NotificationCenter.default.post(name: .receiveMsg, object: message, userInfo: nil)\n        }\n\n    }\n\n    func session(_ session: WCSession, didReceiveUserInfo userInfo: [String: Any] = [:]) {\n        let queue = DispatchQueue.main\n        queue.sync {\n            NotificationCenter.default.post(name: .receiveUserInfo, object: userInfo, userInfo: nil)\n        }\n    }\n}\n\nvar WKSession: WCSession?\n\n\n<\/pre>\n\n\n\n<ul><li>ContentView.swift<\/li><\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\n\nimport SwiftUI\n\nstruct ContentView: View {\n\n    @State var textContent: String = \"\"\n    @State var textInput: String = \"\"\n    var isReceiveMsg = NotificationCenter.default.publisher(for: .receiveMsg)\n    var isReceiveUserInfo = NotificationCenter.default.publisher(for: .receiveUserInfo)\n\n    var body: some View {\n\n        VStack {\n            Text((\"from watch:\\(self.textContent)\"))\n                .padding().onReceive(self.isReceiveMsg) { message in\n                if message.object != nil {\n                    let msgDic = message.object as! [String: Any]\n                    self.textContent = msgDic[\"message\"] as! String\n                }\n            }.onReceive(self.isReceiveUserInfo) { message in\n                if message.object != nil {\n                    let msgDic = message.object as! [String: Any]\n                    self.textContent = msgDic[\"message\"] as! String\n                }\n            }\n\n            TextField(\"please input message\", text: self.$textInput)\n\n            Button(action: {\n                var messageDictionary = Dictionary&lt;String, Any>()\n                messageDictionary.updateValue(self.textInput, forKey: \"message\")\n                WKSession!.sendMessage(messageDictionary, replyHandler: { (replayDic: [String: Any]) -> Void in\n                    print(\"reply\")\n                }, errorHandler: { (error) -> Void in\n                        print(error)\n                    })\n            }) {\n                Text(\"sendMessage\")\n            }\n\n\n            Button(action: {\n                var messageDictionary = Dictionary&lt;String, Any>()\n                messageDictionary.updateValue(self.textInput, forKey: \"message\")\n                WKSession?.transferUserInfo(messageDictionary)\n            }) {\n                Text(\"transferUserInfo\")\n            }.padding(.top, 15)\n        }\n    }\n}\n\n\n<\/pre>\n\n\n\n<p><strong>Apple Watch\u7aef\u4ee3\u7801<\/strong>\uff1a<\/p>\n\n\n\n<ul><li>ExtensionDelegate.swift<\/li><\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import WatchKit\nimport WatchConnectivity\n\nclass ExtensionDelegate: NSObject, WKExtensionDelegate {\n\n    func applicationDidFinishLaunching() {\n        if WCSession.isSupported() {\n            WKSession = WCSession.default\n            WKSession?.delegate = self\n            WKSession?.activate()\n        }\n    }\n\n    func applicationDidBecomeActive() {\n        \/\/ Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.\n    }\n\n    func applicationWillResignActive() {\n        \/\/ Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.\n        \/\/ Use this method to pause ongoing tasks, disable timers, etc.\n    }\n\n    func handle(_ backgroundTasks: Set&lt;WKRefreshBackgroundTask>) {\n        \/\/ Sent when the system needs to launch the application in the background to process tasks. Tasks arrive in a set, so loop through and process each one.\n        for task in backgroundTasks {\n            \/\/ Use a switch statement to check the task type\n            switch task {\n            case let backgroundTask as WKApplicationRefreshBackgroundTask:\n                \/\/ Be sure to complete the background task once you\u2019re done.\n                backgroundTask.setTaskCompletedWithSnapshot(false)\n            case let snapshotTask as WKSnapshotRefreshBackgroundTask:\n                \/\/ Snapshot tasks have a unique completion call, make sure to set your expiration date\n                snapshotTask.setTaskCompleted(restoredDefaultState: true, estimatedSnapshotExpiration: Date.distantFuture, userInfo: nil)\n            case let connectivityTask as WKWatchConnectivityRefreshBackgroundTask:\n                \/\/ Be sure to complete the connectivity task once you\u2019re done.\n                connectivityTask.setTaskCompletedWithSnapshot(false)\n            case let urlSessionTask as WKURLSessionRefreshBackgroundTask:\n                \/\/ Be sure to complete the URL session task once you\u2019re done.\n                urlSessionTask.setTaskCompletedWithSnapshot(false)\n            case let relevantShortcutTask as WKRelevantShortcutRefreshBackgroundTask:\n                \/\/ Be sure to complete the relevant-shortcut task once you're done.\n                relevantShortcutTask.setTaskCompletedWithSnapshot(false)\n            case let intentDidRunTask as WKIntentDidRunRefreshBackgroundTask:\n                \/\/ Be sure to complete the intent-did-run task once you're done.\n                intentDidRunTask.setTaskCompletedWithSnapshot(false)\n            default:\n                \/\/ make sure to complete unhandled task types\n                task.setTaskCompletedWithSnapshot(false)\n            }\n        }\n    }\n\n}\n\nextension ExtensionDelegate: WCSessionDelegate {\n    func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {\n        if activationState == WCSessionActivationState.activated {\n            print(\"App Session Activated\")\n        } else if activationState == WCSessionActivationState.inactive {\n            print(\"App Session Inactive\")\n        } else if activationState == WCSessionActivationState.notActivated {\n            print(\"App Session Not Activate\")\n        }\n    }\n\n    func session(_ session: WCSession, didReceiveMessage message: [String: Any], replyHandler: @escaping ([String: Any]) -> Void) {\n        WKInterfaceDevice.current().play(WKHapticType.notification)\n        NotificationCenter.default.post(name: .receiveMsg, object: message, userInfo: nil)\n    }\n\n    func session(_ session: WCSession, didReceiveUserInfo userInfo: [String: Any] = [:]) {\n        let queue = DispatchQueue.main\n        queue.sync {\n            WKInterfaceDevice.current().play(WKHapticType.notification)\n            NotificationCenter.default.post(name: .receiveUserInfo, object: userInfo, userInfo: nil)\n        }\n\n    }\n}\n\nvar WKSession: WCSession?\n\nextension Notification.Name {\n    static var receiveMsg: Notification.Name {\n        Notification.Name(\"receiveMsg\")\n    }\n\n    static var receiveUserInfo: Notification.Name {\n        Notification.Name(\"receiveUserInfo\")\n    }\n\n}\n<\/pre>\n\n\n\n<ul><li>ContentView.swift<\/li><\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\nimport SwiftUI\n\nstruct ContentView: View {\n\n    var isReceiveMsg = NotificationCenter.default.publisher(for: .receiveMsg)\n\n    var isReceiveUserInfo = NotificationCenter.default.publisher(for: .receiveUserInfo)\n\n\n    @State var textContent: String = \"message\"\n    @State var textInput: String = \"\"\n\n    var body: some View {\n        VStack {\n\n\n            Text((\"from iphone:\\(self.textContent)\"))\n                .padding().onReceive(self.isReceiveMsg) { message in\n                if message.object != nil {\n                    let msgDic = message.object as! [String: Any]\n                    self.textContent = msgDic[\"message\"] as! String\n                }\n            }.onReceive(self.isReceiveUserInfo) { message in\n                if message.object != nil {\n                    let msgDic = message.object as! [String: Any]\n                    self.textContent = msgDic[\"message\"] as! String\n                }\n            }\n\n            TextField(\"please input message\", text: self.$textInput)\n\n            Button(action: {\n                var messageDictionary = Dictionary&lt;String, Any>()\n                messageDictionary.updateValue(self.textInput, forKey: \"message\")\n                WKSession!.sendMessage(messageDictionary, replyHandler: { (replayDic: [String: Any]) -> Void in\n                    print(\"reply\")\n                }, errorHandler: { (error) -> Void in\n                        print(error)\n                    })\n            }) {\n                Text(\"Send Message\")\n            }\n\n            Button(action: {\n                var messageDictionary = Dictionary&lt;String, Any>()\n                messageDictionary.updateValue(self.textInput, forKey: \"message\")\n                WKSession?.transferUserInfo(messageDictionary)\n            }) {\n                Text(\"transferUserInfo\")\n            }\n        }\n\n    }\n}\n\nstruct ContentView_Previews: PreviewProvider {\n    static var previews: some View {\n        ContentView()\n    }\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>WCSession\uff1ahttps:\/\/developer.apple.com\/documentation\/wat [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,5],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/3761"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3761"}],"version-history":[{"count":10,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/3761\/revisions"}],"predecessor-version":[{"id":3773,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/3761\/revisions\/3773"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3761"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3761"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3761"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}