...to Becoming a Mobile Developer!

Month: December 2010

Memory, Constructors, Destructors, new, alloc and init

Quite the catchy title, eh??

I have been looking into some memory management techniques in Objective-C. I’m used to the way C/C++ and many other languages do it, by using the “new” keyword.
In Objective-C, this is done differently.

While you CAN declare a class variable like this:

myClass *object = new myClass;

it is not the most common way. Instead, its more commonly seen like this:

myClass *object = [[myClass alloc] init];

The “new” keyword allocates a space in memory to hold the object. It then initialises it, probably with a constructor and can give variables a default value. So thats 2 things it does. Allocates and initialises. In Objective-C, these are done independantly.

Also, note the nesting of the square brackets. Its making 2 calls, but just doing it in a more readable way.

I mentioned constructors above, and I’ll show you now how to pass a variable to the “constructor”.  Note, I don’t think they’re actually called constructors and destructors outside C++, but just go with it anyway…. 😉

So, in C++, you would create an object and initilise some string/variable like so:

myClass *myObj = new myClass(“Hello World”);

In Objective-C:

myClass *myObj = [[myClass alloc] initWithString:@”Hello World”];

See how we are initialising with a string? This can also be done with contents of a file or URL, as well as many other options (In XCode, press ESC to see a list when it prompts for auto-completion). For example,

myClass *object = [[myClass alloc] initWithFormat:@”Hello, my name is %@ and I am %d years old”,name, age];

Obviously you also need the back-end to handle these initialisations.

Autorelease Pools

Lets say you create an object that contains a method which in turn creates a new object called myAddress, assigns it a value, then return this value, eg “return myAddress”. You will obviously have to release this object to obey proper memory management rules. Since you need to do your tidy up before calling the “return”, how can you destroy the object but still use it on the next code line without getting a compile error? In Objective-C, we use [myObject release] instead of “delete myObject” like in C++. For example, in C++ pseudocode:

main
{
myClass *obj1;
myClass *obj2 = obj1.returnAddress();
}

myClass obj1::returnAddress
{
myClass *myAddress = new myClass("123 City Road");
delete myAddress;
return myAddress;   // Will cause error
}

So, instead of delete myAddress, Objective-C would read [myAddress release]. Now, stay with me here for a minute while I explain the answer to this problem! Objective-C has the “Pool”, which is like a queue of objects that need to be deleted. When you release an object, it gets deleted straight awat. Now, if you use the “autorelease” keyword in place of “release“, you’ll see some magic happening. You see, autorelease puts the object into the pool for deletion but indicates that its still needed for the moment. The Pool will simply wait a few milliseconds and then just go ahead and delete it. The thing here that I am uncertain about is if it just takes a guess and deletes it whenever, or if it watches the object to wait until it goes out of scope. I’d imagine the latter is how it happens, but I’ll need to look into it. Here’s the corrected code in Objective-C(note the return type in the brackets):

-(myClass) returnAddress
{
myClass *myAddress = [[myClass alloc] initWithString:@"123 City Road"];
//[myAddress release];    // Will cause the return statement to crash
[myAddress autorelease];  // Works fine
return myAddress;
}

Destructors

We saw above how to initialise data types in constructors, using the initi keyword. Now I want to quickly show you destructors.

The default destructor is always called “dealloc” and is also always predefined, in case you don’t supply your own. Since its already defined in the header file “Foundation.h”, in Objective-C we need to tell the compiler which one we want to use, so it doesnt get confused. This is done with the “super” keyword. See below:

-(void) dealloc {

// Clean up

[super dealloc];

}

And there you go!

My second attempt at a hackintosh: HP NC4200 laptop

With the knowledge I gained from putting Mac OSX Snow Leopard on my MSI K9AGM3/AMD Athlon64 X2, I decided to put it on my laptop as well, so I can bring it into college with me and work from there.

The specs of my laptop are as follows:

  • HP NC4200
  • Intel Centrino 1.8Ghz
  • Intel 900 series motherboard (I think)
  • 1GB RAM
  • Intel 2200bg wireless (iwi2200)
  • Intel VGA 915GM GMA 900 (could also be GMA950, not sure)
  • AC97 audio
  • Broadcom BCM5751M Eternet

I had originally tried to copy exactly the method for my first Hackintosh, but this gave me problems. On every boot, I got “Waiting for DSMOS“. I looked up some possible fixes, including replacing FakeSMC.kext and disabler.kext but nothing I seemed to do would work(EDIT: I found a fix for this. Don’t use mach_kernel!!)

I then looked into getting a different distribution of Mac Snow Leopard. The first one I came across was called iPortable OSX86. This is a USB loading version promising to work out of the box on any Intel machine, no questions asked! Quite the promise, and I was a bit skeptical, but I gave it a go anyway. I used Mac Leopard from VMWare to prepare the USB stick, and to my surpise…Kernel panic on first boot! This distro is actually designed to boot off a USB stick, not to install from USB. On first boot, it takes you through the process of setting up a new user account, as a normal Mac would, after a fresh install, and then it loads up the desktop directly off the USB stick!!! So you can go to any Intel PC, stick in the USB drive and you’ll have your personal Snow Leopard desktop in a minute or so!

So, intially I was getting a kernel panic on boot relating to ElliottForceLegacyRTC.kext. Simply removing this solved the kernel panic and then I could load up Snow Leopard without any problems!! Success! Almost. There was a few problems, mainly that there was no system clock, or Real-time Clock (RTC – Remember the kext I removed?). Also, when I tried to install anything, it would take maybe 5 attempts for the install to go through. It would either hang on “examining additional disks” or hang on “Preparing for installation“. Another problem was that it would not unmount drives a lot of the time, which is possibly related to the install issues.

Anyway, what I decided to do was, from the USB sticks desktop, prepare my laptop hard drive as if it was a USB stick that I was preparing for iPortable OSX86. So in other words, I was installing a portable version on a non-portable hard drive….makes sense?? 🙂

So I formatted the hard drive, making sure to check the MBR option. Used CopyCatX (included with iPortable OSX86) to apply the included image to the hard-drive and also used iPortable BootFix to make the drive bootable. This gave me the same ElliotForceLEgacyRTC.kext kernel panic so I just booted up into Windows 7 (Oh ye, did I neglect to mention I was dual-booting?) and removed the kext from System/Library/Extensions. I should point out, you will need MacDrive8 to access Mac OSX drives from Windows.

So, now I had a laptop dual-booting with Windows7 and Mac Snow Leopard. The only problem was I still had the above mentioned issues on the Mac. Not to mention no sound, video acceleration and no trackpad clicking (you know you tap the trackpad and it counts as a left-click?). I solved the audio problems with the AppleAC97Audio.kext, which can be found easily enough. I also enabled graphics acceleration (QE/CI) with these GMA900 kexts. The main problem I’m worried about is getting the RTC working. If I manually set the clock, it resets the CMOS so next time I boot into Windows, the clock is also reset. Its a bit of a pain, especially since if your clock is set before the date Snow Leopard was released, it warns you that Applications will behave erratically!

For fixing the RTC issues, apparently you need to patch your DSDT. This basically extracts stuff from your motherboard and changes it to suit the Mac. There is a DSDT patches that comes with iPortable, that I used to generate a dsdt.aml file, which is supposed to fix everything! I tried this but I couldnt get it working so I’m still stuck with no clock, and that issue with installing software. Also, I have a hunch that using this dsdt.aml file somehow helped me get QE/CI working, but I’m not sure.

For my wireless, apparently there’s no support for iwi2200, but luckily I had a USB wifi card with the RTL8187 chipset. This does come with manufacturer support so works perfect with the installer that you can get from their website. I never did manage to get my onboard (BCM5751M) ethernet card working.

So, as it stands, I have Snow Leopard booting fine with the chocolate_kernel (one of 5 included with iPortable OSX86), I have audio, wireless, QE\CI,no clock (RTC) and no ethernet. I also managed to install the latest iPhone SDK, after about 10 attempts and it runs and compiles seemingly fine. I was getting a problem when trying to load Interface Builder though. It just used to hang on load, with its icon jumping around in the Dock. This resolved itself after I installed the proper kexts for my graphics card, and enabled QE/CI. I have no idea if they are related or not, since I don’t have Quartz enabled on my other hackintosh and Interface Builder works fine on that!

