<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>WatchOS &#8211; 隨心所欲</title>
	<atom:link href="https://doeverythingiwant.com/tag/watchos/feed/" rel="self" type="application/rss+xml" />
	<link>https://doeverythingiwant.com</link>
	<description>iOS Developer 的隨筆記錄</description>
	<lastBuildDate>Thu, 04 Jul 2024 15:35:49 +0000</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://doeverythingiwant.com/wp-content/uploads/2025/08/cropped-0802.png</url>
	<title>WatchOS &#8211; 隨心所欲</title>
	<link>https://doeverythingiwant.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>從零開始寫 WatchOS(3) &#8211; Trouble Shootings</title>
		<link>https://doeverythingiwant.com/learning_watchos3_trouble_shootings/</link>
					<comments>https://doeverythingiwant.com/learning_watchos3_trouble_shootings/#respond</comments>
		
		<dc:creator><![CDATA[艾普利]]></dc:creator>
		<pubDate>Sun, 16 Jul 2023 09:24:32 +0000</pubDate>
				<category><![CDATA[寫程式]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[WatchOS]]></category>
		<guid isPermaLink="false">https://doeverythingiwant.com/?p=2072</guid>

					<description><![CDATA[Photo by Raagesh C on Unsplash&#8230;]]></description>
										<content:encoded><![CDATA[
<p class="has-text-align-center">Photo by <a href="https://unsplash.com/@raagesh?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText" target="_blank" rel="noopener">Raagesh C</a> on <a href="https://unsplash.com/wallpapers/iphone/apple-watch?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText" target="_blank" rel="noopener">Unsplash</a></p>



<p>當好不容易把Watch OS App 寫好之後，開開心心按下 Run 然後就發現怎麼跑起來和我想像的不一樣? 就算是用實機也無法順利執行App，如果你也有這個煩惱(?) 這篇把我在開發過程中遇到的問題紀錄下來，也許可以幫助到你</p>



<span id="more-2072"></span>



<h2 class="wp-block-heading">Pair Watch with simulator</h2>



<p>實機的話就比較不會有配對問題，若想要用Simulator 來跑的話，要怎麼樣才能配對Watch ?</p>



<p>首先，請記得下載 Watch Simulator，沒下載就什麼都不用說了</p>



<p>打開Xcode 之後請到 <strong>Window</strong> -> <strong>Devices and Simulators</strong>，選擇想要使用的Simulator在畫面上可以看到 Paired watches 這區，按下 + </p>



<figure class="wp-block-image size-large"><img fetchpriority="high" decoding="async" width="1024" height="686" src="https://doeverythingiwant.com/wp-content/uploads/2023/07/w001-1024x686.png" alt="" class="wp-image-2073" srcset="https://doeverythingiwant.com/wp-content/uploads/2023/07/w001-1024x686.png 1024w, https://doeverythingiwant.com/wp-content/uploads/2023/07/w001-300x201.png 300w, https://doeverythingiwant.com/wp-content/uploads/2023/07/w001-768x514.png 768w, https://doeverythingiwant.com/wp-content/uploads/2023/07/w001-1536x1028.png 1536w, https://doeverythingiwant.com/wp-content/uploads/2023/07/w001-2048x1371.png 2048w, https://doeverythingiwant.com/wp-content/uploads/2023/07/w001-1920x1286.png 1920w, https://doeverythingiwant.com/wp-content/uploads/2023/07/w001-1170x783.png 1170w, https://doeverythingiwant.com/wp-content/uploads/2023/07/w001-585x392.png 585w, https://doeverythingiwant.com/wp-content/uploads/2023/07/w001-263x175.png 263w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p>接著就可以選取想要配對的Watch，要記得自己配對的機種，之後在跑程式的時候記得要跑有配對的Simulator 才行喔</p>



<figure class="wp-block-image size-large"><img decoding="async" width="1024" height="532" src="https://doeverythingiwant.com/wp-content/uploads/2023/07/w002-1024x532.png" alt="" class="wp-image-2074" srcset="https://doeverythingiwant.com/wp-content/uploads/2023/07/w002-1024x532.png 1024w, https://doeverythingiwant.com/wp-content/uploads/2023/07/w002-300x156.png 300w, https://doeverythingiwant.com/wp-content/uploads/2023/07/w002-768x399.png 768w, https://doeverythingiwant.com/wp-content/uploads/2023/07/w002-585x304.png 585w, https://doeverythingiwant.com/wp-content/uploads/2023/07/w002.png 1098w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<h2 class="wp-block-heading">明明沒有出現 Error 訊息，資料卻沒有互相傳遞?</h2>



<p>這時候需要<strong>檢查二個地方</strong></p>



<h3 class="wp-block-heading">First</h3>



<p><strong>iOS App Target </strong>中的 <strong>General</strong> -><strong> Frameworks, Libraries and Embedded Content</strong>，是否有把 Watch OS App 給加進去</p>



<figure class="wp-block-image size-large"><img decoding="async" width="1024" height="179" src="https://doeverythingiwant.com/wp-content/uploads/2023/07/w003-1024x179.png" alt="" class="wp-image-2077" srcset="https://doeverythingiwant.com/wp-content/uploads/2023/07/w003-1024x179.png 1024w, https://doeverythingiwant.com/wp-content/uploads/2023/07/w003-300x52.png 300w, https://doeverythingiwant.com/wp-content/uploads/2023/07/w003-768x134.png 768w, https://doeverythingiwant.com/wp-content/uploads/2023/07/w003-1536x268.png 1536w, https://doeverythingiwant.com/wp-content/uploads/2023/07/w003-1920x336.png 1920w, https://doeverythingiwant.com/wp-content/uploads/2023/07/w003-1170x205.png 1170w, https://doeverythingiwant.com/wp-content/uploads/2023/07/w003-585x102.png 585w, https://doeverythingiwant.com/wp-content/uploads/2023/07/w003.png 1968w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<h3 class="wp-block-heading">Second</h3>



<p>Watch OS Target 中的 Info 裡 <code>WKCompanionAppBundleIdentifier</code> ，這個參數後面要帶上 iOS App 的Bundle id，Watch OS App 是用這個參數在找要連接的 iOS App 所以可不要寫錯了</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="36" src="https://doeverythingiwant.com/wp-content/uploads/2023/07/w004-1024x36.png" alt="" class="wp-image-2078" srcset="https://doeverythingiwant.com/wp-content/uploads/2023/07/w004-1024x36.png 1024w, https://doeverythingiwant.com/wp-content/uploads/2023/07/w004-300x11.png 300w, https://doeverythingiwant.com/wp-content/uploads/2023/07/w004-768x27.png 768w, https://doeverythingiwant.com/wp-content/uploads/2023/07/w004-1170x41.png 1170w, https://doeverythingiwant.com/wp-content/uploads/2023/07/w004-585x21.png 585w, https://doeverythingiwant.com/wp-content/uploads/2023/07/w004.png 1410w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<h2 class="wp-block-heading">打包失敗</h2>



<p>在好不容易跑起來且測試沒有問題時，要打包卻發現打包失敗!?</p>



<p>這時候請檢查 iOS App 與 Watch OS App 的 <strong>Version &amp; Build number 是否有一致</strong>，若沒有一致的話，會打包失敗的</p>



<p></p>



<p>以上皆是我自己遇到的問題，若有誤歡迎告知</p>



<p>最後祝大家 Coding 愉快!!</p>
]]></content:encoded>
					
					<wfw:commentRss>https://doeverythingiwant.com/learning_watchos3_trouble_shootings/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>從零開始寫 WatchOS(2) &#8211; Watch Connectivity</title>
		<link>https://doeverythingiwant.com/learing-watchos2-watch-connectivity/</link>
					<comments>https://doeverythingiwant.com/learing-watchos2-watch-connectivity/#respond</comments>
		
		<dc:creator><![CDATA[艾普利]]></dc:creator>
		<pubDate>Sun, 09 Jul 2023 08:22:30 +0000</pubDate>
				<category><![CDATA[寫程式]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[WatchOS]]></category>
		<guid isPermaLink="false">https://doeverythingiwant.com/?p=2047</guid>

					<description><![CDATA[Photo by Raagesh C on Unsplash&#8230;]]></description>
										<content:encoded><![CDATA[
<p class="has-text-align-center">Photo by <a href="https://unsplash.com/@raagesh?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText" target="_blank" rel="noopener">Raagesh C</a> on <a href="https://unsplash.com/wallpapers/iphone/apple-watch?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText" target="_blank" rel="noopener">Unsplash</a></p>



<p><a href="https://doeverythingiwant.com/learing-watchos2-watch-connectivity/" data-type="link" data-id="https://doeverythingiwant.com/learing-watchos2-watch-connectivity/">上篇</a>利用了 Apple 的官方教學簡單了解要怎麼建立一個 Watch App，不過在教學裡的Watch 資料是可以獨立取得的，在多數的情況下，都是要從 iOS App 中取得資料，需要與 iOS App 溝通才行，而 Watch Connectivity 就是用來與WatchOS 溝通的Framework</p>



<span id="more-2047"></span>



<p>本篇使用 Xcode 14.3 &amp; SwiftUI</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">Watch Connectivity</h2>



<p>它是負責Watch 與 App 之前的溝通，有提供幾種資料傳遞，也有提供背景傳遞方式，本篇內容是利用 Watch Connectivity 中的SendMessage 來進行簡單的資料傳遞，這種方式是必須在手錶不是在背景的情況下才可以進行資料傳送，算是比較即時的資料傳送方式</p>



<p></p>



<p>WatchConnectivity 就如它的名字是負責用來做為 WatchOS App 與 iOS App 之間的溝通，因此在 WatchOS 端與 iOS 端都需要寫相同的function 用來確認連結與傳遞資料，就如圖片所示</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="566" src="https://doeverythingiwant.com/wp-content/uploads/2023/07/截圖-2023-07-02-下午9.11.56-1024x566.png" alt="" class="wp-image-2064" srcset="https://doeverythingiwant.com/wp-content/uploads/2023/07/截圖-2023-07-02-下午9.11.56-1024x566.png 1024w, https://doeverythingiwant.com/wp-content/uploads/2023/07/截圖-2023-07-02-下午9.11.56-300x166.png 300w, https://doeverythingiwant.com/wp-content/uploads/2023/07/截圖-2023-07-02-下午9.11.56-768x424.png 768w, https://doeverythingiwant.com/wp-content/uploads/2023/07/截圖-2023-07-02-下午9.11.56-1536x849.png 1536w, https://doeverythingiwant.com/wp-content/uploads/2023/07/截圖-2023-07-02-下午9.11.56-2048x1132.png 2048w, https://doeverythingiwant.com/wp-content/uploads/2023/07/截圖-2023-07-02-下午9.11.56-1920x1061.png 1920w, https://doeverythingiwant.com/wp-content/uploads/2023/07/截圖-2023-07-02-下午9.11.56-1170x646.png 1170w, https://doeverythingiwant.com/wp-content/uploads/2023/07/截圖-2023-07-02-下午9.11.56-585x323.png 585w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<h2 class="wp-block-heading">WCSection</h2>



<p>WCSession 無論那種傳送方式都是使用這個 Class ，在 iOS App 與 WatchOS App 都要寫上 WCSession，它會進行檢查與傳送工作</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-swift" data-lang="Swift"><code>import WatchConnectivity
if WCSession.isSupported() {
    let session = WCSession.defaultSession()
    session.delegate = self
    session.activateSession()
}</code></pre></div>



<ul class="wp-block-list">
<li>需要先使用 <code>WCSession.isSupported()</code>確認是否有支援互相傳送資料</li>



<li>建立一個Session基本上沒有什麼特別需求的話就直接使用Default 的就好</li>



<li>設定好 Delegate </li>



<li>最後就是 Activate session</li>
</ul>



<h2 class="wp-block-heading">WCSessionDelegate</h2>



<p>這個Delegate 中有用來接收雙方資料的function 也有確認是否連線成功的 function，若使用 SwiftUI 來撰寫，接收會影響UI 更新的參數時，記得要使用 <code>@Published var</code> 來接，不然UI 就不會更新了</p>



<p>下面三個function 在 App 端一定要實作的，Watch 端的話只需要實作第一個，簡單說一下這三個的用途</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-swift" data-lang="Swift"><code>func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
}
    
func sessionDidBecomeInactive(_ session: WCSession) {
}

func sessionDidDeactivate(_ session: WCSession) {
}</code></pre></div>



<ul class="wp-block-list">
<li><code>activationDidCompleteWith</code>，就是當呼叫完 <code>activateSession</code> 之後會觸發</li>



<li><code>sessionDidBecomeInactive</code>，停止連線的時候會觸發</li>



<li><code>sessionDidDeactivate</code>，當所有資料傳送完成且停止連線的時候會觸發</li>
</ul>



<p>本篇中在這三個時機點並沒有要特別處理什麼事情，所以並不會在這三個function 裡寫東西。</p>



<p></p>



<h2 class="wp-block-heading">Send Message</h2>



<p>用 Message 的方式傳送的 Data 是 Dictionary ，Key 可以自定，使用 Dictionary 就可以使用Decodable 的讓它變成 Data Object 這樣之後會比較好處理</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-swift" data-lang="Swift"><code>func sendHiToWatch() {
    guard let wcSection = section, wcSection.isReachable else {
         return
    }
    
    let hiDic = [&quot;Say&quot;: &quot;Hi, Watch&quot;]
    wcSection.sendMessage(hiDic) { result in
        print(&quot;Result : \(result)&quot;)
    }
}</code></pre></div>



<p>在 <code>sendHiToWatch</code> 裡簡單建立一了個 Dictionary ，Key是 &#8220;Say&#8221;，Value是 &#8220;Hi, Watch&#8221;，因為 Message 的傳送方式  Watch App 必需要在前景，因此使用 <code>isReachable</code> 來確認是否可以傳送Message。</p>



<p>再來就是呼叫 <code>sendMessage</code> 這個func 將想要的資料傳送出去</p>



<h2 class="wp-block-heading">Receive Message</h2>



<p>在 <code>WCSessionDelegate</code> 中 <code>didReceiveMessage</code> 就是用來接收，可以寫在App 端與 Watch 端，接收完之後也可以依情況回傳 <code>reply</code> ，<code>reply</code> 的資訊會在 <code>sendMessage</code> 的Result中。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-swift" data-lang="Swift"><code>func session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: @escaping ([String : Any]) -&gt; Void) {
        
     let hiMessage = message[&quot;Say&quot;] as! String
        
     DispatchQueue.main.async {
         self.appMessage = hiMessage
     }
        
     let reply = [&quot;Get&quot;: &quot;Good&quot;]
     replyHandler(reply)
}</code></pre></div>



<p>之所以使用 <code>DispatchQueue.main.async{} </code>是因為 <code>appMessage</code> 這個參數是要用來更新UI 的，更新UI 的參數一定要在main thread 上才行喔。</p>



<h2 class="wp-block-heading">Run</h2>



<p>寫完之後就可以跑跑看啦~ 若是要用Simulator 的話，記得要下載Watch Simulator 並且與 iPhone Simulator 配對，此外還有一些設定要做，不然就會發現App 都可以跑起來但是就是無法傳送資料</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="865" height="1024" src="https://doeverythingiwant.com/wp-content/uploads/2023/07/w005-865x1024.png" alt="" class="wp-image-2075" srcset="https://doeverythingiwant.com/wp-content/uploads/2023/07/w005-865x1024.png 865w, https://doeverythingiwant.com/wp-content/uploads/2023/07/w005-254x300.png 254w, https://doeverythingiwant.com/wp-content/uploads/2023/07/w005-768x909.png 768w, https://doeverythingiwant.com/wp-content/uploads/2023/07/w005-1298x1536.png 1298w, https://doeverythingiwant.com/wp-content/uploads/2023/07/w005-1170x1384.png 1170w, https://doeverythingiwant.com/wp-content/uploads/2023/07/w005-585x692.png 585w, https://doeverythingiwant.com/wp-content/uploads/2023/07/w005.png 1616w" sizes="(max-width: 865px) 100vw, 865px" /></figure>
</div>


<p><a href="https://doeverythingiwant.com/coding/%e5%be%9e%e9%9b%b6%e9%96%8b%e5%a7%8b%e5%af%ab-watchos3-trouble-shootings/" target="_blank" rel="noreferrer noopener">下一篇會整理一下我在這個中間遇到的一些問題，並說明要 Simulator要如何配對</a></p>



<p>最後祝大家 Coding 愉快!!</p>



<h2 class="wp-block-heading">Reference</h2>



<h5 class="wp-block-heading">Video</h5>



<p><a rel="noreferrer noopener" href="https://developer.apple.com/videos/play/wwdc2021/10003/" target="_blank">There and back again: Data transfer on Apple Watch</a></p>



<p><a href="https://developer.apple.com/videos/play/wwdc2015/713/" target="_blank" rel="noreferrer noopener">Introducing Watch Connectivity</a></p>



<h5 class="wp-block-heading">Document</h5>



<p><a rel="noreferrer noopener" href="https://developer.apple.com/documentation/watchconnectivity" target="_blank">Watch Connectivity</a></p>



<p></p>
]]></content:encoded>
					
					<wfw:commentRss>https://doeverythingiwant.com/learing-watchos2-watch-connectivity/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>從零開始寫 WatchOS</title>
		<link>https://doeverythingiwant.com/learning-apple-watch-app/</link>
					<comments>https://doeverythingiwant.com/learning-apple-watch-app/#respond</comments>
		
		<dc:creator><![CDATA[艾普利]]></dc:creator>
		<pubDate>Tue, 04 Apr 2023 15:04:03 +0000</pubDate>
				<category><![CDATA[寫程式]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[WatchOS]]></category>
		<guid isPermaLink="false">https://doeverythingiwant.com/?p=2024</guid>

					<description><![CDATA[Photo by Raagesh C on Unsplash&#8230;]]></description>
										<content:encoded><![CDATA[
<p class="has-text-align-center">Photo by <a href="https://unsplash.com/@raagesh?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText" target="_blank" rel="noopener">Raagesh C</a> on <a href="https://unsplash.com/wallpapers/iphone/apple-watch?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText" target="_blank" rel="noopener">Unsplash</a></p>



<p>此篇主要是我看  Apple <a rel="noreferrer noopener" href="https://developer.apple.com/tutorials/swiftui" target="_blank">SwiftUI&nbsp;Tutorials</a> 中的 <a rel="noreferrer noopener" href="https://developer.apple.com/tutorials/swiftui/creating-a-watchos-app" data-type="URL" data-id="https://developer.apple.com/tutorials/swiftui/creating-a-watchos-app" target="_blank">Creating a watchOS App </a>學習 watchOS 時，因為使用 Xcode 14撰寫有些不同的地方，就想記錄下來，在 Xcode 14 之後建立 watchOS 已經不會出現Extension Folder 了，主因是現在可以建立獨立的watchOS ，不過目前多數 watchOS App 還是屬於輔助 iOS App 的角色</p>



<span id="more-2024"></span>



<p>本篇使用 Xcode 14.2 &amp; SwiftUI</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p>從<a rel="noreferrer noopener" href="https://developer.apple.com/tutorials/swiftui/creating-a-watchos-app" target="_blank">Creating a watchOS App</a> 中下載專用的Project，解壓縮之後可以看到有 &#8220;Complete&#8221; &amp; &#8220;StartingPoint&#8221; Folders，Complete 是已經完成的App，即然是要學習寫 watchOS 當然是開啟 StartingPoint 中的專案，開啟專案之後，會發現這個專案其實是延續先前的教學，若你有<a rel="noreferrer noopener" href="https://developer.apple.com/tutorials/swiftui" target="_blank">SwiftUI&nbsp;Tutorials</a>第一章開始練習的話，應該不陌生。</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">建立 Target</h2>



<p>教學的第一步就是建立 Target ，在開啟 add Target 之後就發現畫面不一樣了，若沒有下載任何watchOS Simulator 的話，Xcode 會提配你要下載，下載Simulator 大概要3G記得確認一下容量。</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="727" src="https://doeverythingiwant.com/wp-content/uploads/2023/04/010-1024x727.png" alt="" class="wp-image-2025" srcset="https://doeverythingiwant.com/wp-content/uploads/2023/04/010-1024x727.png 1024w, https://doeverythingiwant.com/wp-content/uploads/2023/04/010-300x213.png 300w, https://doeverythingiwant.com/wp-content/uploads/2023/04/010-768x545.png 768w, https://doeverythingiwant.com/wp-content/uploads/2023/04/010-1170x830.png 1170w, https://doeverythingiwant.com/wp-content/uploads/2023/04/010-585x415.png 585w, https://doeverythingiwant.com/wp-content/uploads/2023/04/010.png 1460w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p>切到watchOS 的頁籤還是可以看到 App Intents Extension，實際上選它建立出來的就和先前的有87%像，不過少了watchApp這個Target，感覺上怪怪的? 後來看了WWDC的Video 才發現應該要選擇 Application 中的App</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="726" src="https://doeverythingiwant.com/wp-content/uploads/2023/04/011-1024x726.png" alt="" class="wp-image-2027" srcset="https://doeverythingiwant.com/wp-content/uploads/2023/04/011-1024x726.png 1024w, https://doeverythingiwant.com/wp-content/uploads/2023/04/011-300x213.png 300w, https://doeverythingiwant.com/wp-content/uploads/2023/04/011-768x545.png 768w, https://doeverythingiwant.com/wp-content/uploads/2023/04/011-1170x830.png 1170w, https://doeverythingiwant.com/wp-content/uploads/2023/04/011-585x415.png 585w, https://doeverythingiwant.com/wp-content/uploads/2023/04/011.png 1464w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p>按下 Next 之後會發現有幾個選項</p>



<ul class="wp-block-list">
<li>Watch-only App，是建立獨立App 用的</li>



<li>Watch App with New Companion iOS App，選這個會再幫你建立一個 iOS App</li>



<li>Watch App for Existing iOS App，在已存在的App 中建立Watch App </li>
</ul>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="748" height="210" src="https://doeverythingiwant.com/wp-content/uploads/2023/04/012.png" alt="" class="wp-image-2028" srcset="https://doeverythingiwant.com/wp-content/uploads/2023/04/012.png 748w, https://doeverythingiwant.com/wp-content/uploads/2023/04/012-300x84.png 300w, https://doeverythingiwant.com/wp-content/uploads/2023/04/012-585x164.png 585w" sizes="(max-width: 748px) 100vw, 748px" /></figure>



<p>前二個選項應該是給建立新專案時選的，當你直接建立 WatchOS project 的時候，前二個選項使用起來比較合理，若在已經存在的專案加入WatchOS target 前二個選項反而比較不太適用，總之以這個教學App來說就是要選用第三個選項來建立Watch App</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="644" height="198" src="https://doeverythingiwant.com/wp-content/uploads/2023/04/013.png" alt="" class="wp-image-2029" srcset="https://doeverythingiwant.com/wp-content/uploads/2023/04/013.png 644w, https://doeverythingiwant.com/wp-content/uploads/2023/04/013-300x92.png 300w, https://doeverythingiwant.com/wp-content/uploads/2023/04/013-585x180.png 585w" sizes="(max-width: 644px) 100vw, 644px" /></figure>



<p>再來就照著教學指示，輸入名字再按下Next 後 Activate Watch scheme 就可以了。</p>



<p>Supports Running Without iOS App Installation 這個選項可以在Watch App 的 Target 裡找到</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="209" src="https://doeverythingiwant.com/wp-content/uploads/2023/04/014-1024x209.png" alt="" class="wp-image-2030" srcset="https://doeverythingiwant.com/wp-content/uploads/2023/04/014-1024x209.png 1024w, https://doeverythingiwant.com/wp-content/uploads/2023/04/014-300x61.png 300w, https://doeverythingiwant.com/wp-content/uploads/2023/04/014-768x157.png 768w, https://doeverythingiwant.com/wp-content/uploads/2023/04/014-1536x313.png 1536w, https://doeverythingiwant.com/wp-content/uploads/2023/04/014-1920x391.png 1920w, https://doeverythingiwant.com/wp-content/uploads/2023/04/014-1170x239.png 1170w, https://doeverythingiwant.com/wp-content/uploads/2023/04/014-585x119.png 585w, https://doeverythingiwant.com/wp-content/uploads/2023/04/014.png 1952w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<h2 class="wp-block-heading">共享資料</h2>



<p>教學一開始就要求你把Watch 裡的 Assets 刪除，我試過之後發現刪除之後Build 會告知找不到 Watch App Icon，幾次嘗試之後，我決定不把 Watch 的 Assets，直接把icon 圖放上去，在 Xcode 更新之後 App icon 可以只給 1024*1024 的圖，其他尺寸的icon 全都由Xcode 自動處理，說真的這功能很棒且早就應該要這麼做了啊!! icon尺寸真的很多…遇到設計師不是很用心，想請對方出圖的時候就很痛苦，追圖也很痛苦……</p>



<p>另一個就是 <code>LandmarksApp.swift </code>這個檔案可以不用與Watch App 共享，因為Watch App 有自己的入口點了，若命名是照教學取名的話，就是<code>WatchLandmarksApp.swift</code> 這個檔案。記得將原本寫在 <code>LandmarksApp.swift</code> 裡的內容也Copy 到<code>WatchLandmarksApp.swift</code>，主要是這個Watch App 所用的資料並沒有需要從 iOS App 這裡取得，直接Copy 寫法就可以了。</p>



<p>剩下的就可以依照教學選取需要共享的檔案，並在Target Membership 把 WatchLandmarks Watch App 這個打勾</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="524" height="206" src="https://doeverythingiwant.com/wp-content/uploads/2023/04/015.png" alt="" class="wp-image-2032" srcset="https://doeverythingiwant.com/wp-content/uploads/2023/04/015.png 524w, https://doeverythingiwant.com/wp-content/uploads/2023/04/015-300x118.png 300w" sizes="(max-width: 524px) 100vw, 524px" /></figure>



<p></p>



<h2 class="wp-block-heading">建立Detail View &amp;  加上 List</h2>



<p>這二段照著教學做就可以了，基本上沒有什麼需要特別留意的地方，只要記得是直接新增檔案在 Watch App</p>



<p></p>



<h2 class="wp-block-heading">建立 Custom Notification Interface</h2>



<p>先前建立 Watch Extension 的時候會自動建立 <code>NotificationView</code> &amp; <code>NotificationController </code>，但後來就不會自動建立了，所以若是要建立 Custom Notification Interface 的話，上面二個檔案需要自行建立，所以就先到 New -&gt; File 建立這二個檔案之後再照著教學做吧~</p>



<p>當想要Run 起來試試的時候，發現不能測試，這時候需要新增一個 Scheme，並把Watch Interface 選為 <strong>Dynamic Notification</strong>，需要用這個 Scheme 去Run 才可以試 Notification</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="555" src="https://doeverythingiwant.com/wp-content/uploads/2023/04/016-1024x555.png" alt="" class="wp-image-2033" srcset="https://doeverythingiwant.com/wp-content/uploads/2023/04/016-1024x555.png 1024w, https://doeverythingiwant.com/wp-content/uploads/2023/04/016-300x163.png 300w, https://doeverythingiwant.com/wp-content/uploads/2023/04/016-768x416.png 768w, https://doeverythingiwant.com/wp-content/uploads/2023/04/016-1536x832.png 1536w, https://doeverythingiwant.com/wp-content/uploads/2023/04/016-1170x634.png 1170w, https://doeverythingiwant.com/wp-content/uploads/2023/04/016-585x317.png 585w, https://doeverythingiwant.com/wp-content/uploads/2023/04/016.png 1868w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p>另外還要記得 Payload 要加上<code> Simulator Target Bundle</code> 這個 Key，就樣就可以像iPhone 一樣，直接將Payload 文件拖拉到Simulator 上測試啦~</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p>一開始有說到下載的檔案裡有Complete 的 Folder ，若真的不知道要怎麼寫的話，可以從這裡去對照看，我自己是覺得看教學還是要自己親自去打過一遍會比較有印象。</p>



<p>這個教學並沒有教到要如何和 iOS App 做資料傳遞，只是建立簡單的 Watch App，若想要和iOS App 進行資料傳遞是要使用到 Watch Connectivity 這個 Framework，它提供了雙向溝通的方式，之後有機會再寫一篇來實作這個。</p>



<p>最後祝大家 Coding 愉快!!</p>
]]></content:encoded>
					
					<wfw:commentRss>https://doeverythingiwant.com/learning-apple-watch-app/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
