取得用戶的電話本

  //取得用戶的電話本 代碼

-(void)getUserAddressBook{

  //reDic = {"7":"數碼先鋒","1":"最愛街拍"} //打開電話本數據庫   

   ABAddressBookRef addressRef=ABAddressBookCreate();     

   //返回所有聯繫人到一個數組中    

CFArrayRef personArray = ABAddressBookCopyArrayOfAllPeople(addressRef);   

 NSMutableDictionary *jsonDictionary = [[NSMutableDictionary alloc]init];   

 //循環讀取每個聯繫人   

 for (int i =0;i<ABAddressBookGetPersonCount(addressRef);i++){     

           //得到當前聯繫人      

  ABRecordRef personRef=CFArrayGetValueAtIndex(personArray, i);                //==============================================================================         

  CFStringRef firstname=ABRecordCopyValue(personRef, kABPersonFirstNameProperty);     

   CFStringRef lastname=ABRecordCopyValue(personRef, kABPersonLastNameProperty);

//CFStringRef userPhone = ABRecordCopyValue(personRef,kABPersonPhoneProperty );

NSString *allName;if([firstname isEqualToString:@""]||firstname==nil){if([lastname isEqualToString:@""]||lastname==nil){

allName=@"空";

}

  }else if([lastname isEqualToString:@""]||lastname==nil){allName=[NSString stringWithFormat:@"%@",firstname];

}else if([firstname isEqualToString:@""]||firstname==nil){allName=[NSString stringWithFormat:@"%@",lastname];

}else{

allName=[NSString stringWithFormat:@"%@+%@",firstname,lastname];

}       // NSLog(@"%d===%@--%@.",i,(NSString*)firstname,(NSString*)lastname);//NSLog(@"allName=%@",allName);        //==============================================================================      //  NSLog(@"%d===%@",i,(NSString*)userPhone);                //用於循環計數        int j;        //用於保存有多項的屬性值        ABMultiValueRef values ;                //==============================================================================      

  //循環讀出該項的所有值,如果需要修改讀出其它具有多項值的屬性,只需修改KAB...Property        //具有多項值的屬性有電話,地址,email     

   values = (ABMultiValueRef) ABRecordCopyValue(personRef , kABPersonPhoneProperty);

//NSLog(@"values===%@",values);

NSString *getPhoneNum;  

      for(j = 0 ;j < ABMultiValueGetCount(values); j++)        {         

     getPhoneNum = [NSString stringWithFormat:@"%@", (NSString *)ABMultiValueCopyValueAtIndex(values, j)];                    }if(ABMultiValueGetCount(values)==0){

getPhoneNum=@"空";

}//NSLog(@"getPhoneNum=====%@",getPhoneNum);       // values = (ABMultiValueRef) ABRecordCopyValue(personRef , kABPersonEmailProperty);//        for( j = 0 ;j < ABMultiValueGetCount(values); j++)//        {  //            NSLog(@"1-%@", (NSString *)ABMultiValueCopyValueAtIndex(values, j));//            //        }//        //        values = (ABMultiValueRef) ABRecordCopyValue(personRef , kABPersonAddressProperty);//        for( j = 0 ;j < ABMultiValueGetCount(values); j++)//        {  //            NSLog(@"2-%@", (NSString *)ABMultiValueCopyValueAtIndex(values, j));//            //        }[jsonDictionary setObject:getPhoneNum forKey:allName] ;        //==============================================================================            }
//NSLog(@"addressbook=%@",jsonDictionary);    NSString *str = [jsonDictionary JSONRepresentation];NSLog(@"str===%@",str);  //  NSString *d = [str JSONValue];[jsonDictionary release];
  //  NSString *str = [jsonDictionary JSONRepresentation];//    NSMutableDictionary *d = [jsonDictionary JSONValue];//    NSLog(@"%@" , [d description]);       // [dlist release];//NSLog(@"addressbook=%@",jsonDictionary);}

posted @ 2010-11-25 14:11 SsQq 閱讀(116) 評論(0) 編輯
發佈了13 篇原創文章 · 獲贊 1 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章