I should also point out another thing. I kept getting a Kernel Panic every time I went into System Preferences. During one such Kernel panic, I managed to corrupt my boot sector and succumbed to the dreaded “Boot0: Done” error. For this, I had to eventually switch the active partition to the Windows “System Reserved” partition, and edit the Windows Boot menu using BCD-Edit. The bootloader that came with iPortable just died so was unusable. When you use BCDEdit in Windows, it uses Chameleon bootloader, which I was able to boot the mach_kernel with. However, since that causes a “Waiting for DSMOS” error, I had to boot from that USB stick, run OSX86Tools, click “Install Kernel”, select the “Chocolate_kernel” and install that. OSX86Tools just renames the kernel to mach_kernel, so now I have chocolate_kernel just renamed to mach_kernel and it works fine now. 😀

So now you’ve got a Mac, what are you gonna do with it?

Finally, we can now get onto the reason I created this blog! Learning Objective-C!

I’m doing some online tutorials to get the basics right. Since I’m coming from a C and C++  background, I was expecting a lot of similarities. However, there seems to be some fundamentals differences in syntax.

For example, lets take Hello World in a few languages:

printf(“Hello World”);  // C

cout << “Hello World”; // C++

NSLog(@”Hello World”); // Objective-C

First thing,  why the hell is there an @ symbol there??? Secondly,what the hell is NSLog???

Well, the @ symbol is there to tell Objective-C to expect a string. Thats just how it works.

Secondly, NSLog comes from back in the 80’s before Steve Jobs took over Apple. He was working on the NextStep language (NextStep => NS, see?) so when he did become boss in Apple, he rather biasedly (is that a word???) used the NextStep platform to build Mac OSX. So even though its evolved to use Objective-C, they still use the foundation classes from NextStep. FYI, don’t mind the capitalisation of NextStep. It can be written as any of NeXTSTEP, NeXTstep, NeXTStep, or NEXTSTEP.

So anyway, from my first lesson in Objective-C, I have spotted another oddity. This time to do with calling methods in classes. See below:

myClass.doFunction(); // C++

myClass.doFunction(myValue); // C++

[myClass doFunction]; // Objective-C

[myClass doFuction:myValue]; // Objective-C

Thats for calling methods with zero or one parameters. I’m currently trying to get my head around sending 2 parameters in Objective-C! I need to find some example code….

Success: My first Mac!

If you read my previous post about the Hackintosh, you will have noticed I was trying to work up a solution on my own AMD PC so that I could run  MacOS Snow Leopard. Well, I have finally managed to get it working, for the most part.

My Specs:

MSI K9AGM3 motherboard FD/F

AMD 690G chipset MS-7367

AMD Athlon 64 X2 Dual Core 4000+ 2.10Ghz

2GB RAM

VGA: Onboard ATI X1200

Sound1: SB600 chipset ALC888

Sound2: Creative Audigy 4

NIC: Realtek RT8111B

Now, nothing there is the same, or even similar, to any hardware shipped in a Mac, so I’ve had to trawl the internet for kexts (Mac Drivers) to support my hardware. Also, as its not an Intel, I cannot use the default “Vanilla” kernel and Snow Leopard DVD. Because of this, I have employed the use of my trusty Imation Nano Pro 16GB USB stick to do the job for me!

Here’s how I did it:

Step1: Prepare the USB Stick.

For this, you need either a working Mac, or a VMWare image of a Mac. So, boot up the Mac, or VMWare image, and open Disk Utility, which is located in System-> Applications Folder. Insert the USB stick and select it from the left column. Go to “Erase”. Under “Volume Format” you need to select “Mac OS Extended (Journaled)”, name it whatever you want and click “Erase”. This will make your USB stick compatible with the Mac, and allow reading and writing to it.

I’ll assume you have an .iso image of Snow Leopard handy, which you made yourself somehow, or downloaded. So while still working with the USB stick, select “Restore” from the top right of Disk Utility. You will now need to drag your iso into the text box for “source”, and drag the USB partition1 into “Destination”. Click restore and wait half an hour  or so for this to complete.

