Tuesday, December 22, 2009

iphone: tips collection

iPhone: Make your Default.png

A UIPickerView with labels


how to send SMS/Email from an application

You can do this with a short code by way of a SMS/MMS aggregator like OpenMarket or as another person has posted you can use the SMS Gateway but this requires the knowledge of the end users carrier name. If your replying to a SMS on the iPhone I believe you can use the API which in turn would give you the carrier id of the incoming SMS message you would like to reply to.

Launch SMS with New Message Window Open:


[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms:"]]

"sms" - brings up general Text Messages screen

"sms: " - brings up the New Message screen, but with a little blue circle in the To field that it a little confusing and probably needs to be deleted.

"sms:?" - brings up the New Message screen, but with a question mark in a blue circle in the To field

"sms://" - brings up general Text Messages screen

"sms:&" - brings up the New Message screen, but with a ampersand in a blue circle in the To field

"sms:." - brings up the New Message screen, but with a period in a blue circle in the To field

how to invite friends from facebook conenct in iphone app:

You can find info about using FBC on the iPhone here(http://developers.facebook.com/connect.php?tab=iphone)

there are two modes you can use it in. One where your developer secret is in the phone app, if that makes you nervous you can proxy the authentication through your own server. FB developer site has the info along with programmer supported wikis

Facebook Connect 能做什么? 这个是个老外做的,想像了一些商业场景:如果 Amazon/iTunes/iPhone 这些网站//设备实现了Facebook Connect……

how to debug EXC_BAD_ACCESS on iPhone http://www.codza.com/how-to-debug-exc_bad_access-on-iphone

Instruments on Leopard: How to debug those random crashes in your Cocoa app

google geocoding UK postcode issue:

oogle Maps API provides a geocoding feature, for finding the latitude and longitude of places or addresses; but it does not work for UK postcodes. This is thanks to Royal Mail who have a copyright on the data, and are very restrictive with their (expensive) licenses for it.

solution: http://www.tomanthony.co.uk/blog/geocoding-uk-postcodes-with-google-map-api/

if use restFul ajax local search, use this: http://ajax.googleapis.com/ajax/services/search/local?v=1.0&q=xxx&gl=fr

reference: http://code.google.com/apis/ajaxsearch/documentation/

memory leaks detecting:

un the tools within XCode over menu -> run -> start with performance tool ->......

Introduction to Instruments User Guide http://developer.apple.com/iphone/library/documentation/DeveloperTools/Conceptual/InstrumentsUserGuide/Introduction/Introduction.html

Finding iPhone Memory Leaks: A “Leaks” Tool Tutorial


Instruments on Leopard: How to debug those random crashes in your Cocoa app

how to handle & in xml file:

answer: Put the data in a section and you should get what you want.


How to view contents of NSDictionary variable in Xcode debugger?

In the gdb window you can use po to inspect the object.

The difference between initWithString and stringWithString is that stringWithString returns an auto-released pointer. This means that you don't need to release it specifically, since that will be taken care of next time that the auto-release pool cleans up any auto-released pointers. initWithString, on the other hand, returns a pointer with a retain count of 1 - you do need to call release on that pointer, or else it would result in a memory leak.

code for plucking useful bits out of the addressBook:

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker

shouldContinueAfterSelectingPerson:(ABRecordRef)person {

customer.firstName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);

customer.lastName = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);

customer.companyName = (NSString *)ABRecordCopyValue(person, kABPersonOrganizationProperty);

if (!customer.firstName.length) {

customer.firstName = @"*";


if (!customer.lastName.length) {

customer.lastName = @"*";


if (!customer.companyName.length) {

customer.companyName = @"";


CFStringRef email, emailLabel, phone, phoneLabel;

ABMutableMultiValueRef phoneMulti = ABRecordCopyValue(person, kABPersonPhoneProperty);

NSMutableDictionary *myPhoneDict = [NSMutableDictionary dictionaryWithCapacity:ABMultiValueGetCount(phoneMulti)];

for (CFIndex i = 0; i <>

phoneLabel = ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(phoneMulti, i));

phone = ABMultiValueCopyValueAtIndex(phoneMulti, i);

[myPhoneDict setObject:(NSString*)phone forKey:(NSString*)phoneLabel];




if ( [myPhoneDict objectForKey:@"mobile"] != nil) {

customer.phone = [myPhoneDict objectForKey:@"mobile"];

} else if ( [myPhoneDict objectForKey:@"home"] != nil) {

customer.phone = [myPhoneDict objectForKey:@"home"];

} else if ( [myPhoneDict objectForKey:@"work"] != nil) {

customer.phone = [myPhoneDict objectForKey:@"work"];


ABMutableMultiValueRef multi = ABRecordCopyValue(person, kABPersonEmailProperty);

NSMutableDictionary *myEmailDict = [NSMutableDictionary dictionaryWithCapacity:ABMultiValueGetCount(multi)];

for (CFIndex i = 0; i <>

emailLabel = ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(multi, i));

email = ABMultiValueCopyValueAtIndex(multi, i);

[myEmailDict setObject:(NSString*)email forKey:(NSString*)emailLabel];




if ([myEmailDict objectForKey:@"home"] != nil) {

customer.email = [myEmailDict objectForKey:@"home"];

} else if ([myEmailDict objectForKey:@"work"] != nil) {

customer.email = [myEmailDict objectForKey:@"work"];

} else if ([myEmailDict objectForKey:@"other"] != nil) {

customer.email = [myEmailDict objectForKey:@"other"];


// get the address stuff

ABMultiValueRef streets = ABRecordCopyValue(person, kABPersonAddressProperty);

NSMutableDictionary *myAddressDict = [NSMutableDictionary dictionaryWithCapacity:ABMultiValueGetCount(streets)];

for (CFIndex j = 0; j <>

NSMutableDictionary *myLabelDict = [[NSMutableDictionary alloc] init];

CFDictionaryRef dict = ABMultiValueCopyValueAtIndex(streets, j);

CFStringRef typeTmp = ABMultiValueCopyLabelAtIndex(streets, j);

CFStringRef type = ABAddressBookCopyLocalizedLabel(typeTmp);

NSString *street = [(NSString *)CFDictionaryGetValue(dict, kABPersonAddressStreetKey) copy];

NSString *city = [(NSString *)CFDictionaryGetValue(dict, kABPersonAddressCityKey) copy];

NSString *state = [(NSString *)CFDictionaryGetValue(dict, kABPersonAddressStateKey) copy];

NSString *zip = [(NSString *)CFDictionaryGetValue(dict, kABPersonAddressZIPKey) copy];

NSString *country = [(NSString *)CFDictionaryGetValue(dict, kABPersonAddressCountryKey) copy];

if ((street != nil) && (street.length > 0))

[myLabelDict setObject:street forKey:@"street"];

if ((city != nil) && (city.length > 0))

[myLabelDict setObject:city forKey:@"city"];

if ((state != nil) && (state.length > 0))

[myLabelDict setObject:state forKey:@"state"];

if ((zip != nil) && (zip.length > 0))

[myLabelDict setObject:zip forKey:@"zip"];

if ((country != nil) && (country.length > 0))

[myLabelDict setObject:country forKey:@"country"];

[myAddressDict setObject:myLabelDict forKey:(NSString *)type];

[myLabelDict release];

[street release];

[city release];

[state release];

[zip release];

[country release];





NSString *theKey;

if ([myAddressDict objectForKey:@"home"] != nil) {

theKey = @"home";

} else if ([myAddressDict objectForKey:@"work"] != nil) {

theKey = @"work";

} else if ([myAddressDict objectForKey:@"other"] != nil) {

theKey = @"other";


if ([[[myAddressDict objectForKey:theKey] objectForKey:@"street"] length] > 0) {

customer.street = [[myAddressDict objectForKey:theKey] objectForKey:@"street"];

} else {

customer.street = @"";


if ([[[myAddressDict objectForKey:theKey] objectForKey:@"city"] length] > 0) {

customer.city = [[myAddressDict objectForKey:theKey] objectForKey:@"city"];

} else {

customer.city = @"";


if ([[[myAddressDict objectForKey:theKey] objectForKey:@"state"] length] > 0) {

customer.state = [[myAddressDict objectForKey:theKey] objectForKey:@"state"];

} else {

customer.state = @"";


if ([[[myAddressDict objectForKey:theKey] objectForKey:@"zip"] length] > 0) {

customer.postalCode = [[myAddressDict objectForKey:theKey] objectForKey:@"zip"];

} else {

customer.postalCode = @"";


if ([[[myAddressDict objectForKey:theKey] objectForKey:@"country"] length] > 0) {

customer.country = [[myAddressDict objectForKey:theKey] objectForKey:@"country"];

} else {

customer.country = @"";





[customer dehydrate];

[self dismissModalViewControllerAnimated:YES];

return NO;


1 comment: