I just received an email that my iOS Distribution Certificate is expiring. What do I do?

First of all, don’t panic. If your app is already for sale on the App Store, this email has no impact on that. Your app will remain up for sale even if your Distribution Certificate expires. It’s going to be OK.

It’s a scary email, but it won’t cause anything to break with your existing apps on the App Store.

What is the iOS Distribution Certificate for?

The purpose of the iOS Distribution Certificate is actually pretty narrow, it is just used for submitting a build of your app to Apple’s iTunes Connect system so that it can be reviewed and processed for sale on the App Store. The time it is needed is just for the submission and review of an app build by Apple. Once Apple approves the app, and it is made available for sale on the App Store, it doesn’t matter if your iOS Distribution Certificate has expired.

What do I need to do?

So while the email sounds extremely urgent, it only really impacts your ability to submit new builds of your app. Obviously this is important, but it does not change what is already on the App Store.

What needs to be done is to create a new iOS Distribution Certificate on the computer that you use to submit app builds. There are two options to create it: using Xcode, or using the Member Center web tool.

Xcode now has an automated way to do this that handles all the details for you.

  1. Open Xcode preferences, select the Accounts tab.
  2. In the left column, choose the Apple ID for the account that you would like to create an iOS Distribution certificate.
  3. In the right column, select the team for the account. (If your legal entity is a company instead of an individual, there might be more than one option. Choose the one with the “Agent” role.)
  4. Click the “View Details…” button.
  5. Under Signing Identities, there will be list of certificates you can create for that account and role.
  6. Click the “Create” button next to “iOS Distribution”.

If the Create button is grayed out, go back to steps 2 and 3 to make sure you have selected the appropriate account and role for the expired certificate.

If you prefer to do it manually, use the Member Center and follow the steps to create a Production App Store and Ad Hoc certificate.

  1. Go to the Apple Developer certificate management site: https://developer.apple.com/account/ios/certificate/ (You can also use this site to view and manage existing certificates.)
  2. Click the plus button on the top left.
  3. Under the Production section, choose “App Store and Ad Hoc”.
  4. Follow the instructions to use the Keychain Access app on your Mac to create a Certificate Signing Request and upload it to the site.

How can I debounce a method call in Swift 3?

Debouncing calls is great for situations where a repeating user action can cause network requests to happen. The problem is that the user’s actions can generate many requests in a short period of time, which can be slow to respond on a cellular network.

This also applies to other types of wireless communication, such as BLE.

Here is an implementation of a Debouncer class in Swift 3. Github Gist link

import Foundation

class Debouncer {

    // Callback to be debounced
    // Perform the work you would like to be debounced in this callback.
    var callback: (() -> Void)?

    private let interval: TimeInterval // Time interval of the debounce window

    init(interval: TimeInterval) {
        self.interval = interval
    }

    private var timer: Timer?

    // Indicate that the callback should be called. Begins the debounce window.
    func call() {
        // Invalidate existing timer if there is one
        timer?.invalidate()
        // Begin a new timer from now
        timer = Timer.scheduledTimer(timeInterval: interval, target: self, selector: #selector(handleTimer), userInfo: nil, repeats: false)
    }

    @objc private func handleTimer(_ timer: Timer) {
        if callback == nil {
            NSLog("Debouncer timer fired, but callback was nil")
        } else {
            NSLog("Debouncer timer fired")
        }
        callback?()
        callback = nil
    }

}

The Debounce class is straight forward to use:

// Create a Debouncer with a half-second time interval
let debouncer = Debouncer(interval: 0.5)

debouncer.callback = {
    // Send the debounced network request here
    print("Send network request")
}

func textDidChangeDelegateMethod() {
    // When the user performs a repeating action, such as entering text, invoke the `call` method
    debouncer.call()
}

PS: Interestingly, the term debounce comes from electrical engineering. “Bouncing is the tendency of any two metal contacts in an electronic device to generate multiple signals as the contacts close or open.” (http://whatis.techtarget.com/definition/debouncing) So a de-bouncing circuit cleans up this bouncing between the signals, by providing a single signal.

How to calculate tax in Swift

When performing any type of financial calculation, it is best not to use floating point numbers. Performing arithmetic in base 10 will allow you to avoid unexpected errors in how floats represent some decimals. Fortunately, Apple provides the NSDecimalNumber class to handle calculations using base 10 arithmetic.

Tax calculation example:

let price: Int = 15 // $15
let priceDecimalNumber = NSDecimalNumber(integer: price)

let taxPercentage = NSDecimalNumber(string: "0.09") // 9%

let tax = priceDecimalNumber.decimalNumberByMultiplyingBy(taxPercentage)
let taxDouble = tax.doubleValue // Retrieve the double value

More information about floating point arithmetic:
http://floating-point-gui.de/basic/