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

NSData to [Uint8] in Swift

You can avoid first initialising the array to placeholder values, if you go through pointers in a slightly convoluted manner, or via the new Array constructor introduced in Swift 3: Swift 3 let data = “foo”.data(using: .utf8)! // new constructor: let array = [UInt8](data) // …or old style through pointers: let array = data.withUnsafeBytes { … Read more

How to share NSData or phasset video using Facebook iOS SDK 4.0 FBSDKShareDialog

With the new Facebook SDK 4.0, videos must be passed as assets URL. You have to copy your local video path to Assets Library and use that generated URL to share on Facebook. Step 1: NSURL *videoURL=[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@”IMG_1007″ ofType:@”mp4″]]; [self saveToCameraRoll:videoURL]; Step 2: – (void)saveToCameraRoll:(NSURL *)srcURL { NSLog(@”srcURL: %@”, srcURL); ALAssetsLibrary *library = … Read more

Convert hex data string to NSData in Objective C (cocoa)

Code for hex in NSStrings like “00 05 22 1C EA 01 00 FF”. ‘command’ is the hex NSString. command = [command stringByReplacingOccurrencesOfString:@” ” withString:@””]; NSMutableData *commandToSend= [[NSMutableData alloc] init]; unsigned char whole_byte; char byte_chars[3] = {‘\0′,’\0′,’\0’}; for (int i = 0; i < ([command length] / 2); i++) { byte_chars[0] = [command characterAtIndex:i*2]; byte_chars[1] … Read more

Correct way to load image into UIWebView from NSData object

I tested the code with PNG (“image/png”), JPG (“image/jpeg”) and GIF (“image/gif”), and it works as expected: [webView loadData:imageData MIMEType:imageMIMEType textEncodingName:nil baseURL:nil]; Now, what’s wrong with your app? the imageData is not a well-formed image data. Try opening the file with a web browser or an image editor to check it. the MIME type is … Read more

Determine MIME Type of NSData Loaded From a File

Perhaps you could download the file and use this to get the file’s MIME type. + (NSString*) mimeTypeForFileAtPath: (NSString *) path { if (![[NSFileManager defaultManager] fileExistsAtPath:path]) { return nil; } // Borrowed from https://stackoverflow.com/questions/5996797/determine-mime-type-of-nsdata-loaded-from-a-file // itself, derived from https://stackoverflow.com/questions/2439020/wheres-the-iphone-mime-type-database CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (CFStringRef)[path pathExtension], NULL); CFStringRef mimeType = UTTypeCopyPreferredTagWithClass (UTI, kUTTagClassMIMEType); CFRelease(UTI); if (!mimeType) … Read more

Converting JSON to NSData, and NSData to JSON in Swift

Here is code to convert between JSON and NSData in swift 2.0 (adapted from Shuo’s answer) // Convert from NSData to json object func nsdataToJSON(data: NSData) -> AnyObject? { do { return try NSJSONSerialization.JSONObjectWithData(data, options: .MutableContainers) } catch let myJSONError { print(myJSONError) } return nil } // Convert from JSON to nsdata func jsonToNSData(json: AnyObject) … Read more

is it possible to save NSMutableArray or NSDictionary data as file in iOS?

To write in file NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *filePath = [documentsDirectory stringByAppendingPathComponent:FILE_NAME]; [myArray writeToFile:filePath atomically:YES]; To read from file NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *filePath = [documentsDirectory stringByAppendingPathComponent:FILE_NAME]; myArray = [NSArray arrayWithContentsOfFile:filePath]; myArray will be nil if file is not … Read more

Split NSData objects into other NSData objects of a given size

The following piece of code does the fragmentation without copying the data: NSData* myBlob; NSUInteger length = [myBlob length]; NSUInteger chunkSize = 100 * 1024; NSUInteger offset = 0; do { NSUInteger thisChunkSize = length – offset > chunkSize ? chunkSize : length – offset; NSData* chunk = [NSData dataWithBytesNoCopy:(char *)[myBlob bytes] + offset length:thisChunkSize … Read more