<?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 Jimmy Chen on Medium]]></title>
        <description><![CDATA[Stories by Jimmy Chen on Medium]]></description>
        <link>https://medium.com/@jimmy-grow-note?source=rss-c9610a3f53a1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*mGhw7LG_xEn1HEhjRWW-EA.png</url>
            <title>Stories by Jimmy Chen on Medium</title>
            <link>https://medium.com/@jimmy-grow-note?source=rss-c9610a3f53a1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 15:17:33 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jimmy-grow-note/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[How to resize UIImage with Swift]]></title>
            <link>https://medium.com/ios-app%E9%96%8B%E7%99%BC%E4%B9%8B%E4%B8%96%E7%95%8C%E9%80%99%E9%BA%BC%E5%A4%A7/how-to-resize-uiimage-with-swift-8718657306ec?source=rss-c9610a3f53a1------2</link>
            <guid isPermaLink="false">https://medium.com/p/8718657306ec</guid>
            <category><![CDATA[uiimage-scale]]></category>
            <category><![CDATA[uiimage-resizing]]></category>
            <category><![CDATA[resize-uiimage]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[uiimage]]></category>
            <dc:creator><![CDATA[Jimmy Chen]]></dc:creator>
            <pubDate>Tue, 06 Dec 2022 06:53:01 GMT</pubDate>
            <atom:updated>2022-12-08T13:25:45.299Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/770/0*hPiO_7r6cw4711JI.png" /><figcaption>Image by Pixabay</figcaption></figure><blockquote>First, let’s talk about why we have the requirement of resizing UIImage .</blockquote><p>When we upload or download the image data, if the image data size is smaller the task will take less time, so we can resize the UIImage to boost its processing speed, or even we can compress UIImage quality to make its data even smaller.</p><blockquote>I will extend UIImage, this can let us more conveniently to call the func.</blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/533ae83e4239218a57e1aa4e9c8a85a1/href">https://medium.com/media/533ae83e4239218a57e1aa4e9c8a85a1/href</a></iframe><p>The above code gives a new width and the UIImage will scale to fit the new width, so you just call it like following.</p><pre>let originImage = UIImage(named:&quot;apple&quot;)<br>let resizeImage = originImage?.scale(newWidth: 640)</pre><p>I think the code before row 12 you can understand, but you may be confused about ` UIGraphicsBeginImageContextWithOptions(_ size: CGSize, _ opaque: Bool, _ scale: CGFloat)`, it’s all right, I will explain it in the next paragraph.</p><blockquote>What the meaning of UIGraphicsBeginImageContextWithOptions(_:_:_:)</blockquote><p>The function will open a drawing space from the UIImage and draw in the space, you can see there are three parameters, let’s talk about what is each of these for.</p><ul><li>size： Set the UIImage new size, such as. 1980x1024 or 640x480.</li><li>opaque： It means the UIImage will become transparent, if set to true it means not transparent, if set to false it means transparent, normally I set it to true.</li><li>scale： The parameter meaning scale factor, if set to 0, it means not scale, normally you can set it to 0.</li></ul><p>Now, you have some sense of code at row 13. Then let&#39;s see other rows, row 14 which means implement draw.</p><p>The code of row 16 means to get the UIImage from the current draw space and named it newImage.</p><p>Finally, return the newImage if there is some error, it will return the origin UIImage.</p><blockquote>Above is today’s article, stay tuned, I will write more of these article and share to you, if the article can save you a little bit time, please give me a clap, thanks.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8718657306ec" width="1" height="1" alt=""><hr><p><a href="https://medium.com/ios-app%E9%96%8B%E7%99%BC%E4%B9%8B%E4%B8%96%E7%95%8C%E9%80%99%E9%BA%BC%E5%A4%A7/how-to-resize-uiimage-with-swift-8718657306ec">How to resize UIImage with Swift</a> was originally published in <a href="https://medium.com/ios-app%E9%96%8B%E7%99%BC%E4%B9%8B%E4%B8%96%E7%95%8C%E9%80%99%E9%BA%BC%E5%A4%A7">iOS Funny World</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to passing value by delegate in iOS]]></title>
            <link>https://medium.com/ios-app%E9%96%8B%E7%99%BC%E4%B9%8B%E4%B8%96%E7%95%8C%E9%80%99%E9%BA%BC%E5%A4%A7/how-to-passing-value-by-delegate-in-ios-1ae880107f31?source=rss-c9610a3f53a1------2</link>
            <guid isPermaLink="false">https://medium.com/p/1ae880107f31</guid>
            <category><![CDATA[passing]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[protocol]]></category>
            <category><![CDATA[function]]></category>
            <category><![CDATA[delegate]]></category>
            <dc:creator><![CDATA[Jimmy Chen]]></dc:creator>
            <pubDate>Tue, 07 Jun 2022 06:00:53 GMT</pubDate>
            <atom:updated>2022-06-10T03:55:14.924Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*T_iu9XxxrgpPD_bilCJJKA.jpeg" /></figure><h3>How to pass value by delegate in iOS</h3><blockquote>Version : Swift 5.5 , iOS 15 , Xcode 13.0</blockquote><blockquote>In this tutorial :</blockquote><p>You will learn how to pass value by delegate, and use it after receiving.</p><p>Many times we have the need for passing value, just like eating and sleeping, today I want to share how to eat and sleep, Oops! No, it’s passing value by the delegate.</p><p>In fact, the delegate we used is a design pattern, it’s named “Delegation Pattern”, this design pattern has to design a Protocol, you can imagine it as a contract. Who receives the value or has to do something, it is called Party B. And who passing value or call Party B to do something, it’s called Party A.</p><blockquote>First, let’s talk about the process of passing value:</blockquote><p>Party A has the contract(Protocol) and the contract has the statement, it will pass value to Party B, then Party B says “let me do it”, and Party B can receive the value and do some things, but Party A doesn’t matter how Party B wants to do it, as long as Party B does it.</p><h4>Let’s Get Started :</h4><p>I make two pages, such as Figure 1.1, the first page(FirstVC) can navigate to the second page(SecondVC), and show the random number of SecondVC generate, I make the page as simple as possible to easier represent the concept of the delegate.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qAz9JwClUHY4fhBeREM56Q.jpeg" /><figcaption>1.1</figcaption></figure><p>Now, let’s look at SecondVC, it’s the concept of Party A, imagine SecondVC is Party A, it wants to pass value to FirstVC(Party B), and FirstVC wants to take the received value to update UILabel text, since we have to need a contract, let’s write a contract( Protocol).</p><p>You can write the contract( Protocol) anywhere, such as above of class or somewhere, at the sample, I write on a new swift file, as shown in Figure 1.2</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/571/1*5hqNQctSFRN63onx4Xb1gg.png" /><figcaption>1.2</figcaption></figure><p>Now, we need to hold the contract( Protocol), I declare a property and named it “delegate” to comform “PassingValueDelegate”, which means Party B holds the contract(Protocol), and at the same time, I also make the @IBOutlet of UILabel, such as Figure 1.3.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pFBz1lhfblmYBj5U3jaMhQ.png" /><figcaption>1.3</figcaption></figure><p>On SecondVC, I generate a button target action to make a random number and pass the random number to FirstVC, SecondVC just knows the contract has a statement to execute one function to the passing value and doesn’t care who executes or who receives the value. Anyway, SecondVC is Party A, and just having someone to do it for me is good, such as Figure 1.4.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/817/1*nziSuXT1zVuGvuhbkxI-qQ.png" /><figcaption>1.4</figcaption></figure><p>Hmm… so who exactly wants to receive the value, it’s FirstVC, since FirstVC wants to receive the value, so FirstVC also needs to conform to the same contract(Protocol) with SecondVC. So, I add the protocol behind the class and you will see the error later, as shown in Figure 1.5, don’t worry, just fix it, and will see the protocol function auto appear, as shown in Figure 1.6.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*k4iaia1ezLB5ypPPji624w.jpeg" /><figcaption>1.5</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0WXYLe_SkjKgzePviMZ2cg.jpeg" /><figcaption>1.6</figcaption></figure><p>We just mentioned about SecondVC( Party A ) doesn’t know who executes the protocol, so FirstVC( Party B ) has to say “Hey! I will do” or the SecondVC will be too angry to crash because SecondVC finds the delegate is nil, so SecondVC crash.</p><p>We can write the code as shown in Figure 1.7 at the target action function for navigating to SecondVC.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OdPPz98pNOHOlARHyizI6A.jpeg" /><figcaption>1.7</figcaption></figure><p>Notice about line 37, it’s important, it’s mean “The protocol function let me execute”</p><p>Now, FirstVC( Party B ) can execute the PassingValueDelegate protocol function, and we need to write some code that really needs to be implemented, at this example, FirstVC wants to update UILabel text by using receive value from SecondVC, so we can write the code such as Figure 1.8.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YOlAo--ErV5_Sicu3kf3JQ.jpeg" /><figcaption>1.8</figcaption></figure><blockquote>Delegation have many another use case, this article just talk about basic used, I will share more article of iOS to everyone,. If you can give me some claps, I will very glad to that. If I have any error, contact to me , please.</blockquote><p>There is the code example on GitHub: <a href="https://github.com/thunderlatent/DelegatePassingValue.git">Click Here</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1ae880107f31" width="1" height="1" alt=""><hr><p><a href="https://medium.com/ios-app%E9%96%8B%E7%99%BC%E4%B9%8B%E4%B8%96%E7%95%8C%E9%80%99%E9%BA%BC%E5%A4%A7/how-to-passing-value-by-delegate-in-ios-1ae880107f31">How to passing value by delegate in iOS</a> was originally published in <a href="https://medium.com/ios-app%E9%96%8B%E7%99%BC%E4%B9%8B%E4%B8%96%E7%95%8C%E9%80%99%E9%BA%BC%E5%A4%A7">iOS Funny World</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How To Custom UISegmentControl]]></title>
            <link>https://medium.com/ios-app%E9%96%8B%E7%99%BC%E4%B9%8B%E4%B8%96%E7%95%8C%E9%80%99%E9%BA%BC%E5%A4%A7/how-to-custom-uisegmentcontrol-71ac2bd69499?source=rss-c9610a3f53a1------2</link>
            <guid isPermaLink="false">https://medium.com/p/71ac2bd69499</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[uisegmentedcontrol]]></category>
            <dc:creator><![CDATA[Jimmy Chen]]></dc:creator>
            <pubDate>Tue, 31 May 2022 00:07:46 GMT</pubDate>
            <atom:updated>2022-05-31T00:07:46.803Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0Nq3O5E5wCSAaKAqCHs8Zg.jpeg" /></figure><blockquote>Version: Swift 5, iOS 13.0, Xcode 13.0</blockquote><p>In this article, we will learn about how to custom UISegmentControl, such as corner radius, <strong>selected imageView</strong> color.</p><h3>Demo :</h3><figure><img alt="Final effect" src="https://cdn-images-1.medium.com/max/340/1*pd61r8SgAbGOCwkPXi9ObA.gif" /><figcaption>Final Effect</figcaption></figure><p>Have you ever written following code of Figure 0.1 to modify corner radius, and UISegmentedControl not thing changed at all…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/677/1*phRQ5JKwcD28Q8bQ8hFiDA.jpeg" /><figcaption>0.1</figcaption></figure><blockquote>If you have the problem same as me, let’s begin following tutorial !</blockquote><h3>Standard</h3><h4>Step 1.0</h4><p><strong>Create a subclass of UISegmentedControl :</strong></p><p>Create a subclass of UISegmentedControl , named “CustomizableSegmentControl” and configure the first item status was selected.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2e-Hac8W3z0qVe67FnikwQ.jpeg" /></figure><blockquote>We will change it’s corner radius and color in this class.</blockquote><h4>Step 1.1</h4><p><strong>Analyze UISegmentControl’s view hierarchy :</strong></p><p>We have to know UISegmentControl’s view hierarchy, before we want to change it’s radius and color.</p><p>We have to know how many subviews on UISegmentedControl and which subviews these are we want to modify corner radius and color, let’s watch Figure 1.1.1.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/959/1*Ahg-tJeDNrcDkKxU7yUiXw.png" /><figcaption>1.1.1</figcaption></figure><p>In Figure 1.1.1, we can see the bottom view is the orange view and it’s the UISegmentedControl background view, if we want to modify corner radius , we have to find it’s subviews and modify corner radius and color.</p><h4>Step 1.2</h4><p><strong>Modify corner radius :</strong></p><p>Now, let’s define a property to store radius in CustomizableSegmentControl.swift, like Figure 1.2.1.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/667/1*6u64b9xoTpie8ElNDMxQYg.jpeg" /><figcaption>1.2.1</figcaption></figure><p>Every UI element from loading memoery to render on screen, it go through many life cycle, when the UI will render on to screen , it will excuted func layoutSubviews(), so we can override this life cycle and modify corner radius to what we want.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/692/1*8kwl_nJUaCFQrS-pnAiBiw.jpeg" /><figcaption>1.2.2</figcaption></figure><p>If you instaniate CustomizableSegmentControl and add it to view that you want to show, you will see Figure 1.2.3 , it’s great! But, the Figure 1.2.3 have some different with fighre 1.2.4 that we want, let’s keep refining it !</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/417/1*lYgVTxSDq-nhU_kS5Nq9YQ.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/412/1*dkKSg80oXqFrydPB7LV35g.jpeg" /><figcaption>left: 1.2.3 ; right:1.2.4</figcaption></figure><h4>Step 1.3</h4><p><strong>Modify Selected Segment Apperance :</strong></p><p>Let’s see what are the differences between Figure 1.2.3 and Figure 1.2.4</p><ol><li>Different selected segment color.</li><li>Different edge inset.</li><li>Different selected segment corner radius.</li></ol><p>First, let’s handle the first point ,modify the selected color. Go back to Figure 1.1.1, we can the green view is a UIImageView, and it is representing selected segment, we call it “selected imageView”. So, we just need to modify the selected imageView background color to green, well… sounds simply, but, how did we find the selected ImageView ?</p><p>Let’s looking carefully at the view hierarchy of Fighure 1.1.1, we can find selected imageView from subviews of CustomizableSegmentControl’s backgroundView.</p><p>So we can write following code of Figure 1.3.1 to find selectedImageView , and change the color:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1MGthnND6dnP1Dwpc7i06w.jpeg" /><figcaption>1.3.1</figcaption></figure><blockquote>Why we can find selected imageView by the code of Figure 1.3.1 ?</blockquote><p>Let’s see Figure 1.1.1, we can find the selected imageView at the position of subviews[2], if we have three items in UISegmentedControl, the selected imageView position of subview will atsubviews[3].</p><p>According to this logicso, we using nuberOfSegments property to representing selected imageView index of subviews, and get the selected imageView of subviews, then we modify it’s background color and set the image to nil.</p><p>Well! Looks great!! The selected imageView not only color cnaged but also snap to edge, but…we will find the selected imageView center color changed and corner radius changed when we click or swip, just like Figure 1.3.2.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/344/1*_nB4NDuXssiYoGjNmynYKw.gif" /><figcaption>1.3.2</figcaption></figure><p>Let fix it!!</p><h4>Step 1.4</h4><p><strong>Configure selected imageView edge inset :</strong></p><p>We can find , if we set selected imageView color not yet, selected imageView have some inset of edge, however when we set the color , the selected imageView will snap to edge. and the selected imageView appear inset of edge when we click or swip.</p><p>To fix it, we have to define a property to store edge inset, let’s named it “segmentInset” and this peoperty can’t be 0, or the bug can’t be fix.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*17K0VFrdUqNjwRpLnVWPfA.jpeg" /><figcaption>1.4.1</figcaption></figure><p>Then, we add a linke of code to layoutSubviews(), like Figure 1.4.2.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xXuq6bcJkreVm1HPz9mSeQ.jpeg" /><figcaption>1.4.2</figcaption></figure><p>Life cycle layoutSubviews() looks like Figure 1.4.3 :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BZ2bxIBvbm2bYVqjXrvQ0A.jpeg" /><figcaption>1.4.3</figcaption></figure><p>Now, screen looks like Figure 1.4.4 :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/344/1*y_4319jCJNVX5t6TbLu9yg.gif" /><figcaption>1.4.4</figcaption></figure><h4>Step 1.5</h4><p><strong>Configure selected imageView corner radius :</strong></p><p>Now, looks great !! All of click and swip animation are very smooth, but we also still missing some round corners of selected imageView, let’s keep refining this effect.</p><p>In layoutSubviews(), add some code of selected imageView layer , like Figure 1.5.1:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*M0d3NS8TRflL4JYTp7k7Bg.jpeg" /><figcaption>1.5.1</figcaption></figure><p>Great !! We almost complete all function , but I find there is a bug with segmentInset, if segmentInset is slightly larger , we will see a weird effect, just like Figure 1.5.2 :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/344/1*LYc72UYHjdRRu6gcw8eM1Q.gif" /><figcaption>1.5.2</figcaption></figure><p>To fix this werid effect bug, we need to add a code of line in layoutSubview(), just like Figure 1.5.3</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Z2jF5i0GwM5ZNTe0OheWfw.jpeg" /><figcaption>1.5.3</figcaption></figure><p>Now, life cycle layoutSubviews() should look like Figure 1.5.4:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5YV_3ydwQrH4BSPgN4wKYw.jpeg" /><figcaption>1.5.4</figcaption></figure><h4>Step 2.0</h4><p><strong>How to use :</strong></p><p>When we complete create this subclass, just add it to which view we want to show custom UISegmentedControl, just like Figure 2.0.1.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*52584wSv-dEYv5sZTNs1zg.jpeg" /><figcaption>2.0.1</figcaption></figure><p>Phew… we’re finally done this custom sub class of UISegmentedControl !!</p><h4>Here is the complete code :</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/143f3047785eba5fd8dc183bda7a28fd/href">https://medium.com/media/143f3047785eba5fd8dc183bda7a28fd/href</a></iframe><blockquote>If my article save you some time, please give me some claps or any advice, I will very thank you.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=71ac2bd69499" width="1" height="1" alt=""><hr><p><a href="https://medium.com/ios-app%E9%96%8B%E7%99%BC%E4%B9%8B%E4%B8%96%E7%95%8C%E9%80%99%E9%BA%BC%E5%A4%A7/how-to-custom-uisegmentcontrol-71ac2bd69499">How To Custom UISegmentControl</a> was originally published in <a href="https://medium.com/ios-app%E9%96%8B%E7%99%BC%E4%B9%8B%E4%B8%96%E7%95%8C%E9%80%99%E9%BA%BC%E5%A4%A7">iOS Funny World</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to optimize your code by using Enum with Swift]]></title>
            <link>https://medium.com/swift%E7%AD%86%E8%A8%98/how-to-optimize-your-code-by-using-enum-with-swift-241a5b6cbe36?source=rss-c9610a3f53a1------2</link>
            <guid isPermaLink="false">https://medium.com/p/241a5b6cbe36</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[optimization]]></category>
            <category><![CDATA[enum]]></category>
            <category><![CDATA[ios]]></category>
            <dc:creator><![CDATA[Jimmy Chen]]></dc:creator>
            <pubDate>Wed, 16 Feb 2022 10:03:45 GMT</pubDate>
            <atom:updated>2022-02-16T10:03:45.878Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*q76pYuqVcQAv_dpA13x_NQ.jpeg" /></figure><blockquote>Version: Swift 5, iOS 14.5, Xcode 13.0</blockquote><p>This article will let you know what is Enum in Swift, I won’t to explain too detail to make you confuse, I just want you to know 「How to use」and 「Why to use」</p><h3>What is Enum</h3><p>Enum is a type of Swift, it can define different situations or meanings. For example, there are seven days in a week, so we can define an enum to mean everyday in a week.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/560/1*Xq3OqRDMOBZqUF9XfLGdOw.png" /><figcaption>1.1</figcaption></figure><p>Just like image 1.1, we can create an enum and meaning different situations. For example, there are seven day in a week, we can define seven situations, each case represent different weekday. As for how to use enum, it is simply.</p><p>You can create an enum instance by choose one of these two ways in following image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/926/1*nIu3zrqcP-mTUr78Q3m1iA.png" /><figcaption>1.2</figcaption></figure><blockquote>After Swift 3, the case naming pattern in Enum changes to start with lowercase, and Enum itself start with uppercase.</blockquote><h3>How to use</h3><p>Now, you general understanding what enum is, it’s time to explain how to use Enum in exactly project.</p><p>Imagining a situation, there are different things to do at each day in the week, we can use switch with Enum to achieve this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4xjTAPMOTGQgI712QXNChw.png" /><figcaption>2.1</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KDclX3Dr5UFhZEAd0UuS2Q.png" /><figcaption>2.2</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/632/1*qHnyeo0hdAO0c0ZHG6wndw.png" /><figcaption>2.3</figcaption></figure><h3>Why to use</h3><p>Imagining, if the above situation doesn’t use switch and Enum, but use String, then the code will look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GHJlnEBfyYoiQOdpV505RQ.png" /><figcaption>3.1</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0C0O0K2iadjaN1DPDfhfAg.png" /><figcaption>3.2</figcaption></figure><blockquote>Now, do you feel what’s different between using Enum with switch and just using String ?</blockquote><h4>Let’s take a look, what’s the advantages with using switch with Enum:</h4><ol><li>The code looks much cleaner.</li><li>The developer who call whatThingAtToday(today:) , just can enter standardize format.</li><li>When call whatThingAtToday(today:), the parameter just only need to using dot syntax to select different case, it can promote convenience and reduce risk of typing error.</li><li>When you define implementation of whatThingAtToday(today:) function, <br>because this situation is seven days in the week, so there are always only <br>have seven situations , it is impossible to have additional situations .<br>So strictly , there are not have else situation, but if using String, it is possible enter else block because user typing error or typing another text.</li></ol><h3>Let code become meaningful</h3><p>For example, I want set different color for each days in the week, so I set in tableView(_:cellForRowAt:)</p><h4>Original Version:</h4><p>Since this code written by ourself, so we don’t think any problem at this moment.</p><p>After a while, another engineer or ourself read this code, we may have a question : 「What’s meaning about indexPath.row」</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*y016Y4idGKLY965ALeNk1w.png" /><figcaption>4.1</figcaption></figure><p>We can using Enum to make above code of image 4.1 more meaningful.</p><h4>Optimized Version (1) — Enum</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WYKOSAWVOz43DP3AzoRbJw.png" /><figcaption>4.2</figcaption></figure><p>From the code of image 4.2, we can know what’s color with each day simply, and there are only have seven situations in the week, since list all situations, so we don’t need default.</p><p>But if using indexPath.row, we need default to judge additional situations, but exactly , there are only have seven days in the week, the default look like redundant.</p><h4>Optimized Version (2) — CaseIterable AllCases</h4><p>We can more optimize the code of image 4.2, we can optimize the code of line 38, we don’t list all cases in array, and we will using another syntax in Enum : conform Protocol.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/794/1*_rMQJ2995ZbKAU5rdzY5_g.png" /><figcaption>4.3</figcaption></figure><h4>Conform CaseIterable:</h4><p>Enum is same as Struct and Class, it also can conform Protocol , in order to get Enum case list, we have to conform CaseIterable Protocol.</p><h4>Using AllCases:</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5OUYn5jQBfofG6bw4ak0wQ.png" /><figcaption>4.4</figcaption></figure><h4>Optimized Version (3) — Enum Computed Property</h4><p>We can optimize the identifier of image 4.4 , when set Reusable Cell Identifier in line 36, we usually typing String directly, but it is troublesome and caused crash by typing error, so we optimize Reusable Cell Identifier with Enum.</p><p>Define an Enum named CellIdentifier.</p><p>In CellIdentifier Enum, add a computed property named identifierString, return match string by switch.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/998/1*44RFe88ab9DZE4HyGM5NTw.png" /><figcaption>4.5</figcaption></figure><h4>Modify String 「weekDay」with CellIdentifier Enum in line 36:</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IhYNMwVGjBOnGBg3etpx9Q.png" /></figure><h3>Summary:</h3><blockquote>Promote convenience and readability of own code by using Enum is very helpful in project, there are more advanced operations in Enum. For example, Associated Value , I will write article later about advanced operations.</blockquote><blockquote>If my articles give you any help, it’s my pleasure, and give me some clap, thanks.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=241a5b6cbe36" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swift%E7%AD%86%E8%A8%98/how-to-optimize-your-code-by-using-enum-with-swift-241a5b6cbe36">How to optimize your code by using Enum with Swift</a> was originally published in <a href="https://medium.com/swift%E7%AD%86%E8%A8%98">Swift筆記</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Deep Link in iOS App by Universal Link]]></title>
            <link>https://medium.com/ios-app%E9%96%8B%E7%99%BC%E4%B9%8B%E4%B8%96%E7%95%8C%E9%80%99%E9%BA%BC%E5%A4%A7/deep-link-in-ios-app-by-universal-link-81ac762c3c89?source=rss-c9610a3f53a1------2</link>
            <guid isPermaLink="false">https://medium.com/p/81ac762c3c89</guid>
            <category><![CDATA[deep-linking]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[deeplink]]></category>
            <category><![CDATA[universal-links]]></category>
            <dc:creator><![CDATA[Jimmy Chen]]></dc:creator>
            <pubDate>Wed, 09 Feb 2022 11:03:01 GMT</pubDate>
            <atom:updated>2022-02-14T03:42:16.525Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EcXq9kx4wUGqrNh4mfoF9A.jpeg" /></figure><blockquote>Version: Swift 5 , iOS 14.5 , Xcode 13.0</blockquote><h3>Overview</h3><p>I would like to talk about Deep Link before we start discussing Universal Link.</p><p>Universal Link is a technology name, it can make user tap link and navigate to specific page in specific iOS app.</p><p>Currently, there are two main method to make Deep Link.</p><ul><li>Before iOS 9 , using URL Scheme .</li><li>Since iOS 9 in 2015, using Universal Link .</li></ul><p>And another relation technologies, such as Defferred Deep Link , Firebase Dynamic Link , I will write another articles to discuss .</p><p>Since Universal Link is newer, so this article will focus on Universal Link .</p><h3>Universal Link Flow</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*P82DiXXZuqxUuGduYeAstg.jpeg" /></figure><h3>Limit</h3><p>There are some limits to use Universal Link</p><ol><li>You have to owner Domain.</li><li>Your server have to support HTTPS.</li><li>You can upload files to your Web Server .</li><li>Your app deployment version have to greater than or equal to iOS 9.</li><li>Xcode version greater than or equal to 7.</li></ol><h3>Principle</h3><p>When iOS SDK install App , it will check Info.plist in the App , if it find .entitlements , it will look for Associated Domains Value of applinks , and it will look for apple-app-site-association file in Https WebServer , and download to local, make the domain and app link after parse.</p><p>By the way, this is why Universal Link more secure, because no one can upload apple-app-site-association file to Web Server , only you.</p><blockquote>If you are confused about the principle, don’t worry, I will show you step by step.</blockquote><h3>Getting Started</h3><p>Implement Universal Link is not complex, there are two big steps.</p><ul><li>Create link between App and Web Server .</li><li>Handle App page navigate when App be open from Universal Link</li></ul><h3>Step 1.0</h3><h4>Enable Associated Domains in Xcode</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mmg9kBe1fnUhdGC-b0O0oA.jpeg" /></figure><p>Add the Domains you want to link, for example, if your domain is 「testdomain.com」, type in 「applinks:testdomain.com」here.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7GwdoooafVwtkOb1shZt9g.jpeg" /></figure><p>After add domain in Associated Domains , you will find a file the extension name is 「.entitlements」 in Xcode file directory</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/548/1*CsRAud0Min5eJXRxbiMBAg.jpeg" /></figure><blockquote>App will look for the domain that you created in Associated Domains at app first install , and app will look for apple-app-site-association file in Web Server to create link between App and Web Server .</blockquote><h3>Step 1.1</h3><h3>Create 「apple-app-site-association」 file</h3><p>In previous step, we configure domain that we want to link and know App will look for domain to create link between App and Web Server , so in this step , we will create apple-app-site-association file.</p><p>Create a file named 「apple-app-site-association」 using TextEdit App, and press 「shift + command + T」, it will make the file be pure text file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XFRE7Ck9XezOe3At0ljSpA.jpeg" /></figure><p>Follow the format from following image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*j-ssh9CAvMLunj82IuPwOA.jpeg" /></figure><h3>Description</h3><ul><li>appID：Compose of TeamID and BundleID, its format is TeamID.BundleID. You can find TeamID in Apple Developer and find BundleID in Xcode.</li><li>paths：It’s mean the path you want to support Universal Link , it can be a collection, if you want all path support Universal Link , you can type 「*」</li></ul><p>For example, my TeamID is 「AB1234567C」, BundleID is 「com.testDomain」, so my appID will be 「AB1234567C.com.testDomain」</p><blockquote><em>★</em></blockquote><blockquote><em>After typing complete, save and delete extension name, upload it to </em><em>Web Server and it can’t have any extension name</em></blockquote><h3>Step 1.2</h3><h3>Upload 「apple-app-site-association」 to Web Server</h3><p>Send the apple-app-site-association file that you created in previous step to Backend Engineer, and ask him help us to upload the file to one of the two follow path, the iOS Engineer can’t handle this part.</p><blockquote><em>apple-app-site-association file path</em></blockquote><ul><li>At web server root, etc.https://test.com/apple-app-site-association</li><li>At .well-known folder(recommend this directory, Apple change the directory in iOS 9.3, but it still support root directory), if your root directory do not have .well-known folder, you can create .well-known folder(don’t forget dots that before well-known), etc.https://test.com/.well-known/apple-app-site-association</li></ul><h3>Step 1.3</h3><h3>Verify if「apple-app-site-association」 upload was successful</h3><p>Verify steps are simply, you can open browser and navigate the path that you upload apple-app-site-association, so you can type 「https://test.com/apple-app-site-association」or「https://test.com/.well-known/apple-app-site-association」 in url bar.</p><blockquote><em>If upload successful, you can see download page or json data on web.</em></blockquote><blockquote>In the previous steps, we have completed the link between the App and the web server. Then, let’s see how to handle the page navigate when the App opened with Universal Link.</blockquote><h3>Step 2.0</h3><h3>Handling navigation after your app is opened by the universal link</h3><p>When user tap link, open app and trigger specific function, we can get the data that we want by the specific function, for example , we can know the user taped which link to open app, and navigate the specific pages by link path or query.</p><p>There are two ways to handle navigation, if window is managed by AppDelegate.swift, handle navigation by AppDelegate, else if window managed by SceneDelegate.swift, handle navigation by SceneDelegate.</p><blockquote>If you want to know what’s different between AppDelegate and SceneDelegate, you can reference this article：「<a href="https://medium.com/ios-app%E9%96%8B%E7%99%BC%E4%B9%8B%E4%B8%96%E7%95%8C%E9%80%99%E9%BA%BC%E5%A4%A7/ios%E6%9C%80%E4%BD%8E%E6%94%AF%E6%8F%B4%E7%89%88%E6%9C%AC%E4%BD%8E%E6%96%BCios-13%E4%B8%A6%E5%90%8C%E6%99%82%E4%BD%BF%E7%94%A8appdelegate%E4%BB%A5%E5%8F%8Ascenedelegate-f6872d1aefc4?source=your_stories_page----------------------------------------">iOS最低支援版本低於iOS 13並同時使用AppDelegate以及SceneDelegate</a>」.</blockquote><h4>Case in AppDelegate.swift :</h4><p>If your window be managed in AppDelegate, when your app is opened by universal link, application(_:continue:restorationHandler:) will be triggered.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KtqeY_81uVFf7k5aKwlqWQ.jpeg" /></figure><p>The code of above image, it implement simply navigate to specific page by url path or query.</p><p>Line 154 ~155: Get url path parameter.</p><p>Line 157: Determine whether have parameter「product」.</p><p>Line 159: Initialize the ViewController that will be navigated.</p><p>Line 162: Get the UINavigationController.</p><p>Line 164: Navigate to the productVC</p><h4>Case in SceneDelegate.swift :</h4><p>If your window be managed in SceneDelegate, when your app is opened by universal link, scene(_:continue:) will be triggered.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ivkLsX3VG3B7oHGUCYMh3Q.jpeg" /></figure><p>The code of above image, it implement simply navigate to specific page by url path or query.</p><p>Line 37 ~ 38: Get url path parameter.</p><p>Line 40: Determine whether have parameter「product」.</p><p>Line 42: Initialize the ViewController that will be navigated.</p><p>Line 44: Get the UINavigationController.</p><p>Line 46: Navigate to the productVC</p><blockquote>About handling navigation, choose one that fit your project of above these two ways.</blockquote><blockquote>If my articles give you any help, it’s my pleasure, and give me some clap, thanks.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=81ac762c3c89" width="1" height="1" alt=""><hr><p><a href="https://medium.com/ios-app%E9%96%8B%E7%99%BC%E4%B9%8B%E4%B8%96%E7%95%8C%E9%80%99%E9%BA%BC%E5%A4%A7/deep-link-in-ios-app-by-universal-link-81ac762c3c89">Deep Link in iOS App by Universal Link</a> was originally published in <a href="https://medium.com/ios-app%E9%96%8B%E7%99%BC%E4%B9%8B%E4%B8%96%E7%95%8C%E9%80%99%E9%BA%BC%E5%A4%A7">iOS Funny World</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Link to App Store specific app from iOS app by URL scheme]]></title>
            <link>https://medium.com/ios-app%E9%96%8B%E7%99%BC%E4%B9%8B%E4%B8%96%E7%95%8C%E9%80%99%E9%BA%BC%E5%A4%A7/link-to-app-store-specific-app-from-ios-app-by-url-scheme-a98b571b83a6?source=rss-c9610a3f53a1------2</link>
            <guid isPermaLink="false">https://medium.com/p/a98b571b83a6</guid>
            <category><![CDATA[ios-app-development]]></category>
            <category><![CDATA[app-store]]></category>
            <category><![CDATA[scheme]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[deep-linking]]></category>
            <dc:creator><![CDATA[Jimmy Chen]]></dc:creator>
            <pubDate>Tue, 08 Feb 2022 19:07:09 GMT</pubDate>
            <atom:updated>2022-02-08T19:07:09.426Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LvLXjRsiP_gk6Fx-IZHysw.jpeg" /></figure><blockquote>Version: Swift 5 , iOS 14.5 , Xcode 13.0</blockquote><h3>Overview</h3><p>In this article, I will show you how to tap link and navigate to specific app in App Store, this way , you can provide user a simple way to update app to latest version.</p><h3>Let’s Getting Started</h3><blockquote>In this article, I will take a sample for 「LINE」</blockquote><h3>Step 1：Get App ID on from App Store</h3><blockquote><em>Since we want to navigate to specific app, so we need to get the</em><em> App ID from </em><em>App Store, this way, we can navigate to the specific App page on </em><em>App Store.</em></blockquote><p>The way to get ID is simply, we can get it from App link, and this time we want to implement in iOS App, so we open App Store in iPhone , we search 「LINE」, and then click 「share」 button, and then click 「copy link」 , this way, we get the link of 「LINE」</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/750/1*a0CsidaGWZHOh6FJ5QxV1g.jpeg" /></figure><p>Now, we get the link of LINE as 「https://apps.apple.com/tw/app/line/id443904275」, since we only need ID, so we capture the part of ID in the link, then we get the ID of LINE as 「id539883307」</p><h3>Step 2：Create And Open URL Scheme</h3><p>Because we want to open App Store, so we have to get URL Scheme about App Store.</p><p>We can look forURL Scheme of App Store Specific App in following website.</p><blockquote><a href="https://medium.com/@contact.jmeyers/complete-list-of-ios-url-schemes-for-apple-apps-and-services-always-updated-800c64f450f"><strong><em>Complete List of iOS URL Schemes for Apple Apps and Services (Always-Updated)</em></strong></a></blockquote><p>We can find the formate of Open specific app URL Scheme as 「itms-apps://itunes.apple.com/app/id」</p><p>Therefore, we modify the id that at the end of URL Scheme formate to the App ID that we want to open, which is the 「LINE」App ID that we got in Step 1.</p><p>Then, we got the whole URL Scheme -&gt; 「itms-apps://itunes.apple.com/app/id443904275」</p><h3>Code</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dapMMoFYktN_Fq99sQaKzA.jpeg" /></figure><h4>// 1 -&gt; Since App ID at the end of URL, so we capture it by string split, get the App ID from string.</h4><h4>// 2 -&gt; Modify the end of App Store URL Scheme to App ID.</h4><h4>// 3 -&gt; Be a politeness kid, ask SDK may I open the URL, open it if you can.</h4><h3>How To Using</h3><p>Call goToAppStoreSpecificApp(appHttpsURL:) function at any place which you want navigate to App Store.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*J4BZo5ARyoWCMNYUerIx5A.png" /></figure><p>Article is end , thank you for read, if you have any suggestions, please tell me, thanks.</p><blockquote>If my articles give you any help, it’s my pleasure, and give me some clap, thanks.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a98b571b83a6" width="1" height="1" alt=""><hr><p><a href="https://medium.com/ios-app%E9%96%8B%E7%99%BC%E4%B9%8B%E4%B8%96%E7%95%8C%E9%80%99%E9%BA%BC%E5%A4%A7/link-to-app-store-specific-app-from-ios-app-by-url-scheme-a98b571b83a6">Link to App Store specific app from iOS app by URL scheme</a> was originally published in <a href="https://medium.com/ios-app%E9%96%8B%E7%99%BC%E4%B9%8B%E4%B8%96%E7%95%8C%E9%80%99%E9%BA%BC%E5%A4%A7">iOS Funny World</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[在iOS App內開啟第三方App]]></title>
            <link>https://medium.com/ios-app%E9%96%8B%E7%99%BC%E4%B9%8B%E4%B8%96%E7%95%8C%E9%80%99%E9%BA%BC%E5%A4%A7/%E5%9C%A8ios-app%E5%85%A7%E9%96%8B%E5%95%9F%E7%AC%AC%E4%B8%89%E6%96%B9app-73de299efcb0?source=rss-c9610a3f53a1------2</link>
            <guid isPermaLink="false">https://medium.com/p/73de299efcb0</guid>
            <category><![CDATA[第三方app]]></category>
            <category><![CDATA[scheme]]></category>
            <category><![CDATA[ios-apps]]></category>
            <category><![CDATA[line]]></category>
            <category><![CDATA[webview]]></category>
            <dc:creator><![CDATA[Jimmy Chen]]></dc:creator>
            <pubDate>Fri, 21 Jan 2022 06:48:52 GMT</pubDate>
            <atom:updated>2022-01-21T06:50:24.533Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*R7RLAsDa0T5K12jSOvQpuA.png" /></figure><p>版本： Swift 5 , iOS 14.5 , Xcode 13</p><p>想要在 App 內實現跳轉到第三方 App ，由於我想要打開 Line App 的社群頁面，因此我這次用的方法是先使用 WKWebView 導向社群頁面，再利用社群頁面本身自帶的「以LINE開啟」按鈕來跳轉到Line App</p><p>這次的範例頁面是「Swift線上讀書會」，網址如下：</p><pre><a href="https://line.me/ti/g2/JDpSGUrjwaTig_FSyYYOYxJoSnntEuT8PVjkbw?utm_source=invitation&amp;utm_medium=link_copy&amp;utm_campaign=default">https://line.me/ti/g2/JDpSGUrjwaTig_FSyYYOYxJoSnntEuT8PVjkbw?utm_source=invitation&amp;utm_medium=link_copy&amp;utm_campaign=default</a></pre><h3>Step 1</h3><h4>建立WKWebView</h4><p>首先是讓WKWebView載入到這個頁面，因此我們需要建立一個WKWebView，在實例化WKWebView之前，記得import WebKit</p><pre>let webView = WKWebView()</pre><pre>override func loadView() {<br>        view = webView<br>    }</pre><h4>開始載入「Swift線上讀書會」的網址</h4><p>將「Swift線上讀書會」的網址，包裝成URLRequest，然後交給webView讀取，這樣就能載入「Swift線上讀書會」的頁面了，我們寫個extension來包裝這段程式碼：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tICONf-Q7-XDeInTGxNNYg.png" /></figure><p>Extension 寫好之後，在ViewDidLoad這個生命週期內呼叫剛剛寫的方法</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tICONf-Q7-XDeInTGxNNYg.png" /></figure><blockquote><em>以上步驟都做完的話，將App部署到實機上，因為實機才有安裝「Line」 App，畫面載入如下圖，但是會發現點擊「以LINE開啟」，那是一點反應都沒有啊…因為還少了一些步驟</em></blockquote><blockquote><em>當我們點擊「以LINE開啟」這個按鈕時，</em><em>webView會收到要跳轉頁面的事件，我們需要去控制這些事件，並且決定要不要讓頁面跳轉，因此，我們需要遵從</em><em>WKNavigationDelegate來管理這些事件</em></blockquote><h3>Step 2</h3><h4>設定WKNavigationDelegate委派、代理方法</h4><ol><li>在viewDidLoad中設定代理：</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*E9-U4dLdcwjIb06igOfl6w.png" /></figure><p>2. 新增一個Extension，遵從WKNavigationDelegate：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*o_mF_sd90wenkIvmJU4pbg.png" /></figure><p>3. 由於我們要控制webView內點擊「以LINE開啟」的事件，因此我們加入webView(_:decidePolicyFor:decisionHandler:)這段WKNavigationDelegate 內的方法，這段方法是控制當webView收到新的事件時，要進行什麼處理以及頁面是否允許跳轉：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aUsUe--bfJ2xZh6Vn5IqAg.png" /></figure><p>在說明上圖的程式碼之前，我想先說明一下 URL Scheme，URL Scheme 其實就是一種資源定位符，大家可以試試看在電腦的瀏覽器網址列輸入「file://」，就會進入到「Finder」根目錄，這是因為「file://」這個定位符指向了「Finder」的根目錄，接著我們來看「以LINE開啟」的連結，發現連結長得像這樣：</p><pre>line://ti/g2/JDpSGUrjwaTig_FSyYYOYxJoSnntEuT8PVjkbw?utm_source=invitation&amp;utm_medium=link_copy&amp;utm_campaign=default</pre><p>可以看到這串連結的 scheme 是「line」，也就是說「Line」 App 的 scheme是「line」，在iOS SDK 中， 提供了讓使用者可以利用 scheme 來開啟第三方 App 的功能，接下來讓我們開始說明程式碼，來看看如何打開「Line」App</p><p>針對上圖的程式碼說明： 1 → 利用 url 獲得 scheme，並且將 scheme 全部轉成小寫，方便之後進行判斷</p><p>2 → 判斷 scheme 是 「line」之後， 打開 url ，此時 iOS SDK 因為判斷到 scheme ，所以會自動幫我們開啟第三方 App</p><p>3 → 這邊的程式碼是允許頁面是否跳轉，這邊就按照開發者的需求去決定，不過目前由於我只是Demo，所以所有頁面我都允許跳轉。</p><h3>Step 3</h3><h4>新增 Info.plist 屬性</h4><p>想要打開「Line」 App，還需要在 Info.plist 內的「LSApplicationQueriesSchemes」屬性新增一個「Item」，並且型別為String，值為「line」，如圖片中這樣</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*R487j8dn9AdOu9PBS9u2sA.png" /></figure><p>或者也可以將 Info.plist 以 Source Code 開啟，並且在最外層的「&lt;dict&gt;」貼上以下程式碼：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1016/1*e3AbMW56IZr3rc-FXxXThg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Xgh4nRTsb-8tAdal4eWSxw.png" /></figure><p>以上兩種方法選一種就好，只是一個是用XML的方式編輯，一個是用表格的方式，其實都是同一份檔案</p><p>完成以上三個步驟之後，就能夠像下圖這樣在自己的 App 內打開第三方 App 了</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/750/1*hpE_WBk7F0g4SjBzQVqDTg.gif" /></figure><blockquote>如果覺得我的文章對你有幫助的話，請多幫我鼓掌，讓我更有動力更新～</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=73de299efcb0" width="1" height="1" alt=""><hr><p><a href="https://medium.com/ios-app%E9%96%8B%E7%99%BC%E4%B9%8B%E4%B8%96%E7%95%8C%E9%80%99%E9%BA%BC%E5%A4%A7/%E5%9C%A8ios-app%E5%85%A7%E9%96%8B%E5%95%9F%E7%AC%AC%E4%B8%89%E6%96%B9app-73de299efcb0">在iOS App內開啟第三方App</a> was originally published in <a href="https://medium.com/ios-app%E9%96%8B%E7%99%BC%E4%B9%8B%E4%B8%96%E7%95%8C%E9%80%99%E9%BA%BC%E5%A4%A7">iOS Funny World</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[解釋解釋什麼是Swift的泛型]]></title>
            <link>https://medium.com/swift%E7%AD%86%E8%A8%98/%E8%A7%A3%E9%87%8B%E8%A7%A3%E9%87%8B%E4%BB%80%E9%BA%BC%E6%98%AFswift%E7%9A%84%E6%B3%9B%E5%9E%8B-4456bc74ae07?source=rss-c9610a3f53a1------2</link>
            <guid isPermaLink="false">https://medium.com/p/4456bc74ae07</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[generics]]></category>
            <category><![CDATA[泛型]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[generics-in-swift]]></category>
            <dc:creator><![CDATA[Jimmy Chen]]></dc:creator>
            <pubDate>Fri, 03 Dec 2021 09:06:24 GMT</pubDate>
            <atom:updated>2021-12-03T09:06:24.541Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*J42bpevR1SJfQ1xRWENtJg.jpeg" /></figure><blockquote>就我自己的理解，泛型最直接的解釋就是：「不用定義任何型別，只需要給placeholder type name，也就是常常看見的Ｔ，當這個泛型名稱是有意義的，則可以給予名稱，例如：Array&lt;Element&gt;，Element也是泛型」</blockquote><p>其實泛型大家一定都有使用過，只是生活中並不缺少美，而是缺少發現，舉個最簡單的例子，陣列Array就可以看到泛型的存在，因此我們可以放Int, String之類的型別來定義這是一個Array&lt;Int&gt;或者是Array&lt;String&gt;。</p><blockquote>那實際上泛型可以幫助我什麼？可以讓我的程式碼更簡潔嗎？</blockquote><blockquote>答案是：可以的。</blockquote><p>我們想像一個情境，聖誕節快到了，廣志跟美冴為了燃起久違的激情，所以約定好要玩交換禮物，不過在遊戲開始前，先寫了卡片送給對方，要讓兩個人交換卡片，我們可以寫一個函式：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/727/1*3sf91z8aMbHhPZvR6dwrlg.png" /><figcaption>交換卡片的函式</figcaption></figure><p>寫完浪漫的卡片之後，廣志跟美冴決定做一道拿手菜送給對方吃，在家享受難得的浪漫時光，於是我們又要再寫一個函式：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/717/1*lEuH08llk2IHTuK5_Juwwg.png" /></figure><p>享受完對方製作的美味菜餚後，終於來到了最後的交換禮物環節，於是我們還得再寫一個交換禮物的函式：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/693/1*o_swEPXSC6cacXyg2uwpkg.png" /><figcaption>交換禮物的函式</figcaption></figure><p>廣志跟美冴度過了美妙的夜晚，但是肝苦的工程師，寫了好多函式，偏偏這些函式的內容都還一樣，就只是因為帶入參數的型別不一樣而寫了好多個函式……</p><blockquote>那有沒有解決的方法？<br>有！ 用泛型！</blockquote><p>我們可以利用泛型，將以上三個函式整合成一個函式：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/826/1*eWrvHlXzqCko5-Ugo6jeyg.png" /></figure><p>如此一來，就能夠使用同個函式，大幅減少幾乎相同的程式碼，廣志跟美冴也重拾久違的激情，也就是在這天，一個叫做小新的小小生命即將在十個月後來到這個世界。</p><p>參考：</p><p>《The Swift Programming Language》： <a href="https://docs.swift.org/swift-book/LanguageGuide/Generics.html">https://docs.swift.org/swift-book/LanguageGuide/Generics.html</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4456bc74ae07" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swift%E7%AD%86%E8%A8%98/%E8%A7%A3%E9%87%8B%E8%A7%A3%E9%87%8B%E4%BB%80%E9%BA%BC%E6%98%AFswift%E7%9A%84%E6%B3%9B%E5%9E%8B-4456bc74ae07">解釋解釋什麼是Swift的泛型</a> was originally published in <a href="https://medium.com/swift%E7%AD%86%E8%A8%98">Swift筆記</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[iOS最低支援版本低於iOS 13並同時使用AppDelegate以及SceneDelegate]]></title>
            <link>https://medium.com/ios-app%E9%96%8B%E7%99%BC%E4%B9%8B%E4%B8%96%E7%95%8C%E9%80%99%E9%BA%BC%E5%A4%A7/ios%E6%9C%80%E4%BD%8E%E6%94%AF%E6%8F%B4%E7%89%88%E6%9C%AC%E4%BD%8E%E6%96%BCios-13%E4%B8%A6%E5%90%8C%E6%99%82%E4%BD%BF%E7%94%A8appdelegate%E4%BB%A5%E5%8F%8Ascenedelegate-f6872d1aefc4?source=rss-c9610a3f53a1------2</link>
            <guid isPermaLink="false">https://medium.com/p/f6872d1aefc4</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[ios12]]></category>
            <category><![CDATA[app-delegate]]></category>
            <category><![CDATA[scene-delegate]]></category>
            <category><![CDATA[ios-13]]></category>
            <dc:creator><![CDATA[Jimmy Chen]]></dc:creator>
            <pubDate>Fri, 08 Oct 2021 02:07:45 GMT</pubDate>
            <atom:updated>2021-10-08T02:07:45.146Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nDInGSG4gQ_ugDIQYjSs8w.jpeg" /></figure><p>當我們使用<strong>Xcode 11</strong>之後的版本新建專案，<strong>Xcode</strong>會預設產生<strong>AppDelegate.swift</strong>以及<strong>SceneDelegate.swift</strong>，此時如果在建立完專案後，需要將最低支援版本改成低於<strong>iOS 13</strong>，則會看到許多錯誤：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/503/1*gmdixhYpW_kMdX2sIGfT0w.png" /></figure><p>會出現這些錯誤的原因是，在<strong>Xcode 11</strong>之後，預設新增的專案最低支援版本為<strong>iOS 13</strong>，並且新增了一個<strong>SceneDelegate.swift</strong>檔案，那實際上有什麼改變？ 我們先來了解在<strong>Xcode 11</strong>及更早版本之前，<strong>AppDelegate.swift</strong>在做什麼事</p><p><strong>AppDelegate</strong>是App啟動時最一開始載入的檔案，裡面管理著App的<strong>生命週期</strong>，例如App進入到前景，或者App被滑到背景，都會觸發這些function，而以下這個function則是在App即將啟動時，最一開始被呼叫的。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6adbbed636b81e602647c8e3a417d7f3/href">https://medium.com/media/6adbbed636b81e602647c8e3a417d7f3/href</a></iframe><p>當App啟動完成後，會呼叫以下function，在<strong>iOS 12</strong>或更低版本時，我們可以在這個function建立我們的初始控制器，或者在這個function建立一些初始化設定，例如以下程式碼：</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bceccc463461bace1a7b661c66fbf7e9/href">https://medium.com/media/bceccc463461bace1a7b661c66fbf7e9/href</a></iframe><p>而到了<strong>Xcode 11, iOS 13</strong>之後，新增專案時<strong>AppDelegate</strong>內的管理App生命週期的function有所改變了，並且新增<strong>SceneDelegate.swift</strong>檔案，變成由<strong>SceneDelegate</strong>來管理</p><p>以下程式碼是<strong>AppDelegate.swift</strong>內將管理App生命週期事件交給<strong>SceneDelegate</strong>管理的程式碼：</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1117fe2130e4b26065e627302c0b8089/href">https://medium.com/media/1117fe2130e4b26065e627302c0b8089/href</a></iframe><blockquote>那如果想要在Xcode 11建立新專案之後，最低支援版本又低於iOS 13，怎麼辦呢？</blockquote><p>我看網路上有許多教學是直接刪除<strong>SceneDelegate</strong>，全部交由<strong>AppDelegate</strong>來管理，但是考慮到之後有可能會有多場景的需求，因此我不打算刪除<strong>SceneDelegate</strong>，而且刪除<strong>SceneDelegate</strong>之後，又需要去改動到<strong>Info.plist</strong>內的屬性</p><p>這邊採用的做法是判斷目前聲明哪些函示及類別是<strong>iOS 13</strong>或更高版本可用，因此需要用到<strong>#available</strong>以及<strong>@available</strong>關於兩者的差別，下次我們再寫個文章來說明，因此最終程式碼會變成以下樣子：</p><blockquote>AppDelegate：</blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/185c2b72d435852760ff40a10f209882/href">https://medium.com/media/185c2b72d435852760ff40a10f209882/href</a></iframe><p>在<strong>AppDelegate.swift</strong>內可以看到，我在<strong>application(_ :willFinishLaunchingWithOptions:)</strong>實例化一個<strong>window</strong>並設定了初始視圖控制器，並且在<strong>application(_:didFinishLaunchingWithOptions:)</strong>設定讓<strong>window</strong>顯示</p><p>並且在<strong>AppDelegate.swift</strong>內加入了App生命週期，低於<strong>iOS 13</strong>的版本就會進入這些生命週期</p><p>值得一提的是，在Xcode 11產生的<strong>AppDelegate.swift</strong>內，預設是沒有<strong>window</strong>的屬性的，所以需要自己宣告一個<strong>window</strong></p><p>接下來是<strong>SceneDelegate.swift</strong>，由於這個類別只有<strong>iOS 13</strong>才能使用，所以我們可以大膽的直接將整個class上方宣告<strong>@available(iOS 13.0, *)</strong>：</p><blockquote>SceneDelegate.swift</blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7647a8a171946c006296ae937dd74b4e/href">https://medium.com/media/7647a8a171946c006296ae937dd74b4e/href</a></iframe><p><strong>SceneDelegate.swift</strong>的寫法跟<strong>AppDelegate.swift</strong>其實差異不大，差別只是設定初始ViewController的地方寫在<strong>scene(_ :willConnectTo:options:)</strong></p><blockquote>總結：</blockquote><p>當<strong>iOS</strong>版本低於<strong>iOS 13</strong>時，程式碼的部分只會走<strong>AppDelegate.swift</strong>，並且<strong>App生命週期由AppDelegate管理</strong></p><p>當<strong>iOS</strong>版本等於或高於<strong>iOS 13</strong>時，程式碼的部分會一開始先走<strong>AppDelegate.swift</strong>，當啟動完成後，進到<strong>AppDelegate.swift</strong>內配置<strong>SceneDelegate</strong>的function，並且<strong>App生命週期由SceneDelegate.swift管理</strong></p><blockquote>終於寫完了～這篇的目的主要是要解決新建的專案要支援較低（低於iOS 13）版本時，<strong>AppDelegate.swift與SceneDelegate.swift共存的問題</strong>，如果有更好的方式或者我有哪裡寫錯了也歡迎留言一起討論，感謝閱讀～</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f6872d1aefc4" width="1" height="1" alt=""><hr><p><a href="https://medium.com/ios-app%E9%96%8B%E7%99%BC%E4%B9%8B%E4%B8%96%E7%95%8C%E9%80%99%E9%BA%BC%E5%A4%A7/ios%E6%9C%80%E4%BD%8E%E6%94%AF%E6%8F%B4%E7%89%88%E6%9C%AC%E4%BD%8E%E6%96%BCios-13%E4%B8%A6%E5%90%8C%E6%99%82%E4%BD%BF%E7%94%A8appdelegate%E4%BB%A5%E5%8F%8Ascenedelegate-f6872d1aefc4">iOS最低支援版本低於iOS 13並同時使用AppDelegate以及SceneDelegate</a> was originally published in <a href="https://medium.com/ios-app%E9%96%8B%E7%99%BC%E4%B9%8B%E4%B8%96%E7%95%8C%E9%80%99%E9%BA%BC%E5%A4%A7">iOS Funny World</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[iOS 14, Swift 5，Get串接API並使用Codable解析資料 (下)]]></title>
            <link>https://medium.com/ios-app%E9%96%8B%E7%99%BC%E4%B9%8B%E4%B8%96%E7%95%8C%E9%80%99%E9%BA%BC%E5%A4%A7/ios-14-swift-5-get%E4%B8%B2%E6%8E%A5api%E4%B8%A6%E4%BD%BF%E7%94%A8codable%E8%A7%A3%E6%9E%90%E8%B3%87%E6%96%99-%E4%B8%8B-f712bc242861?source=rss-c9610a3f53a1------2</link>
            <guid isPermaLink="false">https://medium.com/p/f712bc242861</guid>
            <category><![CDATA[api]]></category>
            <category><![CDATA[codable]]></category>
            <category><![CDATA[ios-app-development]]></category>
            <category><![CDATA[get]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Jimmy Chen]]></dc:creator>
            <pubDate>Tue, 06 Apr 2021 13:56:13 GMT</pubDate>
            <atom:updated>2021-04-08T14:50:52.807Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HFmaIJiZ7BehRryMgLDvxQ.jpeg" /></figure><h3>iOS串接API(GET)並使用Codable解析資料 (下)</h3><blockquote>使用 iOS 14, Swift 5</blockquote><blockquote>延續著上篇： <a href="https://medium.com/p/810dc886a73b?source=post_stats_page-------------------------------------">iOS 14, Swift 5，Get串接API並使用Codable解析資料 (上)</a></blockquote><blockquote>前情提要 ：</blockquote><p><a href="https://medium.com/ios-app%E9%96%8B%E7%99%BC%E4%B9%8B%E4%B8%96%E7%95%8C%E9%80%99%E9%BA%BC%E5%A4%A7/ios-14-swift-5-get%E4%B8%B2%E6%8E%A5api%E4%B8%A6%E4%BD%BF%E7%94%A8codable%E8%A7%A3%E6%9E%90%E8%B3%87%E6%96%99-%E4%B8%8A-810dc886a73b">上篇文章</a>到宣告完「 getUserData」這支 func ，並且實作完裡面的內容之後告一段落，本篇文章則繼續講講要如何使用這支做好的 func。</p><blockquote>步驟 1 ： 在任何你想要的地方呼叫「getUserData」</blockquote><p>我自己是在點擊按鈕後呼叫這個方法，不過在哪裡呼叫不是重點，重點是呼叫後的閉包內要做些什麼事情：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kYRjDuyu8JTcKngyh4aoAw.png" /><figcaption>填入網址給urlString參數後，completion怎麼辦呢？</figcaption></figure><p>首先我們在「 completion 」按下「 enter 」鍵，來將這個閉包展開來，然後命名這些參數，我自己習慣以型別來小寫命名：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0SsUs3JDnp0y2W2GhZXocw.png" /><figcaption>接下來開始來實作這個閉包內的程式</figcaption></figure><p>這兩個參數( users, networkError )，所代表的意義</p><p>users ： 從網路得到的資料解析成功且丟過來的資料</p><p>networkError ： 我們自訂的錯誤型別，需要再從中判斷是何種錯誤，來進行詳細的錯誤處理</p><p>你應該有注意到，users 跟 networkError 都是 Optional，因此需要先做 Optional Binding，首先來判斷 users 有無資料：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XcbFqMC0M405963Gk8s3gA.png" /><figcaption>判斷完users有資料之後，要做什麼事情就隨客位客官，不一定要跟我一樣</figcaption></figure><p>或許你們會發現多了個第 70 行的陌生 func ，這個 func 功能是將資料顯示 UI上的一段代碼：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/689/1*tBvn7yofk0CMYL51eBbY8g.png" /><figcaption>跟 UI 有關的操作要在主執行緒執行</figcaption></figure><p>關於 171 行的 「self.stopLoading()」，我們這篇文章先不討論，之後寫篇文章來討論～</p><blockquote>要在 UI 上的任何操作，都要在主執行緒執行</blockquote><p>處理完資料的部分，我們也必須針對錯誤來進行處理：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EmnQjeCDITVTyxvybga8Yg.png" /></figure><p>嚴格來說應該也不算是錯誤處理，只是將錯誤顯示出來，主要想展示的是可以將錯誤細分，讓之後 deBug 時比較清楚。</p><p>或許你會覺得要一直針對資料以及錯誤做 Optional Binding 很麻煩，或者是覺得明明有資料就不會有錯誤，有錯誤就不會有資料，那為什麼還要寫成 （ Users?, NetworkError?），明明這兩個東西只會有一個存在啊！！</p><p>因此，在Swift 5 推出了一個型別，叫做 Result Type，下篇文章將來介紹它，並且把目前的寫法改寫成使用 Result Type 的寫法，讓串接 API 的程式碼變得更加簡潔。</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f712bc242861" width="1" height="1" alt=""><hr><p><a href="https://medium.com/ios-app%E9%96%8B%E7%99%BC%E4%B9%8B%E4%B8%96%E7%95%8C%E9%80%99%E9%BA%BC%E5%A4%A7/ios-14-swift-5-get%E4%B8%B2%E6%8E%A5api%E4%B8%A6%E4%BD%BF%E7%94%A8codable%E8%A7%A3%E6%9E%90%E8%B3%87%E6%96%99-%E4%B8%8B-f712bc242861">iOS 14, Swift 5，Get串接API並使用Codable解析資料 (下)</a> was originally published in <a href="https://medium.com/ios-app%E9%96%8B%E7%99%BC%E4%B9%8B%E4%B8%96%E7%95%8C%E9%80%99%E9%BA%BC%E5%A4%A7">iOS Funny World</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>