Skip to content →

Tag: Apple

iPhone Development Helper: Fill up your Address Book

AddressBookFill is a simple application which populates your address book with contacts. This is useful when developing for the iPhone and working on the emulator.

On many occasions, I’ve spent quite a bit of time adding contacts to the emulator to test something. Then when I change emulator version, they all get erased. This is very frustrating. So I put together this simple application which allow you to programatically fill up your address book.

Currenly the address book is populated with U.S. Presidents. Values are set for:
* first name
* last name
* photo
* phone number
* birthday

I’ll make it more robust in the future. But you may find this useful for now. The code might also be interesting if you’re learning how to program the address book.

Download: addressbookfill-10.tar.gz

Comments closed

ASIHTTPRequest – Uploading Photos

Recently I used ASIHTTPRequest to upload images and other data from an iPhone application to a web server. I search around and looked at all the different options and this was best and easiest to use. It’s open source and comes with lots of good examples. I was particularly impressed with the network queue.

The one thing I did not find, was sample code for was posting an image to a server. Here is some sample code:


// Initilize Queue
[networkQueue setUploadProgressDelegate:statusProgressView];
[networkQueue setRequestDidFinishSelector:@selector(imageRequestDidFinish:)];
[networkQueue setQueueDidFinishSelector:@selector(imageQueueDidFinish:)];
[networkQueue setRequestDidFailSelector:@selector(requestDidFail:)];
[networkQueue setShowAccurateProgress:true];
[networkQueue setDelegate:self];

// Initilize Variables
NSURL *url;
ASIFormDataRequest * request;

// Add Image
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dataPath = [documentsDirectory
stringByAppendingPathComponent:@”myImage.jpg”];

// Get Image
NSData *imageData = [[[NSData alloc]
initWithContentsOfFile:dataPath] autorelease];

// Return if there is no image
if(imageData != nil){
url = [NSURL URLWithString:@”http://myserver/upload.php”];
request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease];
[request setPostValue:@”myImageName” forKey:@”name”];
[request setData:imageData forKey:@”file”];
[request setDidFailSelector:@selector(requestWentWrong:)];
[request setTimeOutSeconds:500];
[networkQueue addOperation:request];
queueCount++;
}
[networkQueue go];

Note: It’s not necessary to use a queue here because I’m only uploading 1 thing. But in my application I’m uploading many things and a queue was helpful. It also runs in the asynchronously in the background, which prevents the UI from getting locked up.

Note 2: When sending large files you should use “setFile” instead of “setData”. Files are then read from disk instead of memory. Here is an updated example. Thanks again to Ben Copsey for this library and his useful comments below.

NSString *imagePath = [documentsDirectory
stringByAppendingPathComponent:@”myImage.jpg”];
url = [NSURL URLWithString:@”http://myserver/upload.php”];
ASIFormDataRequest *request =
[[[ASIFormDataRequest alloc] initWithURL:url] autorelease];
[request setPostValue:@”myImageName” forKey:@”name”];
[request setFile:imagePath forKey:@”photo”];

More info on streaming here: http://allseeing-i.com/ASIHTTPRequest/How-to-use#streaming

Comments closed

MacOSX user Disappeared & Restored

The other day, I opened my MacBook Pro a few hours after closing the lid and putting it to sleep.  It wouldn’t wake from sleep.  I tried several things to get it to try and wake up, but had no success.  This has happened a couple times in the past, and I usually just hold down the power key to restart it.  So that is what I did.

When it started back up and loaded the login page, I noticed something strange, my user was missing.  I have 4 user’s registered on my computer and now only 3 of them were showing.  So I logged in as one of the other users who an admin.

Upon login, I could see that my user’s account still existed on disk:  /Users/doug  When I went to the System Preferences, I did not list my user.  I wasn’t sure what was wrong.

I began searching through the file system and found there is a file for each user in /var/db/dslocal/nodes/Default/users/  Example:

/var/db/dslocal/nodes/Default/users/doug.plist

I opened up doug.plist and found that it was a binary file.  I could read some of the text in there and saw some mention of “Kernel” and other thing that suggested a stacktrace or dump.  I opened up the plist for another user and found that it was standard XML.  

So I copied the doug.plist from my Time Machine backup and replaced it with the one on my laptop.  I rebooted and my account was restored.

I’m not exactly sure what happened, but the moral of the store is:

  1. Create a Time Machine Backup
  2. Have at least one other Admin user on your computer you can login to if all fails.
Comments closed