Thursday, June 24, 2010

Research: How to launch iOS app upon device startup

I have long been interested in the deep innerworkings of the iPhone (now iOS). I have gotten into iOS development, and poked into the process of jailbreaking and the filesystem of iDevices.

Today I was given a task to research the possibility of launching an app when the iDevice boots. I know that it is possible, since certain jailbreak apps/ processes are loaded upon startup. My digging led me to learn about daemons and how they work. This is incredibly useful to me in my quest to understand under the hood of iOS. Here are the results from my research.
  • The device will have to be jailbroken in order to have access to the filesystem. I recommend Pwnage. It takes a lot longer than Spirit, but is more secure.
    • With Spirit, you simply plug in your iPhone, open the Spirit app, press a button, and you're jailbroken. Spirit is what's called a "untethered userland jailbreak." Which means it exploits a hack in user (rather than kernel) mode in the UNIX filesystem. It therefore does not require iTunes to reboot the device and perform low-level tasks. Apple usually fills these holes in the minor updates (i.e. 3.1.x).
    • The Pwnage Tool,  builds a custom .ipsw file from an official copy. After that process is complete, you restore it to the device using iTunes. This is my preferred method. It takes about 30 minutes (maybe more), but some Pwnage jailbreaks will persist through software updates.There are Pwnage jailbreaks for 3.2 and 4.0 software already.
  • Here is a list of iOS daemons. Depending on the functionality of the app, we can delete many of these in order to clear up some overhead RAM.
  • We will have to create a daemon to launch our app. This tutorial goes into it as well as this one. The .plist will obviously have to be customized, but these two just prove that it is possible.
  • Overriding the home button is the other issue I'm researching. In addition to SBSSettings, there are other apps which allow you to associate user interactions on the screen (swipe, pinch, touching the volume display, etc) with system actions (e.g opening apps, toggling settings). More research is needed on this, but I do believe it is a feasible task.
This stuff is VERY interesting to me. I'm glad that I'm finally starting to get somewhere in my quest to understand what's happening in the system itself.

Too bad I just decided today that I want an iPhone 4. I'm almost sure they're all gone now.

Sunday, April 25, 2010

Code Specification Development

Recently I was given a unique opportunity. I was charged with the task of creating the code specification document for the official project which resulted from the proof-of-concept I developed. I was eager to take on such an assignment, as it presented a chance to learn a new area of the engineering process.

Since I'd never written one before, it took a bit of explaination from my boss for me to get the picture. Before he explainsed it to me, I had an idea, but was not completely comfortable with what I was doing.

I created an initial revision in Pages, using on a previous document as a template and screen mockups which illustrated the app's intended functionality. I went through each of the screens defined and established global variables, inputs and outputs, and specific functionality and data constructs necessary for the app to operate properly.

Being that it was a small project, the code specification didn't take long to develop. It took about a full work day to complete.

One of my favorite professors, Ben Sweet, preaches about the importance of engineering documentation. The time spent up front on providing detailed specifications saves time down the road on clarification and debugging.

I agree that this process is a necessity, and feel more well-rounded as an engineer because of this experience. It's good to have knowledge in multiple areas of development in order to be a proficient engineer.

- Posted using BlogPress from my iPhone

Monday, April 19, 2010

What is jailbreak?

OK, I obviously know HOW to jailbreak, but I'm interested to know what the actual process does to the phone. I posted on a couple forums to find my answer. Apple Developer Forums was no help (as I suspected). But the folks at Stack Overflow gave a pretty decent explanation/ discussion on the topic. Check it out! Very interesting.

Monday, April 12, 2010

POC proved the concept!

Last week I was given the responsibility of creating a proof-of-concept so that we can present to a company to try to get their business. I was a bit nervous because I had never had this type of pressure put on me. It was up to me to create an application nice enough as to capture the attention of this company and instill in them the necessity to do business with us.


Anyway, sparing you the dramatics (lol), it worked! I got word today that the company wants to move forward with us, and we'll be developing a full app for them. This is exciting for me because I built this app from the ground up. I ran into a couple of snags along the way, and worked through them by finding resources online as well as utilizing my teammates' knowledge.

The POC wasn't perfect, but it was impressive enough to get us the work. Now I'm looking forward to perfecting it for the official builds!

Wednesday, March 3, 2010

Debugging EXC_BAD_ACCESS from a NSString

While writing an iPhone application, I kept getting a mysterious crash during execution. There were no warnings or errors, but when I got to a specific point every time, it would crash. I decided to examine the console for messages. I found the message "EXC_BAD_ACCESS" there.

I Googled this message and found this site on setting the environment variables NSZombieEnabled and MallocStackLoggingNoCompact.

