直接从应用程序转到地址簿/联系电话

时间:2011-11-29 07:09:42

标签: iphone objective-c ios xcode

我想创建一个应用程序,并希望在我触摸/按下特定文本字段或按钮时直接访问联系人号码,然后使用所选联系人号码返回我的应用程序。我怎样才能做到这一点。

3 个答案:

答案 0 :(得分:1)

您需要在.h文件中添加ABPeoplePickerNavigationControllerDelegate委托

并在.m文件中记下以下三种方法:

#pragma mark People Picker Delegate Methods

- (void)peoplePickerNavigationControllerDidCancel:
  (ABPeoplePickerNavigationController *)peoplePicker {
[peoplePicker dismissModalViewControllerAnimated:YES];
[peoplePicker autorelease];
     }

- (BOOL)peoplePickerNavigationController:
  (ABPeoplePickerNavigationController *)peoplePicker 
  shouldContinueAfterSelectingPerson:(ABRecordRef)person {



return YES;
   }


- (BOOL)peoplePickerNavigationController:
(ABPeoplePickerNavigationController *)peoplePicker 
  shouldContinueAfterSelectingPerson:(ABRecordRef)person
                            property:(ABPropertyID)property
                          identifier:(ABMultiValueIdentifier)valueID{


ABPropertyType type = ABPersonGetTypeOfProperty(property);
if (type==kABMultiDictionaryPropertyType) {


    ABMutableMultiValueRef multi = ABRecordCopyValue(person, property);

    CFIndex index = ABMultiValueGetIndexForIdentifier(multi, valueID);
    CFDictionaryRef dic = ABMultiValueCopyValueAtIndex(multi, index);
    CFStringRef street = CFDictionaryGetValue(dic, kABPersonAddressStreetKey);

    NSString* StreetName =(NSString*)street;
    streetNameText.text=StreetName;
    NSLog(@"StreetName:%@",StreetName);
    NSRange range = NSMakeRange (0, 5);
    NSLog (@"Beer shortname: %@", [StreetName substringWithRange:range]);
    int val = [StreetName intValue];
    NSLog(@"StreetName:%d",val);
    NSString *newChange = [[NSString alloc] initWithFormat:@"%d", val];
    streetNOText.text = newChange;

    [newChange release];
    CFRelease(dic);
    CFRelease(multi);
}





[self dismissModalViewControllerAnimated:YES];
return NO;

     }

答案 1 :(得分:0)

我无法给你整个代码。完成以下步骤: 1.在单击按钮上转到必须具有tableview的新视图 2.在该视图中获取地址簿

获取地址簿

  1. 添加以下框架: 地址簿, AddressBookUI

  2. 在您的视图中导入它们

  3. 要从地址簿获取联系人,请参阅以下教程

    http://developer.apple.com/library/ios/#documentation/ContactData/Conceptual/AddressBookProgrammingGuideforiPhone/Chapters/QuickStart.html#//apple_ref/doc/uid/TP40007744-CH2-SW1

    我知道这不是您问题的完整解决方案,但此链接将帮助您完成剩下的任务......祝您好运

答案 2 :(得分:0)

这个方法可以帮到你,只要你想从地址簿中提取记录就调用这个方法。 n当然添加框架AddressBook,AddressBookUI

首先创建数据库(如果您愿意,可以根据您的要求将其存储到NSMutable Array而不是数据库中。)

-(void)fetchRecordsFromAddressBook
{
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);

//NSArray *addresses = (NSArray *) ABAddressBookCopyArrayOfAllPeople(addressBook);

//  [arrayContacts removeAllObjects];

[self emptyDataContext];

for (int i = 0; i < nPeople; i++)
{


    ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, i);


    //////////////////  get first name  ///////////////////

    CFStringRef firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);

    CFStringRef lastName = ABRecordCopyValue(ref, kABPersonLastNameProperty);

    CFStringRef nickName = ABRecordCopyValue(ref, kABPersonNicknameProperty);

    CFStringRef middleName = ABRecordCopyValue(ref, kABPersonMiddleNameProperty);


    //////////////////  get image  ///////////////////

//      ABMultiValueRef ContactImage = (ABMultiValueRef) ABRecordCopyValue(ref,kABPersonImageFormatThumbnail);


    NSData *data=nil;

