Watchos

WWDC2020: What's new in CryptoKit

Published on

CryptoKit, introduced in WWDC2019, allows us to perform cryptographic operations very easily.

While CryptoKit still doesn’t offer many algorithms and functionality, it’s still growing, and this year CryptoKit and do more.

HKDF

Key derivation functions have been available from day one, but it wasn’t possible to derive keys independently. It was only possible to do so if you were using elliptic curve key agreement protocols.

To do this, there is a new HKDF object with static methods. One such method is deriveKey with multiple overloads:


Logging Messages With the Unified Logging System on Apple Platforms

Published on

Last time we talked about the basics of the Unified Logging System, we set the basic concepts and code we need to write logs, along with the different logging levels, and more.

In this article we will talk about actually logging messages, how the framework is “smart enough” to strip out sensitive user info by default, and how we can control what gets stripped.

Logging Messages

The framework supports interpolated strings right out of the box when you are using the new system in Swift.


Introduction to Apple's Unified Logging System on iOS 14 in Swift

Published on

It is no surprise that software tend to write logs to a local file as they execute. As events, errors, or exceptional situations occur, a lot of software takes note of them using a local logging solution. This is done because these practices can allow us to troubleshoot problems for our users, find bugs, and in general understand the behavior of our software in untested or lesser tested scenarios.

When comes to iOS and other Apple platforms, there have always been third party dependencies that allow you to do this. A lot of developers roll their own solution and write events in plain text files. It wasn’t until iOS 8 and macOS 10.10 that Apple provided us with a unified logging system that is easy to use and is very performant - OSLog.


A File Download Queue in Combine for Swift

Published on

Combine allows us to create pipelines for a lot of tasks. Thanks to the fact it can do work concurrently without leveraging callbacks, it is very easy to build things that would otherwise be very complex.

In this short article, we will build a file download queue that downloads images sequentially. You can use this as the base for more complex queues.

The queue will download an array of images sequentially. If you wanted to support concurrent queues, it would probably be wise to instantiate this publisher as many times as necessary.


Error Protocol Specializations in Swift

Published on

Earlier this week I was scrolling through my Twitter feed as usual and I found this tweet that made me realize I may have been handling errors incorrectly in Swift all my life. This prompted me to research a bit more about error handling in Swift, and it turns out there’s many specialized Error protocols you can conform to, and you should probably be using them over the default Error provided by the language. All these specializations conform to Error themselves. In this article, we will explore a few specializations we can use when dealing with errors in Swift.


Using the iOS Keychain in Swift

Published on

This article is an entirely rewritten version of an old tutorial I wrote years ago titled “Using the iOS Keychain”. Originally written in Objective-C, the old version has been archived but it is accessible here.

The Keychain is the place where you would store sensitive data. As secure as iOS currently is, the keychain is the right place to store passwords, authentication tokens, and other sensitive data. You should not store this kind of data in UserDefaults, even if iOS has made it harder to access that data for normal users in the latest versions.


Dependency Injection with Storyboards on Apple Platforms

Published on

Every iOS developer has written a line of code like this one at least once:

class DollInfoViewController: UIViewController {
  
  var dollModel: Doll?

  override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
  }


}

Or in the worst case, you may have seen code like this:

var dollModel: Doll!

Then, when you want to create a view controller of that type you’d do:

// First we need to check if we can actually instantiate the view controller.
guard let dollInfoVc = storyboard?.instantiateViewController(withIdentifier: "DollInfo") as? DollInfoViewController else {
    fatalError("Unable to load view controller.")
}

// Then we pass in the data we want to work with. 
dollInfoVc.dollModel = doll

Code like this is very error prone. The worst part is that up untul iOS 13, it was pretty much necessary to pass data around from view controller to view controller. It’s not possible to do this in any different way in different iOS versions.


Finding Related Words with NLEmbedding

Published on

There may be cases in which you need to find related words to others. With the NSLEmbedding class, you can find related strings based on the proximity of their vectors.

Using NLEmbedding

Using NLEmbedding is very straight forward. A simple task is to get an array of related words, which come as an array of (String, NLDistance) back.

The distance between words tells you how “related” they are

let embedding = NLEmbedding.wordEmbedding(for: .english)
let foundWords = embedding!.neighbors(for: "family", maximumCount: 3)
print(foundWords)

In this example, it will print:


Analyzing Natural Language Text with NLTagger

Published on

In the past few weeks, we have explored how we can tokenize natural language text and how to recognize the language a natural language text is written in. This week we will continue exploring more natural language APIs provided by the NaturalLanguage framework. We will learn about the NLTagger class, which allows us to to analyze natural language text to find parts of speech, lexical classes, lemma, scripts, and more. This API, introduced in iOS 12, implements machine learning to work, and just like the other NaturalLanguage classes, is very easy to use.


Recognizing the language in a Natural Language Text with NLanguageRecognizer

Published on

Continuing my trend of writing about language processing, today I want to discuss about identifying the language of a body of text. This is an interesting task we can do thanks, once again, to Apple’s investment in APIs linked to machine learning.

Today we will explore the NLLanguageRecognizer object. Introduced in iOS 12, this class can do a lot of language recognizing, from detecting the “dominant language” of a string, to all the possible languages.