This week, the Windows Bridge for iOS is gaining a number of new features—including improved support for several widely-used iOS layout APIs—and integrating with the official CoreFoundation framework. Additionally, we’re launching a new repo of open-source sample apps and example code designed to address common app scenarios and demonstrate integrating Universal Windows Platform (UWP) features into your app. To recap, the Windows Bridge for iOS is an open-source project that allows you to create UWP apps that can run on Windows 10 devices using iOS APIs and Objective-C code.
Let’s take a look at this week’s updates to the SDK and the brand-new samples repo.
In December, Apple open sourced the Swift programming language, its associated tooling and standard libraries. Additionally, they also released the complete source code for the CoreFoundation framework.
CoreFoundation classes form the underpinning of most iOS apps. If you’re an iOS developer, you’ve almost certainly used classes like NSString and NSArray; these are both part of the wide array of object types and functionality provided by the Foundation framework, which is the Objective-C-bridged version of CoreFoundation.
The iOS bridge team has merged the official, canonical implementation of CoreFoundation into the WinObjC code base and it is available for use beginning with this week’s release.
The iOS bridge has provided support for preserving your iOS app’s UI since it was first open sourced last summer. The bridge provides support for many UIKit APIs—including everything from low-level controls like UIButton to high-level containers like view controllers—and, through tools like vsimporter and Xib2Nib, does its best to preserve layouts built visually using Xibs and Storyboards in XCode’s Interface Builder.
Using the Cassowary constraint-solving toolkit, the bridge also provides support for Auto Layout, Apple’s first-party mechanism for building constraint-based layouts. Auto Layout encompasses many, many different APIs and features, however, and until recently only a handful of the most versatile and widely-used ones were available in the bridge.
This week, we’re adding support for two recent additions to Auto Layout. In iOS 9, Apple introduced two new classes designed to simplify adding layout constraints to views: NSLayoutAnchor and UILayoutGuide. This greatly reduced the amount of code required to create simple, edge-anchored layouts that scale properly when the host screen or window is rotated or resized.
For instance, say you have a UIView called foo inside a container UIView called bar, and you want to pin foo’s leading and trailing edges to bar’s, so as bar grows and shrinks horizontally, foo does as well. Prior to iOS 9, you could do so with the following code:
[NSLayoutConstraint constraintWithItem:foo attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:bar attribute:NSLayoutAttributeLeadingMargin multiplier:1.0 constant:0.0].active = YES; [NSLayoutConstraint constraintWithItem:foo attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:bar attribute:NSLayoutAttributeTrailingMargin multiplier:1.0 constant:0.0].active = YES;
That works, but it’s not particularly pretty or readable, and for something so simple and widely-used, UIKit was begging for some convenience methods. Beginning with iOS 9, developers could use the new NSLayoutAnchor and UILayoutGuide classes to accomplish the same thing with less and more intuitive code:
UILayoutGuide *margin = bar.layoutMarginsGuide; [foo.leadingAnchor constraintEqualToAnchor:margin.leadingAnchor].active = YES; [foo.trailingAnchor constraintEqualToAnchor:margin.trailingAnchor].active = YES;
But that’s just the beginning. While NSLayoutAnchor is a relatively simple factory class designed for creating NSLayoutConstraint objects using convenience methods, UILayoutGuide is a more far-reaching, robust addition to the platform.
In the past, many common Auto Layout scenarios required the use of dummy views; for example, if you wanted to use constraints to define the size of empty space between two other views, you had to fill that space with a dummy view. While this works (and the use of this technique is widespread), it’s generally considered bad practice, as you’re introducing a lot of unnecessary overhead by instantiating an entire UIView object simply to represent empty space. Additionally, constraint-based layouts can quickly get messy and difficult to parse, especially if you have many sibling views inside a larger container.
The UILayoutGuide class is designed to solve these problems. Wherever you would have used a dummy view in your constraint-based layout previously, you can now use an instance of the much lighter-weight UILayoutGuide class. And since layout guides can contain other views and controls, you can also use them to encapsulate different parts of your view and break your layouts up into modular – and much more manageable – chunks.
Beginning with this week’s release, the iOS bridge now offers full support for both NSLayoutAnchor and UILayoutGuide. In addition, we’ve fixed lots of bugs and made performance improvements to our constraint solving and layout libraries, making for a generally faster and more performant experience using layout constraints with the bridge. We’ve also added support for a commonly requested feature on NSLayoutConstraint itself: priorities. With the latest version of the bridge, you can bring more of your iOS code base and constraint-based layouts to Windows 10 than ever before.
This week also marks the launch of our new samples repo on Github. Over the coming weeks and months, we’ll be adding tons of example code, apps, and documentation to the samples repo to help you get started with the bridge faster.
The samples repo currently offers two kinds of sample projects: bite-size samples and full apps. The bite-size sample projects demonstrate particular functionality and common scenarios with the iOS bridge, while the full apps showcase the end-to-end journey of a developer using the bridge.
Currently, the repo offers two bite-size samples that demonstrate using Windows 10 features directly from Objective-C. You’ll find tutorials for using Live Tiles and popping toast notifications using the native Objective-C code and syntax you’re familiar with. Soon, we’ll be adding many, many more, including samples that showcase Cortana, in-app purchases, maps and push notifications.
The full apps section, meanwhile, offers complete XCode projects for more complex and fully-featured apps that are compatible with the bridge. At launch, we’re including the To Do list and Calculator apps previously featured in tutorial blog posts on the Building Apps for Windows blog (here and here).
Thanks for reading along! Ready to get started with the bridge? Here are some resources to check out: