Archive for the ‘Quick Tips’ Category

Couple of weeks back I spoke in GeekCampSG about iOS security where I demonstrated how insecure some of the iOS apps are when it comes to protecting user data. Popular apps like Whatsapp, EverNote, Instagram also fall into that categories.

During my talk I talked about 3 things:

  • How data is insecure when stored unprotected in Filesystem
  • How data is insecure while application communicates with a backend server
  • How insecure these data can be on a jailbroken device where reverse engineering is possible in GDB prompt

You can view the slides of my GeekCamp talk below. I have already talked about part-2 “which is securing iOS apps through file protection api” in iOS Dev Scout # Sept meetup. I will post the slide of the same here as well.

XCode makes the life of iOS developers easier by providing the ability to debug code using break points, backtrace error logs and pin point the cause of errors when they occur. You can also let the compiler speak out the log message. If you haven’t played with it just go ahead and edit one of your break points to add an action when an error occurs.

But every now and then you will notice this beast “EXC_BAD_ACCESS”. As an iOS newbie you will hate this error and often I have seen pro devs struggle to find out the exact cause of this error. As this is one such error that will mostly throw you to your main.m file and you can’t find what exactly is causing this crash.

One thing I have learnt over last few years and that has helped me to debug an error faster is – “An error is what the error says”.

So before posting your ugly error message on stackoverflow or before even hitting google, stop for a while and look carefully what the error message tells. So let’s say I get this error on my tableViewController, or scrollViewController then before getting into what is wrong in my so and so view controllers, I would probably look first into what does “EXC_BAD_ACCESS” mean if I don’t know about it. Apple’s error messages are verbose and self explanatory. Hence, as one can clearly make out from the description “EXC_BAD_ACESS” means you are doing bad access or in other words you are accessing something that you shouldn’t.

Talking more in Obj-C terms – a message was sent to an object that doesn’t exist or that doesn’t understads the message. Hence, bad access.

You will get EXC_BAD_ACCESS error mostly in the following scenarios:

  1. You are trying to access an object that is not initialized.
  2. You are trying to access an object that no longer exists. Either it’s being released or it’s nil. In ARC mode, make sure you take ownership of the object that you want to use.
  3. You are passing an message to an object that the object doesn’t understand. It can also happen for bad typecast. Like the lines below where I am trying to access an int with %@ in stead of %d.
int myAwesomeInt = 9;
NSLog(@"%@", myAwesomeInt);

How to debug:

Identify what you did that caused the crash. Did it crash while view of a particular view controller didLoad or in a delegate method or on a particular action. That will often help to find the object that is casuing the error.

  • Most of the time “NSZombies” can help to identify the dead object. You can enable NSZombies by editing your scheme Product -> Edit Scheme -> Diagnostics.
  • If you still don’t find the root cause then always go backwards from child view controller to parent view controller to see what object needs to be retained or what message needs to be passed properly.
  • Look into Static Analyzer and Instruments for advanced debugging. 

iOS 5 introduced a new feature called Automatic Reference Counting (ARC) few months back. My initial assumption was – either it brings in garbage collection for iOS Development or memory is released by compiler on a certain period of time like how it works for autorelease pools. But when I dived more into ARC I found it it’s neither of the above and it’s something much more powerful which makes the life of programmer easier.

In XCode 4.2 the new LLVM compiler reduces the effort of developers by automating the process of memory management. So developers don’t need to manually retain, release or autorelease objects. However, to repeat again – this is not garbage collection, ARC just automates the memory management process similar to how syntax checking is done as you type. You can create a new XCode project by selecting to use Automatic Reference Counting or you can also refactor your existing codes to Objective-C Automatic Reference Counting format.

But few things to remember once you do so:

1 – retain, release, autorelease, retainCount methods won’t work

In ARC mode retain, release, autorelease, retainCount and similar manual memory management methods are obsoleted. Usage of these methods will give compiler error as above. Memory of an object is retained, released and managed automatically by compiler based on two primary things.

  • Scope of the Object
  • Its property attributes

2 – dealloc method will be missing from classes

By default dealloc method will be missing from Objective-C classes. dealloc() method shouldn’t be called explicitly rather it gets called when an object no longer has any owner or in other words the retainCount of an object becomes 0. If you need to reset any of the state information or release any resources which are not instances of the class you can create a custom dealloc() method which gets called when the object no longer has any owner. However, you shouldn’t call [super dealloc] as that will be enforced by the compiler.

3 – retain, assign, copy “@property” attributes are no-longer used

In stead of retain, assign, copy “@property” attributes, ARC uses two new attributes to declare the property of the variables.

  • weak : a reference to an object that does not stop it from being deallocated. (Used in place of copy or assign)
  • strong : a reference to an object that stops it from being deallocated (Usually used to retain the variable)

4 – Use @autoreleasepool{} blocks in stead of NSAutoReleasePool

You must not use NSAutoReleasePool objects in ARC compliant code, instead use the @autoreleasepool{}blocks. Refer to main.m file in your XCode project to see how it uses @autoreleasepool{} block in stead of NSAutoReleasePool that was being used earlier.

You can find more about Automatic Reference Counting from the official site.

Recently I met a lot of startup founders and directors through Singapore Entrepreneur & Developer
community. While many of them are interested to provide both web and mobile solution for their
application, some of them are thinking to get the web version out first and think about mobile
applications later.

In either case, they need to think of an approach that’s more agile and easily extensible. Many
developers would follow the conventional way of building an application and adding components for
more platforms they would support. That not only makes them write the same functional features
multiple times but also make it less scalable as and when more users will start using the
application.

I have jotted down an easier and more scalable approach which encourages in creation and adaptation
of an API based route. While in the first approach the number of controllers or methods are directly
proportional to the number of supported platforms, in the second approach they can leverage on the
single set of APIs. As and when they will support more platforms they can parse the API response
and display it as per the device form factor and designs. As most of the modern platforms like
mobile os, web technologies, internet TV, think-clients support XML or JSON parsing it will be
easier to re-use the same code or set of APIs to enable the solution for a new platform.

It has been couple of weeks since our AVP called me and asked if we can do something in the Samsung LED TV that we have in our lab. A week ago, I got little free from my day to day activities to explore that.

The first thing I discovered as if I have got the jackpot that all the apps for Samsung Smart TV can be developed using HTML, CSS and JavaScript. Spending couple of years developing web application helped me here. The whole thing was like a piece of cake for me, and it was just about exploring the framework more.

But to my disappointment I also discovered that the sdk works with windows machine only. Never mind, sometimes in our life we have to use systems we never like. So I started with my 1st application to create a slide show application which can be used in actual practice.

I further discovered, we can’t embed direct PPT files or not even use it from USB devices. However both is possible if we want to display images. One can also use the TV remote to trigger an event like displaying next or previous slide. Another way of doing it would be to convert the PPT presentation to a flash file and embed that in code. One could find all those things as he would get his hands dirty on the tools and frameworks more.

But for beginners the following steps would be helpful:

1. Register for free here: http://www.samsungdforum.com/us and download the SDK.

2. Setup your apache configuration and make sure your app files (widgetlist.xml and files under “widget” directory) are publicly accessible, or at least in the same network with your Internet TV.

Reference: http://pages.samsung.com/appscheduler/guides/[2010]User%20Application%20Guide[Ver%202.00].pdf

3. Create an account by username “develop” in your TV and go to development section under settings to provide the IP of your server and install the application.

Further details can be found from the reference link above.

Note: One of the steps where I got stuck for longer was that in widgetlist.xml the download ip is the publicly accessible IP or an IP which is in the same network of TV.

If you create some cool apps using this you probably can target 500K USD prize money next year here: http://www.samsungsmarttvchallenge.eu/

10 cool tips to code in Java

Posted: September 25, 2008 in Quick Tips
Tags: , ,

* 1st and Most Important tip: Code in Ruby … don’t code in Java.

* Next 9 Tips : Follow the 1st Tip. 🙂

This is the first time I am using Mac and I had faced a very
funny problem last night while trying to listen to some songs
from my old collection of DVDs.

Mac books doesn’t have a typical DVD reader with the eject
button. The device is inside the laptop with some kind of
protected coat over the reader. You just place your DVD there,
and it will pull it inside. And if the DVD is working fine, you don’t
have to worry much. You can use the CTRL + Click operation
on the icon to do normal stuffs.

But I had a corrupted DVD which just stucked inside. And it was
not mounted in the system. Hence, I had no regular options to
eject it. I tried to find the Eject button externally in the Hardware.
But then I thought normal mp3 player have a default eject button.
Hence, I just found the same in Itunes application.

The trick worked for me. But later I tried digging into google for
more options! I came across few solutions but the one I liked the
most is the “drutil” tool.

All you need to do is:

# drutil tray eject

That’s it, It will work fine for you even if the media is not
mounted properly.