Ideally, errors should never occur. File we need should always be available and networks should always be present and reliable. Unfortunately this reality is not ideal and we have to deal with the consequences. Thankfully the Swift team included a great way to handle these deviations from the ideal in Swift 2. Swift Error Handling lets us quickly and easily warn the compiler whether a function can throw an error, actually throw it, and handle it in whatever way seems appropriate.
[Read more…]
Creating and Modifying a URL in your Swift App
In many types of apps, you have to access files. They can be in your app bundle, on the file system, or even somewhere on the web. You need to have someway to refer to where they are in your code. On Apple platforms, you basically have 2 choices, as a String or aURL.
Given the address bar right above you, or any use of a terminal, a Swift String would be a very understandable choice, I mean, that’s all that text is in the address bar, right? Many of the older APIs in the Cocoa and Cocoa Touch SDKs take URLs and Strings (referred to as a “path” in those APIs usually), but things are moving more and more towards just using URL objects everywhere. URL objects have many advantages over String paths, most notably that you can access different parts of the URL from properties, instead of having to write your own code to parse those components from the path String.
Stay tuned as we learn a thing or two about creating and using URL objects in your Swift app.
[Read more…]
Watch Connectivity in Swift — Application Context
In the age of watchOS 1, the watchKit extension was on the paired iOS device, making sharing data between it and the main iOS app easy. For the simplest of data, like preferences, we could just use NSUserDefaults with App Groups functionality. We still should use that when sharing data between other extensions that still remain on the phone, like today view extensions, but not for watchOS apps anymore.
Luckily, Apple gave us a new API to use, that is significantly more robust than piggy-backing on App Groups, Watch Connectivity. Watch Connectivity gives a lot more information about the status of the connection between your Apple Watch and its paired iPhone. It also allows interactive messaging between the iOS App and the watchOS app, as well as background transfers that come in 3 flavors. Those flavors are:
- Application Context
- User Info Transfer
- File Transfer
Today we will be talking about the first one, Application Context.
[Read more…]
watchOS Hello World App in Swift
I have been working on some watchOS updates lately, and thought it would be helpful to share a bit about that. First things first, we need to actually know how to make a watchOS app in the first place. It’s time to do “Hello World!” watchOS style!
Before we begin though, I want to help you the reader, learn what you need to know about programming for iOS, watchOS, tvOS, or OS X in Swift. As such, if you have any topics you would like to learn about on the Coding Explorer Blog, please suggest them to me on Twitter @CodingExplorer or the Contact Page.
[Read more…]
API Availability Checking in Swift
With all of the new and awesome APIs that come out every year for each successive iOS version, it can be tempting to just drop support for the previous versions of iOS, and just go completely to the new stuff. Of course though, we usually can’t do that for many reasons. As such, we need to support older versions of iOS, and maybe dropping the oldest ones after they just become too difficult to support, or when active users on that platform drop to a certain amount.
There have been ways to check for API availability for some time, by checking if something responds to a selector, or just checking the iOS version ourselves and using that in a normal Swift “if” statement. With the arrival of Swift though, we have an even nicer version of API checking, that is even helped by Xcode itself. This allows Xcode to actually warn you if you try to use something that is unavailable on one of the deployment targets for your Swift app.
[Read more…]
- 1
- 2
- 3
- …
- 16
- Next Page »