<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
    <channel>
        <title>Daniel Duan's Articles About iOS</title>
        <link>https://duan.ca/tag/ios/</link>
        <atom:link href="https://duan.ca/tag/ios/feed.xml" rel="self" type="application/rss+xml" />
            <item>
                <title>Smart Color Invert And Your iOS Apps</title>
                <description>&#60;ol&#62;
&#60;li&#62;&#60;a href=&#34;#introduction&#34;&#62;Introduction&#60;/a&#62;&#60;/li&#62;
&#60;li&#62;&#60;a href=&#34;#the-basics&#34;&#62;The Basics&#60;/a&#62;&#60;/li&#62;
&#60;li&#62;&#60;a href=&#34;#compatibility-with-older-ios-versions&#34;&#62;Compatibility with older iOS versions&#60;/a&#62;&#60;/li&#62;
&#60;li&#62;&#60;a href=&#34;#how-to-screenshot-with-inverted-color-settings&#34;&#62;How to screenshot with inverted color settings&#60;/a&#62;&#60;/li&#62;
&#60;li&#62;&#60;a href=&#34;#conclusion&#34;&#62;Conclusion&#60;/a&#62;&#60;/li&#62;
&#60;/ol&#62;
&#60;h2&#62;Introduction&#60;/h2&#62;
&#60;p&#62;Apple sneakily shipped a &#38;quot;dark mode&#38;quot; in iOS 11, native apps looks &#60;em&#62;gorgeous&#60;/em&#62; in this mode (especially on
iPhone X with it&#39;s OLED display):&#60;/p&#62;
&#60;img src=&#34;/assets/2017/12/native-ios-apps-with-smart-invert.png&#34; srcset=&#34;/assets/2017/12/native-ios-apps-with-smart-invert.png 2x&#34; alt=&#34;iOS 11 apps in smart invert mode&#34;/&#62;
&#60;p&#62;To see it for yourself, go to &#60;em&#62;Settings -&#38;gt; General -&#38;gt; Accessibility Display Accommodations -&#38;gt; Invert Colors&#60;/em&#62;
and toggle the switch to on (I personally set triple tap of the power buttton to toggle this on/off):&#60;/p&#62;
&#60;p&#62;&#60;img src=&#34;/assets/2017/12/invert-color-settings.png&#34; alt=&#34;turn on smart invert mode in Settings&#34; /&#62;&#60;/p&#62;
&#60;p&#62;Note that in Apple&#39;s first-party apps, not every view&#39;s color is inverted (hence, &#38;quot;smart&#38;quot;). For apps that we
built, that would not be the case automatically. Instead, we get the full color inversion under this setting:&#60;/p&#62;
&#60;img src=&#34;/assets/2017/12/third-party-app-no-change.png&#34; srcset=&#34;/assets/2017/12/third-party-app-no-change.png 2x&#34; alt=&#34;default third party app before and after smart color&#34;/&#62;
&#60;p&#62;In this article, we&#39;ll see how to best adapt our apps so they look just as good as first-party iOS apps under
the smart invert setting.&#60;/p&#62;
&#60;h2&#62;The Basics&#60;/h2&#62;
&#60;p&#62;&#60;em&#62;TL;DR: use &#60;a href=&#34;https://developer.apple.com/documentation/uikit/uiview/2865843-accessibilityignoresinvertcolors&#34;&#62;accessibilityIgnoresInvertColors&#60;/a&#62; on your &#60;code&#62;UIView&#60;/code&#62;s to prevent their color to be
inverted.&#60;/em&#62;&#60;/p&#62;
&#60;p&#62;iOS 11 introduced &#60;code&#62;accessibilityIgnoresInvertColors&#60;/code&#62;, a property on &#60;code&#62;UIView&#60;/code&#62;. From the &#60;a href=&#34;https://developer.apple.com/documentation/uikit/uiview/2865843-accessibilityignoresinvertcolors&#34;&#62;offical
documentation&#60;/a&#62;:&#60;/p&#62;
&#60;blockquote&#62;
&#60;p&#62;If inverting the colors would have a negative impact on your view&#39;s content, set this property to true to
prevent it from inverting its colors. Setting the property to true prevents the system from inverting the
colors of the view and all of its subviews.&#60;/p&#62;
&#60;/blockquote&#62;
&#60;p&#62;So, for views containing photos or videos with real-world content, we probably want to set it to &#60;code&#62;true&#60;/code&#62;. In
our sample app&#39;s source code:&#60;/p&#62;
&#60;p&#62;&#60;img src=&#34;/assets/2017/12/setting-accessibilityIgnoresInvertColors-to-true-in-code.png&#34; alt=&#34;setting accessibilityIgnoresInvertColors to true in code&#34; /&#62;&#60;/p&#62;
&#60;p&#62;This gives us the desired effect for that view 🎉:&#60;/p&#62;
&#60;img src=&#34;/assets/2017/12/smart-invert-in-code-result.png&#34; srcset=&#34;/assets/2017/12/smart-invert-in-code-result.png 2x&#34; alt=&#34;result of supporting invert color in code&#34;/&#62;
&#60;p&#62;The same can be done in Interface Builder. Let&#39;s fix the other view by setting a runtime attribute:&#60;/p&#62;
&#60;p&#62;&#60;img src=&#34;/assets/2017/12/setting-accessibilityIgnoresInvertColors-to-true-in-interface-builder.png&#34; alt=&#34;setting accessibilityIgnoresInvertColors to true in Interface Builder&#34; /&#62;&#60;/p&#62;
&#60;p&#62;Result:&#60;/p&#62;
&#60;img src=&#34;/assets/2017/12/smart-invert-in-interface-builder-result.png&#34; srcset=&#34;/assets/2017/12/smart-invert-in-interface-builder-result.png 2x&#34; alt=&#34;result of supporting invert color in interface builder&#34;/&#62;
&#60;p&#62;Tada!&#60;/p&#62;
&#60;h2&#62;Compatibility with older iOS versions&#60;/h2&#62;
&#60;p&#62;The story gets slightly complicated when your app need to run on older iOS versions. For one, our code won&#39;t
compile:&#60;/p&#62;
&#60;p&#62;&#60;img src=&#34;/assets/2017/12/compile-error-on-older-oses.png&#34; alt=&#34;compile error on older iOS versions&#34; /&#62;&#60;/p&#62;
&#60;p&#62;But that&#39;s nothing new to iOS developers, really. We can fix that with a &#60;code&#62;#available&#60;/code&#62;:&#60;/p&#62;
&#60;p&#62;&#60;img src=&#34;/assets/2017/12/fix-compile-error.png&#34; alt=&#34;fix compile error on older iOS versions&#34; /&#62;&#60;/p&#62;
&#60;p&#62;When you run the app in older iOS devices or simulators, Xcode will complain about our runtime attribute as
well:&#60;/p&#62;
&#60;p&#62;&#60;img src=&#34;/assets/2017/12/xcode-complain-about-runtime-attirbute.png&#34; alt=&#34;Xcode complains about runtime attribute on older iOS&#34; /&#62;&#60;/p&#62;
&#60;p&#62;If that bothers you, I recommend writing your own wrapper for this property as an extension to &#60;code&#62;UIView&#60;/code&#62;. To
put a cherry on top, make it &#60;code&#62;@IBInspectable&#60;/code&#62;!&#60;/p&#62;
&#60;pre&#62;&#60;code class=&#34;language-swift&#34;&#62;extension UIView {
    /// Whether or not to set accessibilityIgnoresInvertColors to true for iOS 11.
    /// For older OS, this value is false and setting it has no effect.
    @IBInspectable
    public var ignoreColorInversion: Bool {
        get {
            if #available(iOS 11, *) {
                return self.accessibilityIgnoresInvertColors
            }

            return false
        }

        set {
            if #available(iOS 11, *) {
                self.accessibilityIgnoresInvertColors = newValue
            }
        }
    }
}
&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;Having this snippet in the project, we set it in interface builder to any view with a couple of clicks:&#60;/p&#62;
&#60;p&#62;&#60;img src=&#34;/assets/2017/12/set-custom-invert-property-in-interface-builder.png&#34; alt=&#34;set custom invert color properties in Interface Builder&#34; /&#62;&#60;/p&#62;
&#60;h2&#62;How to screenshot with inverted color settings&#60;/h2&#62;
&#60;p&#62;Here&#39;s a bonus hint: screenshots taken on device won&#39;t have their color inverted even if you have the setting
set to &#38;quot;on&#38;quot;! Go ahead, try it :).&#60;/p&#62;
&#60;p&#62;To show off our handy work for supporting smart invert color, we&#39;ll need
some other means to take screenshots. All screenshots in this article is taken using QuickTime on a Mac. So
plug in your iOS device, open QuickTime Player, in menu select &#60;em&#62;File -&#38;gt; New Movie Recording&#60;/em&#62; and select your
device from the drop-down menu by clicking the arrow next to the record button:&#60;/p&#62;
&#60;p&#62;&#60;img src=&#34;/assets/2017/12/select-ios-device-in-quicktime-player.png&#34; alt=&#34;Select iOS device for screenshots in QuickTime Player&#34; /&#62;&#60;/p&#62;
&#60;p&#62;Now you can screen shot the QuickTime Player window the normal way.&#60;/p&#62;
&#60;h2&#62;Conclusion&#60;/h2&#62;
&#60;p&#62;You can &#60;a href=&#34;/assets/2017/12/SmartInvert.zip&#34;&#62;download the sample project&#60;/a&#62; and play with it.&#60;/p&#62;
&#60;p&#62;Smart invert is awesome and I personally wish all 3rd-party apps will update their apps for it as time goes
on. If your favorite app (including your own!) doesn&#39;t support it properly, please consider informing the
developer about &#60;a href=&#34;https://developer.apple.com/documentation/uikit/uiview/2865843-accessibilityignoresinvertcolors&#34;&#62;accessibilityIgnoresInvertColors&#60;/a&#62;, or just send this article their way!&#60;/p&#62;
</description>
                <pubDate>Wed, 20 Dec 2017 13:17:44 -0800</pubDate>
                <link>https://duan.ca/2017/12/20/smart-invert-support-for-you-app/</link>
                <guid isPermaLink="true">https://duan.ca/2017/12/20/smart-invert-support-for-you-app/</guid>
            </item>
            <item>
                <title>Making An Open-Source Swift Project</title>
                <description>&#60;p&#62;This video shows what it takes to open source a Swift project. Starting from some code in a playground,
