How to manage license key for macOS apps
Use a license key service like Gumroad https://help.gumroad.com/article/76-license-keys
func check(productId: String, licenseKey: String) {
var components = URLComponents(url: URL(string: "https://api.gumroad.com/v2/licenses/verify")!, resolvingAgainstBaseURL: false)!
components.queryItems = [
URLQueryItem(name: "product_permalink", value: productId),
URLQueryItem(name: "license_key", value: licenseKey)
]
// https://help.gumroad.com/article/76-license-keys
var request = URLRequest(url: components.url!)
request.httpMethod = "POST"
let task = URLSession.shared.dataTask(with: request, completionHandler: { data, response, error in
guard let data = data else {
self.shake()
return
}
guard let gumroad = try? JSONDecoder().decode(Gumroad.self, from: data) else {
self.shake()
return
}
DispatchQueue.main.async {
self.handle(gumroad: gumroad)
}
})
task.resume()
}
struct Gumroad: Codable {
let uses: Int
}
uses specifies how many times https://api.gumroad.com/v2/licenses/verify has been hit with 200 status code. Then we for example can check uses count
func handle(gumroad: Gumroad) {
guard gumroad.uses < 5 else {
showAlert()
return
}
overlay.removeFromSuperview()
applyLicense()
}
We can use secured user defaults to store license status, or check at app launch using SSL Pinning