Actually, while its restoring, you can search for, and download a tool called OSX86Tools. Its needed for this next bit.

Step 2: Make the USB stick bootable.

When the Restore is complete, open up OSX86Tools and click on “Install EFI/Run FDISK”. A little black box will pop up. Select the USB stick from the top drop-down menu, leave the rest, and click “Install EFI”. This will make the USB stick bootable. You can now reboot.

Step 3: Preparing your PC

You need to decide if you want to Dual Boot with your existing operating system, ie Windows, or if you want to just have a dedicated hard drive for Mac. Either way, there’s gonna be some formatting involved! For a dedicated hard-drive, you can do the formatting later during install (you know formatting deletes everything on your hard drive, by the way, yeah?). For dual boot, go into Windows and split your disk in two. I’m not going to tell you how to do this, because I don’t want to take responsibility if you mess it up! 😀 But you can Right-Click “My Computer”, select “Manage” and then “Disk Management”. You can play around in here with resizing and creating new partitions, or else get a dedicated Partition Program (AVOID PARTITION MAGIC. SERIOUSLY!!). Initially you want to format the chosen partition to FAT32, and Mac isn’t the biggest fan of NTFS. You’ll be formatting it again to Apples format during install anyway, but the Mac installer can just handle FAT32 better. So just do it!

Step 4: Installing Mac OS Snow Leopard.

This is the tricky part. If your computer is not set up to boot from USB, go into the BIOS and set this up (Google if you don’t know how).When it starts to read off the USB stick, you will see a “Darwin/X86”  screen. From here it will hopefully boot. If not, you can press either Escape or F8 and it will take you to a menu where you can put in special boot parameters (eg arch=i386 -v) to make it boot. If its working though, don’t bother with that. You should now be on the main Install screen. From here just select your language, and keep clicking through until it prompts for a hard drive to install to (“Select Destination”). DO NOT SELECT YOUR WINDOWS HARD DRIVE if you are dual booting. If you have the dedicated hard drive then you won’t see anything there. Click on “Utilities” up the top of the screen and select “Disk Utility”. This is the same program you used earlier with the USB stick (unless you burned the .iso to a DVD of course). Select the partition that you intend to use with Mac OS and click on “Erase”. As before, select “Erase”. Under “Volume Format” you need to select “Mac OS Extended (Journaled)”, name it whatever you want and click “Erase”. You can now close “Disk Utility” either by clicking the Red X button up the top left, or selecting “Disk Utility” from the very top left of the screen and quitting in there. This will now take you back to the “Select Destination” screen, and hopefully your hard drive should appear  there now. Select it and click Next.

Step 5: Customising the install

This bit will make or break your install!!

Every install is different for each motherboard, processor, audio chips, video chips, network cards  etc etc etc. Assuming you have decided not to install the original Apple install DVD, and have instead opted for one of the customised installs such as Hazard, kalway, or iDeneb, then the following part will look different. You see, the releaser of each of those editions has customised the installer to include different kexts (drivers) and different patches. They shouldnt stray too far from each other, but still, they’ll look different.  I can tell you what I selected but unless you have identical hardware to me, you will need to select different options. Simple as that.

So, I selected the Mobdin Kernel for AMD, VoodooHDA audio, LegacyAppleIntelPIIXATA and all the other AMD patches. (The chameleon loader comes with the AMD patch. If you are Intel, don’t forget to select a bootloader!)

Depending on which patches you choose for your own machine, you will get errors and crashes. If you get any problems, you just need to go back and select different options. Most new machines should be almost compatible these days so most people will have no problems. And even people who do have problems, can get them fixed with a bit of tinkering. If you can get the install completed and are having boot problems, press F8 before the chameleon loader starts and type “-x -v -f”. The -x loads in safe mode, the -v loads in verbose mode, ie outputs lots of text so you can see where the problem occurs, and -f forces the Mac to load kexts directly from the hard drive, ignoring the cache. Don’t worry about that for now….;)

