Saturday, September 14, 2013

EmergencyContact

#define HelveticaRegular(s) [UIFont fontWithName:@"Helvetica" size:s]

#import <UIKit/UIKit.h>
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
#import "Emergency_Contact.h"

EmergencyContact.h

@interface EmergencyContact : UIViewController<UITableViewDelegate,UITableViewDataSource,ABPeoplePickerNavigationControllerDelegate>{

     
    IBOutlet UITextField *txtName;
    IBOutlet UITextField *txtEmail, *txtPhone;
    IBOutlet UITableView *tblContact;
    
    IBOutlet UIScrollView *scrView;
    
    NSMutableArray *tempArray;
    IBOutlet UIButton *btnSave;
}
@property(nonatomic,strong) NSString *strAllInfo;
@property(nonatomic,retain)Emergency_Contact *objEmergencybean;
@property (nonatomic, retain) NSMutableArray *contactsArray;
@property (nonatomic, retain) ABPeoplePickerNavigationController *contacts;
@property(nonatomic,retain)IBOutlet UIScrollView *scrView;

@end

EmergencyContact.m


#import "EmergencyContact.h"
#import "AppDelegate.h"
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>


@interface EmergencyContact ()

@end

@implementation EmergencyContact

AppDelegate *appDelegate;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"Emergency Contact";
       
    UIBarButtonItem *btnadd = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addContact)];
    self.navigationItem.rightBarButtonItem = btnadd;
    [btnadd release];
    
}

-(void)addContact{
    
    contacts = [[ABPeoplePickerNavigationController alloc] init];
   [contacts setPeoplePickerDelegate:self];
    
   [contacts setDisplayedProperties:[NSArray arrayWithObject:[NSNumber numberWithInt:kABPersonEmailProperty]]];
    [contacts setDisplayedProperties:[NSArray arrayWithObject:[NSNumber numberWithInt:kABPersonPhoneProperty]]];
  
[self presentModalViewController:contacts animated:YES];
}

#pragma mark - AddressBook Delegate Methods

-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person{
    return YES;
}
-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{
    
     NSString *firstName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
    NSString *lastName = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
    
         
    // Compose the full name.
    NSString *fullName = @"";
    // Before adding the first and the last name in the fullName string make sure that these values are filled in.
    if (firstName != nil) {
        fullName = [fullName stringByAppendingString:firstName];
    }
    if (lastName != nil) {
        fullName = [fullName stringByAppendingString:@" "];
        fullName = [fullName stringByAppendingString:lastName];
    }

    tempArray = [[NSMutableArray alloc] init];
    [tempArray addObject:fullName];
    
    NSArray *phones = (NSArray *)ABMultiValueCopyArrayOfAllValues(ABRecordCopyValue(person, kABPersonPhoneProperty));
    NSArray *emails = (NSArray *)ABMultiValueCopyArrayOfAllValues(ABRecordCopyValue(person, kABPersonEmailProperty));
    
      // Make sure that the selected contact has one phone at least filled in.
    if ([phones count] > 0) {
            [tempArray addObject:[phones objectAtIndex:0]];
    }
    else{
        [tempArray addObject:@"No phone number was set."];
    }
    
    // Do the same for the e-mails.
    // Make sure that the selected contact has one email at least filled in.
    if ([emails count] > 0) {
        [tempArray addObject:[emails objectAtIndex:0]];
    }
    else{
        [tempArray addObject:@"No e-mail was set."];
    }
   
    txtName.text =[tempArray objectAtIndex:0];
    txtPhone.text =[tempArray objectAtIndex:1];
    txtEmail.text =[tempArray objectAtIndex:2];
    
    [tempArray release];
    [contacts dismissModalViewControllerAnimated:YES];
    [contacts release];
    
return YES;
}
-(void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker{
[contacts dismissModalViewControllerAnimated:YES];
[contacts release];
}


- (BOOL)validateEmailWithString:(NSString*)email
{
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:email];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end

No comments:

Post a Comment