Saturday, December 31, 2011

Learning Objective C - Part1

Now that I got a Mac, this holiday season, I decided to start working on learning Objective-C. Objective-C is the programming language used in almost everything Apple - including iPhone and iPad development. It is also used in developing Mac native applications. Working on a system, which I don't fully understand, or can develop with, always bothers me, and hence, all the above reasons combined, I start my Objective-C. Right off the bat, it became clear that, this is a totally different beast in terms of syntax. My C, C++ knowledge will surely come handy here, but at the same time, there are so many things that are being done in Objective-C in a totally different way.

As I proceed through Objective-C, I will be using my Blog to document what I would like to refer back.

 

Variable Declarations in Objective-C


Simple variables are declared just like in traditional C. However, when it comes to object declarations, things are different in Objective-C.

Example of simple variable declaration:

int testInteger = 0;

Not a big surprise here, as objective-C has its roots in traditional C language.


Example of declaring a string


NSString *testString;

The object declarations in Objective-C is always with a pointer. We then allocate and initialize this memory location, and gets the reference of the memory location to the pointer variable. More on memory allocation and freeing, later.

By the way... almost all the standard classes in Objective-C start with "NS".... because if NEST history of apple.


Example of declaring a Custom Class (say... Dog class)

Dog *pluto = [Dog new];

The above line of code declares a pointer of name "pluto". This pointer points to a memory location with some space reserved for instance of the object "Dog". The "new" keyword allocates AND initializes the memory location at the same time.


Another (More Commonly used) example of declaring a custom class

Dog *pluto = [[Dog alloc] init];

The above line of code also initializes a memory location. This is done by the "alloc" section of the code. The square brackets tells XCode that this is Objective-C code. Once allocation is done, the result is passed out, but right into the hands of init method. The job of init method is to initialize the memory location. Once the memory is initialized, it returns the pointer to the memory location out , which promptly gets assigned to the pointer variable "*pluto".


Memory Allocation and De-Allocation


What is Memory Leak

Unlike many modern programming languages like Java, C#...etc, Objective-C does NOT have an in-build garbage collector (There is something called Automatic Reference Counter, or ARC in objective-C. We will get to this sometime later). This means that, we are responsible for cleaning up and freeing memory after its use. If we don't do this, the application will gradually eat away available memory - until no more memory is available.  This is called "Memory Leak" - Pretty bad situation to be in, for mobile devices, and a guarentee for 1 star comments at app store... that is, if the application ever makes it to the app market.

Difference between using "new" and "alloc-init" combination to initialize objects

In the above example, we saw 2 ways of instantiating the “Dog” class. One used the “new” keyword and, the other, 2 keywords, namely “alloc” and “init”. Surely, the second option seemed more complex and unnecessary, as we have a one-step way of doing this.

The reason why the second approach is more desired, is because of the different options we have to initialize the memory location. It gives us finer control on initialization, before allocating the location information to pointer variable.

Instead of “init”, we can use many other “specialized” initializations, with parameters, as shown. This is not possible when using “new”.

image

All right.. 2:25AM now… that’s it for part 1. This was primarily capturing whatever I learned till now. There is some more… and it wil be captured in this/next blog. If someone stumbles across this blog, and find it useful, please leave a comment.

Sunday, May 01, 2011

My First Published Android App (And its not Hello World! :) )

So, finally, after lots of sleepless nights, going through thousands of pages of documentations, tutorials, Stack Overflow, and lots of other websites, finally, my first App was ready.

I give you RIFT Assistant.

What is RIFT Assistant?

RIFT Assistant is a mobile app for game called RIFT (doh!). For people not familiar with RIFT, it is a MMORPG (Massively multiplayer online role-playing game), where others you see in the game are real people, just like you. Millions of people play the game, and thus make up a strong community, with guilds, Player Vs Player fights, Economy, Auctions…etc. Its a whole different world.

Consider this game world as a “sandbox/development system” for some of your real world decisions. There are some decisions (esp, the ones driven by peoples emotions, behavior..etc), that you can analyze, before making that in real life – We are digressing.. so, let me leave things about RIFT or WOW (World Of Warcraft) at that.

RIFT Assistant helps the players by providing following features:

  1. Provides Shard (server) status, including information like server population, queue size, recommendations..etc.
  2. Ability to view North American/European shards, and a separate easy to reach tab for Favorites.
  3. Provides players with a Home Screen Widget which can be used to monitor shards.
  4. Provides RIFT official news, and also Patch Notes
  5. Provides Alerts/Notifications in your android device’s task bar when new news/patch notes are received.

