Powered By Blogger

Friday, May 27, 2011

how to fetch contacts from iPhone using programmatically?

 Hi everyone,
This will help you to fetch contact information from your iOS Device Address book. First you have to include "AddressBookUI.framework" into your project and then you have to include "UINavigationControllerDelegate","ABPeoplePickerNavigationControllerDelegate" into header part of the controller. You have to also import "#import <AddressBookUI/AddressBookUI.h>" into current controller's header part.

#pragma mark -
#pragma mark Button Action Event

-(IBAction)callAddressbook:(id)sender
{
            ABPeoplePickerNavigationController *addressBookController = [[ABPeoplePickerNavigationController alloc]init];
            addressBookController.peoplePickerDelegate = self;
            addressBookController.delegate = self;
            addressBookController.navigationBar.tintColor = [UIColor blackColor];
            addressBookController.searchDisplayController.searchBar.tintColor = [UIColor blackColor];
            addressBookController.searchDisplayController.searchBar.backgroundColor = [UIColor blackColor];
            [self presentModalViewController:addressBookController animated:YES];
            [addressBookController release];
            addressBookController = nil;
}

#pragma mark -
#pragma mark ABPeoplePickerNavigationController Delegate Method


- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker // called when address book closed
{
    [peoplePicker dismissModalViewControllerAnimated:YES];
}

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker  // called when select any contact from address book
      shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
  
    ABMultiValueRef addresses = [(NSMutableDictionary *)ABRecordCopyValue(person, kABPersonAddressProperty) autorelease];
    NSArray *addressesArray = [(NSArray *)ABMultiValueCopyArrayOfAllValues(addresses) autorelease];
    NSDictionary *dictAddress = [addressesArray objectAtIndex:0];
  
    ABMultiValueRef phone = [(NSMutableDictionary *)ABRecordCopyValue(person, kABPersonPhoneProperty) autorelease];
    NSArray *phoneArray = [(NSArray *)ABMultiValueCopyArrayOfAllValues(phone) autorelease];
    NSMutableString *strPhone = [NSMutableString string];
  
    for (int i=0; i<[phoneArray count]; i++)
    {
        [strPhone appendString:[NSString stringWithFormat:@"%@,",[phoneArray objectAtIndex:i]]];
    }
  
    [ApplicationData sharedInstance].selectedContact.contactId = (NSInteger) ABRecordGetRecordID(person);
    [ApplicationData sharedInstance].selectedContact.firstName = [(NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty) autorelease]==nil?@"":[(NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty)autorelease];
    [ApplicationData sharedInstance].selectedContact.lastName = [(NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty)autorelease]==nil?@"":[(NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty)autorelease];
    [ApplicationData sharedInstance].selectedContact.street = [dictAddress valueForKey:@"Street"]==nil?@"":[dictAddress valueForKey:@"Street"];
    [ApplicationData sharedInstance].selectedContact.telephone =  [strPhone stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@","]];
    [ApplicationData sharedInstance].selectedContact.city = [dictAddress valueForKey:@"City"]==nil?@"":[dictAddress valueForKey:@"City"];
    [ApplicationData sharedInstance].selectedContact.postcode = [dictAddress valueForKey:@"ZIP"]==nil?@"":[dictAddress valueForKey:@"ZIP"];
    [ApplicationData sharedInstance].selectedContact.state = [dictAddress valueForKey:@"State"]==nil?@"":[dictAddress valueForKey:@"State"];
    [ApplicationData sharedInstance].selectedContact.country = [dictAddress valueForKey:@"CountryCode"]==nil?@"":[dictAddress valueForKey:@"CountryCode"];
  
  
    [ApplicationData sharedInstance].obj_sendOrder.recipientName = [NSString stringWithFormat:@"%@ %@",[ApplicationData sharedInstance].selectedContact.firstName,[ApplicationData sharedInstance].selectedContact.lastName];
    [ApplicationData sharedInstance].obj_sendOrder.street = [ApplicationData sharedInstance].selectedContact.street;
    [ApplicationData sharedInstance].obj_sendOrder.city = [ApplicationData sharedInstance].selectedContact.city;
    [ApplicationData sharedInstance].obj_sendOrder.zip = [ApplicationData sharedInstance].selectedContact.postcode;
    [ApplicationData sharedInstance].obj_sendOrder.state = [ApplicationData sharedInstance].selectedContact.state;
    [ApplicationData sharedInstance].obj_sendOrder.country = [ApplicationData sharedInstance].selectedContact.country;
  
    [ApplicationData sharedInstance].obj_sendOrder.contactType = @"Phone";
  
    dictAddress = nil;
    addresses = nil;
    addressesArray = nil;
  
    phone = nil;
    phoneArray = nil;
    strPhone = nil;
  
    [peoplePicker dismissModalViewControllerAnimated:YES];
    return NO;
}

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
      shouldContinueAfterSelectingPerson:(ABRecordRef)person
                                property:(ABPropertyID)property
                              identifier:(ABMultiValueIdentifier)identifier // called to show detail of contact
{
    return NO;
}
Still if you have questions regarding this then you can ask me anytime.

Thanks & Regards,
Nilesh Prajapati

Saturday, May 21, 2011

AirPrint in iPhone SDK

Hi Everyone,
By using below code , you'll be able to print your view using AirPrint in iPhone SDK.

-(void)sendToPrinter
{

    NSString *filename = [appDelegate.pdfFilePath lastPathComponent];   
    filename = [filename stringByReplacingOccurrencesOfString:@".pdf" withString:@""];
    NSString *path = [[NSBundle mainBundle] pathForResource:filename ofType:@"pdf"];

    NSData *myData = [NSData dataWithContentsOfFile:path];
    UIPrintInteractionController *print = [UIPrintInteractionController sharedPrintController];

    print.delegate = self;
    UIPrintInfo *printInfo = [UIPrintInfo printInfo];
    printInfo.outputType = UIPrintInfoOutputGeneral;
    printInfo.jobName = [appDelegate.pdfFilePath lastPathComponent];
    printInfo.duplex = UIPrintInfoDuplexLongEdge;
    print.printInfo = printInfo;
    print.showsPageRange = YES;
    print.printingItem = myData;
    UIViewPrintFormatter *viewFormatter = [self.view viewPrintFormatter];
    viewFormatter.startPage = 0;
    print.printFormatter = viewFormatter;
   

    UIPrintInteractionCompletionHandler completionHandler = ^(UIPrintInteractionController *printInteractionController, BOOL completed, NSError *error) {};
   
    [print presentAnimated:YES completionHandler:completionHandler];
}

if you have query then send it to me. 

Thanks and Regards,
 Nilesh Prajapati