Set up notifications in the iOS SDK

In order to keep you informed about what is happening in the HelpCrunch SDK, we implemented several notifications.
Written by Andrew
Updated 6 months ago

You can see these notifications in the NSNotificationCenter. Just add your observer in viewWillAppear and don’t forget to remove it in viewWillDisappear:

ObjC:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(NAME_OF_YOUR_METHOD)
                                                 name:NAME_OF_HELPCRUNCH_SDK_EVENT
                                               object:nil];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:NAME_OF_HELPCRUNCH_SDK_EVENT
                                                  object:nil];
}

Swift:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
        
    NotificationCenter.default.addObserver(self,
                                           selector: #selector(NAME_OF_YOUR_METHOD),
                                           name: NAME_OF_HELPCRUNCH_SDK_EVENT,
                                           object: nil)
}
    
override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
        
     NotificationCenter.default.removeObserver(self,
                                               name: NAME_OF_HELPCRUNCH_SDK_EVENT,
                                               object: nil)

Most of the events will send their data such as URL or unread messages' counter in userInfo[@”data”] like this:

ObjC:

- (void)NAME_OF_YOUR_METHOD:(NSNotification *)notification {
    NSLog(@"%@", notification.userInfo[@"data"]);
}

Swift:

func NAME_OF_YOUR_METHOD(notification: NSNotification) {
    print(notification.userInfo["data"])
}

List of the events:

  • HCSURLNotification, HCSImageURLNotification, HCSFileURLNotification – when a user clicks an url, an image or a file correspondingly. URL will be in userInfo[“data”].
  • HCSUserStartedChatNotification – when a user sends the first message via chat. No userInfo attached to the event.
  • HCSUserClosedChatNotification – when a user closes a chat or a welcome screen.
  • HCSUnreadChatsNotification – sends number of unread chats' changes each time. Expect NSNumber in userInfo[“data”]. If you need current number, you can use [HelpCrunch numberOfUnreadChats] method.

Did this answer your question?