从iphone地址簿中检索地址,照片时出现问题

时间:2011-07-15 05:02:52

标签: iphone cordova addressbook

我有一项任务是开发具有电话缺口的应用程序。并在我的应用程序中检索。使用javascript。

我想要使用手机缺口在我的应用程序中使用名字,姓氏,地址,照片,主页,电子邮件,电话号码。我已经完成了以下代码来检索它们。但我没有在我的领域得到照片和地址。

我已完成所有代码以检索本机代码中的联系人。我通过创建javascript函数的jsstring对象将其发送到javascript文件。

这是我阅读地址簿数据的本机工具。

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

    /** Fetch Contact name */
    name = (NSString *)ABRecordCopyCompositeName(person);

    NSLog(@"name:%@",name);

    /** Fetch Mobile number  */
    ABMultiValueRef *phones = (ABMultiValueRef *) ABRecordCopyValue(person, kABPersonPhoneProperty);
    mobile=[[NSString alloc] initWithString:@""];
    NSString *mobileLabel = [[NSString alloc] initWithString:@""];
    for (CFIndex i=0; i < ABMultiValueGetCount(phones); i++) {
        mobileLabel = (NSString *) ABMultiValueCopyLabelAtIndex(phones, i);
        if ([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMobileLabel]) {
            mobile = (NSString *)ABMultiValueCopyValueAtIndex(phones, i);
        }
        else if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneIPhoneLabel]) {
            mobile = (NSString *)ABMultiValueCopyValueAtIndex(phones, i);
        }
    }

    /** Fetch Email addres */
    emailID =  [NSString stringWithFormat:@""];
    ABMultiValueRef emails = (ABMultiValueRef) ABRecordCopyValue(person, kABPersonEmailProperty);
    for (CFIndex i=0; i < ABMultiValueGetCount(emails); i++) {

        [emailID release];
        emailID = (NSString *)ABMultiValueCopyValueAtIndex(emails, 0);  
        NSLog(@"emailid:%@",emailID);
    }


    /** Fetch Homepage */
    ABMultiValueRef homepage = (ABMultiValueRef *) ABRecordCopyValue(person, kABPersonURLProperty);
    NSString *homepageLabel =  [NSString stringWithFormat:@""];
    homepageID =  [NSString stringWithFormat:@""];
    for(CFIndex i=0; i < ABMultiValueGetCount(homepage);i++)
    {
        homepageLabel = (NSString *) ABMultiValueCopyLabelAtIndex(homepage, i);
        if([homepageLabel isEqualToString:(NSString *)kABPersonHomePageLabel]){
            homepageID = (NSString *)ABMultiValueCopyValueAtIndex(homepage, i);
            NSLog(@"homepage:%@",homepageID);
        }
    }

    /** Fatch Image */
    NSData  *imgData = (NSData *)ABPersonCopyImageData(person);

    // call the js to fill the all contact infromation
    {
        NSString* jsString = [[NSString alloc] initWithFormat:@"fillContactInfo('%@','%@','%@','%@','%@');",name ,mobile ,emailID, homepageID, imgData];
        NSLog(@"jstring:%@",jsString);

        [webView stringByEvaluatingJavaScriptFromString:jsString];
        [jsString release];
    }

    [emailID release];
    [mobile release];
    [mobileLabel release];
    [homepageID release];
    [homepageLabel release];
    return YES;
}


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

            /*
            * Set up an ABMultiValue to hold the address values; copy from address
            * book record.
            */
            ABMultiValueRef multi = ABRecordCopyValue(person, kABPersonAddressProperty);
            // Set up an NSArray and copy the values in.
            NSArray *theArray = [(id)ABMultiValueCopyArrayOfAllValues(multi) autorelease];
            // Figure out which values we want and store the index.
            const NSUInteger theIndex = ABMultiValueGetIndexForIdentifier(multi, identifier);
            // Set up an NSDictionary to hold the contents of the array.
            NSDictionary *theDict = [theArray objectAtIndex:theIndex];
            // Set up NSStrings to hold keys and values.  First, how many are there?
            const NSUInteger theCount = [theDict count];
            NSString *keys[theCount];
            NSString *values[theCount];
            // Get the keys and values from the CFDictionary.  Note that because
            // we're using the "GetKeysAndValues" function, you don't need to
            // release keys or values.  It's the "Get Rule" and only applies to
            // CoreFoundation objects.
            [theDict getObjects:values andKeys:keys];
            // Set the address label's text.

            addressid = [NSString stringWithFormat:@"%@, %@",
                [theDict objectForKey:(NSString *)kABPersonAddressStreetKey],
                [theDict objectForKey:(NSString *)kABPersonAddressCityKey]];


    // call the js to fill the all contact infromation
    {
        NSString* jsString = [[NSString alloc] initWithFormat:@"fillContactAddress('%@');",addressid];

        NSLog(@"jscript:%@",jsString);

        [webView stringByEvaluatingJavaScriptFromString:jsString];
        [jsString release];
    }

    [[self appViewController] dismissModalViewControllerAnimated:YES];

        // If they didn't pick an address, return YES here to keep going.
        return NO;


}

此时,我在运行应用程序时获取NSLog控制台中的名称,地址,电话号码,电子邮件地址,主页,照片。以下是我的javascript函数来检索它们:

function fillContactInfo(name,mobile,email,homepage,imgData){

    mobile = mobile.replace(" ", "").replace("(", "").replace(")", "").replace("-", "").replace("+", "").replace("-", "").replace("-", "");

    user.contactname = name;
    user.contactemail = email;
    user.contactphone = mobile;
    user.contactimage = imgData;
    user.contacturl = homepage;

    document.getElementById("contactname").value = user.contactname;        
    document.getElementById("contactemail").value = user.contactemail;
    document.getElementById("contactphone").value = user.contactphone;
    document.getElementById("contactimage").src = "data:image/png;base64,user.contactimage";
    document.getElementById("contacturl").value = user.contacturl;

}


function fillContactAddress(address){

    user.address = address;

    document.getElementById("contactaddress").value = user.contactaddress;

}

在上面的代码中,我开发了在我的应用程序中检索联系人。但是当我在那时运行应用程序时,我只能在我的视图中获得姓名,电子邮件,电话号码和主页。但我不能得到地址和照片。

我也尝试将名称,电话号码,电子邮件和主页放在

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)picker shouldContinueAfterSelectingPerson:(ABRecordRef)person
                            property:(ABPropertyID)property
            identifier:(ABMultiValueIdentifier)identifier

类方法,但它没有给我结果。

我有html图像标签来显示联系人列表的图像。

1 个答案:

答案 0 :(得分:3)

ABMultiValue中address的内容是NSDictionary结构。您可以使用以下键来检索所需的值。详细信息位于this official document

  • const CFStringRef kABPersonAddressStreetKey;
  • const CFStringRef kABPersonAddressCityKey;
  • const CFStringRef kABPersonAddressStateKey;
  • const CFStringRef kABPersonAddressZIPKey;
  • const CFStringRef kABPersonAddressCountryKey;
  • const CFStringRef kABPersonAddressCountryCodeKey;

关于图像数据,我无法清楚地了解如何在javascript中处理它。也许您需要使用base64方式传输这些二进制数据。

编辑:

地址示例:

  ABMultiValueRef aMulti = ABRecordCopyValue(srcRecord, kABPersonAddressProperty);
  if (aMulti != nil) {
     int aMultiCount = ABMultiValueGetCount(aMulti);
     for (int i = 0; i < aMultiCount; ++i) {
         NSDictionary *abDict = (NSDictionary *)ABMultiValueCopyValueAtIndex(aMulti, i); 

         NSString *street = [abDict objectForKey:(NSString *)kABPersonAddressStreetKey];
         NSString *city = [abDict objectForKey:(NSString *)kABPersonAddressCityKey];

         // obtains other properties ....

         [abDict release];
     }
     CFRelease(aMulti);
  }