snapchatdotcom snapchatdotcom - 5 months ago 8
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.

Thanks!

#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]) {
return;
} else if ([self isAppearing]) {
[self setShouldDeferredDismiss:YES];
return;
}
[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);
}

@end

Answer

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 http://nshipster.com/associated-objects/

Comments