A little background
NSZombieEnabled - If I'm understanding this correctly, when this environment variable is set to YES, when an object is deallocated, instead of releasing it, the compiler will set its type to _NSZombie. Because the memory is never freed, the object remains on the stack, but its type is now _NSZombie. With a little command line usage, you can see where it was attempted to be freed, and whether or not it is intentional.

MallocStackLoggingNoCompact - "causes malloc to remember the caller requesting each allocation, and remembers it after the allocation is freed." (from Mac OSX Reference Library)

To set the environment variables, use this procedure:
  1. Ctrl+Click on the executable in your project
  2. Select "Get Info"
  3. Select "Arguments" tab
  4. Enter the variable information as seen in the screenshot below

Now, when I run the program, at the point of the crash, the console says:
2010-03-01 19:13:46.924 CruzNomad[7952:207] * -[CFString stringByAddingPercentEscapesUsingEncoding:]: message sent to deallocated instance 0x58448e0


At the gdb prompt, I issued the command: info malloc-history 0x58448e0. This gave me the object's allocation/ deallocation history:


Alloc: Block address: 0x058448e0 length: 64
Stack - pthread: 0xa0b33500 number of frames: 25
0: 0x98e089bc in malloc_zone_malloc
1: 0x21516aa in _CFRuntimeCreateInstance
2: 0x2152bf8 in __CFStringCreateImmutableFunnel3
3: 0x21567d9 in CFStringCreateCopy
4: 0x21742fc in _CFStringCreateWithFormatAndArgumentsAux
5: 0xdb546 in -[NSPlaceholderString initWithFormat:locale:arguments:]
6: 0xdb4d8 in +[NSString stringWithFormat:]
7: 0x23aa3 in -[BuisnessCardViewController viewDidLoad] at /Users/.../Classes/BuisnessCardViewController.m:85
8: 0x3d6796 in -[UIViewController view]
9: 0x347b4 in -[gm_menuViewController btn5_Pressed:] at /Users/.../Classes/menuViewController.m:535
10: 0x357459 in -[UIApplication sendAction:to:from:forEvent:]
11: 0x3baba2 in -[UIControl sendAction:to:forEvent:]
12: 0x3bcdc3 in -[UIControl(Internal) _sendActionsForEvents:withEvent:]
13: 0x3bbb0f in -[UIControl touchesEnded:withEvent:]
14: 0x370e33 in -[UIWindow _sendTouchesForEvent:]
15: 0x35a81c in -[UIApplication sendEvent:]
16: 0x3610b5 in _UIApplicationHandleEvent
17: 0x2984ed1 in PurpleEventCallback
18: 0x2197b80 in CFRunLoopRunSpecific
19: 0x2196c48 in CFRunLoopRunInMode
20: 0x298378d in GSEventRunModal
21: 0x2983852 in GSEventRun
22: 0x362003 in UIApplicationMain
23: 0x2c8c in main at /Users/.../source/main.m:14
24: 0x2bfa in start
 
Line 7 says the problem was in line 85 of BuisnessCardViewController.m. That line is here:
fullAddress = [NSString stringWithFormat:@"%@ %@", fullAddress, myString];

I read that the method stringWithFormat: returns an autorelease object. I had to retain the variable right after its first usage, earlier in the method.

fullAddress = [NSString stringWithFormat:@"%@ %@", fullAddress, myString];
[fullAddress retain];

I use it a few times in the implementation file. This is the first operation I perform on it, so I decided to retain it here.

I posted this question on StackOverflow and the iPhone Developer Forums (you need a login for the latter).

Such a rewarding feeling to get an issue debugged!

Thursday, February 25, 2010

iPhone Programming Course

A while ago, I stumbled across the Stanford iPhone courses on iTunesU. The courses are videos of the actual iPhone development course at Stanford University. I used these a few months ago to get familiar with the XCode IDE and the iPhone SDK. 

Now that I actually landed a small gig as an iPhone developer, I have been thrown into the belly of the beast. Because I'm in such a fast-paced environment, I'm learning WHAT to do to get things working, but not so much WHY I'm doing it this way. I'd rather not be solely a creature of habit, and would like to understand the inner-workings of the code that I'm....coding.

With that being said, I decided to take some time and revisit the Stanford iPhone course. I believe this will be time well-spent; an investment in my future as a Cocoa/ Objective-C developer. After finishing the first episode, I feel enlightened on a couple of nagging issues I had before. I'm sure this trend will continue as I progress through the course. 

I actually found another iPhone course at RWTH Aachen (a German school). Maybe I'll check this out another time.

Friday, February 19, 2010

Random Thought

I've recently come to the realization that few things are impossible - some things just haven't been done yet. As a software engineer, my job is to figure how to do these things.