Author Archives: langui - Page 8

Xcode Warning: “UIKeyboardBoundsUserInfoKey” is deprecated

Simply use “UIKeyboardFrameBeginUserInfoKey” instead:

NSValue *aValue = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];

NSURL Tips

# Loading a string from a website
(this will block until it loads)

    NSURL *url;
    NSData *data;
    NSString *blork;
    url = [NSURL URLWithString: @"https://langui.net/"];
    data = [url resourceDataUsingCache: NO];
    blork = [[NSString alloc] initWithData: data  encoding: NSASCIIStringEncoding];

# Loading an image from a website
(this will block until it loads)

    NSURL *url;
    NSData *data;
    NSImage *blork;
    url = [NSURL URLWithString: @"https://langui.net/wp-content/uploads/2011/03/banner.png"];
    data = [url resourceDataUsingCache: NO];
    blork = [[NSImage alloc] initWithData: data];

# Open an URL in iphone safari

    NSURL *url = [NSURL URLWithString: @"https://langui.net/"];
    [[UIApplication sharedApplication] openURL: url];

More tips available here: http://borkware.com/quickies/

Converting NSString* to NSData* and Vice Versa

NSString* str = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSData* data = [str dataUsingEncoding: NSASCIIStringEncoding];

How-To: URL Encode NSString in Objective-C

NSString *url = [@"langui.net" stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding];

Converting unsigned char array to NSString *

The code snippet:

NSString* s = [[NSString alloc] initWithBytes:buffer length:sizeof(buffer) encoding:NSASCIIStringEncoding];

Calling C Functions from Objective-C

As Objective-C is a superset of C, it allows developers to mix pure c source files with Object-C source files.

But to call a C function from Objective-C, you should not import the .c file in your Objective-C .m file, that won’t work.

To call a C function from the Objective-C, you need to create a .h header file to include the C function declaration, and, of cause, include the .h file in the .c source file where the C function resides. Then import the .h file in the .m source file where you would like to call the C function, and simply call the function.