2010-03-26 108 views
1

我試圖從iOS上現有的AddressBook聯繫人獲取即時消息帳戶信息。我瀏覽聯繫人並獲得具有即時消息傳遞價值的聯繫人,但我無法閱讀jabber地址。從ABRecordRef檢索即時消息信息

abArray = (NSArray *)ABAddressBookCopyArrayOfAllPeople(ABAddressBookCreate()); 

for(int i=0 ; i<[abArray count];i++) 
{ 
    ABRecordRef record = [abArray objectAtIndex:i]; 

    ABMutableMultiValueRef multi = ABRecordCopyValue(record, kABPersonInstantMessageProperty); 

    for(CFIndex x=0;x<ABMultiValueGetCount(multi);x++) 
    { 
    CFDictionaryRef dict = ABMultiValueCopyValueAtIndex(multi, x); 
    CFStringRef jabber = CFDictionaryGetValue(dict, kABPersonInstantMessageServiceJabber); 

    if(CFDictionaryContainsKey(dict, kABPersonInstantMessageServiceJabber)) 
    { 
    NSLog(@"yes"); 
    } 
    else { 
    NSLog(@"no"); 
    } 

    // only to make it possible to log to console 
    NSString *jaab = (NSString *)jabber; 
    NSLog(@"jabber adress: %@" , jaab); 
    } 
    CFRelease(dict); 
    } 
} 

我在做什麼錯?

+1

「我通過觸點走,我得到它在IM中的條目接觸,但我無法讀取的jabber地址。」爲什麼不呢?會發生什麼呢? – 2010-03-26 09:05:18

回答

0
for(int i=0 ; i<[abArray count];i++) 
{ 
    ABRecordRef record = [abArray objectAtIndex:i]; 
    ABMutableMultiValueRef multi = ABRecordCopyValue(record, kABPersonInstantMessageProperty); 

    for(CFIndex x=0;x<ABMultiValueGetCount(multi);x++) 
    { 
     CFDictionaryRef dict = ABMultiValueCopyValueAtIndex(multi, x); 
     CFStringRef jabber; 


     //Use this piece of code to print the dict to log and check 

     NSDictionary *nsdict = (NSDictionary *)dict; 
     NSString *jabberID = [NSString stringWithString:@""]; 
     NSLog(@"Dict: %@", nsdict); 
     if([[nsdict valueForKey:@"service"] isEqualToString:@"Jabber"]){ 
      jabberID = [nsdict valueForKey:@"username"]; 
     } 
     //Code to print dict to log ends here. Comment the whole piece if not needed. 


     if(CFStringCompare((CFStringRef)@"jabber", CFDictionaryGetValue(dict, @"service"), 0)) 
     { 
      NSLog(@"yes"); 
      jabber = CFDictionaryGetValue(dict, @"username"); 

      // only to make it possible to log to console 
      NSString *jaab = (NSString *)jabber; 
      NSLog(@"jabber adress: %@" , jaab); 
     } 
     else { 
      NSLog(@"no"); 
     } 

    } 
    //CFRelease(dict); 
} 
+0

CFDictionaryContainsKey(字典,kABPersonInstantMessageServiceJabber) 字典詞典不包含密鑰kABPersonInstantMessageServiceJabber。但是如果你嘗試了上面的代碼,你可能會更好地理解字典中的鍵值對。 – 2010-03-26 14:38:27