{"id":2133,"date":"2016-12-12T01:32:51","date_gmt":"2016-12-12T06:32:51","guid":{"rendered":"http:\/\/www.rexfeng.com\/blog\/?p=2133"},"modified":"2016-12-12T01:34:12","modified_gmt":"2016-12-12T06:34:12","slug":"using-fastlane-snapshot-to-generate-screenshots-with-uipickerviews","status":"publish","type":"post","link":"https:\/\/www.rexfeng.com\/blog\/2016\/12\/using-fastlane-snapshot-to-generate-screenshots-with-uipickerviews\/","title":{"rendered":"Using Fastlane Snapshot to generate screenshots with UIPickerViews"},"content":{"rendered":"<p>This week, I released an update for my <a href=\"https:\/\/itunes.apple.com\/us\/app\/tip-solver-premier-gratuity\/id1130814051?mt=8\">Tip Solver calculator<\/a> to add Chinese localization. I had to generate 5 screenshots for 5 devices (iPhone and iPad) across 3 languages. In the time that I spent automating the process with <a href=\"https:\/\/github.com\/fastlane\/fastlane\/tree\/master\/snapshot\">Fastlane Snapshot<\/a>, I could have easily done it manually in way less time. But the good news is that I&#8217;ve set myself up to painlessly generate screenshots for new languages. Snapshot takes some time to run, but it&#8217;s still\u00a0a huge improvement over generating screenshots manually.<\/p>\n<p>It took me a lot longer than I would have liked to setup my Snapshot process due to my usage of UIPickerViews. <a href=\"https:\/\/itunes.apple.com\/us\/app\/tip-solver-premier-gratuity\/id1130814051?mt=8\">Tip Solver<\/a> makes heavy usage of UIPickerView and I ran into many issues with UITest.<\/p>\n<p>Your mileage may vary, but I found I had to do the following to be able to use UITest and UIPickerViews:<\/p>\n<ul>\n<li>disable Ads (which run over the network)<\/li>\n<li>drastically reduce the\u00a0number of UIPickerView rows (in numberOfRowsInComponent)<\/li>\n<li>use\u00a0<a href=\"https:\/\/developer.apple.com\/reference\/uikit\/uipickerviewdelegate\/1614384-pickerview\">titleForRow<\/a> instead of\u00a0<a href=\"https:\/\/developer.apple.com\/reference\/uikit\/uipickerviewdelegate\/1614389-pickerview\">viewForRow<\/a> for UITest running<\/li>\n<\/ul>\n<p>The last one (using\u00a0titleForRow) was a complete non starter since I rely on heavy UIPickerView visual customization. Generating screenshots with incorrect picker views defeats the whole point of the exercise.<\/p>\n<p>I tried using the Xcode&#8217;s UITest recorder, but I ran into many issues. One glaring issue is that while recording, I was able to swipe the UIPicker up, but when I played\u00a0it back, it ended up swiping up the Control Center (instead of adjusting the UIPicker). There is a method (adjustToPickerWheelValue), but I found that it only works with\u00a0titleForRow (which I don&#8217;t use). What I would like is an expansion of the\u00a0XCUIElement API to add a simple increment\/move up or down once.<\/p>\n<p>My final solution (aka work around) was to use a combination of Fastlane launch arguments &amp; brute forcing the UIView (via UIViewController viewDidAppear) to generate my screenshots. My work around isn&#8217;t ideal, but it gets the job done.<\/p>\n<p>In my Fastlane Snapfile, I was able to define arguments:<\/p>\n<pre>launch_arguments([\r\n \"-screenshot 1\",\r\n \"-screenshot 2\",\r\n \"-screenshot 3\",\r\n \"-screenshot 4\",\r\n \"-screenshot 5\"\r\n])<\/pre>\n<p>In my ViewController (running Swift 3), I was able to handle them accordingly:<\/p>\n<pre>let screenshot = UserDefaults.standard.string(forKey: \"screenshot\")\r\nif screenshot == \"1\" {\r\n    \/\/ do something\r\n} else if screenshot == \"2\" {\r\n    \/\/ do something\r\n} else if screenshot == \"3\" {\r\n    \/\/ do something\r\n} else if screenshot == \"4\" {\r\n    \/\/ do something\r\n} else if screenshot == \"5\" {\r\n    \/\/ do something\r\n}<\/pre>\n<p>Once everything is setup, generating screenshots was simply running <em>snapshot<\/em> on the command line.<\/p>\n<p>I&#8217;m sure there&#8217;s room for improvement in the code (using an enum, etc.), but I left it at that\u00a0since it&#8217;s only\u00a0for screenshot generation.<\/p>\n<p>If you&#8217;ve made it all the way down here, thanks for reading. I just wanted to share my experience with UITest and UIPickers. UITest probably\u00a0needs more love\u00a0from Apple as it was not pleasant to work with.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This week, I released an update for my Tip Solver calculator to add Chinese localization. I had to generate 5 screenshots for 5 devices (iPhone and iPad) across 3 languages. In the time that I spent automating the process with Fastlane Snapshot, I could have easily done it manually in way less time. But the [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1029],"tags":[966,1309,1276,1308,1086],"class_list":["post-2133","post","type-post","status-publish","format-standard","hentry","category-programming","tag-ios","tag-uipicker","tag-uipickerview","tag-uitest","tag-xcode"],"_links":{"self":[{"href":"https:\/\/www.rexfeng.com\/blog\/wp-json\/wp\/v2\/posts\/2133","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rexfeng.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rexfeng.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rexfeng.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rexfeng.com\/blog\/wp-json\/wp\/v2\/comments?post=2133"}],"version-history":[{"count":7,"href":"https:\/\/www.rexfeng.com\/blog\/wp-json\/wp\/v2\/posts\/2133\/revisions"}],"predecessor-version":[{"id":2140,"href":"https:\/\/www.rexfeng.com\/blog\/wp-json\/wp\/v2\/posts\/2133\/revisions\/2140"}],"wp:attachment":[{"href":"https:\/\/www.rexfeng.com\/blog\/wp-json\/wp\/v2\/media?parent=2133"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rexfeng.com\/blog\/wp-json\/wp\/v2\/categories?post=2133"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rexfeng.com\/blog\/wp-json\/wp\/v2\/tags?post=2133"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}