I created a framework that supports distribution via Swift Package Manager, CocoaPods, Xcode project and
Carthage. The framework can be used across iOS, macOS, watchOS, tvOS and Linux. In addition, we also added
SwiftLint to the project, added a Makefile, put everything on GitHub and set up continuous integration via
Travis. README, COC, LICENSE and CHANGELOG are stubbed in as well.&#60;/p&#62;
&#60;div class=&#34;video-container&#34;&#62;
    &#60;iframe src=&#34;https://www.youtube.com/embed/pA0T1CdqMt8&#34; frameborder=&#34;0&#34; gesture=&#34;media&#34; allow=&#34;encrypted-media&#34; allowfullscreen&#62;&#60;/iframe&#62;
&#60;/div&#62;
&#60;p&#62;This is not at all a comprehensive list of things a good Open Source project should have. We are still missing
the version tag, documentation generation, automatic Carthage binary generation, test coverage...&#60;/p&#62;
&#60;p&#62;Anyways, here&#39;s some stuff mentioned in the video:&#60;/p&#62;
&#60;ul&#62;
&#60;li&#62;Sample project from the video: &#60;a href=&#34;https://github.com/dduan/BitArray&#34;&#62;https://github.com/dduan/BitArray&#60;/a&#62;&#60;/li&#62;
&#60;li&#62;Video of me writing the code in this project: &#60;a href=&#34;https://www.youtube.com/watch?v=-k_jrIoD56k&#34;&#62;https://www.youtube.com/watch?v=-k_jrIoD56k&#60;/a&#62;&#60;/li&#62;
&#60;li&#62;Swift Package Manager: &#60;a href=&#34;https://swift.org/package-manager/&#34;&#62;https://swift.org/package-manager/&#60;/a&#62;&#60;/li&#62;
&#60;li&#62;CocoaPods: &#60;a href=&#34;https://cocoapods.org&#34;&#62;https://cocoapods.org&#60;/a&#62;&#60;/li&#62;
&#60;li&#62;Carthage: &#60;a href=&#34;https://github.com/Carthage/Carthage&#34;&#62;https://github.com/Carthage/Carthage&#60;/a&#62;&#60;/li&#62;
&#60;li&#62;Xcconfigs for universal framework: &#60;a href=&#34;https://github.com/mrackwitz/xcconfigs&#34;&#62;https://github.com/mrackwitz/xcconfigs&#60;/a&#62;&#60;/li&#62;
&#60;li&#62;SwiftLint: &#60;a href=&#34;https://github.com/realm/SwiftLint&#34;&#62;https://github.com/realm/SwiftLint&#60;/a&#62;&#60;/li&#62;
&#60;li&#62;Travis CI: &#60;a href=&#34;https://travis-ci.org&#34;&#62;https://travis-ci.org&#60;/a&#62;&#60;/li&#62;
&#60;/ul&#62;
</description>
                <pubDate>Sun, 17 Dec 2017 21:24:43 -0800</pubDate>
                <link>https://duan.ca/2017/12/17/making-an-open-source-swift-project/</link>
                <guid isPermaLink="true">https://duan.ca/2017/12/17/making-an-open-source-swift-project/</guid>
            </item>
            <item>
                <title>Build And Run iOS Apps In Commmand Line</title>
                <description>&#60;p&#62;Xcode is slow. Enough said. What&#39;s worse, sometimes I find myself
