Skip to content

Instantly share code, notes, and snippets.

View nishabe's full-sized avatar

nish abe nishabe

View GitHub Profile
@nishabe
nishabe / OBJC: GCD: Creating a serial queue
Last active August 12, 2018 18:21
OBJC: GCD: Creating a serial queue
dispatch_queue_t serialQueue = dispatch_queue_create(""imagesQueue"", DISPATCH_QUEUE_SERIAL);
dispatch_async(serialQueue, ^{
// Perform long running process
dispatch_async(dispatch_get_main_queue(), ^{
// Update the UI
});
});"
@nishabe
nishabe / OBJC: GCD: Creating a concurrent queue
Last active August 12, 2018 18:22
OBJC: GCD: Creating a concurrent queue
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
// Perform long running process
dispatch_async(dispatch_get_main_queue(), ^{
// Update the UI
});
});"
@nishabe
nishabe / OBJC: A Typical Init.txt
Last active August 12, 2018 18:22
OBJC: A Typical init
- (instancetype)init {
self = [super init];
if (self) {
// perform initialization
}
return self;
}
@nishabe
nishabe / OBJC: Date Formatter
Last active August 12, 2018 18:22
OBJC: Date Formatter Sample
NSDate to NSString:
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSDate* myDate = [NSDate date];
NSString *stringFromDate = [dateFormatter stringFromDate:myDate];
NSString to NSDate:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
@nishabe
nishabe / OBJC: GCD Web Server Set up to access a log file
Last active August 12, 2018 18:22
OBJC: GCD Web Server Set up to access a log file
#import "GCDWebServer.h"
#import "GCDWebServerDataResponse.h"
..........
@interface AppDelegate (){
GCDWebServer* _webServer;
}
- (void)startWebServer{
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
@nishabe
nishabe / OBJC: Get a file from documents directory
Last active August 12, 2018 18:23
OBJC: Get path of a file which is in documents directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.log",stringFromDate]];
@nishabe
nishabe / OBJC: PRETTY FUNCTION
Last active August 12, 2018 18:23
OBJC: PRETTY FUNCTION
NSLog(@"%s",__PRETTY_FUNCTION__);
@nishabe
nishabe / OBJC: Read text from File
Last active August 12, 2018 18:23
OBJC: Read text from File
- (NSString*)readString{
NSURL *filePath = [[NSBundle mainBundle] URLForResource:@"info" withExtension:@"txt"];
NSString *path = [filePath path];
NSString *content = @"";
if([[NSFileManager defaultManager] fileExistsAtPath:path]) {
// Do something here
content = [NSString stringWithContentsOfFile:path
encoding:NSUTF8StringEncoding
error:NULL];
}
@nishabe
nishabe / OBJC: HMAC creation - Pass Key and Data as String.txt
Last active August 12, 2018 18:23
OBJC: HMAC creation - Pass Key and Data as String
-(NSString*)generateHMACwith :(NSString* )key :(NSString*)data{
const char *cKey = [key cStringUsingEncoding:NSUTF8StringEncoding];
const char *cData = [data cStringUsingEncoding:NSUTF8StringEncoding];
unsigned char cHMAC[CC_SHA256_DIGEST_LENGTH];
CCHmac(kCCHmacAlgSHA256,
cKey,
strlen(cKey),
cData,
strlen(cData),
cHMAC);
@nishabe
nishabe / OBJC: HMAC creation - Pass Key and Data as NSData.txt
Last active August 12, 2018 18:23
OBJC: HMAC creation - Pass Key and Data as NSData
- (NSData *)generateHMACwith:(NSData *)key key:(NSData *)data {
NSMutableData *macOut = [NSMutableData dataWithLength:CC_SHA256_DIGEST_LENGTH];
CCHmac( kCCHmacAlgSHA256,
key.bytes,
key.length,
data.bytes,
data.length,
macOut.mutableBytes);
return macOut;
}