brandontreb.com Tips And Resources For Software Consultants

Check Out AutoCorrector + Promo Codes!

So, this blog post might sound like a lame press release, but I actually do want to talk about a cool app some buddies of mine created. Just read it in a cheesy used car salesman voice and it will be a lot less more enjoyable.

The Problem

Have you ever tried to type shorthand text to a friend and it come out completely wrong? For example: AFK -> AFT, AKA -> Aja. Or maybe your friend has an uncommon nickname that auto corrects to something hilarious…

Enter AutoCorrector

@cruffenach , @matt_tuzzolo , and @damian_r just released an app for the iPhone called AutoCorrector! It’s an app that allows you add words to your iPhone’s auto correct dictionary. Once added, the iPhone will stop pestering you to auto correct uncommon/shorthand words that you frequently type.

How It Works

Adding words directly to the dictionary would be against the Apple SDK terms of use as it would require your app to run outside of it’s “sandbox”. However, clever @cruffenach realized that when you add comma separated words to a contact’s nickname in the address book, they get added automatically to the iPhone’s dictionary.

This actually makes a lot of sense when you think about it. If you have a friend with a nickname, chances are you will want to address that person at some point by that nickname. It would get very annoying if you iPhone tried to autocorrect you every time you tried to type that friend’s name.

Auto corrector simply uses the AddressBook framework to maintain a contact with a nickname containing a list of words you have added. When you add a new word, it simply updates this contact.

Why Use Auto Corrector?

Sure you could just do this manually, however it’s a pain and requires too many steps (6 taps vs. 2 taps for AC). Also, auto corrector provides a beautiful interface for entering and managing words you have added.

Word Packs!
Bundled with AC, comes the SMS shorthand word pack with 116 words ready to install instantly. In the near future, the guys plan on creating a ton more word packs as well as provide a portal for community packs.

I’m pretty excited about this app and really think it has a good future.

Download it here

What do you think? Would/did you buy it?

Edit
Here are some promo codes!

4YMPYNFJEWNE
MY6FTY49M4F6
H9KYWH9J4YPY
XW4EXHM67R34
AR6JN3RJYFE6

Beginning Jailbroken iOS Development – Building And Deployment

Welcome to the next installment of my jailbroken iOS development series. If you haven’t already done so, read my previous post to get up to speed.

Beginning Jailbroken iOS Development – Getting The Tools

Now that your environment has been set up, it’s time to start hacking your project, building, and deploying it. This tutorial will be fairly quick and will just demonstrate how to build a .deb file and install it on your device. This will not cover creating a Cydia repository for public access (this is for a later tutorial).

Introduction

Start by creating a simple project. Refer to the previous tutorial to determine how to do this. I will use the example of the #1 iphone/application and will be calling it fooProject.

<code class=’bash’>$/opt/theos/bin/nic.pl
NIC 1.0 - New Instance Creator
——————————
  [1.] iphone/application
  [2.] iphone/library
  [3.] iphone/preference_bundle
  [4.] iphone/tool
  [5.] iphone/tweak
Choose a Template (required): 1
Project Name (required): fooProject
Package Name [com.yourcompany.fooproject]:    
Author/Maintainer Name [Brandon Trebitowski]: 
Instantiating iphone/application in fooproject/…
Done.</code>

This will create a new directory for our project with the following files.

  1. control: Contains information about your application/tweak. This is the info you see when you install something from Cydia (name, author, version, etc…)
  2. [applicationName]Application.mm: This is essentially your appDelegate file. It creates a window, adds your RootViewController and displays it.
  3. main.m : Same main file that you would use in an iPhone project. Just initializes your [applicationName]Application.mm file on launch.
  4. Makefile: contains the necessary build commands to compile your project. I’ll go over this in detail in a bit
  5. Resources: This folder contains your info.plist and is also where you can place all of your images
  6. RootViewController.h/mm: A simple view controller

The Makefile

theos provides some great tools that simplify the build process as much as possible. I know some of you might shudder at the sound of a Makefile, but trust me, it’s very painless.

Let’s first take a look at the anatomy of the Makefile that was generated.

<code class=’bash’>include theos/makefiles/common.mk</code>

This line simply tells theos to include the common make commands in your build script to save you some work.

<code class=’bash’>APPLICATION_NAME = fooProject</code>

The name of the application we are building. The Makefile will use this constant to do a number of things under the hood. Don’t modify this unless your application changes names.

<code class=’bash’>[applicationName]_FILES = main.m fooProjectApplication.mm RootViewController.mm</code>

This is the meat of the Makefile. It’s the list of m (or mm in this case) files that need to be compiled. *Note: you do not add your .h files here. So, whenever you add a new .m file to the project, make sure to append it to this list or it won’t get built.

<code class=’bash’>[applicationName]_FRAMEWORKS = UIKit Foundation QuartzCore AudioToolbox CoreGraphics</code>

This line is not included by default, but is needed if you want to use ANY frameworks; be them Apple’s or h4x0red ones.

<code class=’bash’>include $(THEOS_MAKE_PATH)/application.mk</code>

More defaults to help theos build your project.

Setting up Your Environment

Do you remember in the last tutorial that we had to set an environment variable in order to use theos? Well, we need to set a couple more to build and deploy.

Here are the 3 that you MUST have.

<code class=’bash’>export THEOS=/opt/theos/
export SDKVERSION=4.3
export THEOS_DEVICE_IP=192.168.1.122</code>

The first line, you should have already done. Otherwise you would not have been able to run the nic tool above. The second defines your current SDK version. This will tell theos where to look for your frameworks and whatnot. (ex If you only have 4.2 installed, set this to 4.2). Finally, the last line, tells theos the ip address of your device.

Once theos does the package install, it will sftp the package to your device for installation. One thing to note here is your iPhone MUST be on the same network as the computer you are developing on.

If you don’t know your device’s ip address, it’s in the SBSettings menu. If you don’t have SBSettings, go download it from Cydia and think about why you would have a jailbroken device without this app installed…

 

Building The Project

Building is just as easy as running the make command inside of your project’s root directory.

Here is an example of building my fooProject app using the command:

make.

<code class=’bash’>$ make
Making all for application fooProject…
 Compiling main.m…
 Compiling fooProjectApplication.mm…
 Compiling RootViewController.mm…
 Linking application fooProject…
 Stripping fooProject…
 Signing fooProject…</code>

If your project contains errors, they will be flushed out here. Also, theos treats warnings as errors (as should you) 😉 . So, if you have any warnings you won’t be able to build.

When you are ready to distribute your application, you must build a .deb package. Luckily, theos does this for us with a simple command:

make package.

<code class=’bash’>make package
Making all for application fooProject…
make[2]: Nothing to be done for ‘internal-application-compile’.
Making stage for application fooProject…
 Copying resource directories into the application wrapper…
dpkg-deb: building package ‘com.yourcompany.fooproject’ in ‘/Users/brandontreb/Desktop/fooproject/com.yourcompany.fooproject_0.0.1-1_iphoneos-arm.deb’.</code>

You should now see a file like com.yourcompany.fooproject_0.0.1-1_iphoneos-arm.deb in your project’s root directory. One great feature of theos is it handles versioning automatically. So subsequent builds will increment the build number. If you change your version in the info.plist file, it will start the build counter over (2-1, 2-2, etc…).

At this point, you could sftp this file to your device and run:

<code class=’bash’>dpkg -i com.yourcompany.fooproject_0.0.1-1_iphoneos-arm.deb</code>

as root to test the installation. However, that is quite a bit of a pain. Luckily, theos will do all of this for you by simply using the command:

make package install

<code class=’bash’>$ make package install
Making all for application fooProject…
make[2]: Nothing to be done for `internal-application-compile’.
Making stage for application fooProject…
 Copying resource directories into the application wrapper…
dpkg-deb: building package ‘com.yourcompany.fooproject’ in ‘/Users/brandontreb/Desktop/fooproject/com.yourcompany.fooproject_0.0.1-3_iphoneos-arm.deb’.
install.copyFile "/Users/brandontreb/Desktop/fooproject/com.yourcompany.fooproject_0.0.1-3_iphoneos-arm.deb" "com.yourcompany.fooproject_0.0.1-3_iphoneos-arm.deb"
root@192.168.1.122’s password: 
com.yourcompany.fooproject_0.0.1-3_iphoneos-arm.deb                                                                                                                                                                                                                              100% 4434     4.3KB/s   00:00    
install.exec "dpkg -i com.yourcompany.fooproject_0.0.1-3_iphoneos-arm.deb"
root@192.168.1.122’s password: 
Selecting previously deselected package com.yourcompany.fooproject.
(Reading database … 2006 files and directories currently installed.)
Unpacking com.yourcompany.fooproject (from com.yourcompany.fooproject_0.0.1-3_iphoneos-arm.deb) …
Setting up com.yourcompany.fooproject (0.0.1-3) …</code>

You will have to enter your password several times during the process. The default is alpine.

After this has been run, you will need to restart (or respring) your iPhone. Respringing takes a simple button tap if you have SBSettings installed (have you installed it yet?).

Conclusion

You now have all of the tools needed to start hacking your very first jailbroken iOS application. In the next tutorial, we are going to learn about MobileSubstrate hacks and how to overwrite Apple functionality. I will give a basic example of how to display an alert message every time your device boots up.

Until next time, happy hacking!

Half Off iPhone And Android Books From Manning Publishing Today

Last year I wrote the second edition of [iPhone and iPad In Action][1]. It was published by [Manning Publishing][2] and today they are offering half off all of their mobile software development books!

Simply use the code:

dotd0414

when you check out at [manning.com][3] for a half price discount on the following titles:

    </p>
  • <a style="FONT-STYLE: italic; COLOR: #727272; TEXT-DECORATION: none" href="http://www.manning.com/trebitowski/" shape=rect track="on" linktype="link">iPhone and iPad in Action That’s mine!</a>
  • <a style="FONT-STYLE: italic; COLOR: #727272; TEXT-DECORATION: none" href="http://www.manning.com/jharrington/" shape=rect track="on" linktype="link">iOS 4 in Action</a>
  • <a style="FONT-STYLE: italic; COLOR: #727272; TEXT-DECORATION: none" href="http://www.manning.com/cahill/" shape=rect track="on" linktype="link">iPhone in Practice</a>
  • <a style="FONT-STYLE: italic; COLOR: #727272; TEXT-DECORATION: none" href="http://www.manning.com/collins/" shape=rect track="on" linktype="link">Android in practice</a>
  • <a style="FONT-STYLE: italic; COLOR: #727272; TEXT-DECORATION: none" href="http://www.manning.com/franco/" shape=rect track="on" linktype="link">Hello! iPhone</a>
  • <a style="FONT-STYLE: italic; COLOR: #727272; TEXT-DECORATION: none" href="http://www.manning.com/crawford/" shape=rect track="on" linktype="link">iPad in Practice</a>
  • <a style="FONT-STYLE: italic; COLOR: #727272; TEXT-DECORATION: none" href="http://www.manning.com/harwani/" shape=rect track="on" linktype="link">Quick & Easy iPhone Programming</a>
  • <a style="FONT-STYLE: italic; COLOR: #727272; TEXT-DECORATION: none" href="http://www.manning.com/ableson2/" shape=rect track="on" linktype="link">Android in Action, Second Edition</a>
  • Location-Aware Applications 
  • Objective-C Fundamentals 
  • Sencha Touch in Action 
  • Windows Phone 7 in Action 
  • </span></ul>

    This deal is only available for today April 14th, so go right now…

    [1]: http://manning.com/trebitowski [2]: http://manning.com/ [3]: http://manning.com

My Journey To Create The inPulseNotifier For Jailbroken iPhones – Part 2

This post is a continuation from my previous post found here.

A MobileSubstrate Tweak?

After I completed a basic app that allowed me to send various notification types from my iPhone to my inPulse watch, I quickly realized that this would not work if I wanted to hook into the iPhone’s notification system. A Google search revealed to me that I had to make MobileSubstrate hack to overwrite the default notification functionally.

** What the heck is Mobile Substrate? **

Here is the description according to iPhoneDevWiki.net.

MobileSubstrate is the de facto framework that allows 3rd-party developers to provide run-time
patches (“MobileSubstrate extensions”) to system functions, similar to Application Enhancer on
the OS X. MobileSubstrate consists of 3 major components: MobileHooker, MobileLoader and safe mode.

Basically, it allows you to “hook” into any method of any class on the iPhone. I will talk in a later tutorial about how to see what methods are available. Here is a sample for hooking into the CFShow() method:

<code class=’objc’>static void (*original_CFShow)(CFTypeRef obj);  // a function pointer to store the original CFShow().
void replaced_CFShow(CFTypeRef obj) {           // our replacement of CFShow().
  printf("Calling original CFShow(%p)…", obj);
  original_CFShow(obj);                         // calls the original CFShow.
  printf(" done.\n");
}
…
// hook CFShow to our own implementation.
MSHookFunction(CFShow, replaced_CFShow, &original_CFShow);
// From now on any call to CFShow will pass through replaced_CFShow first.
…
CFShow(CFSTR("test"));</code>

Yeah… this looks pretty intense. Luckily, I found a super helpful tool that greatly simplifies this process called theos. Check out my tutorial on getting started with theos.

What lead me to theos was the incredible work of Peter Hajas. He wrote and open sourced his fantastic notifications app called Mobile Notifier. Without it, I would have been dead in the water. After chatting downloading his code and nagging him on Twitter, he pointed me to theos.

Working With theos

As I mentioned in my tutorial, theos is a great tool to aid in the development of a Mobile Substrate Tweak. Instead of the complexities above to hook into the functionality of classes, you simply do something like this:

<code class=’objc’>%hook SpringBoard

-(void)applicationDidFinishLaunching:(id)application
{    
    %orig;

    INPreferenceManager *preferenceManager = [[[INPreferenceManager alloc] init] autorelease];
    BOOL enabled = [[preferenceManager.preferences valueForKey:@"inpulseEnabled"] boolValue];
    if(enabled) {
        manager = [[INAlertManager alloc] init];
        [manager connectToWatch];
    }
}

- (void) dealloc {
    [manager release];
    manager = nil;
    %orig;
}

%end;</code>

This is a simple hook that allows me to overwrite the functionality of Springboard’s applicationDidFinishLaunching method. DHowett (creator of theos), has done a brilliant job giving devs quite a few commands like %orig (which calls the original method) and %hook (starts a hook).

The reason this works is, the file you code in (called Tweaks.xm) gets run through a preprocessor called Logos (which he also wrote). It replaces these simple commands with the complex stuff you see above before compiling your project. Using this technique, you can hook into anything. I will have a tutorial on this in the very near future, so make sure you subscribe to my RSS feed if you haven’t already.

Copy And Paste

OK, so I copied some of Peter’s code (he’s well cited in the source and I kept his license in the headers too). He had already solved the problem of capturing most of the iPhone’s notifications and this is precisely what I needed. So I Frankensteined my code and his to create the first version of inPulseNotifier.

This current version supports SMS, Push Notifications, Local Notifications, and Calendar Notifications.

Deployment

Even though I open sourced the code for the app, I couldn’t possibly expect users to build the code themselves and install it on their devices. So I had to learn about setting up my own Cydia repository.

A Cydia repository allows users to browse your applications/tweaks through the Cydia application created by Saurik. This is the method of choice for deploying jailbroken iPhone applications.

Saurik has a very detailed tutorial on creating a Cydia repository here.

(In case you are wondering, my Cydia repo is hosted at http://brandontreb.com/cydia )

OK, How Do I Install It?

Great question I assume you might ask. I have written a tutorial about installing it on your iPhone here.

Plans For The Future

Well, at the moment development has slowed as I prepare for a few changes in my life. However, in the coming months, I intend on doing the following updates.

  1. Remove the dependency on Insomnia for preventing the phone from sleeping. I have found a version of their source that I can integrate into my own code.
  2. Add support for phone calls
  3. Add support for emails
  4. Create an SBSettings toggle to allow rapid enabling/disabling
  5. Auto-reconnect after disconnecting from the watch

Conclusion

Hacking for the inPulse watch has been a new and exciting experience. The team over there has really created something special and I can’t wait to see what the future holds for them (hopefully a touchscreen).

I’d love to hear your thoughts in the comments. And stay tuned for some jailbroken dev tutorials based on my experiences.

Happy Coding!

My Journey To Create The inPulseNotifier For Jailbroken iPhones – Part 1

Many of you may have seen my tweets or blog posts regarding my development of a native iOS interface for the inPulse Watch. I just wanted to share a bit with you about the development process and talk about my updates moving forward.

What is the inPulse Watch?

For those of you who don’t know, the inPulse watch is a programmable wristwatch with a bluetooth controller. It can run a single app at a time on top of it’s firmware that is installed on the watch via a bluetooth interface with your computer. People have written all sorts of apps from analog clocks to roguelikes and Connway’s Game of Life. Check out http://inpulsewatch.com/watchapps.asp to see the latest apps.





&nbsp

</p>

It Begins

After waiting about 10 days for my watch (yeah it’s a long waiting period since they have seen quite a bit of recent success), it finally came in the mail. Enthusiastically, I opened it up and followed their getting started guide. Within a few minutes I was running some of the sample applications in the forums.

No iOS Support?

One of the coolest features of the watch is the ability to have your smartphone notifications show up on it. You are able to read SMS, Email, Calendar, etc… for a wide variety of Blackberry and Android phones.

After digging around a bit, I was disappointed to see that there was limited (no) iPhone support. They had some old file that used BTStack at some point to communicate a simple canned message to the iPhone. No documentation, no support, nada.

Building The App

As I had no experience developing for a jailbroken iPhone, I had no idea how to even compile this sample app. So, I used the Google machine and found the Google repository for BTStack (the bluetooth library of choice for jailbroken iOS apps). I was able to download the source and build a sample application that basically connected my iPhone to a bluetooth device.

Well, this got me started. So I did what any curious developer would do and started plugging crap in. I replaced the sample file that was packaged with BTStack with a slightly modified version of inPulse’s sample iPhone file. Sure enough, I was able to build and install it.

We Have Connection

After figuring out how to hardcode my watch’s address into the source, I was able to determine that the iPhone had actually connected to the watch. I monitored the packets and threw up an alert view (No NSLogging available at this point) when a connection was made.

So, now that we are connected, let’s trying sending the sample message…fail! I couldn’t even send the time.

At this point, I decided to move the code I had written into a sample iPhone project so that I could do attached debugging. This showed me that the packets were arriving on the watch since I received an ack packet back. So, the watch sees the messages, but doesn’t consider them notifications.

Time to hit the docs


Knowing that the packets were received gave me quite a bit of hope. I figured that I must have had the protocol incorrect. So, I dug into the inPulse protocol document which outlined what the packet structures should look like. My C skills aren’t the strongest so I had to look up quite a few things.

Sure enough, they had upped the version number of their protocol by 1 and mine was outdated. So, I updated what I could and gave it another shot…fail.

No messages were displaying. At this point I was ready to give up. I read online somewhere that the BT implementation on the watch was not supported by inPulse and felt dead in the water. So I took a break for a couple days.

When I came back to it, I was tapping through my watch and noticed that the calendar appointments had some data in them on the watch. Could it be? Did my notifications arrive? (I was trying to send SMS notifications). So I dug into the docs again…

We have a message!!!

Sure enough, I had some fields declared as * uint16_t * when they should have been * uint8_t * . My headers were the wrong size!!! All of a sudden I was able to send every type of notification to the watch and have it displayed.

It was now time to build out an actual application for release to inPulse. The next hurdle would be hooking into the iPhone’s messaging system and forwarding along the messages. Now the real iPhone hacking begins.

This post is continued here