How to get One Signal user's unique player id in iOS?

You need to use OneSignal's observers such as OSSubscriptionObserver.

// Add OSSubscriptionObserver after UIApplicationDelegate
class AppDelegate: UIResponder, UIApplicationDelegate, OSSubscriptionObserver {

   func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
      // Add your AppDelegate as an subscription observer
      OneSignal.add(self as OSSubscriptionObserver)
   }

   // After you add the observer on didFinishLaunching, this method will be called when the notification subscription property changes. 
   func onOSSubscriptionChanged(_ stateChanges: OSSubscriptionStateChanges!) {
      if !stateChanges.from.subscribed && stateChanges.to.subscribed {
         print("Subscribed for OneSignal push notifications!")
      }
      print("SubscriptionStateChange: \n\(stateChanges)")

      //The player id is inside stateChanges. But be careful, this value can be nil if the user has not granted you permission to send notifications. 
      if let playerId = stateChanges.to.userId {
         print("Current playerId \(playerId)")
      }
   }
}

For a better explanation, here is the documentation for addSubscriptionObserver


I do need to get the Player Id (Or UserId) somewhere inside my code, and i dont want to save it anywhere.

I ended up using this code:

let userId = OneSignal.getPermissionSubscriptionState().subscriptionStatus.userId