iOS 8 Swift Programming Cookbook

Hi lovely readers 🙂

My iOS 8 book is out and it is ALLLLL new, rewritten to use Swift with tons of new stuff in it.

Check it out, it is 50% off. Click here to go to O’Reilly’s website to purchase the book.

Also a surprise, I have also done a video course to teach you all about iOS 8 programming with Swift. THAT too is 50% off for a limited time. You can click here to get the videos.

Happy coding everyone

Compiling C files for Mach-O ARM architecture, in OS X Lion

Yesterday I wanted to compile a .c code for ARM architecture using the iOS SDK on my Lion machine. I wanted to create object files and executable files that I can install on my jailbroken iPhone 4. I have explained the process here for you. I hope it will be of help.

We are going to be using the LLVM-GCC compiler. In OS X Lion, this compiler sits here:

/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/llvm-gcc-4.2

Also we need to pass the path to our iOS SDK, to the compiler so that it knows where to get the libraries and frameworks from. In OS X Lion with iOS SDK 5.1 installed, the path of the iOS SDK would be here:

/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.1.sdk

So if your SDK version is different, you need to modify that path. Now let’s write a simple Hello World C code in a file named hello.c

#include <stdio.h>

int main(void){
printf(“Hello, World!\n”);
}

Now let’s go to Terminal and change your current working directory to the directory that contains the above hello.c file. Now let’s tell Terminal where our compiler and SDK folders are:

COMPILER=”/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/llvm-gcc-4.2″
SDKDIR=”/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.1.sdk”

Now we are going to attempt to compile the hello.c source file for armv7 CPU architecture:

$COMPILER hello.c -o hello.o -c -arch armv7 -isysroot $SDKDIR

Now if you get an ls -la on your folder, you will also see an object file called hello.o. If you do the following:

file hello.o

You should see an output like this:

hello.o: Mach-O object arm

So now we have the object file. If you want to link this object file into an executable file, then you can do this:

LINKER=$COMPILER
$LINKER hello.o -o hello -arch armv7 -isysroot $SDKDIR

Note: the linker and compiler are both llvm-gcc-4.2 but we pass the -c to it to compile and if we skip -c, we will link, producing the final executable file.

Now you have 3 files

  • hello.c
  • hello.o
  • hello
The first one is your source file, the second one is the compiled file and the third one is the linked (executable) file. So now do this:
file hello
You should be able to see the following output:
hello: Mach-O executable arm

That’s it. If you have any questions, let me know in the comment section below.

Loading a Nib file, Programmatically (Objective-C)

If you want to load a Nib file at run time by simply allocating and initializing a View object, then you should take a rather strange approach to how you create your class files. Suppose you have a subclass of UIView called MyView and what you want is to allocate and initialize an instance of MyView but have MyView load its outlets and actions from a Nib file. Well, you will need to do two things:

  1. In Interface Builder, change the class name of your View object to MyView.
  2. Then you will have to override MyView’s initWithFrame method like so:

- (id)initWithFrame:(CGRect)paramFrame
{

NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"MyView"
owner:nil
options:nil];

if ([arrayOfViews count] < 1){
[self release];
return nil;
}

MyView *newView = [[arrayOfViews objectAtIndex:0] retain];
[newView setFrame:paramFrame];

[self release];
self = newView;

return self;

 

}

Then you can go ahead and initialize your view like this:

MyView *myView = [[MyView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:myView];
[myView release];

And here an instance of MyView will get added to the view of a view controller. That simple. I hope it helps some of you out there 🙂

Logging in iOS SDK – Ultimate NSLog – Explained Through Video

NSLog in iOS SDK is great. It really is nice but it lacks some fundamentals such as printing out line numbers, class names and etc. In this video you will learn how you can redirect the implementation of NSLog to your own implementation and extend the functionality of NSLog by adding line numbers, class names and etc to its output.

This is the fourth and the final video in the series of videos that I prepared, teaching iOS developers how to take advantage of internals of the SDK in order to make life easier for themselves and those working on the same product as them.

Loggin in iOS SDK – More Tips and Tricks and Magic Macros

This is the ultimate debug-killer! This is the third video in the series of videos that I’ve prepared, teaching iOS developers how to take advantage of some of the most awesome features and macros in the iOS SDK in order to completely replace having to debug their code, with the smooth and easy ride of logging.

In this video, you will learn how to print out logs to the console with the exact line number from where the logs are getting printed. You will also learn how to put all the knowledge you’ve learnt from this video and the previous 2 videos in the series, into 3 handy and magical macros that you can use in your iOS projects. Enjoy!

Logging in iOS SDK – Tips and Tricks – Explained Through Video

This is the second video in the series of videos that I’m preparing, demonstrating to iOS developers how they can take advantage of logging facilities provided in the iOS SDK to be able to find defects and issues in their apps just by looking at the logs, without the need to use a debugger at all.

In this video, I will teach you two very handy tricks that you can use with NSLog method in Objective-C…

Logging in iOS SDK – Learning the Basics – Explained Through Video

Ever wanted to learn to save hours of your and your teammates’ time by creating software that doesn’t crash, has logs and lets you find its defects easily? Ever got sick and tired of using a debugger in order to find defects in your code? Well, I’ve decided to create a series of videos explaining how you can avoid having to debug your application simply by…logging information in your app! This video is the first video in the series. In this video, you will learn how to use NSLog in iOS SDK in order to log information to the console.

Here is the video, enjoy!

My Article on O’Reilly’s Main Page!

I made a video here to teach iOS developers how to write multitasking aware apps. I was on O’Reilly’s website and all of a sudden realized that my article has appeared on top of the main page… Isn’t that awesome? I took a screen shot.

My Article + Video appearing on O'Reilly's main page on top

Writing Multitasking Aware iOS Apps – Explained Through Video

You’ve known for a long time that iOS supports multitasking but you’ve never had a chance to really learn how to implement it in your app. Well, in this video, I will teach you how to do that. It’s simple and straight to the point. Make sure you leave a comment and if you have any questions, let me know. I’ll do my best answering them 🙂