0

I have a task to develop the application with phone gap. And retrieve in my application.using javascript.

I want first name,last name,address,photo,home page,email,phone no in my application using phone gap. i have done the following code to retrive them. but i am not getting photo and address in my field.

I have done all code to retrieve the contact in native code. and i am sending it to the javascript file by creating the jsstring object of javascript function.

This is my native implement to read address book data.

-(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;


}

At this point i am getting the name,address,phone number,email address,home page,photo in NSLog console when i run the application. Following is my javascript function to retrive them:

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;

}

In the above code I have develop to retrive the contact in my application. But when i run the application at that time i only get name, email, phone number, and home page in my view. but i cant get address, and photo in my view.

I also have try to put the name, phone number, email, and home page in the

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

class method but it is not giving me result.

And I have html image tag to display image of the contact list.

Deepak Danduprolu
  • 44,595
  • 12
  • 101
  • 105
Hrushikesh Betai
  • 2,227
  • 5
  • 33
  • 63

1 Answers1

3

The content of address inside the ABMultiValue is a NSDictionary structure. You can use the following keys to retrieve desired values. The detail is in this official document.

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

About image data, I cannot get a clear picture about how it will be processed in javascript. Maybe you need to transfer those binary data with base64 manner.

Edit:

An address example:

  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);
  }
AechoLiu
  • 17,522
  • 9
  • 100
  • 118
  • Can you please explain with sample. to how to fetch address. i can understand properly your answer. – Hrushikesh Betai Jul 15 '11 at 10:31
  • Thanks for reply. but i at present also have one problem to get image. i have already convert the image to data and at the java script i have getting this data but when i am converting it with bse64 png it is not giving me image. so please help me and provide me sample if it is possible. – Hrushikesh Betai Jul 20 '11 at 05:58
  • I am sorry. I don't know how the java script process the image. But when I make image data into vcf or XML formats, they works well by transfering binary data to base64 strings. – AechoLiu Jul 26 '11 at 00:58