2
votes

I have a webapp and the iOS app built in Swift. The thing is I don't know Swift and I'm trying to modify the iOS app in order to add the authorization key to the PubNub client before subscribing/publishing to channels. Link to PubNub docs

PRE:

  • Access Manager is enabled
  • my_auth_key is hardcoded and already enabled form the server for the corresponding channel I want to subscribe.

Here is the code

What's the correct way to set the auth key? Thanks in advance

2
Hi I am also creating demo for ios, but unable to set PAM. After research, I come to know that I can achieve this using function, But not able to understand how it will be achieved. Please help me - Aarti Vadgama

2 Answers

2
votes

Polak, mentioned docs code snippet refer to previously created instance to get it's pre-configured PNConfiguration instance and change required field. This practice can be used in case if you need change something at run-time. If you have data for authKey at the moment of client initialization, you can set it:

var pubnubClient: PubNub = {
    let configuration = PNConfiguration(publishKey: UCConstants.PubNub.publishKey, subscribeKey: UCConstants.PubNub.subscribeKey)
    configuration.authKey = "my_auth_key"
    return PubNub.clientWithConfiguration(configuration)
}()

Also, I've tried exact your code and don't have any issues with setting of authKey, because I can see it with subscribe request.
If you still will have troubles with PAM and auth key usage, please contact us on [email protected]

2
votes

Looks like you can:

let configuration = PNConfiguration(
    authKey: "super_secret",
    publishKey: UCConstants.PubNub.publishKey,
    subscribeKey: UCConstants.PubNub.subscribeKey
)

Based on the Obj-C code: https://github.com/pubnub/objective-c/blob/8c1f0876b5b34176f33681d22844e8d763019635/PubNub/Data/PNConfiguration.m#L174-L181