snapchatdotcom snapchatdotcom - 1 year ago 61
iOS Question

What is the use of SINUIViewController in the demo for Sinch?

I'm new to Sinch. I just downloaded the demo file for calling and I'm just curious as to what this file does. Specifically the last few lines below the file. It seems to me that this view controller is involved with showing the appropriate view controllers when the call ends and starts, but I'm not sure.


#import "SINUIViewController.h"

#import <objc/runtime.h>

// used for associated object references to simulate property-like storage for
// this category
static char sin_deferredDismissalKey;

@implementation SINUIViewController

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
if ([self.view window] == nil) {
_isAppearing = NO;
_isDisappearing = NO;

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

- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
_isAppearing = NO;
[self dismissIfNecessary];

- (void)viewWillDisappear:(BOOL)animated {
_isDisappearing = YES;
[super viewWillDisappear:animated];

- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
_isAppearing = NO;

#pragma mark - Dismissal

- (void)dismiss {
if ([self isDisappearing]) {
} else if ([self isAppearing]) {
[self setShouldDeferredDismiss:YES];
[self dismissViewControllerAnimated:YES completion:nil];

- (void)dismissIfNecessary {
if ([self shouldDeferrDismiss]) {
[self setShouldDeferredDismiss:NO];
dispatch_async(dispatch_get_main_queue(), ^{
[self dismiss];

- (BOOL)shouldDeferrDismiss {
return [self sin_getAssociatedBOOLForKey:&sin_deferredDismissalKey];

- (void)setShouldDeferredDismiss:(BOOL)v {
[self sin_setAssociatedBOOL:v forKey:&sin_deferredDismissalKey];

#pragma mark -

- (BOOL)sin_getAssociatedBOOLForKey:(const void *)key {
NSNumber *v = (NSNumber *)objc_getAssociatedObject(self, key);
return v ? [v boolValue] : NO;

- (void)sin_setAssociatedBOOL:(BOOL)v forKey:(const void *)key {
objc_setAssociatedObject(self, key, [NSNumber numberWithBool:v], OBJC_ASSOCIATION_COPY);


Answer Source

This class is just a base-class helper viewcontroller, help you dismiss viewcontroller more eaiser. Some viewcontroller will be inherrited from this class.

- (BOOL)sin_getAssociatedBOOLForKey:(const void *)key
- (void)sin_setAssociatedBOOL:(BOOL)v forKey:(const void *)key

These above methods use a technique called Associated Object, to avoid declaring a property. You can read more about Associated Object here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download