forked from pubnub/objective-c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PNAppDelegate.m
834 lines (598 loc) · 30.1 KB
/
PNAppDelegate.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
//
// AppDelegate.m
// Tutorial1
//
// Created by gcohen on 5/12/15.
// Copyright (c) 2015 geremy cohen. All rights reserved.
//
#import "PNAppDelegate.h"
#import <PubNub/PubNub.h>
#pragma mark Private interface declaration
@interface PNAppDelegate () <PNObjectEventListener>
#pragma mark - Properties
@property(nonatomic, strong) PubNub *client;
@property(nonatomic, strong) NSString *channel1;
@property(nonatomic, strong) NSString *channel2;
@property(nonatomic, strong) NSString *channelGroup1;
@property(nonatomic, strong) NSString *subKey;
@property(nonatomic, strong) NSString *pubKey;
@property(nonatomic, strong) NSString *authKey;
@property(nonatomic, strong) NSTimer *timer;
@property(nonatomic, strong) PNConfiguration *myConfig;
#pragma mark - Configuration
- (void)updateClientConfiguration;
- (void)printClientConfiguration;
#pragma mark -
@end
#pragma mark - Interface implementation
@implementation PNAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
#pragma mark - PAM Use Case Config
// Settings Config for PAM Example
// Uncomment this section line for a PAM use-case example
// http://www.pubnub.com/console/?channel=good&origin=d.pubnub.com&sub=pam&pub=pam&cipher=&ssl=false&secret=pam&auth=myAuthKey
// self.channel1 = @"good";
// self.channel2 = @"bad";
// self.pubKey = @"pam";
// self.subKey = @"pam";
// self.authKey = @"foo";
#pragma mark - Non-PAM Use Case Config
// Settings Config for Non-PAM Example
self.channel1 = @"bot";
self.channel2 = @"myCh";
self.channelGroup1 = @"myChannelGroup";
self.pubKey = @"demo-36";
self.subKey = @"demo-36";
self.authKey = @"myAuthKey";
#pragma mark - Kick the Tires!
[self tireKicker];
return YES;
}
- (void)pubNubInit {
// Initialize PubNub client.
self.myConfig = [PNConfiguration configurationWithPublishKey:_pubKey subscribeKey:_subKey];
[self updateClientConfiguration];
[self printClientConfiguration];
// Bind config
self.client = [PubNub clientWithConfiguration:self.myConfig];
// Configure logger
self.client.logger.enabled = YES;
self.client.logger.writeToFile = YES;
self.client.logger.maximumLogFileSize = (10 * 1024 * 1024);
self.client.logger.maximumNumberOfLogFiles = 10;
[self.client.logger setLogLevel:PNVerboseLogLevel];
// Bind didReceiveMessage, didReceiveStatus, and didReceivePresenceEvent 'listeners' to this delegate
// just be sure the target has implemented the PNObjectEventListener extension
[self.client addListener:self];
[self pubNubSetState];
}
- (void)tireKicker {
[self pubNubInit];
#pragma mark - Time
[self pubNubTime];
#pragma mark - Publish
[self pubNubPublish];
#pragma mark - History
[self pubNubHistory];
#pragma mark - Channel Groups Subscribe / Unsubscribe
[self pubNubSubscribeToChannelGroup];
[self pubNubUnsubFromChannelGroups];
#pragma mark - Channel Subscribe / Unsubscribe
[self pubNubSubscribeToChannels];
[self pubNubUnsubscribeFromChannels];
#pragma mark - Presence Subscribe / Unsubscribe
[self pubNubSubscribeToPresence];
[self pubNubUnsubFromPresence];
#pragma mark - Here Nows
[self pubNubHereNowForChannel];
[self pubNubGlobalHereNow];
[self pubNubHereNowForChannelGroups];
[self pubNubWhereNow];
#pragma mark - CG Admin
[self pubNubCGAdd];
[self pubNubChannelsForGroup];
[self pubNubCGRemoveAllChannels];
[self pubNubCGRemoveSomeChannels];
#pragma mark - State Admin
[self pubNubSetState];
[self pubNubGetState];
#pragma mark - 3rd Party Push Notifications Admin
[self pubNubAddPushNotifications];
[self pubNubRemovePushNotification];
[self pubNubRemoveAllPushNotifications];
[self pubNubGetAllPushNotifications];
#pragma mark - Public Encryption/Decryption Methods
[self pubNubAESDecrypt];
[self pubNubAESEncrypt];
#pragma mark - Message Size Check Methods
[self pubNubSizeOfMessage];
}
- (void)pubNubSizeOfMessage{
[self.client sizeOfMessage:@"Connected! I'm here!" toChannel:_channel1
withCompletion:^(NSInteger size) {
NSLog(@"^^^^ Message size: %@", @(size));
}];
}
- (void)pubNubAESDecrypt{
/*
[PNAES decrypt:<#(NSString *)object#> withKey:<#(NSString *)key#>];
[PNAES decrypt:<#(NSString *)object#> withKey:<#(NSString *)key#> andError:<#(NSError *__autoreleasing *)error#>];
*/
}
- (void)pubNubAESEncrypt{
/*
[PNAES encrypt:<#(NSData *)data#> withKey:<#(NSString *)key#>];
[PNAES encrypt:<#(NSData *)data#> withKey:<#(NSString *)key#> andError:<#(NSError *__autoreleasing *)error#>];
*/
}
- (void)pubNubAddPushNotifications {
/*
[self.client addPushNotificationsOnChannels:<#(NSArray *)channels#> withDevicePushToken:<#(NSData *)pushToken#> andCompletion:<#(PNPushNotificationsStateModificationCompletionBlock)block#>];
*/
}
- (void)pubNubRemovePushNotification {
/*
[self.client removePushNotificationsFromChannels:<#(NSArray *)channels#> withDevicePushToken:<#(NSData *)pushToken#> andCompletion:<#(PNPushNotificationsStateModificationCompletionBlock)block#>];
*/
}
- (void)pubNubRemoveAllPushNotifications {
/*
[self.client removeAllPushNotificationsFromDeviceWithPushToken:<#(NSData *)pushToken#> andCompletion:<#(PNPushNotificationsStateModificationCompletionBlock)block#>];
*/
}
- (void)pubNubGetAllPushNotifications {
/*
[self.client pushNotificationEnabledChannelsForDeviceWithPushToken:<#(NSData *)pushToken#> andCompletion:<#(PNPushNotificationsStateAuditCompletionBlock)block#>];
*/
}
- (void)pubNubSetState {
__weak __typeof(self) weakSelf = self;
[self.client setState:@{[self randomString] : @{[self randomString] : [self randomString]}} forUUID:_myConfig.uuid onChannel:_channel1 withCompletion:^(PNClientStateUpdateStatus *status) {
__strong __typeof(self) strongSelf = weakSelf;
[strongSelf handleStatus:status];
}];
}
- (void)pubNubGetState{
[self.client stateForUUID:_myConfig.uuid onChannel:_channel1
withCompletion:^(PNChannelClientStateResult *result, PNErrorStatus *status) {
if (status) {
[self handleStatus:status];
}
else if (result) {
NSLog(@"^^^^ Loaded state %@ for channel %@", result.data.state, self->_channel1);
}
}];
/*
[self.client stateForUUID:<#(NSString *)uuid#> onChannelGroup:<#(NSString *)group#> withCompletion:<#(PNChannelGroupStateCompletionBlock)block#>];
*/
}
- (void)pubNubUnsubFromChannelGroups {
[self.client unsubscribeFromChannelGroups:@[@"myChannelGroup"] withPresence:NO];
}
- (void)pubNubSubscribeToPresence {
[self.client subscribeToPresenceChannels:@[_channel1]];
}
- (void)pubNubUnsubFromPresence {
[self.client unsubscribeFromPresenceChannels:@[_channel1]];
}
- (void)pubNubSubscribeToChannels {
[self.client subscribeToChannels:@[_channel1] withPresence:YES clientState:@{_channel1:@{@"foo":@"bar"}}];
/*
[self.client subscribeToChannels:<#(NSArray *)channels#> withPresence:<#(BOOL)shouldObservePresence#>];
[self.client isSubscribedOn:<#(NSString *)name#>]
*/
}
- (void)pubNubUnsubscribeFromChannels {
[self.client unsubscribeFromChannels:@[_channel1] withPresence:YES];
}
- (void)pubNubSubscribeToChannelGroup {
[self.client subscribeToChannelGroups:@[_channelGroup1] withPresence:NO];
/*
[self.client subscribeToChannelGroups:@[_channelGroup1] withPresence:YES clientState:@{@"foo":@"bar"}];
*/
}
- (void)pubNubWhereNow {
[self.client whereNowUUID:@"123456" withCompletion:^(PNPresenceWhereNowResult *result,
PNErrorStatus *status) {
if (status) {
[self handleStatus:status];
}
else if (result) {
NSLog(@"^^^^ Loaded whereNow data: %@", result.data.channels);
}
}];
}
- (void)pubNubCGRemoveSomeChannels {
[self.client removeChannels:@[_channel2] fromGroup:_channelGroup1 withCompletion:^(PNAcknowledgmentStatus *status) {
if (!status.isError) {
NSLog(@"^^^^CG Remove Some Channels request succeeded at timetoken %@.", status);
} else {
NSLog(@"^^^^CG Remove Some Channels request did not succeed. All subscribe operations will autoretry when possible.");
[self handleStatus:status];
}
}];
}
- (void)pubNubCGRemoveAllChannels {
[self.client removeChannelsFromGroup:_channelGroup1
withCompletion:^(PNAcknowledgmentStatus *status) {
if (!status.isError) {
NSLog(@"^^^^CG Remove All Channels request succeeded");
} else {
NSLog(@"^^^^CG Remove All Channels request did not succeed. All subscribe operations will autoretry when possible.");
[self handleStatus:status];
}
}];
}
- (void)pubNubCGAdd {
__weak __typeof(self) weakSelf = self;
[self.client addChannels:@[_channel1, _channel2] toGroup:_channelGroup1
withCompletion:^(PNAcknowledgmentStatus *status) {
__strong __typeof(self) strongSelf = weakSelf;
if (!status.isError) {
NSLog(@"^^^^CGAdd request succeeded");
}
else {
NSLog(@"^^^^CGAdd Subscribe request did not succeed. All subscribe operations will autoretry when possible.");
[strongSelf handleStatus:status];
}
}];
}
- (void)pubNubChannelsForGroup {
[self.client channelsForGroup:_channelGroup1
withCompletion:^(PNChannelGroupChannelsResult *result, PNErrorStatus *status) {
if (status) {
[self handleStatus:status];
}
else if (result) {
NSLog(@"^^^^ Loaded all channels %@ for group %@",
result.data.channels, self->_channelGroup1);
}
}];
}
- (void)pubNubHereNowForChannel {
[self.client hereNowForChannel:_channel1 withCompletion:^(PNPresenceChannelHereNowResult *result,
PNErrorStatus *status) {
if (status) {
[self handleStatus:status];
}
else if (result) {
NSLog(@"^^^^ Loaded hereNowForChannel data: occupancy: %@, uuids: %@", result.data.occupancy, result.data.uuids);
}
}];
// If you want to control the 'verbosity' of the server response -- restrict to (values are additive):
// Occupancy : PNHereNowOccupancy
// Occupancy + UUID : PNHereNowUUID
// Occupancy + UUID + State : PNHereNowState
[self.client hereNowForChannel:_channel1 withVerbosity:PNHereNowState
completion:^(PNPresenceChannelHereNowResult *result, PNErrorStatus *status) {
if (status) {
[self handleStatus:status];
}
else if (result) {
NSLog(@"^^^^ Loaded hereNowForChannel data: occupancy: %@, uuids: %@", result.data.occupancy, result.data.uuids);
}
}];
}
- (void)pubNubGlobalHereNow {
[self.client hereNowWithCompletion:^(PNPresenceGlobalHereNowResult *result, PNErrorStatus *status) {
if (status) {
[self handleStatus:status];
}
else if (result) {
NSLog(@"^^^^ Loaded Global hereNow data: channels: %@, total channels: %@, total occupancy: %@", result.data.channels, result.data.totalChannels, result.data.totalOccupancy);
}
}];
// If you want to control the 'verbosity' of the server response -- restrict to (values are additive):
// Occupancy : PNHereNowOccupancy
// Occupancy + UUID : PNHereNowUUID
// Occupancy + UUID + State : PNHereNowState
[self.client hereNowWithVerbosity:PNHereNowOccupancy completion:^(PNPresenceGlobalHereNowResult *result,
PNErrorStatus *status) {
if (status) {
[self handleStatus:status];
}
else if (result) {
NSLog(@"^^^^ Loaded Global hereNow data: channels: %@, total channels: %@, total occupancy: %@", result.data.channels, result.data.totalChannels, result.data.totalOccupancy);
}
}];
}
- (void)pubNubHereNowForChannelGroups{
/*
[self.client hereNowForChannelGroup:<#(NSString *)group#> withCompletion:<#(PNChannelGroupHereNowCompletionBlock)block#>];
[self.client hereNowForChannelGroup:<#(NSString *)group#> withVerbosity:<#(PNHereNowVerbosityLevel)level#> completion:<#(PNChannelGroupHereNowCompletionBlock)block#>];
*/
}
- (void)pubNubHistory {
// History
[self.client historyForChannel:_channel1 withCompletion:^(PNHistoryResult *result,
PNErrorStatus *status) {
// For completion blocks that provide both result and status parameters, you will only ever
// have a non-nil status or result.
// If you have a result, the data you specifically requested (in this case, history response) is available in result.data
// If you have a status, error or non-error status information is available regarding the call.
if (status) {
// As a status, this contains error or non-error information about the history request, but not the actual history data I requested.
// Timeout Error, PAM Error, etc.
[self handleStatus:status];
}
else if (result) {
// As a result, this contains the messages, start, and end timetoken in the data attribute
NSLog(@"Loaded history data: %@ with start %@ and end %@", result.data.messages, result.data.start, result.data.end);
}
}];
/*
[self.client historyForChannel:<#(NSString *)channel#> start:<#(NSNumber *)startDate#> end:<#(NSNumber *)endDate#> includeTimeToken:<#(BOOL)shouldIncludeTimeToken#> withCompletion:<#(PNHistoryCompletionBlock)block#>];
[self.client historyForChannel:<#(NSString *)channel#> start:<#(NSNumber *)startDate#> end:<#(NSNumber *)endDate#> limit:<#(NSUInteger)limit#> includeTimeToken:<#(BOOL)shouldIncludeTimeToken#> withCompletion:<#(PNHistoryCompletionBlock)block#>];
[self.client historyForChannel:<#(NSString *)channel#> start:<#(NSNumber *)startDate#> end:<#(NSNumber *)endDate#> limit:<#(NSUInteger)limit#> reverse:<#(BOOL)shouldReverseOrder#> includeTimeToken:<#(BOOL)shouldIncludeTimeToken#> withCompletion:<#(PNHistoryCompletionBlock)block#>];
[self.client historyForChannel:<#(NSString *)channel#> start:<#(NSNumber *)startDate#> end:<#(NSNumber *)endDate#> limit:<#(NSUInteger)limit#> reverse:<#(BOOL)shouldReverseOrder#> withCompletion:<#(PNHistoryCompletionBlock)block#>];
[self.client historyForChannel:<#(NSString *)channel#> start:<#(NSNumber *)startDate#> end:<#(NSNumber *)endDate#> limit:<#(NSUInteger)limit#> withCompletion:<#(PNHistoryCompletionBlock)block#>];
[self.client historyForChannel:<#(NSString *)channel#> start:<#(NSNumber *)startDate#> end:<#(NSNumber *)endDate#> withCompletion:<#(PNHistoryCompletionBlock)block#>];
[self.client historyForChannel:<#(NSString *)channel#> withCompletion:<#(PNHistoryCompletionBlock)block#>];
*/
}
- (void)pubNubTime {
[self.client timeWithCompletion:^(PNTimeResult *result, PNErrorStatus *status) {
if (result.data) {
NSLog(@"Result from Time: %@", result.data.timetoken);
}
else if (status) {
[self handleStatus:status];
}
}];
}
- (void)pubNubPublish {
[self.client publish:@"Connected! I'm here!" toChannel:_channel1
withCompletion:^(PNPublishStatus *status) {
if (!status.isError) {
NSLog(@"Message sent at TT: %@", status.data.timetoken);
} else {
[self handleStatus:status];
}
}];
/*
[self.client publish:<#(id)message#> toChannel:<#(NSString *)channel#> compressed:<#(BOOL)compressed#> withCompletion:<#(PNPublishCompletionBlock)block#>];
[self.client publish:<#(id)message#> toChannel:<#(NSString *)channel#> withCompletion:<#(PNPublishCompletionBlock)block#>];
[self.client publish:<#(id)message#> toChannel:<#(NSString *)channel#> storeInHistory:<#(BOOL)shouldStore#> withCompletion:<#(PNPublishCompletionBlock)block#>];
[self.client publish:<#(id)message#> toChannel:<#(NSString *)channel#> storeInHistory:<#(BOOL)shouldStore#> compressed:<#(BOOL)compressed#> withCompletion:<#(PNPublishCompletionBlock)block#>];
[self.client publish:<#(id)message#> toChannel:<#(NSString *)channel#> mobilePushPayload:<#(NSDictionary *)payloads#> withCompletion:<#(PNPublishCompletionBlock)block#>];
[self.client publish:<#(id)message#> toChannel:<#(NSString *)channel#> mobilePushPayload:<#(NSDictionary *)payloads#> compressed:<#(BOOL)compressed#> withCompletion:<#(PNPublishCompletionBlock)block#>];
[self.client publish:<#(id)message#> toChannel:<#(NSString *)channel#> mobilePushPayload:<#(NSDictionary *)payloads#> storeInHistory:<#(BOOL)shouldStore#> withCompletion:<#(PNPublishCompletionBlock)block#>];
[self.client publish:<#(id)message#> toChannel:<#(NSString *)channel#> mobilePushPayload:<#(NSDictionary *)payloads#> storeInHistory:<#(BOOL)shouldStore#> compressed:<#(BOOL)compressed#> withCompletion:<#(PNPublishCompletionBlock)block#>];
*/
}
#pragma mark - Streaming Data didReceiveMessage Listener
- (void)client:(PubNub *)client didReceiveMessage:(PNMessageResult *)message {
if (![message.data.channel isEqualToString:message.data.subscription]) {
// Message has been received on channel group stored in message.data.subscription.
}
else {
// Message has been received on channel stored in message.data.channel.
}
if (message) {
NSLog(@"Received message from '%@': %@ on channel %@ at %@", message.data.publisher,
message.data.message, message.data.channel, message.data.timetoken);
}
}
#pragma mark - Streaming Data didReceivePresenceEvent Listener
- (void)client:(PubNub *)client didReceivePresenceEvent:(PNPresenceEventResult *)event {
if (![event.data.channel isEqualToString:event.data.subscription]) {
// Presence event has been received on channel group stored in event.data.subscription.
}
else {
// Presence event has been received on channel stored in event.data.channel.
}
if (![event.data.presenceEvent isEqualToString:@"state-change"]) {
NSLog(@"%@ \"%@'ed\"\nat: %@ on %@ (Occupancy: %@)", event.data.presence.uuid,
event.data.presenceEvent, event.data.presence.timetoken, event.data.channel,
event.data.presence.occupancy);
}
else {
NSLog(@"%@ changed state at: %@ on %@ to: %@", event.data.presence.uuid,
event.data.presence.timetoken, event.data.channel, event.data.presence.state);
}
}
#pragma mark - Streaming Data didReceiveStatus Listener
- (void)client:(PubNub *)client didReceiveStatus:(PNStatus *)status {
// This is where we'll find ongoing status events from our subscribe loop
// Results (messages) from our subscribe loop will be found in didReceiveMessage
// Results (presence events) from our subscribe loop will be found in didReceiveStatus
[self handleStatus:status];
}
#pragma mark - example status handling
- (void)handleStatus:(PNStatus *)status {
// Two types of status events are possible. Errors, and non-errors. Errors will prevent normal operation of your app.
//
// If this was a subscribe or presence PAM error, the system will continue to retry automatically.
// If this was any other operation, you will need to manually retry the operation.
//
// You can always verify if an operation will auto retry by checking status.willAutomaticallyRetry
// If the operation will not auto retry, you can manually retry by calling [status retry]
// Retry attempts can be cancelled via [status cancelAutomaticRetry]
if (status.isError) {
[self handleErrorStatus:(PNErrorStatus *)status];
} else {
[self handleNonErrorStatus:status];
}
}
- (void)handleErrorStatus:(PNErrorStatus *)status {
NSLog(@"^^^^ Debug: %@", status.debugDescription);
if (status.category == PNAccessDeniedCategory) {
NSLog(@"^^^^ handleErrorStatus: PAM Error: for resource Will Auto Retry?: %@", status.willAutomaticallyRetry ? @"YES" : @"NO");
[self handlePAMError:status];
}
else if (status.category == PNDecryptionErrorCategory) {
NSLog(@"Decryption error. Be sure the data is encrypted and/or encrypted with the correct cipher key.");
NSLog(@"You can find the raw data returned from the server in the status.data attribute: %@", status.associatedObject);
if (status.operation == PNSubscribeOperation) {
NSLog(@"Decryption failed for message from channel: %@\nmessage: %@",
((PNMessageData *)status.associatedObject).channel,
((PNMessageData *)status.associatedObject).message);
}
}
else if (status.category == PNMalformedFilterExpressionCategory) {
NSLog(@"Value which has been passed to -setFilterExpression: malformed.");
NSLog(@"Please verify specified value with declared filtering expression syntax.");
}
else if (status.category == PNMalformedResponseCategory) {
NSLog(@"We were expecting JSON from the server, but we got HTML, or otherwise not legal JSON.");
NSLog(@"This may happen when you connect to a public WiFi Hotspot that requires you to auth via your web browser first,");
NSLog(@"or if there is a proxy somewhere returning an HTML access denied error, or if there was an intermittent server issue.");
}
else if (status.category == PNRequestURITooLongCategory) {
if (status.operation == PNSubscribeOperation) {
NSLog(@"Too many channels has been passed to subscribe API.");
}
else {
NSLog(@"Depending from used API this error may mean what to big message has been publish for publish API,");
NSLog(@" or too many channels has been passed to stream controller at once.");
}
}
else if (status.category == PNTimeoutCategory) {
NSLog(@"For whatever reason, the request timed out. Temporary connectivity issues, etc.");
}
else if (status.category == PNNetworkIssuesCategory) {
NSLog(@"Request can't be processed because of network issues.");
}
else {
// Aside from checking for PAM, this is a generic catch-all if you just want to handle any error, regardless of reason
// status.debugDescription will shed light on exactly whats going on
NSLog(@"Request failed... if this is an issue that is consistently interrupting the performance of your app,");
NSLog(@"email the output of debugDescription to support along with all available log info: %@", [status debugDescription]);
}
if (status.operation == PNHeartbeatOperation) {
NSLog(@"Heartbeat operation failed.");
}
}
- (void)handlePAMError:(PNErrorStatus *)status {
// Access Denied via PAM. Access status.data to determine the resource in question that was denied.
// In addition, you can also change auth key dynamically if needed."
NSString *pamResourceName = status.errorData.channels ? status.errorData.channels.firstObject :
status.errorData.channelGroups.firstObject;
NSString *pamResourceType = status.errorData.channels ? @"channel" : @"channel-groups";
NSLog(@"PAM error on %@ %@", pamResourceType, pamResourceName);
// If its a PAM error on subscribe, lets grab the channel name in question, and unsubscribe from it, and re-subscribe to a channel that we're authed to
if (status.operation == PNSubscribeOperation) {
if ([pamResourceType isEqualToString:@"channel"]) {
NSLog(@"^^^^ Unsubscribing from %@", pamResourceName);
[self reconfigOnPAMError:status];
}
else {
[self.client unsubscribeFromChannelGroups:@[pamResourceName] withPresence:YES];
// the case where we're dealing with CGs instead of CHs... follows the same pattern as above
}
} else if (status.operation == PNPublishOperation) {
NSLog(@"^^^^ Error publishing with authKey: %@ to channel %@.", _authKey, pamResourceName);
NSLog(@"^^^^ Setting auth to an authKey that will allow for both sub and pub");
[self reconfigOnPAMError:status];
}
}
- (void)reconfigOnPAMError:(PNErrorStatus *)status {
// If this is a subscribe PAM error
if (status.operation == PNSubscribeOperation) {
PNSubscribeStatus *subscriberStatus = (PNSubscribeStatus *)status;
NSArray *currentChannels = subscriberStatus.subscribedChannels;
NSArray *currentChannelGroups = subscriberStatus.subscribedChannelGroups;
self.myConfig.authKey = @"myAuthKey";
[self.client copyWithConfiguration:self.myConfig completion:^(PubNub *client){
self.client = client;
[self.client subscribeToChannels:currentChannels withPresence:NO];
[self.client subscribeToChannelGroups:currentChannelGroups withPresence:NO];
}];
}
}
- (void)handleNonErrorStatus:(PNStatus *)status {
// This method demonstrates how to handle status events that are not errors -- that is,
// status events that can safely be ignored, but if you do choose to handle them, you
// can get increased functionality from the client
if (status.category == PNAcknowledgmentCategory) {
NSLog(@"^^^^ Non-error status: ACK");
// For methods like Publish, Channel Group Add|Remove|List, APNS Add|Remove|List
// when the method is executed, and completes, you can receive the 'ack' for it here.
// status.data will contain more server-provided information about the ack as well.
}
if (status.operation == PNSubscribeOperation) {
PNSubscribeStatus *subscribeStatus = (PNSubscribeStatus *)status;
// Specific to the subscribe loop operation, you can handle connection events
// These status checks are only available via the subscribe status completion block or
// on the long-running subscribe loop listener didReceiveStatus
// Connection events are never defined as errors via status.isError
if (status.category == PNUnexpectedDisconnectCategory) {
// PNUnexpectedDisconnect happens as part of our regular operation
// This event happens when radio / connectivity is lost
NSLog(@"^^^^ Non-error status: Unexpected Disconnect, Channel Info: %@",
subscribeStatus.subscribedChannels);
}
else if (status.category == PNConnectedCategory) {
// Connect event. You can do stuff like publish, and know you'll get it.
// Or just use the connected event to confirm you are subscribed for UI / internal notifications, etc
// NSLog(@"Subscribe Connected to %@", status.data[@"channels"]);
NSLog(@"^^^^ Non-error status: Connected, Channel Info: %@",
subscribeStatus.subscribedChannels);
[self pubNubPublish];
}
else if (status.category == PNReconnectedCategory) {
// PNUnexpectedDisconnect happens as part of our regular operation
// This event happens when radio / connectivity is lost
NSLog(@"^^^^ Non-error status: Reconnected, Channel Info: %@",
subscribeStatus.subscribedChannels);
}
else if (status.category == PNRequestMessageCountExceededCategory) {
/**
Looks like client received a lot of messages at once (larget than specified
'requestMessageCountThreshold') and potentially history request maybe required.
*/
NSLog(@"^^^^ Non-error status: Message Count Exceeded");
}
}
else if (status.operation == PNUnsubscribeOperation) {
if (status.category == PNDisconnectedCategory) {
// PNDisconnect happens as part of our regular operation
// No need to monitor for this unless requested by support
NSLog(@"^^^^ Non-error status: Expected Disconnect");
}
}
else if (status.operation == PNHeartbeatOperation) {
NSLog(@"Heartbeat operation successful.");
}
}
#pragma mark - Configuration
- (void)updateClientConfiguration {
// Set PubNub Configuration
self.myConfig.TLSEnabled = NO;
self.myConfig.uuid = [self randomString];
self.myConfig.origin = @"pubsub.pubnub.com";
self.myConfig.authKey = _authKey;
self.myConfig.stripMobilePayload = NO;
// Presence Settings
self.myConfig.presenceHeartbeatValue = 120;
self.myConfig.presenceHeartbeatInterval = 5;
// Cipher Key Settings
// self.myConfig.cipherKey = @"enigma";
// Time Token Handling Settings
self.myConfig.keepTimeTokenOnListChange = YES;
self.myConfig.catchUpOnSubscriptionRestore = YES;
// Messages threshold
self.myConfig.requestMessageCountThreshold = 100;
}
- (NSString *)randomString {
return [NSString stringWithFormat:@"%d", arc4random_uniform(74)];
}
- (void)printClientConfiguration {
// Get PubNub Options
NSLog(@"TLSEnabled: %@", (self.myConfig.isTLSEnabled ? @"YES" : @"NO"));
NSLog(@"Origin: %@", self.myConfig.origin);
NSLog(@"authKey: %@", self.myConfig.authKey);
NSLog(@"UUID: %@", self.myConfig.uuid);
// Time Token Handling Settings
NSLog(@"keepTimeTokenOnChannelChange: %@",
(self.myConfig.shouldKeepTimeTokenOnListChange ? @"YES" : @"NO"));
NSLog(@"catchUpOnSubscriptionRestore: %@",
(self.myConfig.shouldTryCatchUpOnSubscriptionRestore ? @"YES" : @"NO"));
// Get Presence Options
NSLog(@"Heartbeat value: %@", @(self.myConfig.presenceHeartbeatValue));
NSLog(@"Heartbeat interval: %@", @(self.myConfig.presenceHeartbeatInterval));
// Get CipherKey
NSLog(@"Cipher key: %@", self.myConfig.cipherKey);
}
#pragma mark -
@end