relying too much on auto-completion with Cocoa Touch, a blessing and a curse!&#60;/p&#62;
&#60;p&#62;So I searched for an alternative workflow in command line. The result was
rather confusing: there are posts about using &#60;code&#62;xctool&#60;/code&#62; or &#60;code&#62;xcodebuild&#60;/code&#62; to
build Xcode targets, using &#60;code&#62;ios-sim&#60;/code&#62;, &#60;code&#62;simctl&#60;/code&#62;  or &#60;code&#62;instruments&#60;/code&#62; to manage and
manage or launch simulators. Most of the information is out of date.&#60;/p&#62;
&#60;p&#62;Eventually though, I was able to piece together an answer for my needs.
That is, given an iOS project set up with Xcode 6, I want to&#60;/p&#62;
&#60;ol&#62;
&#60;li&#62;build a target.&#60;/li&#62;
&#60;li&#62;launch a iOS simulator.&#60;/li&#62;
&#60;li&#62;install the built .app bundle to the launched simulator.&#60;/li&#62;
&#60;li&#62;run the installed app.&#60;/li&#62;
&#60;li&#62;uninstall the app from the simulator.&#60;/li&#62;
&#60;/ol&#62;
&#60;p&#62;All in command line, with Xcode &#60;em&#62;closed&#60;/em&#62;.&#60;/p&#62;
&#60;p&#62;Before we proceed to the steps, you need to gather a few pieces of information:&#60;/p&#62;
&#60;ol&#62;
&#60;li&#62;the Xcode build scheme of your choice (e.g. &#38;quot;AwesomeApp&#38;quot;).&#60;/li&#62;
&#60;li&#62;your app bundle ID (e.g. &#38;quot;com.awesome.app&#38;quot;).&#60;/li&#62;
&#60;li&#62;name of an existing simulator (e.g. &#38;quot;iPhone 6 Plus&#38;quot;). If you don&#39;t want to
look it up in Xcode GUI, look for it in output of command &#60;code&#62;xcrun simctl list&#60;/code&#62; .&#60;/li&#62;
&#60;/ol&#62;
&#60;p&#62;Ready? Here we go.&#60;/p&#62;
&#60;p&#62;(These commands should be run in the project folder).&#60;/p&#62;
&#60;p&#62;Build the target:&#60;/p&#62;
&#60;pre&#62;&#60;code&#62;xcodebuild -scheme AwesomeApp -destination &#39;platform=iphonesimulator,name=iPhone 6 Plus&#39; -derivedDataPath build
&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;Launch the simulator:&#60;/p&#62;
&#60;pre&#62;&#60;code&#62;xcrun instruments -w &#39;iPhone 6 Plus&#39;
&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;Install the bundle (after simulator is launched and target is built with
previous commands):&#60;/p&#62;
&#60;pre&#62;&#60;code&#62;xcrun simctl install booted build/Build/Products/Debug-iphonesimulator/AwesomeApp.app
&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;Launch the app in simulator (after it&#39;s installed with the previous command):&#60;/p&#62;
&#60;pre&#62;&#60;code&#62;xcrun simctl launch booted com.awesome.app
&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;Uninstall the bundle:&#60;/p&#62;
&#60;pre&#62;&#60;code&#62;xcrun simctl uninstall booted com.awesome.app
&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;Quite a few parameters needs to be added for the build step if you have
a comlex project. Please RTFMs. Write some script to automate the steps, if
are a lazy typiest like me.&#60;/p&#62;
</description>
                <pubDate>Sat, 07 Feb 2015 20:35:02 -0800</pubDate>
                <link>https://duan.ca/2015/02/07/build-and-run-ios-apps-in-commmand-line/</link>
                <guid isPermaLink="true">https://duan.ca/2015/02/07/build-and-run-ios-apps-in-commmand-line/</guid>
            </item>
            <item>
                <title>tableView:didSelectRowAtIndexPath: In Two Lines</title>
                <description>&#60;p&#62;You have a &#60;code&#62;UITableViewController&#60;/code&#62; with a couple of static cells, you want to
invoke some code for each cell in the delegate. Here&#39;s a quick way to do it:&#60;/p&#62;
&#60;pre&#62;&#60;code&#62;    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        
        SEL action = (SEL[]){@selector(method1), @selector(method2)}[indexPath.row];
        ((void (*)(id, SEL))[self methodForSelector: action])(self, action);
    }
&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;It&#39;s got everything you love about C and Objective-C: array literals, function
pointers, casting, selectors and something called &#60;a href=&#34;http://www.cocoawithlove.com/2008/02/imp-of-current-method.html&#34;&#62;IMPs&#60;/a&#62;.&#60;/p&#62;
&#60;p&#62;This piece of code maps selected cells to methods by putting methods by
indexing selectors in a C array.&#60;/p&#62;
&#60;p&#62;Why all the fuss on the second line? wouldn&#39;t a simple &#60;code&#62;performSelector:&#60;/code&#62;
work? The short answer is: to show the compiler that we are responsible
adults. You can read more about it [here](SO Answer);&#60;/p&#62;
</description>
                <pubDate>Sat, 03 May 2014 13:42:00 -0600</pubDate>
                <link>https://duan.ca/2014/05/03/tableview58didselectrowatindexpath58-in-two-lines/</link>
                <guid isPermaLink="true">https://duan.ca/2014/05/03/tableview58didselectrowatindexpath58-in-two-lines/</guid>
            </item>
    </channel>
</rss>