NSData-AES Class Encryption/Decryption in Cocoa

Why not use the built-in encryption algorithms? Here’s an NSData+AES i wrote which uses CCCrypt with a 256it key for AES256 encryption. You can use it like: NSData *data = [[NSData dataWithContentsOfFile:@”/etc/passwd”] encryptWithString:@”mykey”]; and decrypt it with: NSData *file = [data decryptWithString:@”mykey”]; DISCLAIMER: There no guarantee my NSData+AES is bug-free 🙂 It’s fairly new. I … Read more

How do I draw an NSString at an angle?

Here’s an example that uses a transform to rotate the drawing context. Essentially it’s just like setting a color or shadow, just make sure to use -concat instead of -set. CGFloat rotateDeg = 4.0f; NSAffineTransform *rotate = [[NSAffineTransform alloc] init]; [rotate rotateByDegrees:rotateDeg]; [rotate concat]; // Lock focus if needed and draw strings, images here. [rotate … Read more

NSDate between two given NSDates

This isn’t perfect, but you could use [NSDate compare:] to check your date against both boundaries: NSDate *firstDate = … NSDate *secondDate = … NSDate *myDate = [NSDate date]; switch ([myDate compare:firstDate]) { case NSOrderedAscending: NSLog(@”myDate is older”); // do something break; case NSOrderedSame: NSLog(@”myDate is the same as firstDate”); // do something break; case … Read more

Placing an NSTimer in a separate thread

If the only reason you are spawning a new thread is to allow your timer to run while the user is interacting with the UI you can just add it in different runloop modes: NSTimer *uiTimer = [NSTimer timerWithTimeInterval:(1.0 / 5.0) target:self selector:@selector(uiTimerFired:) userInfo:nil repeats:YES]; [[NSRunLoop mainRunLoop] addTimer:uiTimer forMode:NSRunLoopCommonModes]; As an addendum to this answer … Read more

How to pause an NSThread until notified?

Look into NSCondition and the Conditions section in the Threading guide. The code will look something like: NSCondition* condition; // initialize and release this is your app requires. //Worker thread: while([NSThread currentThread] isCancelled] == NO) { [condition lock]; while(partySuppliesAvailable == NO) { [condition wait]; } // party! partySuppliesAvailable = NO; [condition unlock]; } //Main thread: … Read more