Cross Platform C#
What's in iOS 9 for C# Developers?
A high-level look at some of the more interesting features of the newest Apple mobile OS that can trip up C# developers making apps for it.
At the Apple Worldwide Developer Conference in September, the company introduced the first public beta of iOS 9 (as well as the first beta of OS X El Capitan). Xamarin got access to beta copies of iOS 9 at the same time as every other company, but almost immediately, Xamarin followed up the Apple announcements by making available Xamarin.iOS 9 and Xamarin Studio 6, which adds iOS 9 support.
What about supporting new hardware? Xamarin is in the same boat as the rest of us there, it doesn't get an advanced look at beta hardware. Xamarin orders its iOS 9 hardware, then stands in line like the rest of us. Expect Xamarin to release a number of updates to its software as iOS 9 hardware gets baked. This is normal as issues are found and resolved on multiple types of hardware.
And so what about new programming APIs? As always, each new version of iOS has a tremendous number of them, too many to be covered in a single article. For the purposes of this article, I'll look at the new back button, search and some other useful features in iOS 9 that might trip up C# developers.
Cross-App Back Button
Android users have had the back button on their system for years. Initially, this was a hardware button, but with Android 4-plus, it's a soft button. With the back button, Android users could go back to the previous screen, even if that screen was within a different application. For example, if a user was in mail, selected a link, and opened the browser, the user could select the back button and go from the Web browser back to the mail application.
Attempting to do the same thing in iOS was not as smooth. It had no back button that worked across applications in iOS 8 and older. The user would have to double tap the home button, scroll through the running apps, and select the app to return to. While not bad, this was a bit clumsy. Users should be able to go back to the previous app easily and in a manner similar to how the back button currently works in iOS 8. With the release of iOS 9, Apple has built this feature into the OS. Thankfully, your application gets the cross-application back button just by running on iOS 9.
Search
Applications have been self-contained units in iOS for years. There has been no way for applications to easily share data between apps running on a device. It's been a limitation in iOS due to security concerns. iOS has SQLite, a file-based database, built into the OS, so most applications will use it for storing data locally. Applications shouldn't be able to access data from other applications by just attempting to access their data via the file system. Android has had a feature to allow for applications to share their information for years. With iOS 9, Apple now has a feature that allows for applications to access data in one application from another. Much like in Android, it will be the responsibility of the developer to expose the appropriate information from an application.
In iOS 9, search has evolved to provide applications with new ways to share and access data from within an iOS 9 application. It has a number of new search APIs that allow for app data to be searchable and presentable via Spotlight, Safari, Handoff, Siri, and Suggestions. This can allow an application to present data from deep within an application.
Which Content To Expose?
Apple provides several suggestions for determining what content to make searchable and what actions to provide:
- It recommends any content that is viewed, created or curated by the users from within the application.
- It recommends navigation points and features that are important within an application.
- It also suggests new content within an application. This can include new messages sent to a user, content that's regularly displayed by the app and new data that has been sent to the user.
App Search APIs
App search is composed of three separate APIs. These include:
- NSUserActivity: An addition to the existing iOS 8 Handoff API, NSUserActivity will be used to make an application's interaction history searchable publically and privately by a user.
- Core Spotlight: Used for indexing private content, this API allows an application to index content that can be presented in search results. Core Spotlight is similar to a database API. Data elements can be added or removed. WebMarkup: Designed for apps that provide their content through a Web interface/Safari, this API allows content to be marked up with special links that can be crawled by Apple and that can provide deep linking to an application in iOS 9.
Which approach to making content searchable makes the most sense? It depends on the application type, as well as the type of content that's being presented to the user. Apple suggests:
- The NSUserActivity framework should be used to provide search ability for both public and private content, as well as search ability of navigation points within an application.
- The Core Spotlight framework should be used to provide search ability to private data stored on the device.
- The WebMarkup framework should be used to provide access to data for apps that present their content within a Web site, as well as within an app.
Let's look at some sample code from Xamarin evangelists Craig Dunn and Larry O'Brien. Listing 1 shows sample code that provides the ability to add and remove content from an index.
The first step in the code is to add content; you can see this in the Index method in Listing 1. In this method, a business object of type Task (not to be confused with a System.Thread.Tasks.Task) is added to an index. A searchable item is created with a primary key, and it's added to the Spotlight search domain to be found when a user searches for content. The second method shows how to delete an item from the index.
Listing 1: Methods to Add and Delete a Record from Being Searchable by Spotlight
public static void Index (Task t) {
var attributeSet = new CSSearchableItemAttributeSet (UTType.Text);
attributeSet.Title = t.Name;
attributeSet.ContentDescription = t.Notes;
attributeSet.TextContent = t.Notes;
var dataItem = new CSSearchableItem (t.Id.ToString(), Spotlight.SearchDomain, attributeSet);
CSSearchableIndex.DefaultSearchableIndex.Index (new CSSearchableItem[] {dataItem}, err => {
if (err != null) {
Console.WriteLine (err);
} else { Console.WriteLine ("Indexed inividual item successfully");
}
});
}
public static void Delete (Task t) {
CSSearchableIndex.DefaultSearchableIndex.Delete (new string[] {t.Id.ToString()}, err => {
if (err != null) {
Console.WriteLine (err);
} else {
Console.WriteLine ("Deleted inividual item from CS index");
}
});
}
Figure 1 shows a search in Apple Spotlight in the iOS Simulator returning search data. Figure 2 shows the data within the application within search.
I'm just scratching the surface, of course. Briefly, here are a number of other interesting features that I'll look more closely into moving forward:
- Multitasking: Even with the large screen on the iPad, users have been limited to only being able to run one application at a time. Being able to run multiple applications at a time has been a feature of modern OSes for years. With iOS 9, your iPad can now support having two applications open on your screen simultaneously.
- 3D Touch: With the new "s" versions of the iPhone 6, Apple has released support for pressure-sensitive touch. This allows for an application to determine the amount of pressure being exerted by the user on the screen.
- Stack View: The Stack View control allows an application to manage a stack of subviews. These views can dynamically respond to the iOS device's orientation, as well as the screen size.
- Other Enhancements: A number of features and APIs have improvements. One involves the Contacts and Contacts UI. These APIs have been designed to replace the Address Book and Address Book UI APIs. There are new and enhanced gaming APIs. There are also enhancements to HomeKit, Handoff, CloudKit, HealthKit, MapKit and other frameworks in iOS 9.
What's in Store
As of Sept. 24, 2015, Apple claimed that more than 50 percent of its customers had upgraded to iOS 9. Personally, I find this number a bit high to be believable. Even if that figure were half-true, it may not be a bad idea to begin taking advantage of iOS 9 features in your apps now.
About the Author
Wallace (Wally) B. McClure has authored books on iPhone programming with Mono/Monotouch, Android programming with Mono for Android, application architecture, ADO.NET, SQL Server and AJAX. He's a Microsoft MVP, an ASPInsider and a partner at Scalable Development Inc. He maintains a blog, and can be followed on Twitter.