Thursday, April 28, 2011

iOS Contact Details in App

Made an update to a game I wrote ages ago, and added my contact details: Such that the user can add your details to their Contacts Address book, aswell as call and email, and URL links to my Twitter and mobile Linkedin... Some handy code examples in here, that took me a while to make sense of..That ABRecordRef stuff is weird.

Just add #import
#import

Suss out the syntax for a ABRecordRef and then present the contact thing modally.. done.

#import "CreditsViewController.h"
#import "WebViewController.h"

@interface CreditsViewController()

- (void) sendMail;

@end

@implementation CreditsViewController

// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization.
self.navigationItem.title = @"Credits";
}
return self;
}


/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
}

- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}


- (void)dealloc {
[navigationPicker release];
[super dealloc];
}

#pragma mark -
#pragma mark IBAction

- (IBAction) btnClickContact : (id) sender{
UIAlertView *alertBox = [[UIAlertView alloc] initWithTitle: @""
message:@""
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Call", @"Email", @"Add to Contacts", @"Linkedin", @"Twitter", nil ];
[alertBox show];
[alertBox release];
}

#pragma mark -
#pragma mark UIAlertViewDelegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

switch (buttonIndex) {
case 0:

break;
case 1:
[[UIApplication sharedApplication] openURL:
[NSURL URLWithString:@"tel:+61413393190"]];
break;
case 2:
[self sendMail];
break;
case 3:
[self addNewContact];

break;
case 4:
NSLog(@"");
WebViewController *aWebView = [[WebViewController alloc] initWithNibName: nil
bundle: nil
webURL: @"http://m.linkedin.com/members/48410032/profile"
webTitle: @"LinkedIn"];
[self.navigationController pushViewController: aWebView animated:YES];
[aWebView release];
break;
case 5:
NSLog(@"");
WebViewController *webViewTwitter = [[WebViewController alloc] initWithNibName: nil
bundle: nil
webURL: @"http://www.twitter.com/daveNZdeveloper"
webTitle: @"Twitter"];
[self.navigationController pushViewController: webViewTwitter
animated: YES];
[webViewTwitter release];
break;
default:
break;
}
}

- (void)alertViewCancel:(UIAlertView *)alertView{
NSLog(@"cancel");
}

#pragma mark -
#pragma mark Private Methods

- (void) sendMail{

NSString * to_s = @"dvd.nzl@gmail.com" ;
NSString * subject_s = @"Contact from using Green Block Game" ;
NSString * body_s = @"" ;

NSString *mailString = [NSString stringWithFormat:@"mailto:?to=%@&subject=%@&body=%@",
[to_s stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],
[subject_s stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],
[body_s stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:mailString]];
}

- (void)newPersonViewController:(ABNewPersonViewController *)newPersonViewController
didCompleteWithNewPerson:(ABRecordRef)person{
NSLog(@"Complete");
[navigationPicker dismissModalViewControllerAnimated: YES];
}

- (void) addNewContact{

ABRecordRef aRecord = ABPersonCreate();

CFErrorRef anError = NULL;

ABRecordSetValue(aRecord, kABPersonFirstNameProperty, CFSTR("Dave"), &anError);
ABRecordSetValue(aRecord, kABPersonLastNameProperty, CFSTR("van Dugteren"), &anError);
ABRecordSetValue(aRecord, kABPersonOrganizationProperty, CFSTR("Senior iPhone Developer"), &anError);

ABMutableMultiValueRef multiHome = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);

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

[addressDictionary setObject:@"Sydney" forKey:(NSString *)kABPersonAddressCityKey];

[addressDictionary setObject:@"NSW" forKey:(NSString *)kABPersonAddressStateKey];

[addressDictionary setObject:@"2007" forKey:(NSString *)kABPersonAddressZIPKey];

[addressDictionary setObject:@"Australia" forKey:(NSString *)kABPersonAddressCountryCodeKey];

[addressDictionary setObject:@"Australia" forKey:(NSString *)kABPersonAddressCountryKey];

ABMultiValueAddValueAndLabel(multiHome, addressDictionary, kABHomeLabel, NULL);

ABRecordSetValue(aRecord, kABPersonAddressProperty, multiHome, &anError);

ABMutableMultiValueRef homePage = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(homePage, CFSTR("www.kiappsta.com"), kABPersonHomePageLabel, NULL);
ABMultiValueAddValueAndLabel(homePage, CFSTR("www.davevandugteren.net"), kABPersonHomePageLabel, NULL);
ABMultiValueAddValueAndLabel(homePage, CFSTR("http://www.linkedin.com/in/davidvandugteren"), kABPersonHomePageLabel, NULL);
ABRecordSetValue(aRecord, kABPersonURLProperty, homePage, NULL);
CFRelease(homePage);

ABMutableMultiValueRef phoneNumber = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(phoneNumber, CFSTR("+6104 133 93190"), kABPersonPhoneMobileLabel, NULL);
ABMultiValueAddValueAndLabel(phoneNumber, CFSTR("+6104 133 93190"), kABPersonPhoneIPhoneLabel, NULL);
ABRecordSetValue(aRecord, kABPersonPhoneProperty, phoneNumber, NULL);
CFRelease(phoneNumber);

ABMutableMultiValueRef multiEmail = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(multiEmail, CFSTR("dvd.nzl@gmail.com"), kABWorkLabel, NULL);
ABRecordSetValue(aRecord, kABPersonEmailProperty, multiEmail, &anError);
CFRelease(multiEmail);

UIImageView *imageView = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @"me.png"]];


ABPersonSetImageData(aRecord, (CFDataRef) (UIImageJPEGRepresentation([imageView image], 1.0f)), &anError);

if (anError != NULL) {
NSLog(@"error while creating..");
}

ABNewPersonViewController *picker = [[ABNewPersonViewController alloc] init];
picker.newPersonViewDelegate = self;

picker.displayedPerson = aRecord;

if (navigationPicker == nil)
navigationPicker = [[UINavigationController alloc] initWithRootViewController:picker];

[self presentModalViewController:navigationPicker animated:YES];

[picker release];
}


And the interface:


#import
#import
#import

@interface CreditsViewController : UIViewController {
UIWebView *webView;
UINavigationController *navigationPicker;
}

- (IBAction) btnClickContact : (id) sender;
- (void) addNewContact;
@end