Here are some screen shots of the app

HomeWidgets   NA_Shard_Status notifications Patch

settings_main_page Main Screen

Why RIFT Assistant?

So, why did I start developing this app? Learning android development was one of my highest priority things for quiet some time. When I say “learn android”, I don't just mean coding. I also mean aspects of the project like publishing, marketing, interacting with users – Basically any and everything from A to Z. For me, this is a perfect miniature scenario of corporate world – One which I can use to learn / understand the big picture of what I am a part of in corporate world.

And so, on April 2nd night, I began. Days that followed found my sometimes happy, depressed, irritated… all kinds of emotions while I progressed slowly through Android development. I even thought of giving up, but just kept going. After 2 weeks or so, something strange started happening.

You see, when searching for many of the issues/problems I encountered as a beginner, I was getting lots of not-so-useful answers when searching forums and other online resources, and finally, after like an hour or so, I would get the solution I was looking for. However, the efforts stuck with me… what I mean by that is, in the process of looking for something, I unknowingly picked up many other solutions (though I was not looking for at that time). But then, as I dived deeper into Android development, I started encountering those problems one by one – and imagine my excitement when, like magic, I was able to recollect/at least have an idea of how to approach the issue. Things were going easy then for some time. Then, I hit the next steep curve up.

So, in any technology, you will see a lot of beginners, then a lot of people who have good knowledge of the subject (not beginners, but intermediate knowledge). However, with some of the things I was trying to do, I was slowing drifting away from even intermediate, to advanced or super advanced stuff in android. It was like going from a thickly populated city to a rural area – all of a sudden I saw myself searching for answers longer and longer, and sometimes, the answers were not there online, as I probably was the first person to hot that problem.  One site that really helped me a lot was Stack Overflow. What followed was lots of trial and errors, while I applied the knowledge I got from beginner and intermediate steps – and every single problem – fell before me – Some took hours & some days. But, eventually, the problem will get solved. I never really knew what “never give up” as seen in movie dialog..etc was all about… but having lived that in this situation – it felt good, and sense of accomplishment.

RIFT Assistant was the means/goal by which I was formulating requirements/problems, and then, searching or learning solutions. Its still in making, and there are a lot of features to be added, each coming with a lot of learning experience as well.

Upcoming Updates/Features

 

In the coming days, I will use this blog to write about some key things I learned. Hopefully it will make the life of another android beginner a tad easier!

Saturday, April 30, 2011

Starting Android Development with Motorola Xoom

I was excited to finally purchase Motorola Xoom tablet, and even more excited at the thought of starting android development for the tablet. So, as soon as I reached home, I connected the xoom to my PC, and was eager to push an existing app from eclipse workspace to the tablet… things are never easy… are they… there is always a roadblock… and, later, once you solve it, the sheer joy!..

The Problem

Eclipse/ADB was not ready to push the APK file into the device. When looking at the list of devices, it does come up, but it shows as “unknown” target, and offline, as shown in screen shot below:

image

 

Cause

There are 2 primary reasons for the above problem

1) Xoom Driver
2) Google has not included Xoom in their driver ini files (yet).

At the time of writing this BLOG, you can download driver files from following location:

http://www.motorola.com/Support/US-EN/Support-Homepage/Software_and_Drivers/USB-and-PC-Charging-Drivers

Solution

First, download and install the driver from above URL, and make sure your OS recognizes  Xoom. Now comes the part where we are going to tell ADB to use debug drivers provided by Google to debug apps in your new Xoom tablet. To do this, follow these steps:

  1. Go to your SDK installation folder, and find a folder named “google-usb_driver”.
  2. In the folder, you will see a file named “android_winusb.inf”
  3. Open the file in notepad or another text editor
  4. Paste the following at the end of sections labelled “[Google.NTx86]” and “[Google.NTamd64]”

;
;Xoom
%SingleAdbInterface%        = USB_Install, USB\VID_22B8&PID_70A9
%CompositeAdbInterface%     = USB_Install, USB\VID_22B8&PID_70A9&MI_01

 

Restart your ADB and make sure you have debug mode turned on in your device by going to your device application settings (Standard Android app development stuff)…. but if you dont know what I am talking about, please head over to : http://developer.android.com/resources/faq/commontasks.html#neweclipseandroidproject

Once done, your device listing should look like the following:

image

 

References

http://community.developer.motorola.com/t5/Android-App-Development-for/Getting-USB-Debugging-ADB-working-with-Xoom/td-p/12060