//  NSLog(@"Image Testing is : %@",ref);

    if(ABPersonHasImageData(ref))
    {
        data = [(NSData *) ABPersonCopyImageData(ref) autorelease];
        if(data)
        {
        //  NSLog(@"Im Testing is : %@",data);

            //image = [[UIImage alloc] initWithData:data];
        }
    }

//      NSLog(@"Image is : %@",ContactImage);
//      NSLog(@" Name is : %@",firstName);



    //////////////////  get email  ///////////////////

    ABMultiValueRef emails = (ABMultiValueRef) ABRecordCopyValue(ref, kABPersonEmailProperty);

    NSString *emailID=@"";

    if(ABMultiValueGetCount(emails)>=1)
    {
        emailID = (NSString *)ABMultiValueCopyValueAtIndex(emails,0);
    }


    //////////////////  get phone number  ///////////////////

    ABMultiValueRef phones = (ABMultiValueRef) ABRecordCopyValue(ref, kABPersonPhoneProperty);

    NSString *phone=@"";

    NSString *homeNumber = @"";

    NSString *worknumber = @"";

    if(ABMultiValueGetCount(phones)>=1)
    {
        //int ph = [ABMultiValueCopyValueAtIndex(phones, 0) intValue];
        phone = (NSString *)ABMultiValueCopyValueAtIndex(phones,0);
    }
//  NSLog(@"%@",(NSString*)phone);  


    if(ABMultiValueGetCount(phones)>=2)
    {
        homeNumber = (NSString *)ABMultiValueCopyValueAtIndex(phones,1);
    }

    if(ABMultiValueGetCount(phones)>=3)
    {
        worknumber = (NSString *)ABMultiValueCopyValueAtIndex(phones,2);
    }


    NSMutableArray *arrayContacts = [[NSMutableArray alloc] init ];


    /////////////////////////////          insert into array               ////////////////////////////

    arrayContacts = [CoreDataAPIMethods getObjectsFromContext:@"AllContactData" :@"Index" :NO :self.managedObjectContext];

    ////////////////////////////         insert Index         ///////////////////////////////
    int NewEntryID;

    if ([arrayContacts count] > 0) 
    {
        AllContactData * Contacdata = [arrayContacts objectAtIndex:0];

        NewEntryID = [Contacdata.Index intValue] +1;

    }
    else 
    {
        NewEntryID = 1;
    }

    NSString *capitalisedSentence = 
    [(NSString *)firstName stringByReplacingCharactersInRange:NSMakeRange(0,1)  
                                        withString:[[(NSString *)firstName  substringToIndex:1] capitalizedString]];

    AllContactData *Contactitem=(AllContactData *)[NSEntityDescription insertNewObjectForEntityForName:@"AllContactData" inManagedObjectContext:self.managedObjectContext];

//      NSLog(@"%@",capitalisedSentence);

    Contactitem.Name = capitalisedSentence;

    Contactitem.LastName = (NSString*)lastName;

    Contactitem.NickName = (NSString*)nickName;

    Contactitem.MiddleName = (NSString*)middleName;

    Contactitem.Email=(NSString*)emailID;

    phone = [phone stringByReplacingOccurrencesOfString:@"(" withString:@""];

    phone = [phone stringByReplacingOccurrencesOfString:@")" withString:@""];

    phone = [phone stringByReplacingOccurrencesOfString:@"+" withString:@""];

    phone = [phone stringByReplacingOccurrencesOfString:@" " withString:@""];

    phone = [phone stringByReplacingOccurrencesOfString:@"-" withString:@""];

    NSLog(@"The Replaced String is : %@", phone);

    Contactitem.PhoneNumber=(NSString*)phone;

    Contactitem.HomeNumber=(NSString*)homeNumber;

    Contactitem.WorkNumber=(NSString*)worknumber;

    Contactitem.Index = [NSNumber numberWithInt:NewEntryID];

    Contactitem.Image = data;

//      NSLog(@"Image in databse  is : %@",(NSData *)ContactImage);

    if(firstName!=nil)
    {
        CFRelease(firstName);
    }
    CFRelease(ref);

}
CFRelease(allPeople);


    /////////////////////////////         save entries              ////////////////////////////

NSError *error;
if (![managedObjectContext save:&error]) {
    // Handle the error...
}


}