This time of year it's common to have multiple versions of Xcode installed.
Today we're going to learn how to tell our system which version of Xcode's tools to use when working with Xcode from the command line. But first we'll check out a helpful tool to actually installXcode from command line.
Let's dive in.
The first tool we'll look at can be used to install Xcode versions directly from the command line. It's an alternative to using the Mac App Store (or just managing downloads manually).
By default, xcode-install only prints the last few major versions.
(Sidenote: For fun and nostalgia try running xcversion list --all to print all the available versions of Xcode going all the way back to 4.3 for OS X Lion 😱).
We can install a version like this:
xcversion install "9 beta 4"
We'll be prompted for our Apple Developer credentials which will be stored in our Keychain, and then the version will be download, installed, and moved into place, all without leaving the command line.
We can include one or more file paths here. It supports images, videos, and even Live Photos.
After running, the files will be imported and appear in the Photo Library:
Next: iCloud Syncing.
We can explicitly force a sync of iCloud using this command:
xcrun simctl icloud_sync booted
What's nice about this is we'll even get errors printed if (for example) iCloud isn't yet configured in this simulated device:
An error was encountered processing the command (domain=BRCloudDocsErrorDomain, code=2):
The operation couldn’t be completed.
(BRCloudDocsErrorDomain error 2 - Logged out - iCloud Drive is not configured)
This next one's a doozy. We can use simctl to record and stream live video, and capture screenshots of any screen of our iOS Simulator.
Asset Catalogs have been around for a few Xcode releases. They're a great way for us to organize and configure graphical assets (among many other things) for our app. Today we'll check out the improvements to Asset Catalogs in Xcode 9. Let's dive in.
First up, colors.
Yep, we can now define and organize named colors inside of an Asset Catalog!
We can select "New Color Set", and then use all of the normal features of Asset Catalogs, including new Wide Gamut support.
Then, in our code we can reference these colors like this:
The second big addition we're going to look at is "real" vector-based asset support.
In past Xcode releases, we were able to add image assets to our catalogs with a format of PDF. This worked great, but under the hood, Xcode would simply render our asset at the @1x, @2x, and @3x sizes and save non-vector (i.e. png images) into our app's bundle.
In Xcode 9 however, we're given a beautiful new checkbox called "Preserve Vector Data".
This means that if our image is loaded in our code, and we ask it to display at a a larger size than it is by default, it will be scaled up at runtime by the system.
This means we can now write code like this without any quality loss at render time:
The clipsToBounds ensures the "clipping" takes place, and the next rounds all four corners with a radius of 8:
This approach works great for rounding all four corners of a view. But what if we wanted to round only some of the four corners? Previously we'd need to drop down and create our own mask layer, giving it a path we created manually. Bummer.
Continuing our look at the new tidbits and goodies from WWDC 2017, today we'll learn about the changes to screen edge behavior in iOS 11. Let's dive in.
What we're really talking about here is the behavior when a user drags from offscreen. This could be the user trying to pull down Notification Center from the top, or bring up Control Center from the bottom of the screen.
In past iOS releases, the system has looked at the visibility of the status bar to determine how to behave in these cases.
If we configured one of our view controllers to hide the status bar, the system would show a little "tab" UI with an arrow that the user would need to drag a second time before Notification Center or Control Center would be shown:
Keying off of the visibility of the status bar probably isn't the best way for us to "tell" the system what to do here.
In iOS 11, we've been given a wonderful new way to describe how our app should behave when the user performs these gestures.
Now, all we need to do is override this function in our view controlller:
There's always one more fun trick tucked away in UIFontMetrics, and that is scaling arbitrary values. This is great for helping us size our UI elements (for example buttons or headers) to accomodate dynamically sized fonts that live inside:
First, the same UICollectionView is actually reused when all of this magic happens. The pushed on view controller will not create its own. This may or may not matter for any given app, but good to know.
Second, the root view controller (SmallViewController in our case) will still be set as the delegate and dataSource when the new view controller is pushed on.
If we needed to change this behavior, we could conform to the UINavigationControllerDelegate protocol and change these values each time a different view controller is about to be be shown. The code might look something like this:
We're welcoming back one of our favorite sponsors this week, it's PSPDFKit! PSPDFKit delivers an intuitive & seamless SDK for integrating PDF functionality into apps on iOS, Android, and the Web.
PDF files have been around a long time. Over that time they evolved to become an incredibly versatile and functional format. They're also wide-ranging in terms of how they're used. Comic books, Apartment leases, flight manuals, etc. All powered by PDFs.
Being able to take advantage of all this great functionality in our apps would be huge for our users.
With it, we can beautifully display PDFs, and allow our users to intuitively annotate documents, and much, much more.
We create a new PSPDFDocument, then a new view controller to show it. We use PSPDFKit's awesome PSPDFConfiguration type to neatly build up our settings.
From here we can simply present it like any other view controller. Neat!
PSPDFKit stands out with an awesome, well thought-out API. It's clear that they care deeply about API design, and developer experience.
Before we go, let's try out another couple features. First up, Annotations:
// Create a new PSPDFDocumentletdocument=PSPDFDocument(url:documentURL)// Create a link annotation, and set its URLletlinkAnnotation=PSPDFLinkAnnotation(url:URL(string:"https://pspdfkit.com")!)
// Position the annotation in the documentletboundingBox=CGRect(x:200,y:400,width:50,height:300)linkAnnotation.boundingBox=boundingBox// Customize the link annotation's appearancePSPDFLinkAnnotationView.appearance().borderColor=.blue// Add the newly created annotation to the documentdocument.add([linkAnnotation])
Very cool, Next up, Forms. Our users can of course fill out any form fields in documents they open, but we can actually fill them out programmtically too: