Adding support for iPhone 5 screens

Not long ago, i had to extend an app to support iPhone 5. Yes… iOS has autolayout functionality, that should aid the development of UIView’s.

But how about assets?. How do you load assets specially crafted for 568px height screens?. Furthermore.. how do you patch classes that inherit from UITableViewCell, to return dynamic height, according to the device?.

Simple!. By means of a super short UIScreen extension. The header should look like this…:

[cc lang=”objc”]
@interface UIScreen (Lantean)
+(BOOL)isH568;
@end
[/cc]

Write the following snippet in the .m file:

[cc lang=”objc”]
@implementation UIScreen (Lantean)
+(BOOL)isH568
{
static BOOL isH568 = NO;
static dispatch_once_t _once;

dispatch_once(&_once, ^
{
isH568 = ([UIScreen mainScreen].bounds.size.height == 568);
});
return isH568;
}
@end
[/cc]

Fair enough. By checking [UIScreen isH568], you should be able to proceed as required… if the current device has the new 4 inches screen!.

Calculating NSData’s MD5

Suppose that you need to calculate a NSData’s MD5 signature.. for whatever reason. Say… you need to check the backend’s signature.

Well… simply add a NSData extension, with the following method:

[cc lang=”objc”]
– (NSString*)md5
{
unsigned char result[16];
CC_MD5( self.bytes, self.length, result ); // This is the md5 call
return [NSString stringWithFormat:@”%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x”,
result[0], result[1], result[2], result[3],
result[4], result[5], result[6], result[7],
result[8], result[9], result[10], result[11],
result[12], result[13], result[14], result[15]
];
}
[/cc]

Again, please, don’t you forget the imports!

[cc lang=”objc”]
#import
[/cc]

Encrypting and Decrypting NSData with AES256

Today we’re gonna encrypt information, stored in a NSData object, using AES-256 algorithm. As you may already know, AES-256 is a symmetric encryption schema. Which means that you need to share, somehow, the key you’re gonna use (in order to decrypt the data).

I’ve encapsulated a couple helper methods, as a NSData extension, for the sake of simplicity. I’ve based myself on this. Let’s see…

[cc lang=”objc”]
– (NSData*)aes256EncryptWithKey:(NSString*)key
{
// ‘key’ should be 32 bytes for AES256, will be null-padded otherwise
char keyPtr[kCCKeySizeAES256 + 1];
bzero( keyPtr, sizeof( keyPtr ) );

// fetch key data
[key getCString:keyPtr maxLength:sizeof( keyPtr ) encoding:NSUTF8StringEncoding];

NSUInteger dataLength = [self length];

// See the doc: For block ciphers, the output size will always be less than or equal to the input size plus the size of one block.
// That’s why we need to add the size of one block here
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc( bufferSize );

size_t numBytesEncrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt( kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
keyPtr, kCCKeySizeAES256,
NULL /* initialization vector (optional) */,
[self bytes], dataLength, /* input */
buffer, bufferSize, /* output */
&numBytesEncrypted );
if( cryptStatus == kCCSuccess )
{
// The returned NSData takes ownership of the buffer and will free it on deallocation
return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
}

free( buffer );
return nil;
}
[/cc]

And now… the decrypt routine…

[cc lang=”objc”]
– (NSData*)aes256DecryptWithKey:(NSString*)key
{
// ‘key’ should be 32 bytes for AES256, will be null-padded otherwise
char keyPtr[kCCKeySizeAES256+1];
bzero( keyPtr, sizeof( keyPtr ) );

// fetch key data
[key getCString:keyPtr maxLength:sizeof( keyPtr ) encoding:NSUTF8StringEncoding];

NSUInteger dataLength = [self length];

// See the doc: For block ciphers, the output size will always be less than or equal to the input size plus the size of one block.
// That’s why we need to add the size of one block here
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc( bufferSize );

size_t numBytesDecrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt( kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
keyPtr, kCCKeySizeAES256,
NULL /* initialization vector (optional) */,
[self bytes], dataLength, /* input */
buffer, bufferSize, /* output */
&numBytesDecrypted );

if( cryptStatus == kCCSuccess )
{
// The returned NSData takes ownership of the buffer and will free it on deallocation
return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
}

free( buffer );
return nil;
}
[/cc]

Ahhhh… don’t you forget about the imports!

[cc lang=”objc”]
#import
#import
[/cc]

Encode your email in base64, and confuse spammers!

How many of you run a website? come on! raise your hands!. Most of you should know, at this point, that publishing your email (in raw) is a pretty bad idea. Spammers, scammers, and whatsoever will target you, almost for sure.

So what can you do?. Well.. you can try encoding your email in base64!. Which means that… the browser will understand your email, it’ll get printed… but any automated spider-bot will fail to search ‘@gmail’… at least without consuming quite a lot of CPU power.

So… if you’re still interested, try this link. I’ve found it really helpful!.

Fixing ñ characters in code-igniter

It seems this is one of the oldest issues of PHP develoment, right?. I’ve developed a PHP website, based on codeigniter framework (and a mysql database), and i ran into a lot of problems with the ñ, not showing up well in google.

Here’s what i’ve done to fix this problem:

  1. Set the database collation / columns to utf8_general_ci (by means of phpmyadmin).
  2. In codeigniter’s ‘config.php’ file.
    [cc lang=”php”]$config[‘charset’] = ‘UTF-8’;[/cc]
  3. In codeigniter’s ‘database.php’:
    [cc lang=”php”]$db[‘default’][‘char_set’] = ‘utf8’;
    $db[‘default’][‘dbcollat’] = ‘utf8_general_ci’;[/cc]
  4. In the ‘general’ html-rendering, before anything else gets echo’ed (beware, i’m using HTML5.. HTML4 is different):
    [cc lang=”html”][/cc]
  5. And finally… in my .htaccess file:
    [cc lang=”bash”]AddDefaultCharset UTF-8[/cc]

I hope this helps. I’ve spent many hours reading… testing… and a couple days waiting for Google’s update!.

%d