Skip to content

Instantly share code, notes, and snippets.

@ChrisMash
Last active April 30, 2023 12:01
Show Gist options
  • Save ChrisMash/d52d90e6e9eba99447c3a201f62ee601 to your computer and use it in GitHub Desktop.
Save ChrisMash/d52d90e6e9eba99447c3a201f62ee601 to your computer and use it in GitHub Desktop.
Example of updating an app's provisioning profile expiry time in a Firebase realtime database (gist for https://chris-mash.medium.com/using-provisioningprofile-and-firebase-to-monitor-profile-expiries-remotely-7832f475d7b7)
//
// Created by Chris Mash on 10/02/2022.
//
import UIKit
import ProvisioningProfile
import FirebaseDatabase
// NOTE: this gist uses some personal extensions I've written which aren't shared here but you should be able to swap out easily enough:
// Bundle.main.appVersion
// Bundle.main.appName
// UIDevice.current.deviceModel
public struct ProfileExpiryLogger {
public static func logProfileExpiry() {
// Grab the expiry time using https://github.com/ChrisMash/ProvisioningProfile
guard let expiry = ProvisioningProfile.profile()?.expiryDate else {
NSLog("ProfileExpiryLogger ERR: Failed to get provisioning profile expiry, not logging anything to Firebase")
return
}
// Grab the vendor ID and log all this to the DB
getIDForVendorAndUpdateDB(with: [
"version": Bundle.main.appVersion,
"expiry": expiry.timeIntervalSinceReferenceDate,
"updated": Date.now.timeIntervalSinceReferenceDate
])
}
// MARK: Private functions
private static func getIDForVendorAndUpdateDB(with data: [String: Any]) {
// Grab the vendor ID as a unique (unstable, read up on it if you don't understand why) identifier for the device
guard let idForVendor = UIDevice.current.identifierForVendor else {
// NOTE: This can happen prior to, or just after, the device unlocking (after a restart)
let delay = 2
print("ProfileExpiryLogger: Failed to get vendor ID, trying again in \(delay)s")
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(delay)) {
getIDForVendorAndUpdateDB(with: data)
}
return
}
// Create a reference for the device using the device model and the vendor ID
let deviceID = "\(UIDevice.current.deviceModel): \(idForVendor)"
// Update (or add if it doesn't exist yet) the DB entry for this device
Database.database(url: "your-database-url").reference()
.child("devices")
.child(deviceID)
.child("apps")
.child(Bundle.main.appName)
.setValue(data) { error, dbRef in
if let error = error {
NSLog("ProfileExpiryLogger ERR: Failed to log profile expiry details to Firebase: \(error)")
} else {
NSLog("ProfileExpiryLogger: Successfully logged profile expiry details to Firebase")
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment