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
Tags:
ABAddressBookRef,
ABAddressBookSave,
ABMultiValueAddValueAndLabel,
ABMutableMultiValueRef,
ABRecordRef,
ABRecordSetValue,
apple,
Code,
iPhone,
Mac
Posted in Code, iPhone |
3 Comments » | June 12th, 2009
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
Tags:
apple,
Code,
iPhone,
Mac
Posted in Code, iPhone |
14 Comments » | April 8th, 2009
I was recently adding some code to check for the availability of the network, so I can display an error to the user if unavailable.
I looked at the SeismicXML application and borrowed the following code:
// Use the SystemConfiguration framework to determine if the host that provides
// the RSS feed is available.
- (BOOL)isDataSourceAvailable
{
static BOOL checkNetwork = YES;
if (checkNetwork) {
// Since checking the reachability of a host can be expensive,
// cache the result and perform the reachability check once.
checkNetwork = NO;
Boolean success;
const char *host_name = "earthquake.usgs.gov";
//const char *host_name = "localhost";
SCNetworkReachabilityRef reachability =
SCNetworkReachabilityCreateWithName(NULL, host_name);
SCNetworkReachabilityFlags flags;
success = SCNetworkReachabilityGetFlags(reachability, &flags);
_isDataSourceAvailable = success &&
(flags & kSCNetworkFlagsReachable) &&
!(flags & kSCNetworkFlagsConnectionRequired);
CFRelease(reachability);
}
return _isDataSourceAvailable;
}
I wouldn’t compile unless I added the following import:
#import <SystemConfiguration/SystemConfiguration.h>
After adding this I still go 2 more errors, but these were a bit more cryptic.
Undefined symbols:
"_SCNetworkReachabilityCreateWithName", referenced from:
-[TheElementsAppDelegate isDataSourceAvailable] in TheElementsAppDelegate.o
"_SCNetworkReachabilityGetFlags", referenced from:
-[TheElementsAppDelegate isDataSourceAvailable] in TheElementsAppDelegate.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
After some digging around I realized that I was missing the SystemConfiguration.framework framework.
So here is what I did to add it.
1. I right click on “Frameworks” and choose: Add < Existing Framework

2. Browsed to the file: /System/Library/Frameworks/SystemConfiguration.framework
3. Rebuilt the project and my error was gone.
Posted in Code, iPhone |
5 Comments » | December 23rd, 2008
I found a great tutorial, with source code, for basic usage of the iPhone Address book:
http://iphone.zcentric.com/2008/09/19/access-the-address-book/
It was very helpful for getting started with using the address book in my code.
Tags:
apple,
iPhone,
Mac
Posted in Code, iPhone |
1 Comment » | November 11th, 2008
I recently spent about 2 hours trying to get my Distribution Provisioning Profile to show up. I followed the instructions and did everything, but still it would not show up.
Then I came across this thread: http://www.v2ex.com/2008/10/22/distribution-provisioning-profile-not-showing-up/
The first suggestion was to create a new project. I created a new project and my Distribution Provisioning profile appeared in it. Then I went back to my main project and it appeared there too. Problem solved!
Posted in Code, iPhone |
No Comments » | November 11th, 2008
Recently I was trying to figure out how to make the padding in the cells of a <mx:comboBox> smaller than the default. This proved to be much harder than I thought. But I eventually found a solution.
Here is what the default looks like:

Here is the code I came up with:
<mx:Script xmlns:mx="http://www.adobe.com/2006/mxml">
<![CDATA[
import mx.controls.Alert;
import mx.core.Application;
import mx.events.DropdownEvent;
public var app:* = Application( Application.application );
private function comboBox_open(evt:DropdownEvent):void {
evt.currentTarget.dropdown.variableRowHeight = true;
}
]]>
</mx:Script>
<mx:ComboBox id=”myComboBox” dataProvider=”{mx.core.Application.application.bbapp.cityXML}” labelField=”NAME”
close=”app.closeHandler(event)” cornerRadius=”0? dropdownStyleName=”gameSelectBox” visible=”false” fontSize=”12? open=”comboBox_open(event);”>
<mx:itemRenderer>
<mx:Component>
<mx:Label text=”{data.NAME}” paddingTop=”-5? paddingBottom=”-5?/>
</mx:Component>
</mx:itemRenderer>
</mx:ComboBox>
</mx:HBox>
And here is the final output:

Posted in Code, Flex |
1 Comment » | November 2nd, 2008
Here some sample code showing you how to open a link in Safari when clicked on in your UIWebView. The method shouldStartLoadWithRequest is a delegate method that is called when a link is clicked on. You can override the method and tell it to open in Safari.
- (BOOL)webView:(UIWebView *)webView
shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType; {
NSURL *requestURL = [ [ request URL ] retain ];
// Check to see what protocol/scheme the requested URL is.
if ( ( [ [ requestURL scheme ] isEqualToString: @"http" ]
|| [ [ requestURL scheme ] isEqualToString: @"https" ] )
&& ( navigationType == UIWebViewNavigationTypeLinkClicked ) ) {
return ![ [ UIApplication sharedApplication ] openURL: [ requestURL autorelease ] ];
}
// Auto release
[ requestURL release ];
// If request url is something other than http or https it will open
// in UIWebView. You could also check for the other following
// protocols: tel, mailto and sms
return YES;
}
Posted in Code, iPhone |
2 Comments » | October 12th, 2008
Today I was trying to figure out the name of a sequence in the db, so I could add it to my hibernate mapping. I usually use pgAdmin, but that wasn’t an option today. After some searching, I eventually figured it out, but it took me awhile…
Get a list of all Sequences in a schema:
select sequence_name from information_schema.sequences where sequence_schema='public'
Get a list of all tables:
select table_name from information_schema.tables where table_schema='public'
Get the next sequence value for sequence: foo_seq
SELECT setval('foo_seq', 42);
Update the sequence to 42 for sequence: foo_seq
select nextval('foo_seq');
Note if you are looking for more info on sequences check here: Sequence-Manipulation Functions
Posted in Code |
No Comments » | October 10th, 2008
I recent created a new iPhone XCode project based off of one of the samples provided by Apple. Beause of this the XCode project was called AppleSample.xcodeproj and it built an app called AppleSample.app. I wanted to rename the project and give it my own name. It’s not as easy as it sounds. Here are some instructions which are based off some instructions I found here (http://aplus.rs/cocoa/how-to-rename-project-in-xcode-3x/)
- Copy/rename the folder into new name
- Get inside the new folder and rename the .pch and .xcodeproj files. Example rename AppleSample.xcodeproj to MyCoolApp.xcodeproj — and — rename AppleSamplePrefix.pch to MyCoolApp_Prefix.pch
- Delete the build folder. (Using the finder)
- Open .xcodeproj file in text editor, like TextMate or TextWrangler. That’s actually a folder, which contains 4 files (you can also right-click and do Show package contents, which will reveal the files)
- Open project.pbxproj in text editor and replace all instances of the old name with the new name. Be careful not to do a find an replace. Since the default name was AppleSample, there were several references to: AppleSampleAppDelegate.h and AppleSampleAppDelegate.m. You do not want to replace these unless you also rename that file.
- Open .pbxuser where is your user name. Then repeat the previous step by renaming. I’m not sure if this is necessary. You may even be able to delete this file? But changing this file worked for me.
- Load the project file in XCode, do Build/Clean all targets
After following these steps, I was able to open the project and build it to the simulator and the device.
I had one additional problem that I came across later that required a fix. When I finially built the application for “deployment” to the App Store, I had problems signing the code. I complained that it couldn’t find the Certificate for “Scott Anguish”. I looked in the build settings and couldn’t find reference to this name. So again I opened project.pbxproj inside of MyCoolApp.xcodeproj and found 2 instances of this line:
CODE_SIGN_IDENTITY = “Scott Anguish”;
I replaced both of them with the correct code signing identity and it worked!
UPDATE: Check out this bash script which does it for you automatically:
renameXcodeProject.sh
Tags:
Code,
iPhone,
Mac,
xcode
Posted in Code, iPhone |
7 Comments » | October 9th, 2008
I’ve been doing some iPhone development with XCode recently and had problems using the debugger on the device (not the simulator) and also with taking screenshots.
When installing an application on to the device, the debugger would say:
"Failed to start remote debugserver for MyApp.app"
I search around looking for a solution. In a thread somewhere, someone suggested reinstalling the SDK from scratch. I had first installed the SDK about a year ago when it came out. I then installed several updates over it. For whatever reason, that cause problems.
After reinstalling the latest SDK from Apple website, the debugger worked perfectly! And I was able to take screenshots from the device.
Posted in Code, iPhone |
No Comments » | October 2nd, 2008