2011-07-15 54 views
0

我有一個任務,開發與電話差距的應用程序。並檢索我的application.using JavaScript。從iPhone地址簿中檢索地址,照片時出現問題

我想在我的應用程序中使用手機間隙的名字,姓氏,地址,照片,主頁,電子郵件,電話號碼。我已經完成了以下代碼來檢索它們。但我沒有在我的領域獲得照片和地址。

我已經完成了所有代碼來檢索本機代碼中的聯繫人。我通過創建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圖像標籤來顯示聯繫人列表的圖像。

回答

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); 
    } 
+0

可以請你解釋一下樣品。如何取地址。我可以正確理解你的答案。 –

+0

感謝您的回覆。但我目前也有一個問題要獲得形象。我已經將圖像轉換爲數據,並在java腳本中獲取這些數據,但是當我將它轉換爲bse64 png時,它不會給我圖像。所以請幫助我,並提供示例,如果有可能的話。 –

+0

對不起。我不知道java腳本如何處理圖像。但是,當我將圖像數據轉換爲vcf或XML格式時,它們通過將二進制數據傳輸到base64字符串可以很好地工作。 – AechoLiu