|
| 1 | +// |
| 2 | +// 01-app-context.swift |
| 3 | +// |
| 4 | +// Copyright (c) PubNub Inc. |
| 5 | +// All rights reserved. |
| 6 | +// |
| 7 | +// This source code is licensed under the license found in the |
| 8 | +// LICENSE file in the root directory of this source tree. |
| 9 | +// |
| 10 | + |
| 11 | +// snippet.import |
| 12 | +import PubNubSDK |
| 13 | + |
| 14 | +// snippet.end |
| 15 | + |
| 16 | +// snippet.pubnub |
| 17 | +// Initializes a PubNub object with the configuration |
| 18 | +let pubnub = PubNub( |
| 19 | + configuration: PubNubConfiguration( |
| 20 | + publishKey: "demo", |
| 21 | + subscribeKey: "demo", |
| 22 | + userId: "myUniqueUserId" |
| 23 | + ) |
| 24 | +) |
| 25 | + |
| 26 | +// snippet.end |
| 27 | + |
| 28 | +// snippet.all-user-metadata |
| 29 | +// Retrieve all user metadata |
| 30 | +pubnub.allUserMetadata { result in |
| 31 | + switch result { |
| 32 | + case let .success(response): |
| 33 | + print("The user metadata objects: \(response.users)") |
| 34 | + print("The next page used for pagination: \(String(describing: response.next))") |
| 35 | + case let .failure(error): |
| 36 | + print("Fetch All request failed with error: \(error.localizedDescription)") |
| 37 | + } |
| 38 | +} |
| 39 | +// snippet.end |
| 40 | + |
| 41 | +// snippet.fetch-user-metadata |
| 42 | +// Retrieve user metadata for a specific identifier |
| 43 | +pubnub.fetchUserMetadata("some-id") { result in |
| 44 | + switch result { |
| 45 | + case let .success(userMetadata): |
| 46 | + print("The metadata for `\(userMetadata.metadataId)`: \(userMetadata)") |
| 47 | + case let .failure(error): |
| 48 | + print("Fetch request failed with error: \(error.localizedDescription)") |
| 49 | + } |
| 50 | +} |
| 51 | +// snippet.end |
| 52 | + |
| 53 | +// snippet.set-user-metadata |
| 54 | +// Set user metadata for a specific identifier |
| 55 | +let userMetadataToSet = PubNubUserMetadataBase( |
| 56 | + metadataId: "some-id", |
| 57 | + name: "Some User", |
| 58 | + custom: ["department": "Engineering"] |
| 59 | +) |
| 60 | + |
| 61 | +pubnub.setUserMetadata(userMetadataToSet) { result in |
| 62 | + switch result { |
| 63 | + case let .success(userMetadata): |
| 64 | + print("The metadata for `\(userMetadata.metadataId)`: \(userMetadata)") |
| 65 | + case let .failure(error): |
| 66 | + print("Create request failed with error: \(error.localizedDescription)") |
| 67 | + } |
| 68 | +} |
| 69 | +// snippet.end |
| 70 | + |
| 71 | +// snippet.remove-user-metadata |
| 72 | +// Remove user metadata for a specific identifier |
| 73 | +pubnub.removeUserMetadata("some-id") { result in |
| 74 | + switch result { |
| 75 | + case let .success(metadataId): |
| 76 | + print("The metadata has been removed for the identifier `\(metadataId)`") |
| 77 | + case let .failure(error): |
| 78 | + print("Delete request failed with error: \(error.localizedDescription)") |
| 79 | + } |
| 80 | +} |
| 81 | +// snippet.end |
| 82 | + |
| 83 | +// snippet.all-channel-metadata |
| 84 | +// Retrieve all channel metadata |
| 85 | +pubnub.allChannelMetadata { result in |
| 86 | + switch result { |
| 87 | + case let .success(response): |
| 88 | + print("The channel metadata objects \(response.channels)") |
| 89 | + print("The next page used for pagination: \(String(describing: response.next))") |
| 90 | + case let .failure(error): |
| 91 | + print("Fetch All request failed with error: \(error.localizedDescription)") |
| 92 | + } |
| 93 | +} |
| 94 | +// snippet.end |
| 95 | + |
| 96 | +// snippet.fetch-channel-metadata |
| 97 | +// Retrieve channel metadata for a specific identifier |
| 98 | +pubnub.fetchChannelMetadata("some-channel-id") { result in |
| 99 | + switch result { |
| 100 | + case let .success(channelMetadata): |
| 101 | + print("The metadata for `\(channelMetadata.metadataId)`: \(channelMetadata)") |
| 102 | + case let .failure(error): |
| 103 | + print("Fetch request failed with error: \(error.localizedDescription)") |
| 104 | + } |
| 105 | +} |
| 106 | +// snippet.end |
| 107 | + |
| 108 | +// snippet.set-channel-metadata |
| 109 | +// Set channel metadata for a specific identifier |
| 110 | +let channelMetadataToSet = PubNubChannelMetadataBase( |
| 111 | + metadataId: "some-channel-id", |
| 112 | + name: "Channel Name" |
| 113 | +) |
| 114 | + |
| 115 | +pubnub.setChannelMetadata(channelMetadataToSet) { result in |
| 116 | + switch result { |
| 117 | + case let .success(channelMetadata): |
| 118 | + print("The metadata for `\(channelMetadata.metadataId)`: \(channelMetadata)") |
| 119 | + case let .failure(error): |
| 120 | + print("Create request failed with error: \(error.localizedDescription)") |
| 121 | + } |
| 122 | +} |
| 123 | +// snippet.end |
| 124 | + |
| 125 | +// snippet.update-existing-channel-metadata |
| 126 | +// Simulates a reference to already fetched channel metadata |
| 127 | +var existingChannelMetadata = PubNubChannelMetadataBase( |
| 128 | + metadataId: "yourChannelMetadataId", |
| 129 | + name: "Offtopic", |
| 130 | + type: "Miscellaneous", |
| 131 | + status: "active", |
| 132 | + custom: ["admin": "John Stones"] |
| 133 | +) |
| 134 | + |
| 135 | +// Modify properties directly, such as updating `custom` and `name` |
| 136 | +existingChannelMetadata.custom = ["admin": "Charles Dawson"] |
| 137 | +existingChannelMetadata.name = "Unrelated" |
| 138 | + |
| 139 | +// When you're ready, set the updated metadata on the server to persist the changes: |
| 140 | +pubnub.setChannelMetadata(existingChannelMetadata) { result in |
| 141 | + switch result { |
| 142 | + case let .success(metadata): |
| 143 | + print("Did successfully set channel metadata with \(metadata.metadataId)") |
| 144 | + case let .failure(error): |
| 145 | + print("Unexpected error along the way: \(error)") |
| 146 | + } |
| 147 | +} |
| 148 | +// snippet.end |
| 149 | + |
| 150 | +// snippet.remove-channel-metadata |
| 151 | +// Remove channel metadata for a specific identifier |
| 152 | +pubnub.removeChannelMetadata("some-channel-id") { result in |
| 153 | + switch result { |
| 154 | + case let .success(metadataId): |
| 155 | + print("The metadata has been removed for the channel `\(metadataId)`") |
| 156 | + case let .failure(error): |
| 157 | + print("Delete request failed with error: \(error.localizedDescription)") |
| 158 | + } |
| 159 | +} |
| 160 | +// snippet.end |
| 161 | + |
| 162 | +// snippet.fetch-memberships |
| 163 | +// Retrieve channel memberships for a specific user identifier |
| 164 | +pubnub.fetchMemberships(userId: "some-user-id") { result in |
| 165 | + switch result { |
| 166 | + case let .success(response): |
| 167 | + print("The channel memberships for the userId: \(response.memberships)") |
| 168 | + print("The next page used for pagination: \(String(describing: response.next))") |
| 169 | + case let .failure(error): |
| 170 | + print("Fetch Memberships request failed with error: \(error.localizedDescription)") |
| 171 | + } |
| 172 | +} |
| 173 | +// snippet.end |
| 174 | + |
| 175 | +// snippet.fetch-memberships-sort |
| 176 | +// Retrieve channel memberships for a specific user identifier and sort them by name |
| 177 | +pubnub.fetchMemberships( |
| 178 | + userId: "someUserId", |
| 179 | + sort: [.init(property: .object(.name))] |
| 180 | +) { result in |
| 181 | + switch result { |
| 182 | + case let .success(response): |
| 183 | + print("List of channel memberships: \(response.memberships)") |
| 184 | + print("The next page used for pagination: \(String(describing: response.next))") |
| 185 | + case let .failure(error): |
| 186 | + print("An error occurred: \(error.localizedDescription)") |
| 187 | + } |
| 188 | +} |
| 189 | +// snippet.end |
| 190 | + |
| 191 | +// snippet.set-memberships |
| 192 | +let membershipToSet = PubNubMembershipMetadataBase( |
| 193 | + userMetadataId: "my_user", |
| 194 | + channelMetadataId: "my_channel" |
| 195 | +) |
| 196 | + |
| 197 | +// Set channel memberships for a specific user identifier |
| 198 | +pubnub.setMemberships( |
| 199 | + userId: membershipToSet.userMetadataId, |
| 200 | + channels: [membershipToSet] |
| 201 | +) { result in |
| 202 | + switch result { |
| 203 | + case let .success(response): |
| 204 | + print("The channel memberships for the userId: \(response.memberships)") |
| 205 | + print("The next page used for pagination: \(String(describing: response.next))") |
| 206 | + case let .failure(error): |
| 207 | + print("Update Memberships request failed with error: \(error.localizedDescription)") |
| 208 | + } |
| 209 | +} |
| 210 | +// snippet.end |
| 211 | + |
| 212 | +// snippet.remove-memberships |
| 213 | +let membershipToRemove = PubNubMembershipMetadataBase( |
| 214 | + userMetadataId: "my_user", |
| 215 | + channelMetadataId: "my_channel" |
| 216 | +) |
| 217 | + |
| 218 | +// Remove channel memberships for a specific user identifier |
| 219 | +pubnub.removeMemberships( |
| 220 | + userId: membershipToRemove.userMetadataId, |
| 221 | + channels: [membershipToRemove] |
| 222 | +) { result in |
| 223 | + switch result { |
| 224 | + case let .success(response): |
| 225 | + print("The channel memberships for the userId: \(response.memberships)") |
| 226 | + print("The next page used for pagination: \(String(describing: response.next))") |
| 227 | + case let .failure(error): |
| 228 | + print("Update Memberships request failed with error: \(error.localizedDescription)") |
| 229 | + } |
| 230 | +} |
| 231 | +// snippet.end |
| 232 | + |
| 233 | +// snippet.fetch-members |
| 234 | +// Retrieve members for a specific channel |
| 235 | +pubnub.fetchMembers(channel: "my_channel_id") { result in |
| 236 | + switch result { |
| 237 | + case let .success(response): |
| 238 | + print("The user members for the channel: \(response.memberships)") |
| 239 | + print("The next page used for pagination: \(String(describing: response.next))") |
| 240 | + case let .failure(error): |
| 241 | + print("Fetch Memberships request failed with error: \(error.localizedDescription)") |
| 242 | + } |
| 243 | +} |
| 244 | +// snippet.end |
| 245 | + |
| 246 | +// snippet.set-members |
| 247 | +let userToSet = PubNubMembershipMetadataBase( |
| 248 | + userMetadataId: "my_user", |
| 249 | + channelMetadataId: "my_channel" |
| 250 | +) |
| 251 | + |
| 252 | +// Set members for a specific channel |
| 253 | +pubnub.setMembers( |
| 254 | + channel: userToSet.channelMetadataId, |
| 255 | + users: [userToSet] |
| 256 | +) { result in |
| 257 | + switch result { |
| 258 | + case let .success(response): |
| 259 | + print("The user members for the channel: \(response.memberships)") |
| 260 | + print("The next page used for pagination: \(String(describing: response.next))") |
| 261 | + case let .failure(error): |
| 262 | + print("Update Memberships request failed with error: \(error.localizedDescription)") |
| 263 | + } |
| 264 | +} |
| 265 | +// snippet.end |
| 266 | + |
| 267 | +// snippet.remove-members |
| 268 | +let memberToRemove = PubNubMembershipMetadataBase( |
| 269 | + userMetadataId: "my_user", |
| 270 | + channelMetadataId: "my_channel" |
| 271 | +) |
| 272 | + |
| 273 | +// Remove members for a specific channel |
| 274 | +pubnub.removeMembers( |
| 275 | + channel: memberToRemove.channelMetadataId, |
| 276 | + users: [memberToRemove] |
| 277 | +) { result in |
| 278 | + switch result { |
| 279 | + case let .success(response): |
| 280 | + print("The user members of the channel: \(response.memberships)") |
| 281 | + print("The next page used for pagination: \(String(describing: response.next))") |
| 282 | + case let .failure(error): |
| 283 | + print("Update Memberships request failed with error: \(error.localizedDescription)") |
| 284 | + } |
| 285 | +} |
| 286 | +// snippet.end |
0 commit comments