There is one more thing. On my PC, when I booted up for the first time, it got me to create a new user account, which is all normal. However, once I filled in the details, it either hung indefinately, or it brought me through an infinite loop of asking me to fill in my details over and over again. The fix for this, is to boot into single user mode, by typing “-s” at the boot prompt. It should load up in under a minute and you’ll see some stuff above it relating to “fsck -fy”. Type in the few lines that it provides and now you’re into single user mode. From here, you will have to manually create your user account. Google it! 😉

So, with a bit more tinkering, you may get yourself an acceptably working substitute for a Mac, that you can finally run XCode and the iPhone SDK on sufficiently. At present, my machine is working OK, but because Hackintosh’s are relient on non-Apple employees making kexts to work with non-standard hardware, you can either get lucky and have kexts available to you, or you’ll never have support for certain devices, eg graphics card/sound cards/network cards.

I could get my ATI Radeon X1200 onboard graphics working to basic levels, but it looks like it will never get support for QE/CI. I managed to get a kext for it called EVOenable_X1800.kext, which got System Profiler to pick it up, but thats about it. No resolution change, no Quartz etc etc…

I also had to search high and low for a kext for my network card so I could just go online with it! I found a generic Realtek one which seems to have worked (RealtekR1000SL.kext).

So, thats it for now. I hope you enjoyed literally my longest ever blog post! If you have any questions, feel free to leave a comment!

Cheers.

Enter: THE HACKINTOSH!!

As you may have realised by now, I don’t actually own a Mac, or even have easy access to one. Sure, we have the lab computers in college, but with my coursework and studying and stuff taking up most of my day, I resort to doing my coding at night! And Macs in a lab several miles away are not exactly ideal.

So….what am I gonna do about it???

Well, after a little bit more research, I have stumbled into the dark, murky underworld known as HackintOsh!! So what exactly is it…?

Well, as you may or may not be aware, Apple used to run their Macs on IBM PowerPC CPUs. The MacOS was then built around this type of chip, which is incompatible with your standard PC chips, like Intel Pentiums, and AMD Athlons etc etc. Recently (in 2006), Apple decided to move over to Intel chips in their new Macs. This opened up the world to Hackintosh. You see, by Apple moving over to Intel based processors, they now share a common architecture with your standard PC. So now, if someone changes a few things in the Mac operating system and tweaks it a little, in theory, it should work fine alongside a regular PC. Whereas previously, when Macs were IBM based, that would be impossible!

So in a nutshell, a hackintosh is basically a PC running MacOSX. This also means that anything you can run on a Mac, you can run on your own bog-standard PC….including the iPhone SDK.

Of course, the legality of this is in question. I’m not sure Apple appreciate losing potential customers, who don’t need to splash out on a Mac anymore, either! I managed to find a MacOSX Leopard DVD for roughly €30, so it’s not a bad start to development on a budget.

EDIT: I have run into some trouble with this. I forgot that my current PC actually has an AMD processor. According to a few sites online this is manageable, though, with some different drivers, or “kexts”.

Further research, and alternatives to writing apps on MAC OSX

After writing the previous post, I decided to do some research to see if there was other ways, on a student budget, to write iPhone apps without a Mac and Apple developer account.

Well, the good news is that its possible to write the apps without going over budget. The bad news is, theres no other way to submit them to the app store without purchasing the $99 subscription. Sure, you can give them to someone else to submit, but then they get the credit…and we don’t want that! 😉

With regards the actual coding, this bits a bit tricky.

Now, while it is definitely possible to actually write code, and compile it on Windows, it is not exactly ideal. The reasons behind this are as follows:

  1. Some IDEs actually facilitate HTML5/javascript coding. This is then “wrapped” in an app and the built in browser is used to render it. While it works, its still not native code so you may suffer from a performance hit. I did find an IDE that lets you write your own Objective-C, but you then have to upload your code to their own server, which is Mac based. It then compiles, converts to an app, and provides a download link to the compiled app. If you’re worried about people stealing your source code, you may want to avoid these!
  2. The compilers that natively compile Objective-C do not support the Foundation classes, which give the iPhone its look and feel. Remember, Objective-C was around long before the iPhone, and even Mac OSX. You can perhaps somehow copy over the foundation classes from XCode and compile a fully-fledged iPhone app, but then you get this problem….
  3. Testing. There are 2 ways to test an iPhone app. 1: Copy it via USB directly to the iPhone, or 2: Use the iPhone simulator/emulator. First of all, to transfer the app to the iPhone, you need the developer account, and you also need the iPhone SDK…on a Mac. Secondly, the simulator/emulator only works on the iPhone SDK…on a Mac.

