<?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 Anny on Medium]]></title>
        <description><![CDATA[Stories by Anny on Medium]]></description>
        <link>https://medium.com/@anny86023?source=rss-d85bd75177d8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*xqu9kJLdcSXhg4bwPLa8UQ.jpeg</url>
            <title>Stories by Anny on Medium</title>
            <link>https://medium.com/@anny86023?source=rss-d85bd75177d8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 09:42:16 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@anny86023/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[iOS APP 使用 Facebook 第三方登入]]></title>
            <link>https://anny86023.medium.com/ios-app-%E4%BD%BF%E7%94%A8-facebook-%E7%AC%AC%E4%B8%89%E6%96%B9%E7%99%BB%E5%85%A5-6694aeae5194?source=rss-d85bd75177d8------2</link>
            <guid isPermaLink="false">https://medium.com/p/6694aeae5194</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[social-login]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[facebook]]></category>
            <dc:creator><![CDATA[Anny]]></dc:creator>
            <pubDate>Sun, 29 Nov 2020 10:12:02 GMT</pubDate>
            <atom:updated>2020-11-29T10:12:02.193Z</atom:updated>
            <content:encoded><![CDATA[<p>現在很多的 APP 登入方式都會開放使用FB或者Google 帳號，不用再麻煩的註冊新帳號。</p><p>第三方登入成為一個趨勢，尤其在手遊APP上，這樣可以讓玩家避免忘記帳號及密碼，也能增加用戶的留存機率。</p><p>所以～今天來看看如何使用 Facebook 作為第三方登入 — Facebook Login。</p><p>讓我們來實作 FB 登入功能吧！！！</p><p>本篇內容包含：</p><ul><li>註冊 App 並搭配 Facebook 使用</li><li>使用 SPM 安裝 Facebook iOS SDK</li><li>在 AppDelegate 與 SceneDelegate加入 FB 相關程式</li><li>加入 Facebook 登入程式碼</li></ul><p>FB iOS SDK 的官方文件，今天就是按照步驟來實際執行啦～～～</p><p><a href="https://developers.facebook.com/docs/facebook-login/ios">iOS - Facebook Login - Documentation - Meta for Developers</a></p><h4>1. 選擇應用程式或建立新的應用程式</h4><p>當然第一步驟要先登入 Facebook 啦。</p><p>再來，點選<strong> “我的應用程式” </strong>來新增應用程式，為了在 iOS App 加入 FB 登入功能，我們得先在 FB 開發網站上建立一個對應的 App。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GRXQ1c16hOGfmV3qmddczQ.png" /></figure><p>接著 創建應用程式的名稱，這裡要注意喔～不能輸入有關fb的文字，否則會出錯，像下面那張圖。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bg8PkhIcTVf892_x4JuyXw.png" /></figure><h4>2. 註冊應用程式並設成可搭配 Facebook 使用</h4><p>註冊完後，要來設定應用程式，才能使用 Facebook 登入。</p><p>在 App 下方的 “<strong>設定” </strong>中選擇 “<strong>基本資料”</strong></p><p>接著滑到頁面底部，點選 “新增平台” 新增 iOS 設定區塊</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TdNk1T8cERY6dHcZW_1NsA.png" /><figcaption>“<strong>設定” </strong>中選擇 “<strong>基本資料” </strong>新增平台</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4nRPNWfCvn7eDaHecph-xQ.png" /><figcaption>新增 iOS 設定區塊</figcaption></figure><h4>設定 iOS 區塊</h4><p>輸入 iOS App 的套件識別碼也就是 Bundle ID ，並且啟用應用程式的單一登入。</p><p><strong>套件識別碼，</strong>在 Xcode 專案的 General 頁面查詢，找到套件識別碼。</p><p>然後！！！重要！！！請注意！！！<br><strong>專案名稱不能為 FacebookLogin，因為後續import 也是 FacebookLogin。</strong><br>Xcode一直報錯說找不到這個套件QQ，試了很多次，一開始以為是我版本有問題，因為剛更新成 Xcode 12.2 版本，試了很多次QQ，改個名字就對了。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*V7gkngGGhhrg1-wiBoWGwA.png" /><figcaption>在 Xcode 查詢 <strong>套件識別碼</strong></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vZMQ600FUH5bMHsi9uEvZA.png" /><figcaption>輸入套件識別碼，並且開啟單一登入。</figcaption></figure><p>如果你是直接在 Fackbook 官方文件，也可以直接做到以上功能喔！</p><p><a href="https://developers.facebook.com/docs/facebook-login/ios/">https://developers.facebook.com/docs/facebook-login/ios/</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/584/1*bNX8_ghPPH3Sxqf5OmnwqQ.png" /></figure><h4>3. 設定開發環境 — 利用 SPM 安裝 Facebook iOS SDK</h4><p>想在 App 加入 FB 登入功能，必須先將 Facebook iOS SDK 加到 Xcode 專案。<br>使用 Swift 套件管理工具（SPM）<br>注意！！！僅適用於 Xcode 11.2 以上版本喔。<br>除了 SPM 也可以使用Cocoapods 安裝，加入 pod FacebookLogin。</p><p><a href="https://github.com/facebook/facebook-ios-sdk">GitHub - facebook/facebook-ios-sdk: Used to integrate the Facebook Platform with your iOS &amp; tvOS apps.</a></p><ol><li>在 Xcode 中，點擊 File -&gt; Swift Packages -&gt; Add Package Dependency。</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xrVQhdqZ7OBaV5PFJ6XOeQ.png" /></figure><p>2. 新增套件時輸入 Facebook iOS SDK 的 GitHub 網址：<a href="https://github.com/facebook/facebook-ios-sdk">https://github.com/facebook/facebook-ios-sdk</a>。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*romM8aCp1lI0EmuxDxyHqQ.png" /></figure><p>3. 在 Version中，選擇 Up to Next Major。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oab1AjY_135wNsQum4z-Nw.png" /></figure><p>4. 勾選 FacebookLogin</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8CHUo1n80QU0tEipwfEmdA.png" /></figure><p>完成套件的安裝後長這樣， Swift Package Dependencies 增加了 Facebook。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PP6Wk7cHCsfCdGUp0DzYUw.png" /></figure><h4>4. 在 Xcode 專案設定 Facebook 相關資訊</h4><p>接下來要在專案裡設定 FB 的資訊，才能來實作一些 FB 的功能。</p><p>這裡的設定方式有兩種～～～兩種都會實際操作給大家看。</p><blockquote>第一種 — XML 程式碼設定Info.plist</blockquote><p>使用 XML 程式碼來設定資訊屬性清單檔案，也就是Info.plist。</p><p>點選 Info.plist 右鍵 -&gt; Open As -&gt; Source Code，就可以用 XML 編輯囉！</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/646/1*6Fu8LZhtd7nxLx2hroFfeg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1002/1*kJrI1tz04kJz3Qcyu2xvig.png" /></figure><p>Fackbook 官方文件，可以在這裡複製好喔～～～就不用自己打了&gt;&lt;</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hxxuL6J0TruWIMGT6ox72g.png" /></figure><p>設定好後會就會呈現這樣</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zWfoW7VdPHFpWy8-ZbArGQ.png" /></figure><blockquote>第二種 — 手動設定Info.plist</blockquote><p>到 Info 頁面，URL Types 點選下面 <strong>“+”</strong> 來新增 URL Type。<br>這步驟也就是上面程式碼 標註<strong>1號</strong>的部分！</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/394/1*56rGgjIZtz9RwCUHBliz2Q.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eRG6qCJiIFrlSpTPteff5Q.png" /><figcaption>新增 URL Type</figcaption></figure><p>URL Schemes 輸入應用程式 ID，記得要在 ID 前面加上 fb 喔。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rYWYgDLBAJtXM-kV5yLR6g.png" /></figure><p>第二和三步驟就在Info 頁面加入 FacebookAppID 和 FacebookDisplayName。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/738/1*J02RhJOppJC56rMIX6K3Jw.png" /></figure><p>在自己的應用程式中可以查詢 ID。</p><p>第四步驟是新增型別 array 的 LSApplicationQueriesSchemes。<br>可以全部都加入，也可以只加入fbapi、fb-messenger-share-api、fbauth2 、fbshareextension，這樣才能使用 FB 的相關 App。</p><h4>5. 在 AppDelegate 與SceneDelegate加入 FB 相關程式</h4><blockquote>AppDelegate</blockquote><p>首先 <strong>import</strong> FacebookCore ，才能使用 FB 的相關程式。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FL7UqXYyyJhFItRUo6RnbA.png" /></figure><p>接著 在 application(_:didFinishLaunchingWithOptions:)裡執行上述程式碼，這樣一來可以達到，在一啟動App 就完成了 FB 的設定。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BU312rhy3QlFnhgaSz-KKg.png" /></figure><p>再來新增一個func，application(_:open:options:)，如果切換到 FB後，要再從 FB 切換回原本的 App 時會呼叫這個func。</p><p>完整程式碼</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e1ed3ce7ad019d7b79ff3d852e9cc3a5/href">https://medium.com/media/e1ed3ce7ad019d7b79ff3d852e9cc3a5/href</a></iframe><blockquote>SceneDelegate</blockquote><p>如果你的 APP 版本在 iOS 13 以上，還需要在 SceneDelegate 加入程式碼。</p><p>一樣要 <strong>import</strong> FacebookCore 喔！</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0ewOEUD1-clq9aD1Y1qWxw.png" /><figcaption>SceneDelegate.swift 程式碼</figcaption></figure><h4>6. Facebook 登入程式碼</h4><p>做完前面那一堆設定之後！終於要進入今天的重頭戲啦！！！</p><p><strong>首先 import</strong> FacebookLogin</p><p>生成 FBLoginButton，設定一下Button位置在正中心點，接著將Button加入view中。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*REU8mYsRzABJLQ-RQiPwig.png" /></figure><p>來看看成果吧！</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/325/1*QmeaOV-IGJTWm2Bh6YVdFw.gif" /></figure><p>今天先到這裡囉～～～</p><p>之後再來實作其他更多 Facebook 登入之後的操作，讀取使用者FB profile 資訊，檢查目前的登入狀態等等…</p><p>最後一樣附上 Github 給大家參考～～～</p><p><a href="https://github.com/anny86023/FBLogin">anny86023/FBLogin</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6694aeae5194" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[iOS 如何選擇照片與拍照 — UIImagePickerController]]></title>
            <link>https://anny86023.medium.com/ios-%E5%A6%82%E4%BD%95%E9%81%B8%E6%93%87%E7%85%A7%E7%89%87%E8%88%87%E6%8B%8D%E7%85%A7-uiimagepickercontroller-2a1246a87e63?source=rss-d85bd75177d8------2</link>
            <guid isPermaLink="false">https://medium.com/p/2a1246a87e63</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[uiimagepickercontroller]]></category>
            <category><![CDATA[carma]]></category>
            <dc:creator><![CDATA[Anny]]></dc:creator>
            <pubDate>Wed, 18 Nov 2020 16:01:09 GMT</pubDate>
            <atom:updated>2020-11-18T16:01:09.091Z</atom:updated>
            <content:encoded><![CDATA[<h3>iOS 如何選擇照片與拍照 — UIImagePickerController</h3><p>從相簿中挑選照片，或者是使用相機拍照，用於取得照片或者編輯照片等等，這都是很常在開發過程中需要使用的功能，今天就來看看要如何撰寫這個功能吧。那就需要提到 UIImagePickerController 啦！！！</p><p>本篇內容包含：</p><ul><li>UIImagePickerController 三種 Types</li><li>如何顯示從相簿中挑選照片</li><li>如何顯示使用手機相機拍照</li><li>結合兩種方式～可以自己選擇～～～</li></ul><p>成果如下：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/296/1*XjG8_7tzGUBd0UPoSP7dQA.gif" /></figure><blockquote>UIImagePickerController 三種 Types</blockquote><p>UIImagePickerController 用來選取相簿的照片、影片、或是使用相機拍照。</p><p>UIImagePickerController 有三種 Types 設定如何選擇圖片：</p><ol><li>UIImagePickerController.sourceType.photoLibrary (從圖庫取得照片)</li><li>UIImagePickerController.sourceType.camera (使用相機拍攝取得照片)</li><li>UIImagePickerController.sourceType.savedPhotosAlbum (相簿取得照片)</li></ol><p>設定完 Types，利用 present 的方式呈現。</p><blockquote>如何顯示從相簿中挑選照片</blockquote><p>一開始需要讀取使用者手機內的相簿，因此需要設定可以使用相簿的權限，<br>在Info中新增一個 Privacy — Photo Library Usage Description</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ftFICnnVwuN6lQDie42QEw.png" /><figcaption>開啟相簿的權限設定</figcaption></figure><p>進入程式碼部分～首先設定 UIImagePickerController 的 delegate。<br>加入 UIImagePickerControllerDelegate, UINavigationControllerDelegate</p><p>因為 delegate 的型別，所以 ViewController 要同時遵守 protocol UIImagePickerControllerDelegate 還有 UINavigationControllerDelegate！</p><p>UIImagePickerControllerDelegate (處理選取照片後的function)<br>UINavigationControllerDelegate (開啟相機或選取照片時畫面跳轉時需要的)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ftSDud0KwdH4MHa9DRLaGg.png" /></figure><p>接著在 storyboard 放入 UIImageView 顯示圖片，還有一個按鈕來選擇照片。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/264/1*xvcqtrurcFd6IVB4SN112g.png" /></figure><p>按鈕的function</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1016/1*vSWfW24sP_FZTknM_ED9cw.png" /><figcaption>按鈕的function</figcaption></figure><p>使用UIImagePickerController.sourceType.savedPhotosAlbum (相簿取得照片)</p><ol><li>先創立 UIImagePickerController()</li><li>接著設置 .sourceType 這裏使用.photoLibrary (從圖庫取得照片)</li><li>再來 .delegate是要引用此方法必須的</li><li>最後 present 這個 controller 也就是 UIImagePickerController()</li></ol><p>接著當使用者拍照或選照片後，會呼叫 UIImagePickerControllerDelegate 中function imagePickerController(_:didFinishPickingMediaWithInfo:)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5OYAG9bZ2_ciQgj7UcggrQ.png" /><figcaption>選取照片的function</figcaption></figure><p>可以從參數 info 拿到圖片的資料，info 的型別是 dictionary，傳入 key 中。而 info[.originalImage]可得到圖片。所以我們從function 的 info 中拿取照片(info[.originalImage]) 把他轉型成 UIImage ，然後顯示在UIImageView上，最後要記得使用 dismiss ，在選擇之後把這個controller的收起來喔～～～</p><p>提醒！這個選取照片，模擬器無法執行喔，只能在實機上測試！</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/296/1*Q-ANsN0e4j3z5d-2YLKNmg.gif" /></figure><blockquote>如何顯示使用手機相機拍照</blockquote><p>接著我們嘗試開啟相機拍照，跟選照片一樣，需要使用開啟手機相機功能，因此需要設定同意使用相機拍照。在Info中新增Privacy — Camera Usage Description</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*izKcy-RUQLNwjfBhl8-EZQ.png" /><figcaption>開啟相機的權限設定</figcaption></figure><p>程式碼部分，做法跟上面幾乎相同，只是在設置 .sourceType 這裡使用.camera (使用相機拍攝取得照片)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yDIO5OUdOH7Ahhxspu7LkQ.png" /></figure><p>提醒！這個拍攝照片，模擬器無法執行喔，只能在實機上測試！</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/296/1*IuNzRJLqDYoyGROjndYiGw.gif" /></figure><blockquote><em>結合兩種選擇方式</em></blockquote><p>最後我們來將這兩種選擇方式結合在一起吧！使用 UIAlertController 選擇。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*b-Znn8bO4mRU-THCFovlwQ.png" /></figure><p>使用 UIAlertController .actionSheet 選單方式 由下往上顯示</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/314/1*vrN1YWrGEJJMAKAzaq5vkA.png" /></figure><p>最後來看看執行結果啦～～～</p><p>提醒！模擬器無法執行喔，只能在實機上測試！</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/296/1*XjG8_7tzGUBd0UPoSP7dQA.gif" /></figure><p>附上Github 連結</p><p><a href="https://github.com/anny86023/PhotoSelect">anny86023/PhotoSelect</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2a1246a87e63" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Swift 資料庫 Core Data CRUD 方式]]></title>
            <link>https://anny86023.medium.com/swift-%E8%B3%87%E6%96%99%E5%BA%AB-core-data-crud-%E6%96%B9%E5%BC%8F-d875cf8d4403?source=rss-d85bd75177d8------2</link>
            <guid isPermaLink="false">https://medium.com/p/d875cf8d4403</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[core-data]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[crud]]></category>
            <category><![CDATA[database]]></category>
            <dc:creator><![CDATA[Anny]]></dc:creator>
            <pubDate>Wed, 18 Nov 2020 07:52:21 GMT</pubDate>
            <atom:updated>2020-11-18T07:52:21.358Z</atom:updated>
            <content:encoded><![CDATA[<p>Core Data 儲存資料的框架，底層操作一樣是 SQLite ，但簡化了資料庫的處理，不需看懂複雜的SQL語法，不用了解 SQL 指令也可以快速的建立還有使用資料庫。</p><p>其他 iOS 會用的資料庫 包含了～～～SQLite 、 CoreData 、Realm</p><p>本文章內容包含：</p><ul><li>在專案中加入Core Data</li><li>設定 Entity 與 Attribute</li><li>Core Data 新增、查詢、更新、刪除資料</li></ul><blockquote><em>在專案中加入Core Data</em></blockquote><p>建立專案的時候，記得將 <strong>Use Core Data </strong>打勾，如下圖：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*N01cYzsyTm0-GGdMxiitnA.png" /></figure><p>一開始忘記勾選也沒關係～～～<br>可以透過新增 File 來增加 : New File 的 Core Data 裡面，新增 Data Model。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_eNnWKSUlp9_8HH01T8k3Q.png" /></figure><blockquote>設定 Entity 與 Attribute</blockquote><p>建立好專案後，檔案列表中有一個.xcdatamodeld的檔案。<br>這是用來設定 Entity 與 Attribute 的檔案。</p><p>設定步驟～～～ 設定結果如下圖：</p><ol><li>Add Entity 新增一個新的資料庫表格table</li><li>幫這個 Entity 命名 (範例為製作使用者資料，因此命名為User)</li><li>Add Attribute 新增資料欄位</li><li>增加兩個 Attribute ：name, phone ，Type 也就是每一個 Attribute 的類型，兩者均為 String。</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cogQItNFr39L1BA7CMtYCw.png" /></figure><p>這樣就完成加入 Core Data 的步驟了喔！！！</p><p>接下來就來看 Core Data 如何新增、查詢、修改、刪除資料吧～～～CRUD</p><blockquote>使用 Core Data</blockquote><p>建立好資料庫屬性及型別後，就要來存入資料了。</p><p>先 <strong>import</strong> CoreData</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vmMfBco2enxfGwScqVZrBg.png" /></figure><p>如果你查看 AppDelegate.swift 就會發現在我們一開始創建專案，並將CoreData加入，則會新增以下程式碼。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3_f2FoYnPVfsvZl6th1c3w.png" /></figure><pre>// 用來操作 Core Data 的常數</pre><pre><strong>let</strong> container = (UIApplication.shared.delegate <strong>as</strong>! AppDelegate).persistentContainer</pre><pre><strong>let</strong> context = (UIApplication.shared.delegate <strong>as</strong>! AppDelegate).persistentContainer.viewContext</pre><p>所以上述程式是以委任的方式，取得 AppDelegate.swift 的屬性，來操作 Core Data 。</p><p>此時的 container 型態為NSPersistentContainer <br>而context 型態為NSManagedObjectContext</p><blockquote>Core Data 新增、查詢、更新、刪除資料</blockquote><p>接著用簡單的範例來說明 Core Data CRUD 的方式吧～～～</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/294/1*65KLbwMb3dgT1-tXMLCfUA.png" /></figure><p>輸入使用者姓名與手機，按下送出則將使用者訊息存進資料庫中。</p><p>先來創建送出按鈕的執行Action吧</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*h3qWcYgsQ8xAbUd3j_wb_A.png" /></figure><p>先判斷輸入的姓名與手機欄位是否為空，若兩者皆不為空，則呼叫新增資料的function，也就是addUser。</p><h4>Core Data 新增資料</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/626/1*76ptkYyR0HcuFFiTM2V4Mg.png" /></figure><p>將我們創建的資料庫名為 User 的實體，指派給常數 user，並帶入剛剛型態為NSManagedObjectContext 的 context。</p><p>只要需要存入資料或進行其他資料庫的操作，都需要用到建立好的資料庫。 也就是 User(context: NSManagedObjectContext)<br>我們就會拿到NSPersistentContainer中的viewContext部分。</p><p>接下來就可以在 user 各個 attribute 設定將取得的UITextField資料存入。</p><p>存完之後要將資料庫存檔，在做完任何操作都需要存檔案，viewContext<br>有新的變化時，就要進行viewContext.save() 他是一個throwing function。<br>在進行資料庫操作都需要執行do try catch。</p><p>在 AppDelegate.swift 新增延伸的.saveContext()程式碼，這樣之後需要使用時就可以直接呼叫，container.saveContext()。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WVI4MCxvNBHLPSKq5XR6AQ.png" /></figure><h4>Core Data 查詢資料</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/936/1*V2_XXwJQAg0dn6SthF6njQ.png" /></figure><p>查詢則會使用到 NSFetchRequest <br>設定常數 request = NSFetchRequest&lt;User&gt;(entityName: “User”)<br>此時的 request 型態為 NSFetchRequest&lt;User&gt; 抓取 User 資料庫的請求。</p><p>在進行資料庫操作時，都需要執行do try catch。<br>context.fetch(request) 進入資料庫抓資料，接著將裡面的各個資料印出來。</p><pre><strong>for</strong> result <strong>in</strong> results {<br>    print(“姓名: \(result.name!), 手機: \(result.phone!)”)<br>}</pre><h4>Core Data 修改資料</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*y4pGc6VxQv9CznIDOaLS1w.png" /></figure><p>跟剛剛一樣，設定 request = NSFetchRequest&lt;User&gt;(entityName: “User”)<br>此時的 request 型態為 NSFetchRequest&lt;User&gt; 抓取 User 資料庫的請求。</p><p>接著給出你要修改的項目條件，在這裡我要修改使用者名字為anny的手機。<br>先將使用者anny 名字存入 等等要做比對。<br>比對時我們會使用，predicate 後面會給他格式，也就是你要比對的項目。<br>有兩種方式都可以比對 ：<br>request.predicate = NSPredicate(format: “name == %@”, name)<br>request.predicate = NSPredicate(format: “name CONTAINS[cd]%@”, name)</p><p>如果今天資料庫中有 唯一的 id 時，format 可以直接寫成 &quot;id = \(updateID)&quot;<br>request.predicate = NSPredicate(format: &quot;id = \(updateID)&quot;)<br>就不需要字串的比對了。</p><h4>Core Data 刪除資料</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*POzVgijuAB9BLPtENQPsxg.png" /></figure><p>刪除的方法也跟上面一樣，<br>先設定 request = NSFetchRequest&lt;User&gt;(entityName: “User”)<br>然後設定要刪除的項目，predicate 後面會給他你要刪除的項目格式。<br>然後使用 context.delete(result) 刪除該項目。</p><p>如果你今天是要清空整個資料庫的內容，就不用 predicate 直接for迴圈。</p><pre><strong>func</strong> cleanUser(){<br>    <strong>let</strong> request = NSFetchRequest&lt;User&gt;(entityName: &quot;User&quot;)<br>    <strong>do</strong> {<br>        <strong>let</strong> results = <strong>try</strong> context.fetch(request)<br>        <strong>for</strong> result <strong>in</strong> results {<br>            context.delete(result)<br>        }<br>    } <strong>catch</strong> {<br>        fatalError(&quot;Could not fetch. \(error)&quot;)<br>    }<br>}</pre><p>最後呈現一下所有資料庫進行操作的結果！！！</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kQcmtcgbJDQa3GzTvBUnBg.png" /></figure><p>今天就這樣結束囉～～～</p><p>一樣附上Github連接：</p><p><a href="https://github.com/anny86023/CoreData">anny86023/CoreData</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d875cf8d4403" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Swift 串接 PTX API — 火車動態即時查詢、UISearchController]]></title>
            <link>https://anny86023.medium.com/swift-%E4%B8%B2%E6%8E%A5-ptx-api-%E7%81%AB%E8%BB%8A%E5%8B%95%E6%85%8B%E5%8D%B3%E6%99%82%E6%9F%A5%E8%A9%A2-uisearchcontroller-47b1c0775796?source=rss-d85bd75177d8------2</link>
            <guid isPermaLink="false">https://medium.com/p/47b1c0775796</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ptx]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[uisearchcontroller]]></category>
            <category><![CDATA[api]]></category>
            <dc:creator><![CDATA[Anny]]></dc:creator>
            <pubDate>Wed, 04 Nov 2020 08:42:38 GMT</pubDate>
            <atom:updated>2020-11-04T08:42:38.739Z</atom:updated>
            <content:encoded><![CDATA[<h3>Swift 串接 PTX API — 火車動態即時查詢、UISearchController</h3><p>前面介紹了 Swift 串接 API 下載資料，以及如何解析JSON 與 XML 資料了。</p><p>那這次我們就來製作 火車動態即時查詢 的APP吧！！！</p><p>本篇內容包含：</p><ul><li>串接 PTX API — 到 PTX 申請 APPID 和 APPKey</li><li>如何設定 HTTP header — Authorization 和 x-date 欄位</li><li>取得所有站名，顯示在 Table View 中</li><li>UISearchController 搭配 Table View — 用來搜尋火車站</li><li>取得車站的動態即時到站時刻 — 車次是否誤點 <a href="https://ptx.transportdata.tw/MOTC?t=Rail&amp;v=2#!/TRA/TRAApi_LiveBoard_1">(動態前後30分鐘的車次</a>)</li><li>最後會附上 Github 連結 (在最下面)</li></ul><p>先附上成果～～～</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/380/1*urMgckn-declWdP8unNcEw.gif" /></figure><p>讓我們開始吧～～～</p><blockquote>串接 PTX API — 到 PTX 申請 APPID 和 APPKey</blockquote><p>到<a href="https://ptx.transportdata.tw/PTX/Management/AccountApply"><strong>公共運輸整合資訊流通服務平台</strong></a><strong> 申請會員！！！</strong></p><p><a href="https://ptx.transportdata.tw/PTX/Management/AccountApply">公共運輸整合資訊流通服務平台 Public Transport data eXchange</a></p><p>申請一般會員就可以囉！！！將欄位填寫完畢後送出審查，通常不到一天就可以在信箱中收到結果了，通過的話則會收到 APP ID 和 APP Key。</p><p><a href="https://ptx.transportdata.tw/MOTC?t=Rail&amp;v=2#/">PTX OpenAPI Specification</a></p><blockquote>如何設定 HTTP header — Authorization 和 x-date 欄位</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EXIaBG_m9Ime159PYZwmOw.png" /></figure><p>詳細授權驗證可以到以下網站查看</p><p><a href="https://ptxmotc.gitbooks.io/ptx-api-documentation/content/api-shi-yong/hmac.html">授權驗證</a></p><p>首先是取得時間x-date — 發送 request 的時間，定義一個 function getTimeString PTX API要求格式的時間字串。</p><pre><strong>func</strong> getTimeString() -&gt; String {<br>    <strong>let</strong> dateFormater = DateFormatter()<br>    dateFormater.dateFormat = &quot;EEE, dd MMM yyyy HH:mm:ww zzz&quot;<br>    dateFormater.locale = Locale(identifier: &quot;en_US&quot;)<br>    dateFormater.timeZone = TimeZone(secondsFromGMT: 0)<br>    <strong>return</strong> dateFormater.string(from: Date())<br>}</pre><p>接下來設定 HMAC &amp; SHA 加密。import CryptoKit，可以方便寫加密相關程式。官網有提供 swift 的程式範例 ，可以到下列網址參考。</p><p><a href="https://github.com/ptxmotc/Sample-code/blob/master/Swift/HMAC-Signature/HMAC-Signature/main.swift">ptxmotc/Sample-code</a></p><p>但官網提供的加密方式採用 SHA1，但是 SHA1 比較不安全，我使用的是SHA256，也提供各位不同的加密方式作參考～～～</p><p>設定 HTTP header Authorization 和 x-date 欄位的完整程式如下：<br>APP_ID 和 APP_KEY，要輸入自己申請的 ID 和 KEY 喔～～～</p><pre><strong>let</strong> APP_ID = &quot;自己申請的ID&quot;<br><strong>let</strong> APP_KEY = &quot;自己申請的KEY&quot;</pre><pre><strong>let</strong> xdate = getTimeString() // 取得時間字串func<br><strong>let</strong> signDate = &quot;x-date: &quot; + xdate;</pre><pre><strong>let</strong> key = SymmetricKey(data: Data(APP_KEY.utf8))<br><strong>let</strong> hmac = HMAC&lt;SHA256&gt;.authenticationCode(for: Data(signDate.utf8), using: key)<br><strong>let</strong> base64HmacString = Data(hmac).base64EncodedString()<br><strong>let</strong> authorization = &quot;&quot;&quot;<br>hmac username=&quot;\(APP_ID)&quot;, algorithm=&quot;hmac-sha256&quot;, headers=&quot;x-date&quot;, signature=&quot;\(base64HmacString)&quot;<br>&quot;&quot;&quot;</pre><p>設定完後，就可以產生 URLRequest ，產生 URLSessionDataTask 抓取資料。</p><pre><strong>// 取得全部車站站名<br>let</strong> url = URL(string: &quot;https://ptx.transportdata.tw/MOTC/v2/Rail/TRA/Station?$format=JSON&quot;)!</pre><pre><strong>var</strong> request = URLRequest(url: url)<br>request.setValue(xdate, forHTTPHeaderField: &quot;x-date&quot;)<br>request.setValue(authorization, forHTTPHeaderField: &quot;Authorization&quot;)<br>request.setValue(&quot;gzip&quot;, forHTTPHeaderField: &quot;Accept-Encoding&quot;)</pre><pre>// URLSessionDataTask 抓取資料<br>URLSession.shared.dataTask(with: request) { (data, response, error) <strong>in<br>    if</strong> <strong>let</strong> loadedData = data {<br>        <strong>do</strong>{<br>            // 使用JSONDecoder解析Json格式<br>            <strong>let</strong> stationData = <strong>try</strong> JSONDecoder().decode([Station].<strong>self</strong>, from: loadedData)<br>            <strong>self</strong>.stations = stationData<br>            //寫入Stations資料 將清單存到手機中<br>            Station.saveToFile(stations: <strong>self</strong>.stations)<br>        }<strong>catch</strong>{<br>            print(error.localizedDescription)<br>        }<br>    }<br>}.resume()</pre><p>如何解析 JSON 資料 可以看先前發過的這篇文章～～～</p><p><a href="https://anny86023.medium.com/swift-%E4%B8%B2%E6%8E%A5api-%E8%A7%A3%E6%9E%90json-413750e838a5">Swift — 串接API &amp; 解析JSON</a></p><p>我在解析資料的同時，也將全部車站站名 使用 UserDefaults 存起來。</p><pre><strong>struct</strong> Station:Codable {<br>    <strong>var</strong> StationID: String<br>    <strong>var</strong> StationName: NameType</pre><pre><strong>    static</strong> <strong>func</strong> saveToFile(stations:[Station]){<br>        <strong>let</strong> saveStations = <strong>try</strong>? JSONEncoder().encode(stations)<br>        UserDefaults.standard.set(saveStations, forKey: &quot;Stations&quot;)<br>    }</pre><pre><strong>    static</strong> <strong>func</strong> loadFromFile() -&gt; [Station]?{<br>        <strong>guard</strong> <strong>let</strong> loadDate = UserDefaults.standard.data(forKey: &quot;Stations&quot;) <strong>else</strong> {<strong>return</strong> <strong>nil</strong>}<br>        <strong>return</strong> <strong>try</strong>? JSONDecoder().decode([Station].<strong>self</strong>, from: loadDate)<br>    }<br>}</pre><pre><strong>struct</strong> NameType:Codable {<br>    <strong>var</strong> Zh_tw: String<br>    <strong>var</strong> En: String<br>}</pre><blockquote>UISearchController 搭配 Table View — 用來搜尋火車站</blockquote><p>上述做完 URLSessionDataTask 解析完 JSON 資料後，將車站站名以及ID 存進 stations 中，接著在 Table View 中顯示。但是車站那麼多，我們慢慢滑的話會很耗時間，這時我們就需要 UISearchController 來搭配 Table View</p><p>來做 Table View 的搜尋列喔～～～</p><ul><li>先宣告 <strong>var</strong> searchController: UISearchController?</li><li>設置兩個存放的陣列</li><li>一個是全部的車站 <strong>var</strong> stations = [Station]()</li><li>一個是輸入文字後，搜尋到的車站 <strong>var</strong> searchStations = [Station]()</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/808/1*QDoNlMrJIj7EAjFNyixYUA.png" /></figure><p>再來 建立設置 UISearchController 的function</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NuBV7wK1Oo1rp-l-Ro3XbA.png" /></figure><ol><li>初始化SearchController</li></ol><pre>searchController = UISearchController(searchResultsController: <strong>nil</strong>)</pre><p>2. 增加 UISearchResultsUpdating 的 delegate</p><pre><strong>class</strong> TRAStationTableViewController: UITableViewController, UISearchResultsUpdating {</pre><p>並將指派屬性</p><pre>searchController?.searchResultsUpdater = <strong>self</strong></pre><p>3. 在 navigation 中加入 searchController 搜尋列</p><pre>navigationItem.searchController = searchController</pre><p>接下來設置 Table View 的 numberOfRowsInSection 和 cellForRowAt</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CdZtVLl3Ep6yUG705kRrEQ.png" /></figure><p>簡單來說就是 偵測 searchController 是否有被執行，如果有就顯示 搜尋到的車站 searchStations 。</p><p>最後就是如何 更新搜尋過後的 Table View ，要來比對字串！</p><p>使用 .filter</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Y9TVDvrGMNUkuhuw2g70Gw.png" /></figure><p>這樣基本搜尋功能就完成囉～～～</p><blockquote>取得車站的動態即時到站時刻 — 車次是否誤點 <a href="https://ptx.transportdata.tw/MOTC?t=Rail&amp;v=2#!/TRA/TRAApi_LiveBoard_1">(動態前後30分鐘的車次</a>)</blockquote><p>最後就是點選車站，顯示 <a href="https://ptx.transportdata.tw/MOTC?t=Rail&amp;v=2#!/TRA/TRAApi_LiveBoard_1">動態前後30分鐘的車次</a> 是否誤點。</p><p>在前一頁面的 Table View 需要傳值到下一頁。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*c4ED3ZjF3PB3eey8X9951A.png" /></figure><p>接著車次的資訊 我們資料格式為</p><ul><li>車站 ID</li><li>車站站名</li><li>方向 (北上南下)</li><li>火車種類 (區間、自強、莒光)</li><li>終點站</li><li>表定抵達時間</li><li>誤點 (0:準點、≥1 誤點幾分鐘)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/774/1*B_GWK_Xzb6g2UyjBVl55Mw.png" /></figure><p>下載的方式跟前面一樣，網址部分帶入 stationID</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aSez2UcKI5XNBPuAZ0GIoA.png" /></figure><p>最後就一點～文字顯示格式的部分啦～～～美化一下介面文字</p><h4>車次時間</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/342/1*UNnO-yWpWh1iRPLUS6-mkA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vk3XChfaGi6puLECJgHQyA.png" /></figure><h4>車次 準點或誤點的提示</h4><p>利用 layer.masksToBounds = <strong>true</strong></p><p><strong>以及 </strong>layer.cornerRadius = 10 來設置背景圓角～～～</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/904/1*eegZeV244OzsCUVZSSoSlQ.png" /></figure><p>最後附上完整 Github 連結～～～</p><p><a href="https://github.com/anny86023/TaiwanRailway">anny86023/TaiwanRailway</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=47b1c0775796" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Swift — 串接API & 解析XML 資料]]></title>
            <link>https://anny86023.medium.com/swift-%E4%B8%B2%E6%8E%A5api-%E8%A7%A3%E6%9E%90xml-%E8%B3%87%E6%96%99-2a5fd3770884?source=rss-d85bd75177d8------2</link>
            <guid isPermaLink="false">https://medium.com/p/2a5fd3770884</guid>
            <category><![CDATA[xml]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[api]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[urlsessions]]></category>
            <dc:creator><![CDATA[Anny]]></dc:creator>
            <pubDate>Sun, 01 Nov 2020 11:53:23 GMT</pubDate>
            <atom:updated>2020-11-01T11:53:23.466Z</atom:updated>
            <content:encoded><![CDATA[<h3>Swift — 串接API &amp; 解析XML 資料</h3><p>上一次串接API 說明了解析JSON資料的方式～～～</p><p><a href="https://anny86023.medium.com/swift-%E4%B8%B2%E6%8E%A5api-%E8%A7%A3%E6%9E%90json-413750e838a5">Swift — 串接API &amp; 解析JSON</a></p><p>這一次要來說明如何解析 XML 資料！！！</p><p>今天的例子為：</p><p><a href="https://www.cnet.com/rss/"><strong>RSS Feeds</strong></a><strong> — 新聞閱讀文章</strong></p><p><a href="https://www.cnet.com/rss/">RSS Feeds</a></p><p>API網址：<a href="https://www.cnet.com/rss/news/">https://www.cnet.com/rss/news/</a></p><p>成果如下：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/360/1*tBx9Acb8KYD-Dz1vKFIYnQ.gif" /></figure><ol><li>先決定要取得什麼資料</li><li>新聞標題title — 放在 table view 中顯示新聞標題</li><li>新聞網址link — 點擊新聞標題進入新聞內容(link連結顯示網頁)</li></ol><ul><li>如何使用URLSession、Data下載資料 <br>如何顯示網頁 以及 loading 符號<br>可以看這篇文章～～～</li></ul><p><a href="https://anny86023.medium.com/%E4%BD%BF%E7%94%A8urlsession-data%E4%B8%8B%E8%BC%89%E8%B3%87%E6%96%99-%E5%A6%82%E4%BD%95%E9%A1%AF%E7%A4%BA%E7%B6%B2%E9%A0%81-b616327e3be8">使用URLSession、Data下載資料、如何顯示網頁</a></p><blockquote>解析XML資料 — XMLParserDelegate 程式碼</blockquote><ol><li>建立一個 新檔案 XMLParserDelegate 程式碼</li><li>創立 ParserDelegate class</li><li>要先服從 NSObject 才能服從 XMLParserDelegate</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/984/1*VJqdfUlC9v75rjUnERz10g.png" /></figure><ul><li>我們需要解析的 tag 有 item 裡面的 title 和 link</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Gib0zYAqtvYpS5dBNejq1Q.png" /></figure><ul><li>新增三個屬性</li></ul><ol><li><strong>var</strong> currentItem: NewsItem? — 目前物件(型態為NewsItem 有title &amp; link)</li><li><strong>var</strong> currentElementValue: String? — 目前元素內容值(存放解析後的字串)</li><li><strong>var</strong> resultArray = [NewsItem]() — 最後取得的新聞陣列</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Sf_vx5J8ni6f_GFOPuN-5Q.png" /></figure><ul><li><strong>func</strong> parser(<strong>_</strong> parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:])</li><li>碰到標籤tag &lt;&gt; 就會觸發 開始解析</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yfaAT4RDdOPB-sZdEfz2Dg.png" /></figure><ol><li>如果 tag 為 item — 開始一個 新的新聞 currentItem = NewsItem()</li><li>如果 tag 為 title— 將目前元素內容值設為nil(重新開始)</li><li>如果 tag 為 link— 將目前元素內容值設為nil(重新開始)</li></ol><p>— — — — — — — — — — — — — — — — — — — — — — — — — — — — — —</p><ul><li><strong>func</strong> parser(<strong>_</strong> parser: XMLParser, foundCharacters string: String)</li><li>碰到標籤的內文字母時 就會開時讀取</li><li>將解析的字串累加放進 currentElementValue</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tVfwQk2aqcMWarJkhoeGsA.png" /></figure><ul><li><strong>func</strong> parser(<strong>_</strong> parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?)</li><li>再次 碰到相同標籤tag&lt;/&gt;就會觸發 結束這段解析</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TtIYOlz0D0-412B6tVSSZQ.png" /></figure><ol><li>如果 tag 為 item — 將目前新聞內容(currentItem)append放進resultArray<br>並將目前元素內容值(currentElementValue)重新設為nil</li><li>如果 tag 為 title — 將currentElementValue，放進currentItem.title</li><li>如果 tag 為 link — 將currentElementValue，放進currentItem.link</li></ol><p>完整流程執行順序如下～～～</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dmZD8bHPOiwr0pjCKj9QEA.png" /></figure><ul><li>最後設置一個取得資料的func</li><li>回傳 resultArray 取得所有新聞的title 和 link</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/668/1*EFDMcHpxESSTMK9m5Or4vw.png" /></figure><ul><li>接著回到原本的程式碼中 <strong>下載 XML 資料</strong></li></ul><pre><strong>if</strong> <strong>let</strong> loadData = data{<br>    <strong>let</strong> parser = XMLParser(data: loadData)<br>    <strong>let</strong> rssParserDelegate = RssParserDelegate()<br>    parser.delegate = rssParserDelegate<br>    <strong>if</strong> parser.parse() == <strong>true</strong>{<br>        <strong>self</strong>.news = rssParserDelegate.getResult()<br>        DispatchQueue.main.async {<br>            <strong>self</strong>.myTableView.reloadData()<br>        }<br>    }<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2Jgszx4mNJu_4qKhVQAmyg.png" /></figure><p>如果還有不懂的地方</p><p>完整程式碼可以看這裡～～～</p><p><a href="https://github.com/anny86023/RSSReader">anny86023/RSSReader</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2a5fd3770884" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Swift — 串接API & 解析JSON]]></title>
            <link>https://anny86023.medium.com/swift-%E4%B8%B2%E6%8E%A5api-%E8%A7%A3%E6%9E%90json-413750e838a5?source=rss-d85bd75177d8------2</link>
            <guid isPermaLink="false">https://medium.com/p/413750e838a5</guid>
            <category><![CDATA[api]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[urlsessions]]></category>
            <category><![CDATA[json]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Anny]]></dc:creator>
            <pubDate>Fri, 30 Oct 2020 08:47:24 GMT</pubDate>
            <atom:updated>2020-10-30T08:47:24.940Z</atom:updated>
            <content:encoded><![CDATA[<h3>Swift — 串接API &amp; 解析JSON</h3><p>使用 Decodable 協議方式，來串接 API 還有解析 JSON格式資料。</p><p>今天以一個簡單的 API 為例子：</p><p><a href="https://randomuser.me/"><strong>Random User Generator</strong></a><strong> — 隨機產生使用者資訊</strong></p><p><a href="https://randomuser.me/">Random User Generator</a></p><p>API網址：<a href="https://randomuser.me/api/">https://randomuser.me/api/</a></p><p>很好取得的 API 不需要註冊填寫資料，是個方便入手的學習＆講解的材料！！！</p><p>成果如下：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/359/1*-dKWWYVwfVPyOKoQB9wi0A.gif" /></figure><blockquote><a href="https://anny86023.medium.com/%E4%BD%BF%E7%94%A8urlsession-data%E4%B8%8B%E8%BC%89%E8%B3%87%E6%96%99-%E5%A6%82%E4%BD%95%E9%A1%AF%E7%A4%BA%E7%B6%B2%E9%A0%81-b616327e3be8"><strong>URLSession、Data下載資料</strong></a></blockquote><p>可以先看這篇～如何下載資料～以下載圖片為例</p><p><a href="https://anny86023.medium.com/%E4%BD%BF%E7%94%A8urlsession-data%E4%B8%8B%E8%BC%89%E8%B3%87%E6%96%99-%E5%A6%82%E4%BD%95%E9%A1%AF%E7%A4%BA%E7%B6%B2%E9%A0%81-b616327e3be8">使用URLSession、Data下載資料、如何顯示網頁</a></p><p>那如果今天我們下載的資料是JSON格式，我們必須先解析他，才能取得裡面的資料～～～</p><blockquote>來解析JSON吧</blockquote><ol><li>先看懂資料的JSON格式</li><li>選擇自己要的資料有哪些 是在 {}字典中 還是 []陣列中</li></ol><ul><li>注意：搞清楚格式很重要！！！</li><li>注意：解析的名字需與JSON格式 一模一樣！！！</li></ul><p>來看以下範例～～～</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VXYbiWri4Gi--lGtp0XQyg.png" /></figure><ul><li>我們需要的資料有 — 整個 {} 字典中，key 為 results 裡面的資料</li><li>程式碼解析 results ，類型先設為Any (因為內部還有資料，等等再設定)</li></ul><pre><strong>struct</strong> AllData: Codable {<br>    <strong>var</strong> results: Any?<br>}</pre><ul><li>接下來是 results 中，key 為 name、email、phone、picture</li><li>程式碼解析 name、email、phone、picture</li><li>email、phone 格式為 String</li><li>name、picture 類型先設為Any (因為內部還有資料，等等再設定)</li></ul><pre><strong>struct</strong> SingleData: Codable {<br>    <strong>var</strong> name: Any?<br>    <strong>var</strong> email: String<br>    <strong>var</strong> phone: String<br>    <strong>var</strong> picture: Any?<br>}</pre><ul><li>name {} 字典中我們需要的 key 為 first、last</li><li>程式碼解析 first、last 格式為 String</li><li>picture {} 字典中我們需要的 key 為 large</li><li>程式碼解析 large 格式為 String</li></ul><pre><strong>struct</strong> Name: Codable {<br>    <strong>var</strong> first: String<br>    <strong>var</strong> last: String<br>}<br><strong>struct</strong> Picture: Codable {<br>    <strong>var</strong> large: String<br>}</pre><ul><li>完成後我們返回將前面的格式設定好</li><li>name 格式為 Name</li><li>picture 格式為 Picture</li><li>results 格式為 [SingleData]</li><li>注意！！！results 內部是一個格式為SingleData 的 [] 陣列</li></ul><p>完整程式碼 與 JSON格式對照！！！</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*b96fWLdPqa8R_rlqSbMS-A.png" /></figure><p>格式設定好後，來用程式解析吧～～～</p><ul><li>使用URLSession.shared.dataTask 取得資料後呢</li><li>使用 <strong>try</strong> JSONDecoder().decode(AllData.<strong>self</strong>, from: loadedData)</li><li>解析 格式為 AllData</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*O9eSnVmg_v_i_7tnYGbiLw.png" /></figure><ul><li>再來就是 利用picture 取得的網址，來下載使用者照片囉～～～</li><li>下載圖片使用 — URLSession.shared.downloadTask</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wCNPUfPSMCoSTyIe8jMnHQ.png" /></figure><blockquote>如何讓照片用成圓形</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9Mf--eDfDgLYob6VSjbXjQ.png" /></figure><blockquote>設置刷新按鍵</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/622/1*QDC01omaubDP6dVQD3OidQ.png" /></figure><p>我在 bar 上面有設置一個刷新使用者的按鈕</p><ul><li>連接一個新action</li><li>設置 <strong>var</strong> isDownloading = <strong>false</strong></li><li><strong>記錄是否在正在下載，沒有下載時才刷新，正在下載則不能刷新</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*e3UwIW6iBUNn1z3YvBqQ7A.png" /></figure><p>詳細完整程式碼可以到我的Github看～～～</p><p><a href="https://github.com/anny86023/RadomUser">anny86023/RadomUser</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=413750e838a5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[使用URLSession、Data下載資料、如何顯示網頁]]></title>
            <link>https://anny86023.medium.com/%E4%BD%BF%E7%94%A8urlsession-data%E4%B8%8B%E8%BC%89%E8%B3%87%E6%96%99-%E5%A6%82%E4%BD%95%E9%A1%AF%E7%A4%BA%E7%B6%B2%E9%A0%81-b616327e3be8?source=rss-d85bd75177d8------2</link>
            <guid isPermaLink="false">https://medium.com/p/b616327e3be8</guid>
            <category><![CDATA[wkwebview]]></category>
            <category><![CDATA[urlsessiondatatask]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[urlsessions]]></category>
            <dc:creator><![CDATA[Anny]]></dc:creator>
            <pubDate>Sun, 25 Oct 2020 07:28:29 GMT</pubDate>
            <atom:updated>2021-07-21T03:44:02.427Z</atom:updated>
            <content:encoded><![CDATA[<h3>使用URLSession、Data下載資料、如何顯示網頁 WKWebView</h3><p>除了在手機本身上操作之外，我們也需要從網路下載資料到手機中。</p><p>可能是JSON文字檔或是圖片等等…</p><p>今天來看看如何顯示網頁～以及如何下載資料吧～～～</p><p>本篇內容包含：</p><ul><li>連結網路顯示網頁 — WKWebView</li><li>等待網頁 Loading 的符號 — UIActivityIndicatorView</li><li>使用 Data 下載資料</li><li>使用 URLSession 下載資料 — DataTask &amp; DownloadTask</li><li>處理沒有連網的錯誤</li></ul><blockquote>連結網路顯示網頁 — WKWebView</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/332/1*yVAmQZUrrh5yZRFJRhyPUQ.png" /></figure><ol><li>在 Main.storyboard 加入 WebKit View</li><li>連結到程式碼中 然後記得 <strong>import</strong> WebKit 才能使用喔！</li><li>將網址轉成 URL</li><li>將 URL 放進 myWebView.load()</li></ol><pre><strong>@IBOutlet</strong> <strong>weak</strong> <strong>var</strong> myWebView: WKWebView!</pre><pre><strong>override</strong> <strong>func</strong> viewDidLoad() {<br>    <strong>super</strong>.viewDidLoad()</pre><pre><strong>    if</strong> <strong>let</strong> url = URL(string: &quot;https://www.youtube.com/&quot;){<br>        <strong>let</strong> request = URLRequest(url: url)<br>        myWebView.load(request)<br>    }<br>}<br></pre><p>有些網址會有打不開的情況，這時候就需要去 Info.plist 修改設定。</p><ol><li>在 Info.plist 中增加 App Transport Security Settings</li><li>在裡面(打開三角形) 再次新增 Allow Arbitrary Loads</li><li>並將原本設定的 No 改成 Yes !!!</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*r_InI3hhdeu61iphNeFdwQ.png" /></figure><blockquote>等待網頁 Loading 的符號 — UIActivityIndicatorView</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/392/1*nc79ynmFZ9f11JWYsntVtw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/452/1*YdIrhGpElc9Bl2RMHVEiIw.png" /></figure><ol><li>在 Main.storyboard 加入 Activity Indicator View</li><li>將設定中的 Hides When Stopped 打勾，這樣當程式碼呼叫停止時 ，Loading 的符號就會隱藏。</li></ol><p>再來就是！我們要讓網頁在讀取載入時，出現等待網頁的 Loading 符號。</p><p>所以，我們要得知網頁正在載入中，以及網頁什麼時候載入成功。</p><p>WKWebView在讀取網頁的過程中，如需要用到navigationDelegate。</p><ol><li>要先服從 WKNavigationDelegate 的協定</li><li>接著 myWebView.navigationDelegate = <strong>self</strong></li><li>使用兩個 func didStartProvisionalNavigation &amp; didFinish navigation</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rluRE0UgQbAaf4A-t5UgmQ.png" /></figure><p>接著進入重點啦！！！要來使用URLSession、Data下載資料了！！！</p><blockquote>使用 Data 下載資料</blockquote><ol><li>先將網址放進常數中</li><li>使用 URL 將剛剛存好的網址型態轉成URL</li><li>將 URL 放進 Data 中下載資料 (需要使用try do catch)</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3SAV8QP1TRWxIWZAxmmbuQ.png" /></figure><ul><li>在主佇列上(main queue)上 — 要放非同步</li><li>在共時佇列上(global queue)上 — 同步非同步都可</li></ul><p>下載圖片要在共時佇列上( global queue)上，才不會讓main的畫面卡住不動，載好之後 再到 main queue上 更新畫面！</p><blockquote>使用 URLSession 下載資料 — DataTask &amp; DownloadTask</blockquote><blockquote>DataTask— 使用在連結網站拿到文字XML或JSON — 因為回傳data</blockquote><ol><li>建立 URLSession</li><li>將網址放進常數中</li><li>使用 URL 將剛剛存好的網址型態轉成URL</li><li>接著開一個task — 使用 URLSession.dataTask 後面帶入 URL 以及一個Closure函式</li><li>data 就是我們下載到的資料啦</li><li>最後記得呼叫 task.resume()</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xQh34m8YAYsYsm0bnVVnUQ.png" /></figure><p>URLSession 跟 Data 下載方式不一樣的地方在於</p><ul><li>URLSession 是在 共時佇列上(global queue)下載</li><li>Data 是在主佇列上(main queue)下載</li></ul><p>所以 只需要在最後顯示時回到 主佇列上(main queue)上</p><blockquote>使用 URLSession 下載資料</blockquote><blockquote>DownloadTask — 使用在下載圖片 — 因為回傳url</blockquote><p>基本上 DownloadTask 的使用方式跟 DataTask 一樣</p><p>差別在於 DownloadTask Closure函式 回傳的是url</p><ul><li>DataTask — 使用在連結網站拿到文字XML或JSON — 因為回傳data</li><li>DownloadTask — 使用在下載圖片 — 因為回傳url</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bCDvyL5VNqQiHHkx1moIGg.png" /></figure><blockquote>處理沒有連網的錯誤</blockquote><p>最後簡單說明一下，如何判斷沒有網路連線的錯誤</p><ol><li>先將 error 使用 NSError.code 得到錯誤資訊</li><li>error 錯誤訊號為 -1009 代表 沒有網路連線</li><li>show 一個Alert 警告視窗，提醒告知使用者沒有網路連線</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OsT-xh_E_6LW7WKdFLWpKA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_hNhgxqgL-dULtixX9ID3w.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b616327e3be8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[To Do List — CRUD、UserDefault資料儲存、設定時間與推播通知]]></title>
            <link>https://anny86023.medium.com/to-do-list-crud-userdefault%E8%B3%87%E6%96%99%E5%84%B2%E5%AD%98-%E8%A8%AD%E5%AE%9A%E6%99%82%E9%96%93%E8%88%87%E6%8E%A8%E6%92%AD%E9%80%9A%E7%9F%A5-ac47772f6f66?source=rss-d85bd75177d8------2</link>
            <guid isPermaLink="false">https://medium.com/p/ac47772f6f66</guid>
            <category><![CDATA[userdefaults]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[notifications]]></category>
            <category><![CDATA[to-do-list]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Anny]]></dc:creator>
            <pubDate>Fri, 23 Oct 2020 05:47:32 GMT</pubDate>
            <atom:updated>2020-10-23T05:47:32.351Z</atom:updated>
            <content:encoded><![CDATA[<h3>To Do List — CRUD、UserDefault資料儲存、設定時間與推播通知</h3><h4>來介紹 To Do List 功能如何實作吧～～～</h4><h4>先來看看成果：To Do List 功能包含</h4><blockquote>Part.1</blockquote><ul><li>新增、刪除事項</li><li>To Do List Button 打勾完成事項 (需判斷UIButton所在Cell位置)</li><li>兩個 Section 一個代辦事項、一個完成事項 (兩個 Section 互相傳遞)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/325/1*msmh6d4sRZqayPl4diUKAA.gif" /></figure><blockquote>Part.2</blockquote><ul><li>編輯 List 順序 (兩個 Section 不能互相編輯！固定在自己Section內移動！)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/325/1*eL6kYwa2IxvPmss_L9vmyA.gif" /></figure><blockquote>Part.3</blockquote><ul><li>換頁更改代辦事項、加入備註note</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/325/1*qskLGcisGyrjgHrF2sD9nA.gif" /></figure><blockquote>Part.4</blockquote><ul><li>UserDefault 存取與讀取資料</li><li>剛剛編輯的List順序，修改的代辦事項還有備註，關掉 App 重新開啟資料依然都在。</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/325/1*IxN4zjObUfixNjq5st0NOQ.gif" /></figure><blockquote>Part.5</blockquote><ul><li>設定日期、推播通知 (實機測試！！！)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/296/1*9VY5yp9XraE8TG00Bn6odA.gif" /></figure><blockquote>Part.6</blockquote><ul><li>使用Switch 控制 Cell 顯示/隱藏</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/325/1*bgUDxAV7DUSxYTYs8QW7ww.gif" /></figure><blockquote>Part.7</blockquote><ul><li>設置App icon 和啟動畫面</li></ul><p>可以看這篇文章～～～</p><p><a href="https://anny86023.medium.com/ios-app-icon-%E5%95%9F%E5%8B%95%E7%95%AB%E9%9D%A2-6dbc735a2bd4">Ios App icon &amp; 啟動畫面</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/325/1*iz6lnRW75iG3nnNNEQ4z1w.gif" /></figure><h3>Part.1</h3><ul><li>To Do List Button 打勾完成事項 (需判斷UIButton所在Cell位置)</li></ul><ol><li>TableView動態 cell 如何連結Button~~~ 看這篇</li></ol><p><a href="https://anny86023.medium.com/tableview%E5%8B%95%E6%85%8B-%E9%9D%9C%E6%85%8Bcell-%E4%BB%A5%E5%8F%8A%E9%8C%AF%E8%AA%A4%E8%A8%8A%E6%81%AF-outlets-cannot-be-connected-to-repeating-content-ios-30804931c983">TableView動態&amp;靜態cell, 以及錯誤訊息“Outlets cannot be connected to repeating content iOS”</a></p><p>2. 如何判斷UIButton所在Cell位置~~~ 看這篇</p><p><a href="https://anny86023.medium.com/tableviewcell%E5%81%B5%E6%B8%ACbutton%E9%BB%9E%E6%93%8A%E7%9A%84%E6%96%B9%E6%B3%95-3c35c837fe82">TableViewCell偵測Button點擊的方法</a></p><ul><li>兩個 Section 一個代辦事項、一個完成事項 (兩個 Section 互相傳遞)</li></ul><p>上方 UNDONE Section 按下 cell Button 移至下方 FINISH Section</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hsTEWMLBuzerzzoYbUdCiQ.png" /></figure><ul><li>完成代辦事項移至下方 FINISH Section 字體“灰色斜體刪除線”</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-iWnndb16FK9kQBnxQQ31w.png" /></figure><p>完整 NSMutableAttributedString屬性 可以看這篇～～～</p><p><a href="https://anny86023.medium.com/uilabel%E4%B8%8D%E5%90%8C%E7%9A%84%E9%A1%8F%E8%89%B2%E8%88%87%E5%AD%97%E5%9E%8B-nsmutableattributedstring%E5%B1%AC%E6%80%A7%E6%87%89%E7%94%A8-7e1d93357dc9">UILabel不同的顏色與字型 — NSMutableAttributedString屬性應用</a></p><h3>Part.2</h3><ul><li>編輯 List 順序 (兩個 Section 不能互相編輯！固定在自己Section內移動！)</li></ul><ol><li>先控制讓兩個 Section 不能互相編輯！固定在自己Section內移動！</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zO8xFBg-UnoBpIbeRSERkQ.png" /></figure><p>2. 要編輯 List 順序需要使用 moveRowAt fromIndexPath</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LMEFGBy2NcOT7LrV6TfO7Q.png" /></figure><h3>Part.3</h3><ul><li>換頁更改代辦事項、加入備註note</li></ul><ol><li>換頁 要將 ToDoList的title傳到下一頁的text_Input</li><li>使用 prepare 將 segue.destination 目標指定給下一個頁面DetailTableViewController</li><li>就可以取得 DetailTableViewController 裡面設置的數值</li><li>將點選的todolist 值 傳給下一頁</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GqnNW0iXCef_bSQcCWYCfg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NvqqLosIgASy2Sa75WTN6A.png" /></figure><ul><li>下一頁 修改項目以及設定時間的頁面 &gt;&gt;&gt; DetailTableViewController</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IJR4sxXf_Y_hs5r4bAuQ6Q.png" /></figure><ul><li>檢查代辦事項是否為空！！！提醒使用者要輸入！！！</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PcDU7ZdWpA-FC55-L4vpcg.png" /></figure><h3>Part.4</h3><ul><li>UserDefault 存取與讀取資料</li><li>剛剛編輯的List順序，修改的代辦事項還有備註，關掉 App 重新開啟資料依然都在。</li><li>使用 Codable 把資料存成自己想要的形式</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5iA_64SuzrOcXL4NnrQ-0Q.png" /></figure><p>讀取時</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*e8Myi_4_73_sGGVHKpMbNQ.png" /></figure><p>寫入時</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/950/1*msCK2F7LBYmt08-FlEorgQ.png" /></figure><h3>Part.5</h3><ul><li>設定日期、推播通知 (實機測試！！！)</li><li>在 AppDelegate 的 didFinishLaunchingWithOptions launchOptions 中加入</li><li>詢問使用者是否願意收到通知的操作</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CgNnMKzfmCj_03LwsorD7A.png" /></figure><ul><li>提醒推播通知</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WBoj2mE7q5hgxo6PXiVQSw.png" /></figure><ul><li>在 主程式碼 中加入</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TYShCxiTJK0fgpd96slB7w.png" /></figure><p>詳細可以看 推播操作可以看這篇～～～</p><p><a href="https://www.appcoda.com.tw/ios10-user-notifications/">結合 iOS 10 的 User Notifications：傳送米花兒的幸福打氣通知</a></p><h3>Part.6</h3><ul><li>使用Switch 控制 Cell 顯示/隱藏</li><li>在這裡使用的是 heightForRowAt indexPath</li><li>設定 cell 的高度</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4mbFQgKfpuZusGekzE_PnQ.png" /></figure><ul><li>注意！！！ 在 switch 中變化時要加入 以下程式碼</li><li>(我當初因為這個卡很久QAQ)</li></ul><pre>tableView.beginUpdates()</pre><pre>tableView.endUpdates()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8yR6dPkDLQWNdYy2pXlR2A.png" /></figure><p>完整程式碼可以參考Github</p><p><a href="https://github.com/anny86023/ToDoList">anny86023/ToDoList</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ac47772f6f66" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Ios App icon & 啟動畫面]]></title>
            <link>https://anny86023.medium.com/ios-app-icon-%E5%95%9F%E5%8B%95%E7%95%AB%E9%9D%A2-6dbc735a2bd4?source=rss-d85bd75177d8------2</link>
            <guid isPermaLink="false">https://medium.com/p/6dbc735a2bd4</guid>
            <category><![CDATA[icons]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[launch-screen]]></category>
            <category><![CDATA[ios]]></category>
            <dc:creator><![CDATA[Anny]]></dc:creator>
            <pubDate>Thu, 22 Oct 2020 06:47:15 GMT</pubDate>
            <atom:updated>2020-10-22T06:47:15.917Z</atom:updated>
            <content:encoded><![CDATA[<p>做完 App 功能之後～～～就要來美化囉～～～</p><p>來看看如何在 App 加入 icon 還有開啟 App 時的啟動畫面吧。</p><p>今天內容有：</p><ul><li>如何製作 — icon</li><li>如何製作 — 啟動畫面</li></ul><blockquote>如何製作 — icon</blockquote><p>有兩個推薦的網址~~~</p><p>可以將你製作好的 icon 圖丟進去，生成 ios app需要的格式。</p><p>注意！圖片最好是做成正方形的，圖片才不會變形喔～～～</p><ul><li><a href="https://appicon.co/">App Icon Generator</a></li><li><a href="https://makeappicon.com/">Developer Tool - App Icon Resizer</a></li></ul><p><a href="https://appicon.co/"><strong>App Icon Generator</strong></a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oG6eVzek0xwGlkIecI1-tg.png" /></figure><ol><li>製作好之後，放進<a href="https://appicon.co/"><strong>App Icon Generator</strong></a>，下載檔案後解壓縮。</li><li>在 Xcode 專案中，將 Assets.xcassets 裡的 AppIcon 刪除。</li><li>解壓縮的資料夾找到 AppIcon.appiconset 檔案夾，圖片資料都在裡面。</li><li>AppIcon.appiconset這個檔案夾 放進 Assets.xcassets 。</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jOiB46BNSlkXIox1sfr3AQ.png" /></figure><p><a href="https://makeappicon.com/"><strong>Developer Tool — App Icon Resizer</strong></a></p><ol><li>製作好之後，放進<strong>烤箱機中 XD</strong>，檔案會寄到e-mail中後下載解壓縮。</li><li>在 Xcode 專案中，進入Assets.xcassets 裡的 AppIcon 。</li><li>將下載的圖片貼進 AppIcon 中的 iPhone App iOS 7–13 60pt。</li><li>注意！有分 2x 和3x 喔！不要貼錯喔～～～</li></ol><blockquote>如何製作 — 啟動畫面</blockquote><p>製作啟動畫面也是很簡單的～～～</p><ol><li>進入LaunchScreen.storyboard</li><li>接著就像平常在做畫面一樣，製作imageView和Label等等…</li><li>這樣就完成一個簡單的 啟動畫面囉～～～</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*f-x-aaR-rsHZwzGWTz-gVQ.png" /></figure><p>完成成果如下～～～</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/325/1*iz6lnRW75iG3nnNNEQ4z1w.gif" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6dbc735a2bd4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[UITableView Swipe Action — 客製化滑動按鈕動作]]></title>
            <link>https://anny86023.medium.com/uitableview-swipe-action-%E5%AE%A2%E8%A3%BD%E5%8C%96%E6%BB%91%E5%8B%95%E6%8C%89%E9%88%95%E5%8B%95%E4%BD%9C-7b9e90155815?source=rss-d85bd75177d8------2</link>
            <guid isPermaLink="false">https://medium.com/p/7b9e90155815</guid>
            <category><![CDATA[uitableview]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[swipeaction]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Anny]]></dc:creator>
            <pubDate>Mon, 19 Oct 2020 08:37:00 GMT</pubDate>
            <atom:updated>2020-10-19T08:37:00.857Z</atom:updated>
            <content:encoded><![CDATA[<h3>UITableView Swipe Action — 客製化滑動按鈕動作</h3><p>今天來聊聊 UITableView Swipe Action 滑動按鈕動作吧～～～</p><p>向左向右滑動，會出現功能按鈕，還要執行動作～～～</p><blockquote>如下圖顯示</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5yYnBxD7BfimxH86ubAv6g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*TAYrs3KTo9N9EVXemFDJ-A.gif" /></figure><p>原本的版本為</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rmC2ACh61U3T4VBS_WXZwQ.png" /></figure><p>在 iOS 11，更新了！！！</p><p>使用 UIContextualAction</p><p>tableView(_:trailingSwipeActionsConfigurationForRowAt) 以及tableView(_:leadingSwipeActionsConfigurationForRowAt)</p><p>就可以實現 TableView 左滑和右滑 顯示多個可點選執行的 button。</p><p>今天更新版本與舊的版本皆會做介紹～～～</p><blockquote>如果只是要普通刪除按鈕用法</blockquote><p>使用commit editingStyle 即可</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/274ae85b37cfceb7d826dfafe69319a8/href">https://medium.com/media/274ae85b37cfceb7d826dfafe69319a8/href</a></iframe><blockquote>客製化向<strong>左滑按鈕</strong></blockquote><p>想要顯示其他文字的<strong>按鈕</strong></p><p>使用以下function</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XE7CG3cM4R2QTyCKnuAYUw.png" /></figure><p>使用 UIContextualAction 來製作按鈕</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lmc_wI_lPGXNS7eAInF4Sw.png" /></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4f5cc0ce2ae41fd3d0dd4e02cea12fdf/href">https://medium.com/media/4f5cc0ce2ae41fd3d0dd4e02cea12fdf/href</a></iframe><blockquote>客製化向右<strong>滑按鈕</strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CY6MI2Wh3m1f6aEF8t5iNA.png" /></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e2a101d44198a7c2093383a31daa61d2/href">https://medium.com/media/e2a101d44198a7c2093383a31daa61d2/href</a></iframe><p>其他注意事項～～～</p><ol><li>記得要呼叫 UIContextualAction 最後一個Bool 回傳值，不然點選 button 後cell 不會回到正常顯示的狀態。</li><li>Button 可以更改背景顏色與增加圖片。</li><li>若要防止 cell 滑到底之後，不小心觸發第一個按鈕，可以將 performsFirstActionWithFullSwipe 設為 false。</li></ol><pre><strong>let</strong> prevention = UISwipeActionsConfiguration(actions: [doneAction, undoneAction])</pre><pre>prevention.performsFirstActionWithFullSwipe = <strong>false</strong></pre><pre><strong>return</strong> prevention</pre><blockquote>舊版 editActionsForRowAt 與 UITableViewRowAction</blockquote><p>最後介紹一下舊版本，目前是只能左滑。</p><p>使用以下function</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CQT6pHb9Uyk9NHUt30YqeA.png" /></figure><p>使用 UITableViewRowAction 來製作按鈕</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DWkeOvWg0hSoYebPTKkBgQ.png" /></figure><pre><strong>override</strong> <strong>func</strong> tableView(<strong>_</strong> tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -&gt; [UITableViewRowAction]? {</pre><pre><strong>    let</strong> doneAction = UITableViewRowAction(style: .normal, title: &quot;完成了&quot;) { (UITableViewRowAction, IndexPathindex:IndexPath) <strong>in<br>        self</strong>.label.text = <strong>self</strong>.todo[indexPath.row] + &quot; 完成了&quot;<br>    }</pre><pre><strong>    let</strong> undoneAction =  UITableViewRowAction(style: .destructive, title: &quot;未完成&quot;) { (UITableViewRowAction, IndexPathindex:IndexPath) <strong>in<br>        self</strong>.label.text = <strong>self</strong>.todo[indexPath.row] + &quot; 未完成&quot;<br>    }</pre><pre><strong>return</strong> [doneAction, undoneAction]</pre><pre>}</pre><p>完整程式碼</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9cc90a2e07dbdf0e7a6a6086d1622bab/href">https://medium.com/media/9cc90a2e07dbdf0e7a6a6086d1622bab/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7b9e90155815" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>