<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Alisha Tsai on Medium]]></title>
        <description><![CDATA[Stories by Alisha Tsai on Medium]]></description>
        <link>https://medium.com/@alishatsai9?source=rss-9cf1a62e57ca------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*luTU5yJoghLalYIl6EEWCw.jpeg</url>
            <title>Stories by Alisha Tsai on Medium</title>
            <link>https://medium.com/@alishatsai9?source=rss-9cf1a62e57ca------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 22:59:40 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@alishatsai9/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[#20 西洋棋棋盤的 nested loop 練習]]></title>
            <link>https://alishatsai9.medium.com/20-%E8%A5%BF%E6%B4%8B%E6%A3%8B%E6%A3%8B%E7%9B%A4%E7%9A%84-nested-loop-%E7%B7%B4%E7%BF%92-9f24bc7ccf8c?source=rss-9cf1a62e57ca------2</link>
            <guid isPermaLink="false">https://medium.com/p/9f24bc7ccf8c</guid>
            <category><![CDATA[nested-loop]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Alisha Tsai]]></dc:creator>
            <pubDate>Sun, 12 Feb 2023 05:23:12 GMT</pubDate>
            <atom:updated>2023-02-13T13:16:36.357Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong><em>題目來源</em></strong></h3><p><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84%E8%A9%A6%E7%85%89-%E5%8B%87%E8%80%85%E7%9A%84-100-%E9%81%93-swift-ios-app-%E8%AC%8E%E9%A1%8C/199-%E8%A5%BF%E6%B4%8B%E6%A3%8B%E6%A3%8B%E7%9B%A4%E7%9A%84-nested-loop-%E7%B7%B4%E7%BF%92-59a4092d11ab">#199 西洋棋棋盤的 nested loop 練習</a></p><h3><strong><em>完成作品</em></strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/327/1*XZOfkXpEymMjXmsaBH_Ouw.png" /></figure><h3>程式內容</h3><p>這次先練習基本題</p><p>1.設定底層的contentView</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/755/1*S9Cg1WsQitCwzGkpOd0IGw.png" /></figure><p>2.運用nested loop製作chessboard</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pz7uJfPIyfr9dztyBkRldA.png" /></figure><p>3.自訂顏色並且使用extension擴充UIColor</p><p><a href="https://alishatsai9.medium.com/%E8%87%AA%E8%A8%82color-set%E4%B8%A6%E4%B8%94extension-uicolor%E7%9A%84%E6%96%B9%E5%BC%8F-9c489cdb749c">自訂Color Set並且extension UIColor的方式</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/468/1*eQMpO3eMQRAxb5rWLGB8wg.png" /></figure><p>4.擺放旗子</p><p>將每個旗子設定1~32的編號</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/487/1*7aFxSM6wbBut7EIC2v2e-g.png" /></figure><p>運用if判斷擺放旗子</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Aq_ljloj0W7g0UE3ydvYVg.png" /></figure><p><a href="https://github.com/alishatsai/chess">GitHub - alishatsai/chess</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9f24bc7ccf8c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[自訂Color Set並且extension UIColor的方式]]></title>
            <link>https://alishatsai9.medium.com/%E8%87%AA%E8%A8%82color-set%E4%B8%A6%E4%B8%94extension-uicolor%E7%9A%84%E6%96%B9%E5%BC%8F-9c489cdb749c?source=rss-9cf1a62e57ca------2</link>
            <guid isPermaLink="false">https://medium.com/p/9c489cdb749c</guid>
            <category><![CDATA[extension]]></category>
            <category><![CDATA[color-set]]></category>
            <category><![CDATA[uicolor]]></category>
            <dc:creator><![CDATA[Alisha Tsai]]></dc:creator>
            <pubDate>Sun, 12 Feb 2023 00:59:03 GMT</pubDate>
            <atom:updated>2023-02-12T00:59:03.095Z</atom:updated>
            <content:encoded><![CDATA[<ol><li>在Assets對著空白處按右鍵新增New Color Set</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IDZ32-jHtj7y9fvpiM_CTg.png" /></figure><p>2.調整自訂顏色內容</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5BkeN2AZD7pkXIcp7t7dcQ.png" /></figure><p>3.重新命名自訂顏色</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/974/1*KZ5DdlmrWj1vKIjoDzOumg.png" /></figure><p>4.擴充extension UIColor</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/868/1*WkKNVEP-Jr2x-pfEZYHRtg.png" /></figure><p>綠色方塊的添加方式</p><p>點選xcode右上方的+號</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/244/1*-9WGGeXU6fEm60N4vKAXgg.png" /></figure><p>便可以看到先前在assets自訂的顏色</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*b2IXk62BuSpf3zQu0ycYTg.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9c489cdb749c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[#19. FB Login-使用cocoapods安裝套件]]></title>
            <link>https://medium.com/peter-pan-swift-ios-class/19-fb-login-%E4%BD%BF%E7%94%A8cocoapods%E5%AE%89%E8%A3%9D%E5%A5%97%E4%BB%B6-c4a6de81f23f?source=rss-9cf1a62e57ca------2</link>
            <guid isPermaLink="false">https://medium.com/p/c4a6de81f23f</guid>
            <category><![CDATA[fb-login]]></category>
            <dc:creator><![CDATA[Alisha Tsai]]></dc:creator>
            <pubDate>Sun, 09 Jan 2022 14:39:58 GMT</pubDate>
            <atom:updated>2022-01-10T14:15:21.535Z</atom:updated>
            <content:encoded><![CDATA[<h3>先在FB 開發網站建立對應的App</h3><p>可參考Peter大這篇</p><p><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E5%95%8F%E9%A1%8C%E8%A7%A3%E7%AD%94%E9%9B%86/%E5%AF%A6%E4%BD%9C-ios-app-%E7%9A%84-facebook-%E7%99%BB%E5%85%A5%E5%8A%9F%E8%83%BD-96c981d482cc">實作 iOS App 的 Facebook 登入功能</a></p><h3>設定開發環境</h3><p>可照著QuickStart的步驟一步一步進行</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/899/1*JMSd5-7arAhqcrMvmlh77g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/883/1*-8IK4zMYDSpbznqTV8_RzA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/824/1*ShTn5IEM_VcWWgGihdymIA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/656/1*x3k6zaWQsrKNcWyKkete0g.png" /></figure><p>這邊我結合自己實際上遇到的問題，做個紀錄</p><ol><li>安裝之前先更新Ruby</li></ol><pre><strong>為何是Ruby?</strong></pre><pre>CocoaPods 是基於 Ruby 語言開發而成，每台Mac都有自帶 Ruby 系統。若你本身熟悉 Ruby 語言，那麼 CocoaPods 的 pods 就近乎於 Ruby 的 gems。</pre><pre>-以上擷取至<a href="https://www.appcoda.com.tw/cocoapods/">https://www.appcoda.com.tw/cocoapods/</a></pre><p>打開terminal終端機輸入以下字串</p><pre>sudo gem update --system</pre><p>再輸入一次測試，就顯示已經更新到最新版</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/699/1*GIKoSut9jPA2i8IT8j1s8Q.png" /></figure><p>2. 開始安裝cocoapods</p><pre>sudo gem install cocoapods</pre><p>出現以下畫面就安裝成功了！</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/745/1*9mXFmqN1DTKde8LpzYrtnQ.png" /></figure><p>3.檢查cocoapods安裝版本</p><pre>pod --version</pre><p>可到開發者的gitHub比對是否已安裝到最新版本</p><p><a href="https://github.com/CocoaPods/CocoaPods">https://github.com/CocoaPods/CocoaPods</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/699/1*C-JcDbeFxVi0RCqD6ei8KQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*25CoOwbMzuw_qu1fZk9DOQ.png" /></figure><p>4.打開terminal，輸入專案所在(也可以直接拖曳資料夾)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/736/1*rygFw3nOAe2-fdERiBsIuQ.gif" /></figure><p>5.建立Podfile</p><pre>pod init</pre><p>此時資料夾會多一個Podfile記事本檔案</p><p>6.在Podfile記事本輸入以下字串，大小寫要完全一樣，最後請記得存檔</p><pre>pod &#39;FBSDKLoginKit&#39;<br>pod &#39;FacebookLogin&#39;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/574/1*imiZiJNu43mSRZfouGejLw.png" /></figure><p>7.回到terminal安裝套件</p><pre>pod install</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/585/1*SEw-n1vM5X55dFQrcsy5fQ.png" /></figure><p>安裝之後，專案資料夾會多出以下檔案</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/365/1*krk9S_CJEdih8b9RW6sIsg.png" /></figure><p>8.先關閉原本已經打開的專案，改開啟.xcworkspace，點開之後會發現FB套件已經安裝成功</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/255/1*Sgi9B2OAQUAP0lg9r4tyLw.png" /></figure><p>9.此時就可以開心import FB套件啦！</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/436/1*6k53BRGkU5itUGJ9e8T2Uw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/436/1*zmUi1W7tQPqwfgJHFEuCEA.png" /></figure><p>回到FB iOS SDK官方文件，繼續步驟二</p><h3>填入Bundle Identifier</h3><p>在general中可看到Bundle Identifier，將整串直接複製貼上到官方文件的步驟二，記得save</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/656/1*0aIIsr7e_kw66ZpfrzZdgQ.png" /><figcaption>copy</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/766/1*wyI8z8dHxJKj5Y9hZ-LGTg.png" /><figcaption>paste</figcaption></figure><h3>設定info.plist</h3><p>1.info.plist按右鍵選擇Source Code</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/875/1*AyS1rrhKygZQ2R8xv0RtmQ.png" /></figure><p>2.複製文件中的兩組程式碼</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/775/1*M5AbOcnrtRgPih-94bqWvw.png" /></figure><p>3.轉貼到info.plist中的&lt;dict&gt;…&lt;/dict&gt;</p><p>記得替換正確資料到<strong>&lt;string&gt;</strong>APP-ID<strong>&lt;/string&gt;、&lt;string&gt;</strong>CLIENT-TOKEN<strong>&lt;/string&gt;、&lt;string&gt;</strong>APP-NAME<strong>&lt;/string&gt;</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/796/1*L3KBSOP5y3Hstlj1kmn5Kg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/796/1*YYyn3TwkFR1CaSubPm8Jgw.png" /></figure><p>APP-ID</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*W2TjXH07ESu2uMCbuqXjIA.png" /></figure><p>CLIENT-TOKEN</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VcWEpzZ8Abrhutizqu-Lig.png" /></figure><p>APP-NAME</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DUNAeCYdqzQt7dz0-OadKA.png" /></figure><h3>連結App Delegate</h3><p>第一組程式複製之後全部取代專案中的AppDelegate程式碼，若是iOS 13的版本，就需要再額外添加第二組程式碼到SceneDelegate</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/820/1*iFemZ0aNM3whteu0098A7A.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/820/1*vaz1ypAdLSNy4uW2UASDZg.png" /><figcaption>藍色區段是新增的程式碼</figcaption></figure><h3>設計UI</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/252/1*8kLTjaX_B1R9n5shzFfPsg.png" /></figure><pre>override func viewDidLoad() {<br>super.viewDidLoad()<br>  loginConfig()<br>  logoutConfig()<br>  profileImageView.image = UIImage(systemName: &quot;person.crop.square&quot;)<br>}</pre><pre>func loginConfig() {<br>  var configuration = UIButton.Configuration.filled()<br>  configuration.title = &quot;Facebook Login&quot;<br>  configuration.buttonSize = .large<br>  configuration.titleAlignment = .center<br>  configuration.cornerStyle = .capsule<br>  configuration.image = UIImage(systemName: &quot;person.fill&quot;,   withConfiguration: UIImage.SymbolConfiguration(scale: .large))<br>  configuration.imagePlacement = .trailing<br>  configuration.imagePadding = 15<br>  loginBtn.configuration = configuration<br>  loginBtn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 20)<br>}</pre><pre>func logoutConfig() {<br>  var configuration = UIButton.Configuration.gray()<br>  configuration.title = &quot;Log Out&quot;<br>  configuration.buttonSize = .large<br>  configuration.cornerStyle = .capsule<br>  logoutBtn.configuration = configuration<br>  logoutBtn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 20)<br>}</pre><h3>設定loginManager</h3><p>permissions：傳入想要的權限<br>viewController：呈現資料的controller頁面<br>closure{}：若沒有取消也沒出現錯誤，就會直接跳到getFacebookData的程式</p><pre>func login() {<br>  let loginManager = LoginManager()</pre><pre>  loginManager.logIn(permissions: [&quot;public_profile&quot;,&quot;email&quot;], viewController: self) { (result) in<br>    switch result {<br>    case .cancelled:<br>      print(&quot;使用者取消登入&quot;)<br>    case .failed(let error):<br>      print(error.localizedDescription)<br>    case .success(granted: _, declined: _, token: _):<br>      self.getFacebookData()<br>    }<br>  }<br>}</pre><pre>@IBAction func fbLogin(_ sender: UIButton) {<br>  self.login()<br>}</pre><h3>登入成功後取得FB資料</h3><p>若登入成功，便可透過 FB的Graph API取得個人資料<br>詳細的Graph API說明可參考下方連結</p><p><a href="https://developers.facebook.com/docs/graph-api/overview?locale=en_US">Overview - Graph API - Documentation - Facebook for Developers</a></p><p>graphPath: 傳入想要取得的node類型，比如個人頁面、文章、照片…，輸入”me”可以取得已登入者的個人頁面</p><p>The /me node is a special endpoint that translates to the object ID of the person or Page whose access token is currently being used to make the API calls.</p><p>parameters: node的下一層是fields，fields包含個人頁面的詳細資料</p><p>connection:接到Graph API的連線</p><p>result:由GraphRequest得到的資料，是以JSON格式寫成</p><p>將result轉型成字典後，再抓出name/email/de每個key的value，接著運用URLSession下載profile photo</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/865/1*nkLaqJBusQgNy9qF6icrzg.png" /><figcaption>print(dict)印出的畫面</figcaption></figure><pre>func getFacebookData() {</pre><pre>  if AccessToken.current != nil {<br>    <strong>GraphRequest(graphPath: &quot;me&quot;, parameters: [&quot;fields&quot;:&quot;id,email,name,picture.type(large)&quot;]).start { connection, result, error</strong> in</pre><pre>      if error == nil {<br>        let dict = result as! [String:AnyObject] as <strong>NSDictionary</strong><br>        let id = dict.object(forKey: &quot;id&quot;) as! String<br>        let name = dict.object(forKey: &quot;name&quot;) as! String<br>        let email = dict.object(forKey: &quot;email&quot;) as! String</pre><pre>        print(&quot;Name: \(name)&quot;)<br>        print(&quot;Email: \(email)&quot;)<br>        print(&quot;Id: \(id)&quot;)<br>        <strong>print(dict)</strong><br>        <br>        self.nameLabel.text = name<br>        self.emailLabel.text = email<br>        self.idLabel.text = id</pre><pre>        if let profilePicture = dict.object(forKey: &quot;picture&quot;) as? [String: Any] {<br>          if let profilePicData = profilePicture[&quot;data&quot;] as? [String: Any] {<br>           print(&quot;Data: \(profilePicData)&quot;)<br>           if let profilePicStr = profilePicData[&quot;url&quot;] as? String {<br>              let url = URL(string: profilePicStr)!<br>              print(url)</pre><pre>              <strong>URLSession.shared.dataTask(with: url)</strong> { data, response, error in<br>                print(&quot;enter URLSession&quot;)<br>                if let data = data,<br>                   let image = UIImage(data: data) {<br>                     DispatchQueue.main.async {<br>                       print(&quot;enter main queue&quot;)<br>                       self.profileImageView.image = image<br>                     }<br>                 }<br>              }<strong>.resume()</strong><br>            }<br>          }<br>        }<br>        self.logoutBtn.isHidden = false<br>        self.loginBtn.isHidden = true<br>     } else {<br>        print(error?.localizedDescription as Any)<br>     }<br>   }<br>} else {<br>  print(&quot;Access Token is NIL&quot;)<br>}</pre><h3>登出</h3><pre>@IBAction func fbLogout(_ sender: UIButton) {<br>  let logout = LoginManager()<br>  logout.logOut()<br>  loginBtn.isHidden = false<br>  logoutBtn.isHidden = true<br>  profileImageView.image = UIImage(systemName: &quot;person.crop.square&quot;)<br>  nameLabel.text = &quot;&quot;<br>  emailLabel.text = &quot;&quot;<br>  idLabel.text = &quot;&quot;<br>}</pre><h3>模擬畫面</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/254/1*mIykpN0oOIPx2gmWOAlvyQ.gif" /></figure><h3>遇到問題</h3><p>運行模擬器之後，過了很久出現以下錯誤訊息</p><pre><strong>&lt;Warning&gt;: Please set a value for FacebookAutoLogAppEventsEnabled. Set the flag to TRUE if you want to collect app install, app launch and in-app purchase events automatically. To request user consent before collecting data, set the flag value to FALSE, then change to TRUE once user consent is received. Learn more: https://developers.facebook.com/docs/app-events/getting-started-app-events-ios#disable-auto-events.</strong></pre><pre><strong>&lt;Warning&gt;: You haven&#39;t set a value for FacebookAdvertiserIDCollectionEnabled. Set the flag to TRUE if you want to collect Advertiser ID for better advertising and analytics results. To request user consent before collecting data, set the flag value to FALSE, then change to TRUE once user consent is received. Learn more: https://developers.facebook.com/docs/app-events/getting-started-app-events-ios#disable-auto-events.</strong></pre><p><strong>解法參考</strong></p><p><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E6%95%99%E5%AE%A4/warning-please-set-a-value-for-facebookautologappeventsenabled-3c1163916d66">“&lt;Warning&gt;: Please set a value for FacebookAutoLogAppEventsEnabled.</a></p><p>所以只要將下方程式碼再貼到info.plist的Source Code中即可</p><pre>&lt;key&gt;FacebookAutoLogAppEventsEnabled&lt;/key&gt;<br>&lt;string&gt;TRUE&lt;/string&gt;<br>&lt;key&gt;FacebookAdvertiserIDCollectionEnabled&lt;/key&gt;<br>&lt;string&gt;TRUE&lt;/string&gt;</pre><p>一陣子沒摸程式，整個思路就會頓頓的，要天天碰才行啊…😨</p><h3>參考資料</h3><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FCCzA59p-LuA%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DCCzA59p-LuA&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FCCzA59p-LuA%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/56db1edb828ba2008e9a5f86da14c096/href">https://medium.com/media/56db1edb828ba2008e9a5f86da14c096/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c4a6de81f23f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/peter-pan-swift-ios-class/19-fb-login-%E4%BD%BF%E7%94%A8cocoapods%E5%AE%89%E8%A3%9D%E5%A5%97%E4%BB%B6-c4a6de81f23f">#19. FB Login-使用cocoapods安裝套件</a> was originally published in <a href="https://medium.com/peter-pan-swift-ios-class">彼得潘的 Swift iOS / Flutter App 開發教室</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[iOS]麻古茶坊MACU 訂飲料APP-Part3.自動切換廣告橫幅/UISegmentedControl in TableViewCell]]></title>
            <link>https://medium.com/peter-pan-swift-ios-class/ios-%E9%BA%BB%E5%8F%A4%E8%8C%B6%E5%9D%8Amacu-%E8%A8%82%E9%A3%B2%E6%96%99app-part3-%E8%87%AA%E5%8B%95%E5%88%87%E6%8F%9B%E5%BB%A3%E5%91%8A%E6%A9%AB%E5%B9%85-uisegmentedcontrol-in-tableviewcell-c3065ea79faf?source=rss-9cf1a62e57ca------2</link>
            <guid isPermaLink="false">https://medium.com/p/c3065ea79faf</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[uisegmentedcontrol]]></category>
            <dc:creator><![CDATA[Alisha Tsai]]></dc:creator>
            <pubDate>Tue, 19 Oct 2021 11:33:13 GMT</pubDate>
            <atom:updated>2021-10-20T23:36:09.171Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*v1U9R_Vli8nZURfSJ_CX8g.gif" /></figure><h3>本篇重點</h3><ol><li>自動切換廣告橫幅</li><li>UISegmentedControl in TableViewCell</li></ol><h3>1.自動切換廣告橫幅</h3><blockquote>Banner Image Array</blockquote><p>上方的廣告橫幅其實是用collectionView製作，每個item代表一張廣告，所以先設定廣告圖片的array，用append搭配迴圈就可以快速做出image array了！</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/78a05da12159bf2497cb773a4a9ed2e0/href">https://medium.com/media/78a05da12159bf2497cb773a4a9ed2e0/href</a></iframe><blockquote>設定Delegate、DataSource</blockquote><p>使用extension的方式，讓MainViewController遵從UICollectionViewDelegate、UICollectionViewDataSource，item數量就是看有幾張廣告，將廣告array生成的UIImage派給cell的imageView，還有一定要記得設定cell ID</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c0f6ec0b54026dcdc2299515561777eb/href">https://medium.com/media/c0f6ec0b54026dcdc2299515561777eb/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/278/1*TWpTvWXkFAweqMF9g34WUg.png" /><figcaption>記得設定cell id</figcaption></figure><blockquote>UICollectionViewDelegateFlowLayout</blockquote><p>設定cell的尺寸跟collectionView一樣大，並且將cell的最小行距設成０，每個廣告之間才不會有空白。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1bc1c5b7158dcb014ca9f2cb8a0ad7a8/href">https://medium.com/media/1bc1c5b7158dcb014ca9f2cb8a0ad7a8/href</a></iframe><blockquote>開始切換廣告</blockquote><p>設定切換速度是2.5秒，並且無限repeat。<br>呼叫collectionView的scrollToItem，可以讓他捲動到我們所需的cell，centeredHorizontally則是水平滑動。<br>最後將startTimer()放在viewDidLoad就可以一進入畫面就啟動廣告切換了！</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a95164bc6c6d4b8d4932912779f1eb33/href">https://medium.com/media/a95164bc6c6d4b8d4932912779f1eb33/href</a></iframe><h3>2. UISegmentedControl in TableViewCell</h3><p>由於我希望使用者點飲料的時候，可以懶懶的就點好一杯，所以選擇用UISegmentedControl，方便使用者還沒點選之前就有預設值，只是要如何在Dynamic Cell的情況下設定呢…上網找了好久終於找到了😀，原來是要出動Protocol</p><blockquote>設定Protocol</blockquote><p>在TableViewCell中寫入自訂Protocol，並先寫好function的名稱，內容在這邊還不會寫到，只是先定義名稱。</p><pre>protocol SugarTableViewCellDelegate: AnyObject {</pre><pre>    func toggleSugarSegmentedCtrl(with index: Int)</pre><pre>}</pre><blockquote>設定變數delegate並遵從protocol</blockquote><p>設定變數delegate，並且要讓它遵從稍早設定的protocol協議。<br>讓變數index等於segmentedControl選定的index。<br>接著拉出IBAction讓delegate可以呼叫切換segmentedControl的function</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/061aa12916d544cd909bdf704d432f3f/href">https://medium.com/media/061aa12916d544cd909bdf704d432f3f/href</a></iframe><blockquote>設定function的內容</blockquote><p>接著就可以來到訂單畫面，讓OrderViewController遵從Protocol，並且開始設定function內容</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5805ed150569e729de3f81f0702dac06/href">https://medium.com/media/5805ed150569e729de3f81f0702dac06/href</a></iframe><p>但是…寫好以上這些，發現還是不能動…原來是少了最重要的一步！</p><blockquote><strong>cell的 delegate</strong></blockquote><p>一定要記得在cellForRowAt寫入以下程式，不然前功盡棄啊!<br>這邊的self代表的是OrderViewController 或是 EditViewController</p><pre><strong>cell.delegate = self</strong></pre><p>終於完成訂飲料App了！整個過程讓我費盡心思，明明是看起來這麼簡單的頁面啊…<br>然後我的進度真的非常緩慢🐌，但也只好一步一步前進了！希望之後的開發速度可以快一些👀</p><p>最後～該來一杯麻古犒賞自己一下啦！🧋</p><h3>GitHub</h3><p><a href="https://github.com/alishatsai/TeaApp">GitHub - alishatsai/TeaApp</a></p><h3>參考資料</h3><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FChjXkkqog5k%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DChjXkkqog5k&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FChjXkkqog5k%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/f934610cb6806ca51b5e4ad79868a26b/href">https://medium.com/media/f934610cb6806ca51b5e4ad79868a26b/href</a></iframe><p><a href="https://stackoverflow.com/questions/39947076/uitableviewcell-buttons-with-action">UITableViewCell Buttons with action</a></p><h3>其他篇</h3><ul><li><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E6%95%99%E5%AE%A4/%E9%BA%BB%E5%8F%A4%E8%8C%B6%E5%9D%8Amacu-%E8%A8%82%E9%A3%B2%E6%96%99app-part1-airtable%E4%B8%B2%E6%8E%A5-%E4%B8%8B%E8%BC%89-%E4%B8%8A%E5%82%B3-9de592c4bdfb">[iOS]麻古茶坊MACU 訂飲料APP-Part1. Airtable串接/下載/上傳</a></li><li><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E6%95%99%E5%AE%A4/ios-%E9%BA%BB%E5%8F%A4%E8%8C%B6%E5%9D%8Amacu-%E8%A8%82%E9%A3%B2%E6%96%99app-part2-%E5%88%AA%E9%99%A4-%E4%BF%AE%E6%94%B9-70ce8e9b54d8">[iOS]麻古茶坊MACU 訂飲料APP-Part2.刪除/修改</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c3065ea79faf" width="1" height="1" alt=""><hr><p><a href="https://medium.com/peter-pan-swift-ios-class/ios-%E9%BA%BB%E5%8F%A4%E8%8C%B6%E5%9D%8Amacu-%E8%A8%82%E9%A3%B2%E6%96%99app-part3-%E8%87%AA%E5%8B%95%E5%88%87%E6%8F%9B%E5%BB%A3%E5%91%8A%E6%A9%AB%E5%B9%85-uisegmentedcontrol-in-tableviewcell-c3065ea79faf">[iOS]麻古茶坊MACU 訂飲料APP-Part3.自動切換廣告橫幅/UISegmentedControl in TableViewCell</a> was originally published in <a href="https://medium.com/peter-pan-swift-ios-class">彼得潘的 Swift iOS / Flutter App 開發教室</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[iOS]麻古茶坊MACU 訂飲料APP-Part2.刪除/修改]]></title>
            <link>https://medium.com/peter-pan-swift-ios-class/ios-%E9%BA%BB%E5%8F%A4%E8%8C%B6%E5%9D%8Amacu-%E8%A8%82%E9%A3%B2%E6%96%99app-part2-%E5%88%AA%E9%99%A4-%E4%BF%AE%E6%94%B9-70ce8e9b54d8?source=rss-9cf1a62e57ca------2</link>
            <guid isPermaLink="false">https://medium.com/p/70ce8e9b54d8</guid>
            <category><![CDATA[airtable]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Alisha Tsai]]></dc:creator>
            <pubDate>Sat, 16 Oct 2021 14:55:17 GMT</pubDate>
            <atom:updated>2021-10-19T11:44:41.329Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*39ybMy9R01IQ-KQ97iUtEA.png" /></figure><h3>本篇介紹重點</h3><ol><li>刪除(DELETE)-Order</li><li>修改(PATCH)-Order</li></ol><h3>1.刪除(DELETE)</h3><blockquote>MenuController-deleteOrder</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GB9M4bqtDqkUS2JHgJ3BSQ.png" /></figure><p>參考上圖文件說明，request的httpMethod要寫”DELETE”，但是要特別注意url的結尾要加上id，不然是無法刪除訂單的。</p><p>由於delete不像下載(GET)和上傳(POST)需要對資料做解碼/編碼，所以使用Result Type的時候，成功的一方就儲存String就好。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4ed0d91c6f78fc88e30e39a11374b5b5/href">https://medium.com/media/4ed0d91c6f78fc88e30e39a11374b5b5/href</a></iframe><blockquote>刪除訂單</blockquote><p>將Cell往左滑可顯示刪除，會先詢問是否確定刪除，確認之後就印出該筆被刪除的訂單資料，接著移除orderList aray中該筆資料，表格也要deleteRow，並且再次更新總計以及Tab訂單數，tableView也要reloadData。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6df7865fd514346ea6a6e4dbd0388c37/href">https://medium.com/media/6df7865fd514346ea6a6e4dbd0388c37/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JGNe8l-_TycpTCsdZ7b98Q.gif" /></figure><h3>2.修改(PATCH)</h3><blockquote>UpdateOrderResponse</blockquote><p>我另寫一組UpdateOrderResponse，只抓出需要上傳的資料。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/468466808f1afc9b801279256bbbe4ba/href">https://medium.com/media/468466808f1afc9b801279256bbbe4ba/href</a></iframe><blockquote>MenuController-updateOrder</blockquote><p>更新訂單可以選擇put或是patch，但我這邊只會修正部分資料，所以我選擇Patch。</p><p>Put會把原本在airtable上的資料清空，再上傳現有資料。但是我的putOrderResponse並沒有完整對應airtable上的資料，所以不適合我使用。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Vso9MG_rwAtAt7r-KUtc5A.png" /></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9a00ec20c1f60d124437b9179b225aab/href">https://medium.com/media/9a00ec20c1f60d124437b9179b225aab/href</a></iframe><blockquote>修改訂單</blockquote><p>點擊Cell就可以跳到修正頁面，修改頁面會紀錄原本的訂單內容，可以部分修改後再送出訂單。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/efaa0d2a306fb73cef9bca3539a1cb7a/href">https://medium.com/media/efaa0d2a306fb73cef9bca3539a1cb7a/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tWB5gkbu9Nig0EtTTVFAdw.gif" /></figure><blockquote>遇到的問題－無法更新訂單</blockquote><p>更新訂單的時候一直出現以下錯誤</p><pre>{&quot;error&quot;:{&quot;type&quot;:&quot;INVALID_REQUEST_MISSING_FIELDS&quot;,&quot;message&quot;:&quot;Could not find field \&quot;fields\&quot; in the request body&quot;}}</pre><p>詢問Peter之後，後來發現是我的struct少了id，Peter建議可以印出request的httpBody檢查上傳的Json內容是否正確。</p><pre>print(String(data: request.httpBody!, encoding: .utf8)!)</pre><h3>其他篇</h3><ul><li><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E6%95%99%E5%AE%A4/%E9%BA%BB%E5%8F%A4%E8%8C%B6%E5%9D%8Amacu-%E8%A8%82%E9%A3%B2%E6%96%99app-part1-airtable%E4%B8%B2%E6%8E%A5-%E4%B8%8B%E8%BC%89-%E4%B8%8A%E5%82%B3-9de592c4bdfb">[iOS]麻古茶坊MACU 訂飲料APP-Part1. Airtable串接/下載/上傳</a></li><li><a href="https://alishatsai9.medium.com/ios-%E9%BA%BB%E5%8F%A4%E8%8C%B6%E5%9D%8Amacu-%E8%A8%82%E9%A3%B2%E6%96%99app-part3-%E8%87%AA%E5%8B%95%E5%88%87%E6%8F%9B%E5%BB%A3%E5%91%8A%E6%A9%AB%E5%B9%85-uisegmentedcontrol-in-tableviewcell-c3065ea79faf">[iOS]麻古茶坊MACU 訂飲料APP-Part3.自動切換廣告橫幅/UISegmentedControl in TableViewCell</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=70ce8e9b54d8" width="1" height="1" alt=""><hr><p><a href="https://medium.com/peter-pan-swift-ios-class/ios-%E9%BA%BB%E5%8F%A4%E8%8C%B6%E5%9D%8Amacu-%E8%A8%82%E9%A3%B2%E6%96%99app-part2-%E5%88%AA%E9%99%A4-%E4%BF%AE%E6%94%B9-70ce8e9b54d8">[iOS]麻古茶坊MACU 訂飲料APP-Part2.刪除/修改</a> was originally published in <a href="https://medium.com/peter-pan-swift-ios-class">彼得潘的 Swift iOS / Flutter App 開發教室</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[iOS]麻古茶坊MACU 訂飲料APP-Part1. Airtable串接/下載/上傳]]></title>
            <link>https://medium.com/peter-pan-swift-ios-class/%E9%BA%BB%E5%8F%A4%E8%8C%B6%E5%9D%8Amacu-%E8%A8%82%E9%A3%B2%E6%96%99app-part1-airtable%E4%B8%B2%E6%8E%A5-%E4%B8%8B%E8%BC%89-%E4%B8%8A%E5%82%B3-9de592c4bdfb?source=rss-9cf1a62e57ca------2</link>
            <guid isPermaLink="false">https://medium.com/p/9de592c4bdfb</guid>
            <category><![CDATA[airtable]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Alisha Tsai]]></dc:creator>
            <pubDate>Tue, 12 Oct 2021 15:37:24 GMT</pubDate>
            <atom:updated>2021-10-19T11:45:18.111Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9xiP0UPJrGe9FETgcgnNww.png" /></figure><h3>APP操作畫面</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/388/1*B9lc6jLjSVgI9xGMfqlIlA.gif" /></figure><h3><strong>本篇介紹重點</strong></h3><ol><li>Airtable當後台傳送資料</li><li>下載(GET)-Menu/圖片</li><li>上傳(POST)-Order</li></ol><h3><strong>架構</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*d_73u3olGZ-_Gh8dE5pDXA.png" /></figure><h3><strong>1.Airtable當後台傳送資料</strong></h3><p>首先在Airtable建立好Menu以及Order頁面，我一共建立八頁menu和一頁order</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IsYO3f5xIK4yM17ESRsMLw.png" /></figure><p>點進下方網址即可看到自己設定的table名稱，點進去即可看到API相關設定</p><p><a href="https://airtable.com/api">Airtable | Everyone&#39;s app platform</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HlH_BKttOlb9r6wRS4dy1g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uxPPiMlRUqWNzWANbKvtdg.png" /></figure><p>從AUTHENTICATION可以看到API網址，API Key則放在account</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1ktzPDkWeezXsRDmC7q1Ow.png" /></figure><p>也可以用Postman先檢查看看API是否運作正常</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*N81I8JcdN5CRu1qbXqMCpQ.png" /></figure><p>按下Send之後可看到下方的Json就代表API正常</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Tpq12WANpuF_oPjJi2BuDg.png" /></figure><h3><strong>2.下載(GET)</strong></h3><blockquote>MenuResponse</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qUIldP75Ut1wWNSYHk-gvQ.png" /></figure><p>參考上圖的Json就可取自己所需定義MenuResponse</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2ca605fc8ceb49af7a95b779e746a2c6/href">https://medium.com/media/2ca605fc8ceb49af7a95b779e746a2c6/href</a></iframe><blockquote>建立集中主要程式的MenuController</blockquote><p>將主要程式集中於menuController，static let shared方便其他controller呼叫。由於URL前段內容均相同，可建立baseURL簡化程式碼，也方便尾端添加資料。</p><pre>class  MenuController {<br>    static let shared = MenuController()<br>    let baseURL = URL(string: &quot;<a href="https://api.airtable.com/v0/appCtlMy2sw2htv9o/">https://api.airtable.com/v0/appCtlMy2sw2htv9o/</a>&quot;)!<br>}</pre><blockquote>MenuController-fetchMenuRecords</blockquote><p>使用Result Type讓邏輯更清晰易懂，因為是抓取menu所以httpMethod是&quot;GET&quot;，抓取資料需要先建立decoder進行解碼。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/384b55b4589facb1fb4c914f33c2b0ca/href">https://medium.com/media/384b55b4589facb1fb4c914f33c2b0ca/href</a></iframe><blockquote>MenuController-fetchImage</blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2263d7bc297aae7377c94a68d3099732/href">https://medium.com/media/2263d7bc297aae7377c94a68d3099732/href</a></iframe><blockquote>下載Menu</blockquote><p>建立好上方程式之後，就可以在MenuCollectionViewController的viewDidLoad直接寫入MenuController.shared呼叫程式。<br>由於當初.success中是指定存入型別Array&lt;MenuRecords&gt;，updateUI需要帶入[MenuRecords]才會符合.success指定的型別。若抓取失敗直接跳出Error訊息。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/64f13ae10cbdf5f32e1e0e40c4d84c4d/href">https://medium.com/media/64f13ae10cbdf5f32e1e0e40c4d84c4d/href</a></iframe><p>抓圖片寫在cellForItemAt之中，.success中指定存入UIImage型別，因此let image就必須是UIImage。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c0a344cd0f1d0cfbfb66c07cec7b68db/href">https://medium.com/media/c0a344cd0f1d0cfbfb66c07cec7b68db/href</a></iframe><h3><strong>3.上傳(POST)</strong></h3><blockquote>OrderResponse</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*r8G56d_V_kx677n7Vmrmag.png" /></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/891c840190d5fa6fae5ddaf88d075a47/href">https://medium.com/media/891c840190d5fa6fae5ddaf88d075a47/href</a></iframe><blockquote>MenuController-postOrder</blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/36f2d5c6808377b706031b243a2445e5/href">https://medium.com/media/36f2d5c6808377b706031b243a2445e5/href</a></iframe><blockquote>使用IBSegueAction傳送資料</blockquote><pre>// Menu1CollectionViewController</pre><pre>@IBSegueAction func showDrinkDetail(_ coder: NSCoder) -&gt; OrderViewController? {</pre><pre>    guard let item =     collectionView.indexPathsForSelectedItems?.first?.item else { return nil }</pre><pre>    return OrderViewController.init(coder: coder, menuItem: menuRecords, indexPath: item)</pre><pre>}</pre><blockquote>上傳訂單</blockquote><p>上傳訂單之前，需要把Menu資料從前一頁傳送過來，利用改寫.init(coder: NSCoder)的寫法就相當方便，資料直接寫在接收頁面，一目瞭然。</p><p>id和createdTime是airtable自行產生，但上傳訂單用不到先傳nil</p><p>如果orderer為空值，就跳出警示提醒訂購者輸入大名，確定有值才能上傳訂單</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8be52ce2c98a48abc7f5bf70607046e8/href">https://medium.com/media/8be52ce2c98a48abc7f5bf70607046e8/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yctG6lgR-o1LQp-aPhq7Lw.gif" /></figure><h3><strong>參考資料</strong></h3><ul><li><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E5%95%8F%E9%A1%8C%E8%A7%A3%E7%AD%94%E9%9B%86/%E5%88%A9%E7%94%A8-airtable-%E8%A3%BD%E4%BD%9C-ios-app-%E9%80%A3%E7%B5%90%E7%9A%84%E5%BE%8C%E5%8F%B0%E8%B3%87%E6%96%99-e22378536acb">利用 Airtable 製作 iOS App 連結的後台資料</a></li><li><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E5%95%8F%E9%A1%8C%E8%A7%A3%E7%AD%94%E9%9B%86/%E6%A8%A1%E4%BB%BF-apple-%E5%AE%98%E6%96%B9%E7%AF%84%E4%BE%8B%E4%B8%B2%E6%8E%A5-json-api-%E5%AE%9A%E7%BE%A9-function-%E5%9E%8B%E5%88%A5%E7%9A%84-completion-%E5%8F%83%E6%95%B8-%E4%BD%BF%E7%94%A8-result-type-9b058c77df5d">模仿 Apple 官方範例串接 JSON API，定義 function 型別的 completion 參數 &amp; 使用 Result type</a></li></ul><h3>其他篇</h3><ul><li><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E6%95%99%E5%AE%A4/ios-%E9%BA%BB%E5%8F%A4%E8%8C%B6%E5%9D%8Amacu-%E8%A8%82%E9%A3%B2%E6%96%99app-part2-%E5%88%AA%E9%99%A4-%E4%BF%AE%E6%94%B9-70ce8e9b54d8">[iOS]麻古茶坊MACU 訂飲料APP-Part2.刪除/修改</a></li><li><a href="https://alishatsai9.medium.com/ios-%E9%BA%BB%E5%8F%A4%E8%8C%B6%E5%9D%8Amacu-%E8%A8%82%E9%A3%B2%E6%96%99app-part3-%E8%87%AA%E5%8B%95%E5%88%87%E6%8F%9B%E5%BB%A3%E5%91%8A%E6%A9%AB%E5%B9%85-uisegmentedcontrol-in-tableviewcell-c3065ea79faf">[iOS]麻古茶坊MACU 訂飲料APP-Part3.自動切換廣告橫幅/UISegmentedControl in TableViewCell</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9de592c4bdfb" width="1" height="1" alt=""><hr><p><a href="https://medium.com/peter-pan-swift-ios-class/%E9%BA%BB%E5%8F%A4%E8%8C%B6%E5%9D%8Amacu-%E8%A8%82%E9%A3%B2%E6%96%99app-part1-airtable%E4%B8%B2%E6%8E%A5-%E4%B8%8B%E8%BC%89-%E4%B8%8A%E5%82%B3-9de592c4bdfb">[iOS]麻古茶坊MACU 訂飲料APP-Part1. Airtable串接/下載/上傳</a> was originally published in <a href="https://medium.com/peter-pan-swift-ios-class">彼得潘的 Swift iOS / Flutter App 開發教室</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[iOS] Weather App-Part2.串接OpenWeatherMap]]></title>
            <link>https://medium.com/peter-pan-swift-ios-class/ios-weather-app-part2-%E4%B8%B2%E6%8E%A5openweathermap-5921ee4d903e?source=rss-9cf1a62e57ca------2</link>
            <guid isPermaLink="false">https://medium.com/p/5921ee4d903e</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[openweathermap]]></category>
            <dc:creator><![CDATA[Alisha Tsai]]></dc:creator>
            <pubDate>Thu, 29 Jul 2021 16:22:47 GMT</pubDate>
            <atom:updated>2021-07-31T09:14:40.217Z</atom:updated>
            <content:encoded><![CDATA[<p>CoreLocation/CLLocationManagerDelegate/UISearchBarDelegate/searchBarSearchButtonClicked(_:)/JSONDecoder()/reloadData()/Type Casting/deselectRow(at:animated:)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CLUic9CKYiyVLx1w7O8WyA.png" /></figure><h3><strong>功能</strong></h3><ol><li>請求定位權限取得所在地點，顯示當地天氣與時間</li><li>搜尋世界各城市的天氣，顯示當地天氣與時間</li><li>顯示未來五天每間隔3小時的天氣預報</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/426/1*KkzucwhYR-nbex4fSomlYA.gif" /></figure><h3>架構</h3><p><strong>Model <br></strong>API、CurrentWeatherData、ForecastWeatherData</p><p><strong>View<br></strong>Main.storyboard、LaunchScreen.storyboard</p><p><strong>Controller</strong><br>CurrentViewController、ForecastTableViewController、ForecastTableViewCell</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*A2HqCYfPuvNtDq4dX_6vcA.png" /></figure><h3>Model</h3><p>因為我總共使用兩組API，為了方便取用url，我直接將API相關資訊獨立成一個Data</p><pre>import Foundation</pre><pre>class API {</pre><pre>      static let apiKey = &quot;Your API Key&quot;</pre><pre>      static let weather = &quot;weather&quot;</pre><pre>      static let forecast = &quot;forecast&quot;</pre><pre>      static let imperial = &quot;imperial&quot;</pre><pre>}</pre><p>CurrentViewController的urlstr</p><pre>let urlstr = &quot;https://api.openweathermap.org/data/2.5/\(<strong>API.weather</strong>)?lat=\(lat)&amp;lon=\(lon)&amp;appid=\(<strong>API.apiKey</strong>)&amp;units=\(<strong>API.imperial</strong>)&quot;</pre><p>ForecastViewController的urlstr</p><pre>let urlstr = &quot;https://api.openweathermap.org/data/2.5/\(<strong>API.forecast</strong>)?q=\(city)&amp;appid=\(<strong>API.apiKey</strong>)&amp;units=\(<strong>API.imperial</strong>)&amp;lang=zh_tw&quot;</pre><p>至於CurrentWeatherData、ForecastWeatherData的解析JASON邏輯可參考上一篇</p><h3>CurrentViewController</h3><h4>請求定位權限</h4><blockquote>CoreLocation</blockquote><blockquote>CLLocationManagerDelegate</blockquote><p>為了取得使用者當前位置，要先套入 CoreLocation，並且讓CurrentViewController遵從CLLocationManagerDelegate</p><pre>import UIKit</pre><pre><strong>import CoreLocation</strong></pre><pre>class CurrentViewController: UIViewController, <strong>CLLocationManagerDelegate</strong> {</pre><pre>}</pre><blockquote>CLLocationManager()</blockquote><p>接著創建locationManager</p><pre>let locationManager = CLLocationManager()</pre><p>創建setupLocation()，讓CurrentViewController成為locationManager的代理人，並透過requestWhenInUseAuthorization()請求定位權限，接著開始更新地點</p><pre>func setupLoaction() {</pre><pre>     locationManager.delegate = self</pre><pre>     locationManager.requestWhenInUseAuthorization()</pre><pre>     locationManager.startUpdatingLocation()</pre><pre>}</pre><p>為了讓App初使用的時候跳出請求定位權限的視窗，必須到info新增彈出視窗敘述</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1002/1*sxu0yDgzBEefSZgy4Ejv4Q.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tULodF_8x2dmsejMjJVN5A.png" /></figure><p>運行之後便會出現彈出視窗</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/410/1*1vmsiYWcqeOD2_wiksg5kw.png" /></figure><p>若運行失敗出現以下錯誤，可參考連結說明</p><blockquote>This app has attempted to access privacy-sensitive data without a usage description. The app’s Info.plist must contain both “NSLocationAlwaysAndWhenInUseUsageDescription” and “NSLocationWhenInUseUsageDescription” keys with string values explaining to the user how the app uses this data</blockquote><p><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E6%95%99%E5%AE%A4/this-app-has-attempted-to-access-privacy-sensitive-data-without-a-usage-description-error-50c2dd5bad57">“This app has attempted to access privacy-sensitive data without a usage description” error…</a></p><p>若一直無法更新定位，極有可能是模擬器的location設定None的關係，只要調整成Apple地點測試一下即可</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/781/1*syzMgkz1FYw7K1H-EKUUlQ.png" /></figure><blockquote>CLLocation</blockquote><p>定義全域變數獲得目前的定位資訊</p><pre>var currentLocation: CLLocation?</pre><p>根據Apple文件CLLocation物件包含裝置的地理位置(經緯度)、海拔高度的資訊。</p><p>A CLLocation object contains the geographical location and altitude of a device, along with values indicating the accuracy of those measurements and when they were collected.</p><blockquote>locationManager(_ :didUpdateLocations:)</blockquote><p>接著告訴代理人已經可以開始更新地點了</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/781/1*Nz8ka6ecKajTaqoDDD03XA.png" /></figure><p>如果locations有資料但是currentLocation還是空的狀態，將locations陣列中的第一筆資料派給currentLocation，接著停止更新地點，最後透過自定義的程式fetchDataFromCoordinate()抓取API資料</p><pre>func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {</pre><pre>     if !locations.isEmpty, currentLocation == nil {</pre><pre>         currentLocation = locations.first</pre><pre>         locationManager.stopUpdatingLocation()</pre><pre>         fetchDataFromCoordinate()</pre><pre>     }</pre><pre>}</pre><h4>使用SearchBar搜尋</h4><blockquote>UISearchBarDelegate</blockquote><p>首先讓CurrentViewController遵從UISearchBarDelegate</p><pre>class CurrentViewController: UIViewController, CLLocationManagerDelegate, <strong>UISearchBarDelegate</strong> {</pre><p>接著讓CurrentViewController成為searchBar的代理人</p><pre>override func viewDidLoad() {</pre><pre>      super.viewDidLoad()</pre><pre>    <strong>  searchBar.delegate = self</strong></pre><pre>      searchBar.isHidden = true</pre><pre>      searchBar.placeholder = &quot;Please enter a city name.&quot;</pre><pre>}</pre><blockquote>searchBarSearchButtonClicked(_:)</blockquote><p>當searchBar按下搜尋之後，resignFirstResponder()可以收鍵盤，而當locationString拿到使用者輸入的地點之後，便可開始用fetchDataFromCity抓取新地點的天氣資料。</p><pre>func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {</pre><pre>      //收鍵盤</pre><pre>      searchBar.resignFirstResponder()</pre><pre>      if let locataionString = searchBar.text,</pre><pre>         !locataionString.isEmpty {</pre><pre>              //用搜尋地點抓取資料更新 </pre><pre>              fetchDataFromCity(city: locataionString)</pre><pre>              searchBar.isHidden = true</pre><pre>       }</pre><pre>}</pre><h4>抓取資料</h4><p>fetchDataFromCoordinate()、fetchDataFromCity(city: String)程式設定如下</p><p>首先設定全域變數weatherInfo繼承CurrentWeatherData中的struct CurrentWeather</p><blockquote>JSONDecoder()</blockquote><p>用URLSession執行抓取url資料的任務，並選擇用JSONDecoder()解析資料</p><p>關於解析時間可參考這篇</p><p><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E5%95%8F%E9%A1%8C%E8%A7%A3%E7%AD%94%E9%9B%86/jsondecoder-%E8%A7%A3%E6%9E%90%E6%99%82%E9%96%93%E7%9A%84-datedecodingstrategy-a4095481f193">JSONDecoder 解析時間的 DateDecodingStrategy</a></p><p>因為可能抓不到資料，所以用if let 做保險，讓decoder去解析CurrentWeather，取得到的Response指派給weatherInfo，接著開始讓背景抓取到的資料，透過DispatchQueue.main.async顯示在畫面(Main Queue)</p><p>白天黑夜的背景設定是運用icon的最後一個英文字，n是夜晚，d是白天，使用.suffix(1)即可抓到最後一個字</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e798554f0e12eb6d5a6e493a807597b5/href">https://medium.com/media/e798554f0e12eb6d5a6e493a807597b5/href</a></iframe><p>設定三組格式轉換，因為風速我想顯示的是km/h單位，於是url選擇用imperial單位，但這個單位設定是華氏，所以額外編寫華氏轉攝氏的程式</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/96954e16773a27cda304829a15655331/href">https://medium.com/media/96954e16773a27cda304829a15655331/href</a></iframe><h3>ForecastTableViewController</h3><h4><strong>抓取資料</strong></h4><p>另外解析未來五天的天氣預報API，自訂型別struct之後，設定全域變數繼承，每一行row將會對應List裡面的資料。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/28872c78e3ecf1063ba3ceea00ea2e82/href">https://medium.com/media/28872c78e3ecf1063ba3ceea00ea2e82/href</a></iframe><blockquote>reloadData()</blockquote><p>這邊比較不一樣的地方是第17行的.reloadData()，根據Apple文件此方法可以重新載入表格中的所有資訊。</p><p>因為更新畫面要在main queue，所以要寫在DispatchQueue.main.async裏面。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/513/1*EEn6iCv1nNo1t5hC8hcmFQ.png" /></figure><p>Call this method to reload all the data that is used to construct the table, including cells, section headers and footers, index arrays, and so on.</p><h4><strong>Section&amp;Row</strong></h4><p>section設定1（也可不設定，預設是1），row設定forecastRow的數量。</p><p>先到storyboard設定cell的id等同ForecastTableViewCell的名稱，之後在第12行的withIdentifier就可以直接用\()選取，避免打錯字</p><blockquote>Type Casting轉型</blockquote><p>從第11行可得知此段程式最終會回傳UITableViewCell，但是我創建的cell是ForecastTableViewCell，於是使用as向下轉換(downcast)。<br>至於為何是as?，因為有可能轉型失敗。<br>而使用guard let寫法也可讓程式更易理解。</p><blockquote>deselectRow(at:animated:)</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/651/1*l0ISgeJ6IMNgBX5AQq9afg.png" /></figure><p>第14行用此方法並且讓animated為false，可以立刻執行取消的動畫。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a74742d63f9f61bb5c99c392bc197e6b/href">https://medium.com/media/a74742d63f9f61bb5c99c392bc197e6b/href</a></iframe><h3>ForecastTableViewCell</h3><p>這裡我只有拉好storyboard上的outlet，其餘無動作。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/60298e64f0bc334c95e3db80d074c417/href">https://medium.com/media/60298e64f0bc334c95e3db80d074c417/href</a></iframe><h3>利用Prepare傳資料</h3><p>為了讓ForecastTableViewController可以顯示未來五天的天氣預報，必須將CurrentViewController的location傳送過去。</p><p>首先到ForecastTableViewController創建全域變數cityName</p><pre>var cityName: String?</pre><p>到storyboard設定segue的id為showForecast</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vRtQtYqVPAROcvuIeVgkpg.png" /></figure><p>再來回到CurrentViewController寫入prepare方法，將locationLabel的text派給ForecastTableViewController的變數cityName。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/23a9ce8e109d0d577c8650602a1744ce/href">https://medium.com/media/23a9ce8e109d0d577c8650602a1744ce/href</a></iframe><p>這次練習參考了很多高手的文章跟影片，終於完成心中想要的功能！🌈</p><h3>GitHub</h3><p><a href="https://github.com/alishatsai/WeatherAPI">GitHub - alishatsai/WeatherAPI</a></p><h3>參考資料</h3><p><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E6%95%99%E5%AE%A4/simple-weather-app-%EF%B8%8F-%EF%B8%8F-589b51556b51">Simple Weather App ☀️🌦⛈❄️</a></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FXc6q_JltHSI%3Fstart%3D1158%26feature%3Doembed%26start%3D1158&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DXc6q_JltHSI&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FXc6q_JltHSI%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/2622d67c325c21b057577fe88307bfdd/href">https://medium.com/media/2622d67c325c21b057577fe88307bfdd/href</a></iframe><p><a href="https://www.figma.com/community/file/885501292477669105">Figma - Weather Forecast App | Hello, This is a weather forecast app exploration. I tried using some shadow effects to make the ...</a></p><h3>上一篇</h3><p><a href="https://alishatsai9.medium.com/ios-weather-app-part1-%E7%94%B3%E8%AB%8Bapi-key-%E8%A7%A3%E6%9E%90json-3a90f41dbf68">[iOS] Weather App-Part1.申請API Key/解析JSON</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5921ee4d903e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/peter-pan-swift-ios-class/ios-weather-app-part2-%E4%B8%B2%E6%8E%A5openweathermap-5921ee4d903e">[iOS] Weather App-Part2.串接OpenWeatherMap</a> was originally published in <a href="https://medium.com/peter-pan-swift-ios-class">彼得潘的 Swift iOS / Flutter App 開發教室</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[iOS] Weather App-Part1.申請API Key/解析JSON]]></title>
            <link>https://medium.com/peter-pan-swift-ios-class/ios-weather-app-part1-%E7%94%B3%E8%AB%8Bapi-key-%E8%A7%A3%E6%9E%90json-3a90f41dbf68?source=rss-9cf1a62e57ca------2</link>
            <guid isPermaLink="false">https://medium.com/p/3a90f41dbf68</guid>
            <category><![CDATA[openweathermap]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[json-api]]></category>
            <dc:creator><![CDATA[Alisha Tsai]]></dc:creator>
            <pubDate>Thu, 29 Jul 2021 15:57:07 GMT</pubDate>
            <atom:updated>2021-07-30T15:13:17.112Z</atom:updated>
            <content:encoded><![CDATA[<p>API(Application Programming Interface)/Postman/CodingKey/OpenWeatherMap</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8K4xL06LOuPV8hmrJNR_BA.png" /></figure><p>本次分成２篇<br><strong>Part1.申請API Key/解析JSON</strong><br>Part2.串接OpenWeatherMap/使用TableView呈現</p><h3>What is an API?</h3><p>對API的概念還很模糊的話，可以看看這個影片，解釋得非常清楚！</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FzvKadd9Cflc%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DzvKadd9Cflc&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FzvKadd9Cflc%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/0da855fa16ec0fca89db157b3e1152e5/href">https://medium.com/media/0da855fa16ec0fca89db157b3e1152e5/href</a></iframe><h3>OpenWeatherMap</h3><p>許多網站都有提供開發者串接API的服務，有些免費，有些就要掏錢出來啦！本次我選擇串接OpenWeatherMap網站上，其中兩組免費的API， Current Weather Data、5Day/3Hour Forecast。</p><p><a href="https://openweathermap.org/">Сurrent weather and forecast - OpenWeatherMap</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/975/1*QqTENwNS6_EjmtmjOn8jhA.png" /></figure><h3><strong>申請流程</strong></h3><ol><li>註冊帳號</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kryb0uZ_aYucMnsGhN2EYw.png" /></figure><p>2.完成信箱驗證之後再次登入，即可於右上角的下方選單產生屬於你自己的API Keys</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*t7Fi9HKID1KR9i-C1R0zaQ.png" /></figure><h3>閱讀API文件</h3><p>每組API都附有API文件，必看！</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*L2aWbS9ATreptDUMxqUS5A.png" /></figure><p>Current Weather Data總共有４種方式可取得資料(城市名稱/城市ID/經緯度/郵遞區號)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dt1BrPUA_l5Kd_k0Bcmadg.png" /></figure><p>若是以city name取資料，網址如下</p><pre>https://api.openweathermap.org/data/2.5/weather?q=<strong>{city name}</strong>&amp;appid=<strong>{API Key}</strong></pre><p>也就是若city name 是Taipei，而取得的API Keys是abcdefgh123456789，網址就會是這樣</p><pre>https://api.openweathermap.org/data/2.5/weather?q=<strong>taipei</strong>&amp;appid=<strong>abcdefgh123456789</strong></pre><p>網站也很貼心的示範JSON內容有什麼</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/910/1*F0yKAIj0KgLurZAOv58p9Q.png" /></figure><p>對應的資料是什麼也很貼心的附上說明。<br>像是溫度跟風速，有單位可設定，只要在網址後方再加上去，就可直接取得你想要的單位，不用額外再寫程式轉換。<br>例如溫度預設是凱式，但若你想要的是攝氏溫度，網址就要寫成下方這樣。</p><pre>https://api.openweathermap.org/data/2.5/weather?q=taipei&amp;appid=abcdefgh123456789<strong>&amp;units=metric</strong></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/694/1*Qh3_ran1O0kf9d5Sn8Qv2Q.png" /></figure><p>最下方有個項目是支援多種語言，但是只能應用在city name當中的description範圍，以下示範繁中</p><pre>https://api.openweathermap.org/data/2.5/weather?q=taipei&amp;appid=abcdefgh123456789&amp;units=metric<strong>&amp;lang=zh_tw&quot;</strong></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YSZLAcWioEQAHONjiTSfjw.png" /></figure><p>API文件了解的差不多之後，接下來就是解析JSON啦！</p><h3>解析JSON</h3><p>可使用Postman解析網址，也可以用許多免安裝的解析JSON網站解析資料</p><p><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E5%95%8F%E9%A1%8C%E8%A7%A3%E7%AD%94%E9%9B%86/%E4%BD%BF%E7%94%A8-postman-%E7%A0%94%E7%A9%B6-api-%E7%94%A2%E7%94%9F-swift-%E7%A8%8B%E5%BC%8F-12e9417c7352">使用 Postman 研究 API &amp; 產生 Swift 程式</a></p><p>解析邏輯可參考這篇</p><p><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E5%95%8F%E9%A1%8C%E8%A7%A3%E7%AD%94%E9%9B%86/%E5%88%A9%E7%94%A8-swift-4-%E7%9A%84-jsondecoder-%E5%92%8C-codable-%E8%A7%A3%E6%9E%90-json-%E5%92%8C%E7%94%9F%E6%88%90%E8%87%AA%E8%A8%82%E5%9E%8B%E5%88%A5%E8%B3%87%E6%96%99-ee793622629e">利用 JSONDecoder 把 JSON 轉換成自訂型別的資料</a></p><p>解析完成之後如下，若所要製作的App不需要這麼多資料，就抓想要的出來即可</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ff37037b2ede9d590c1d828be529b2ae/href">https://medium.com/media/ff37037b2ede9d590c1d828be529b2ae/href</a></iframe><p>若不滿意預設的property格式，比如有底線，可以運用CodingKey客製化，<br>要特別注意的事情是，enum 後方的<strong>CodingKeys</strong>大小寫要完全一樣，我因為K打成小寫，就抓不到資料了😞</p><p><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E5%95%8F%E9%A1%8C%E8%A7%A3%E7%AD%94%E9%9B%86/%E5%88%A9%E7%94%A8-enum-codingkeys-%E5%AE%A2%E8%A3%BD-json-%E5%B0%8D%E6%87%89%E7%9A%84-property-1b27f29c0c32">利用 CodingKey 客製 JSON 對應的 property</a></p><p>前置作業就到這邊了，接下來就是寫程式啦！</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3a90f41dbf68" width="1" height="1" alt=""><hr><p><a href="https://medium.com/peter-pan-swift-ios-class/ios-weather-app-part1-%E7%94%B3%E8%AB%8Bapi-key-%E8%A7%A3%E6%9E%90json-3a90f41dbf68">[iOS] Weather App-Part1.申請API Key/解析JSON</a> was originally published in <a href="https://medium.com/peter-pan-swift-ios-class">彼得潘的 Swift iOS / Flutter App 開發教室</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[iOS]Mickey Photos相片編輯App-Part4.貼圖/背景圖/文字/儲存]]></title>
            <link>https://medium.com/peter-pan-swift-ios-class/ios-mickey-photos%E7%9B%B8%E7%89%87%E7%B7%A8%E8%BC%AFapp-part4-%E8%B2%BC%E5%9C%96-%E8%83%8C%E6%99%AF%E5%9C%96-%E6%96%87%E5%AD%97-%E5%84%B2%E5%AD%98-ffe9b10fb90d?source=rss-9cf1a62e57ca------2</link>
            <guid isPermaLink="false">https://medium.com/p/ffe9b10fb90d</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[uiactivityviewcontroller]]></category>
            <dc:creator><![CDATA[Alisha Tsai]]></dc:creator>
            <pubDate>Thu, 01 Jul 2021 07:49:06 GMT</pubDate>
            <atom:updated>2021-07-04T15:02:56.182Z</atom:updated>
            <content:encoded><![CDATA[<p>UIColorPickerViewControllerDelegate/UIGraphicsImageRenderer/UIActivityViewController</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*E20U1r7xdRzNm504MH3mJQ.png" /></figure><p>這次作品分成四篇</p><p>Part1.Delegate/資料傳遞<br>Part2.翻轉/裁切<br>Part3.濾鏡<br><strong>Part4.貼圖/背景圖/文字/儲存</strong></p><p>其他篇在這～</p><ul><li><a href="https://alishatsai9.medium.com/ios-mickey-photos%E7%9B%B8%E7%89%87%E7%B7%A8%E8%BC%AFapp-part1-delegate-%E8%B3%87%E6%96%99%E5%82%B3%E9%81%9E-63b530002335">[iOS]Mickey Photos相片編輯App-Part1.Delegate/資料傳遞</a></li><li><a href="https://alishatsai9.medium.com/ios-mickey-photos%E7%9B%B8%E7%89%87%E7%B7%A8%E8%BC%AFapp-part2-%E7%BF%BB%E8%BD%89-%E8%A3%81%E5%88%87-624be6854441">[iOS]Mickey Photos相片編輯App-Part2.翻轉/裁切</a></li><li><a href="https://alishatsai9.medium.com/ios-mickey-photos%E7%9B%B8%E7%89%87%E7%B7%A8%E8%BC%AFapp-part3-%E6%BF%BE%E9%8F%A1-d5a53a4899d5">[iOS]Mickey Photos相片編輯App-Part3.濾鏡</a></li></ul><h3><strong>貼圖</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/322/1*812jaYZ55F7zd1NyiclEBQ.gif" /></figure><p>在class新增變數var cuteImg = UIImageView()</p><pre>@IBAction func pressCuteImg(_ sender: UIButton) {</pre><pre>    upDownSlider.value = 0</pre><pre>    leftRightSlider.value = 0</pre><pre>    sizeSlider.value = 1</pre><pre>    cuteImg.image = sender.currentImage</pre><pre>    cuteImg.contentMode = .scaleAspectFill</pre><pre>    let width = editImageView.frame.size.width*0.5</pre><pre>    let height = editImageView.frame.size.width*0.5</pre><pre>    cuteImg.frame = CGRect(x: 0, y: 0, width: width, height: height)</pre><pre>    cuteImg.center = editBackgroundView.center</pre><pre>    editBackgroundView.addSubview(cuteImg) <br>}</pre><h3><strong>背景圖</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/322/1*joNggBedYRl7n8ucuXE1AQ.gif" /></figure><p>在class新增以下兩變數</p><pre>var backView = UIView() //裝載backgroundImageView</pre><pre>var backgroundImageView = UIImageView()</pre><p>必須把backView指定在editBackgroundView的上方的第0層，如果不是寫editBackgroundView.insertSubview(backView, at: 0)，而是寫editBackgroundView.addSubview(backView)</p><p>如此一來，主圖就會被背景圖蓋住了…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/322/1*lwJINvCrnhjfSqADuh67wQ.gif" /><figcaption>糟糕IU消失了…</figcaption></figure><p>用playground呈現一下關係圖，第22行就是上圖IU被蓋住的情況</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/909/1*aXTEHp81VgBVsA_8nMbCsg.png" /></figure><pre>@IBAction func pressBackground(<strong>_</strong> sender: UIButton) {</pre><pre>    backgroundImageView.image = sender.currentBackgroundImage</pre><pre>    backgroundImageView.contentMode = .scaleAspectFill</pre><pre>    backView.addSubview(backgroundImageView) </pre><pre>   <strong> editBackgroundView.insertSubview(backView, at: 0)</strong></pre><pre>}</pre><blockquote><strong>清除貼圖或背景圖</strong></blockquote><pre>@IBAction func clear(_ sender: UIButton) {</pre><pre>    if sender == clearItemBtn {</pre><pre>    cuteImg.image = .none</pre><pre>    }</pre><pre>    if sender == clearBgBtn {</pre><pre>    background.image = .none</pre><pre>    }</pre><pre>}</pre><h3><strong>文字</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/322/1*oo8jD1wBePlLr3QvXRikNQ.gif" /></figure><p>於 struct新增變數var getBoldTimes、var getItalicTimes、var getBigTimes、var getSmallTimes，以便用slider調整粗體\斜體\大小。</p><p>於struct新增變數var isCuteImg、var isShowWord，這樣當我調整slider的時候，就可以直接判斷現在是要調整字體還是貼圖大小。</p><pre>struct PhotoSetting {</pre><pre>    var getBoldTimes = 0</pre><pre>    var getItalicTimes = 0</pre><pre>    var getBigTimes = 0</pre><pre>    var getSmallTimes = 0</pre><pre>    var pressWordTimes = 0</pre><pre>    var isCuteImg = false</pre><pre>    var isShowWord = false</pre><pre>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3Dpofd06uXNJYpaXMGu1OQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zzz25_bN7QOvIJAWK32B_w.png" /></figure><blockquote><strong>運用slider變化貼圖或字體大小與位置</strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jscc0Xgxzk5Gyi5GhuX29A.png" /></figure><blockquote><strong>運用UIColorPickerViewControllerDelegate改變字體顏色</strong></blockquote><p>必須先讓EditPhotoViewController遵從protocol <strong>UIColorPickerViewControllerDelegate，</strong>否則EditPhotoViewController無法成為代理人</p><pre>class EditPhotoViewController: UIViewController ,<strong>UIColorPickerViewControllerDelegate</strong></pre><p>再新增下方程式</p><pre>func colorPickerViewControllerDidSelectColor(_ viewController: UIColorPickerViewController) {</pre><pre>    wordTextField.textColor = viewController.selectedColor</pre><pre>}</pre><h3>儲存</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/322/1*Evr4Sd-D1o0GD7c8k1A9tw.gif" /></figure><blockquote><strong>利用UIGraphicsImageRenderer讓指定的區塊變成UIImage</strong></blockquote><blockquote><strong>利用UIActivityViewController分享或儲存圖片</strong></blockquote><pre>@IBAction func saveEditedPhoto(_ sender: UIButton) {</pre><pre>    let renderer = <strong>UIGraphicsImageRenderer</strong>(size:    editBackgroundView.bounds.size)</pre><pre>    let editedImage = renderer.image { UIGraphicsImageRendererContext in</pre><pre>    editBackgroundView.drawHierarchy(in: editBackgroundView.bounds, afterScreenUpdates: true)</pre><pre>    }</pre><pre>    let activityViewController = <strong>UIActivityViewController</strong>(activityItems: [editedImage], applicationActivities: nil)</pre><pre>    present(activityViewController, animated: true, completion: nil)</pre><pre>}</pre><p>終於完成這次的作業，寫文章雖然很花時間，但也因此發現許多奇怪的地方😆，再繼續前往下個作業了～</p><h3>GitHub</h3><p><a href="https://github.com/alishatsai/photoEdit">alishatsai/photoEdit</a></p><h3>參考資料</h3><ul><li><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E5%95%8F%E9%A1%8C%E8%A7%A3%E7%AD%94%E9%9B%86/ios-sdk-%E9%81%B8%E6%9D%B1%E8%A5%BF%E7%9A%84-view-controller-delegate-%E7%AF%84%E4%BE%8B-c3f0b5238933">iOS SDK 選東西的 view controller &amp; delegate 範例</a></li><li><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E5%95%8F%E9%A1%8C%E8%A7%A3%E7%AD%94%E9%9B%86/%E5%88%A9%E7%94%A8-uigraphicsimagerenderer-%E5%B0%87-view-%E8%AE%8A%E6%88%90%E5%9C%96%E7%89%87-41d00c568903">利用 UIGraphicsImageRenderer 將 view 變成圖片(包含只擷取 view 的部分區塊）</a></li><li><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E5%95%8F%E9%A1%8C%E8%A7%A3%E7%AD%94%E9%9B%86/%E5%B0%87%E5%9C%96%E7%89%87%E5%84%B2%E5%AD%98%E5%88%B0-ios-11-%E7%9A%84-photo-album-c8761bb21139">將圖片儲存到 iOS 的 photo album</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ffe9b10fb90d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/peter-pan-swift-ios-class/ios-mickey-photos%E7%9B%B8%E7%89%87%E7%B7%A8%E8%BC%AFapp-part4-%E8%B2%BC%E5%9C%96-%E8%83%8C%E6%99%AF%E5%9C%96-%E6%96%87%E5%AD%97-%E5%84%B2%E5%AD%98-ffe9b10fb90d">[iOS]Mickey Photos相片編輯App-Part4.貼圖/背景圖/文字/儲存</a> was originally published in <a href="https://medium.com/peter-pan-swift-ios-class">彼得潘的 Swift iOS / Flutter App 開發教室</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[iOS]Mickey Photos相片編輯App-Part3.濾鏡]]></title>
            <link>https://medium.com/peter-pan-swift-ios-class/ios-mickey-photos%E7%9B%B8%E7%89%87%E7%B7%A8%E8%BC%AFapp-part3-%E6%BF%BE%E9%8F%A1-d5a53a4899d5?source=rss-9cf1a62e57ca------2</link>
            <guid isPermaLink="false">https://medium.com/p/d5a53a4899d5</guid>
            <category><![CDATA[cifilter]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ciimage]]></category>
            <dc:creator><![CDATA[Alisha Tsai]]></dc:creator>
            <pubDate>Wed, 30 Jun 2021 15:06:47 GMT</pubDate>
            <atom:updated>2021-07-01T08:03:30.880Z</atom:updated>
            <content:encoded><![CDATA[<p>CoreImage.CIFilterBuiltins/CIImage/CIFilter/CIContex</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_I0lLRV5mAM68VrNxkQOnA.png" /></figure><p>這次作品分成四篇</p><p>Part1.Delegate/資料傳遞<br>Part2.翻轉/裁切<br><strong>Part3.濾鏡</strong><br>Part4.貼圖/背景圖/文字/儲存</p><p>其他篇在這～</p><ul><li><a href="https://alishatsai9.medium.com/ios-mickey-photos%E7%9B%B8%E7%89%87%E7%B7%A8%E8%BC%AFapp-part1-delegate-%E8%B3%87%E6%96%99%E5%82%B3%E9%81%9E-63b530002335">[iOS]Mickey Photos相片編輯App-Part1.Delegate/資料傳遞</a></li><li><a href="https://alishatsai9.medium.com/ios-mickey-photos%E7%9B%B8%E7%89%87%E7%B7%A8%E8%BC%AFapp-part2-%E7%BF%BB%E8%BD%89-%E8%A3%81%E5%88%87-624be6854441">[iOS]Mickey Photos相片編輯App-Part2.翻轉/裁切</a></li><li><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E6%95%99%E5%AE%A4/ios-mickey-photos%E7%9B%B8%E7%89%87%E7%B7%A8%E8%BC%AFapp-part4-%E8%B2%BC%E5%9C%96-%E8%83%8C%E6%99%AF%E5%9C%96-%E6%96%87%E5%AD%97-%E5%84%B2%E5%AD%98-ffe9b10fb90d">[iOS]Mickey Photos相片編輯App-Part4.貼圖/背景圖/文字/儲存</a></li></ul><h3>濾鏡</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/322/1*i5CJn8DKvR_LzpNaWq10IQ.gif" /></figure><blockquote><strong>加入套件CoreImage.CIFilterBuiltins</strong></blockquote><p>首先import CoreImage.CIFilterBuiltins</p><blockquote><strong>各種濾鏡風格</strong></blockquote><p>接著從下方連結挑選自己想要的濾鏡風格，打包到array之中</p><p><a href="https://developer.apple.com/library/archive/documentation/GraphicsImaging/Reference/CoreImageFilterReference/index.html#//apple_ref/doc/filter/ci/CIMultiplyBlendMode">Core Image Filter Reference</a></p><blockquote><strong>產生CIImage</strong></blockquote><p>因為CIFilter只能作用在CIImage，所以必須先讓UIImge轉換到CIImage</p><blockquote><strong>傳入kCIInputImageKey</strong></blockquote><p>在forKey參數傳入kCIInputImageKey告訴他我們要套濾鏡的圖片，也就是ciImage</p><blockquote><strong>輸出已套用濾鏡的圖片</strong></blockquote><p>這邊運用if let 語法(optional binding)，是因為輸出的過程很有可能產生錯誤，為了確保程式不會發生錯誤閃退，使用if let 做一層保險。</p><p>.outputImage可輸出經過濾鏡的圖片，但是要再經過CIContext()的createCGimage才能得到我們想要的圖像大小，從官方文件可看到from參數是要傳入CGRect</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/786/1*Q_a9397rqN1X3FcFJyzgBw.png" /></figure><p>.extent代表的是圖片範圍，繼承CGRect</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/786/1*Ah9UdNbTGAyHAosBfIsTUg.png" /></figure><p>接著要再把CGImage轉換回去UIImage，怎麼過去的就要怎麼回來啊!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*S5krCU_DXxGzkfM2rT4YdQ.png" /></figure><p>將濾鏡按鈕設定tag數字，即可對應filterArray之中的濾鏡效果</p><pre>//選取濾鏡風格</pre><pre>@IBAction func pressFilterBtn(_ sender: UIButton) {</pre><pre>    if sender.tag == 0 {</pre><pre>    editImageView.image = photoImg.photo</pre><pre>    }</pre><pre>    else {</pre><pre>    filterNum = sender.tag</pre><pre>    buildFilter()</pre><pre>    }</pre><pre>}</pre><h3>參考資料</h3><p>Processing an Image Using Built-in Filters</p><ul><li><a href="https://developer.apple.com/documentation/coreimage/processing_an_image_using_built-in_filters">Processing an Image Using Built-in Filters | Apple Developer Documentation</a></li><li><a href="https://developer.apple.com/library/archive/documentation/GraphicsImaging/Reference/CoreImageFilterReference/#//apple_ref/doc/filter/ci/CIHueAdjust">Core Image Filter Reference</a></li><li><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84%E8%A9%A6%E7%85%89-%E5%8B%87%E8%80%85%E7%9A%84-100-%E9%81%93-swift-ios-app-%E8%AC%8E%E9%A1%8C/74-%E5%88%A9%E7%94%A8-cifilter-%E5%AF%A6%E7%8F%BE%E7%BE%8E%E9%BA%97%E7%9A%84%E5%9C%96%E7%89%87%E6%BF%BE%E9%8F%A1-6b7323612188">#74 利用 CIFilter 實現美麗的圖片濾鏡</a></li><li><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E6%95%99%E5%AE%A4/74-cifilter-%E5%AF%A6%E7%8F%BE%E6%BF%BE%E9%8F%A1%E5%8A%9F%E8%83%BD-855f70a0260">#74 CIFilter 實現濾鏡功能</a></li><li><a href="https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E6%95%99%E5%AE%A4/good-mood-camera-%E5%A5%BD%E5%BF%83%E6%83%85%E7%9B%B8%E6%A9%9F-40e67d61b18a">Good Mood Camera(好心情相機) 📷</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d5a53a4899d5" width="1" height="1" alt=""><hr><p><a href="https://medium.com/peter-pan-swift-ios-class/ios-mickey-photos%E7%9B%B8%E7%89%87%E7%B7%A8%E8%BC%AFapp-part3-%E6%BF%BE%E9%8F%A1-d5a53a4899d5">[iOS]Mickey Photos相片編輯App-Part3.濾鏡</a> was originally published in <a href="https://medium.com/peter-pan-swift-ios-class">彼得潘的 Swift iOS / Flutter App 開發教室</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>