If you are interested, I’ll list the software I came across and you can test them yourselves. Note, I havent tested any of these myself as I am only at the early stages myself!

  • AirPlay (probably the closest you’ll get, compiles to Obj-C)
  • Appcelerator Titanium (HTML/javascript)
  • Flash CS5 (HTML/javascript)
  • Unity 3D (for games, needs Mac to compile)
  • StoneStrip S3D (for games, needs Mac to compile)
  • Genuitec MobiOne (HTML/Javascript)
  • Dragonfire SDK (code in Obj-C, upload to THEIR server, which compiles your code.  Then you download the app)

Also, I should point out that you still need to have the Apple Developer Account for these pieces of software, like Adobe CS5, since you will need to transfer the app to the iPhone, and this is only done by provisioning it with your Developer Code!!

So there you have it. Not the greatest of options on Windows, for a change. Although I hear Mac Minis are going for about €800/$600 these days…;)

So, you wanna be an iPhone developer

OK, lets start this off. If you want to make an iPhone app, here’s what you’re gonna need:
=

1. A Mac, with Mac OSX Snow Leopard
Yes, you’re gonna have to go out and splash the cash on a Mac. These cost about a grand for a new one so it can all be a bit pricey. Though you can also maybe try and find a second hand Mac online somewhere. Also, you will preferable have Snow Leopard included for latest compatibility with iPhones (see below).

2. XCode and the iPhone SDK
YoThe latest version of the iPhone Software Development Kit (SDK) will only run on Snow Leopard. You can always run an older version on Leopard I suppose, but it just won’t have the latest features and support for the new iPhone, iTouch and iPad.

3. Apple Developer account
http://developer.apple.com/programs/register/ – To download the iPhone SDK, you will need to be registered as an Apple Developer. Additionally, to submit apps to the appstore, you will also need to join the iOS Developer program, which costs $99 per year. FYI, the “iOS” is the “iPhone Operating System”.

4. The ability to learn Objective-C
Objective-C is the language all Apple software is written in. You will use this language to write the app code in XCode.
Try get yourself a good book:
-Objective-C for Dummies
-iPhone in Action:- Introduction to Web and SDK Development

or online resource:
Lynda.com

5. An Apple device
You might need an actual handset, like an iPhone, iTouch or iPad to test your software on. The SDK comes with an emulator, which can run the apps, but is missing some key features like GPS, camera and the shake function. If you’re coding apps to take advantage of these features, then yes, you’ll need an iPhone/iTouch/iPad.

=

Once you’re registered as an Apple Developer on the iOS program, they will provide you with an authentication key that you will use to transfer the apps to your device over USB cable, but we’ll worry about that later.

If you have the majority of items in that checklist, then you’re doing well. At the moment, all I have is a book on iPhone programming, and the internet! Not the greatest of starts, but I suppose thats the whole point of this project…to acquire the necessary instruments I’ll need to develop apps!

Have I missed anything? Let me know…

Hello and Welcome

Hi folks,

Welcome to my new blog!

I created this blog as part of my college project, in which I’ll be making my first iPhone app. This blog is partly here so I can keep track of what I have learned, and share it with anyone who’s interested! I intend to go over the tools used for programming on the iPhone, ie XCode, as well as languages such as Objective-C, C++, and maybe some Java. I might also throw in a few tips on how to use Mac OS X if I have time! 😀

After this project is complete, I hope to become a competent programmer who’ll actually still enjoy programming on the iPhone enough to have a little business on the side for a bit of money, but I think thats getting a little bit ahead of myself for now….

I can’t promise this will always be interesting, but if you’re thinking of getting into the field of iPhone app development, the experiences I post in this blog will hopefully help in some way! And if not, well, why not leave a comment and tell me how i can improve on my post!

Cheers,

Eoin