Thursday, July 12, 2012

fetch contact list from iPhone and send in JSON format



-(void)showPeoplePickerController
{
    
    ABAddressBookRef addressBook = ABAddressBookCreate();
    
    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
    CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
    [menuArray removeAllObjects];
    if(menuArray ==nil)
        menuArray = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < nPeople; i++) 
    {
        NSMutableDictionary *localDic=[[NSMutableDictionary alloc]init];
        ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, i);
        
        //get firstname
        CFStringRef firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
        [localDic setObject:[NSString stringWithFormat:@"%@",firstName] forKey:@"first_name"];
        
        //get lastname
        CFStringRef lastName = ABRecordCopyValue(ref, kABPersonLastNameProperty);
        [localDic setObject:[NSString stringWithFormat:@"%@",lastName] forKey:@"last_name"];
        
        
        //get Phonenumber
        ABMutableMultiValueRef phoneNumbers = ABRecordCopyValue(ref, kABPersonPhoneProperty);
        CFTypeRef mobileNumber;
        NSString *mobileLabel;
        
        
        for (CFIndex i = 0; i < ABMultiValueGetCount(phoneNumbers); i++) {
            mobileLabel=[NSString stringWithFormat:@"%@",ABMultiValueCopyLabelAtIndex(phoneNumbers,i)];
            
            if([mobileLabel isEqualToString:@"_$!<Mobile>!$_"])
            {
                mobileNumber = ABMultiValueCopyValueAtIndex(phoneNumbers,i);
                [localDic setObject:[NSString stringWithFormat:@"%@",mobileNumber] forKey:@"Phone_Mobile"];
                
            }
            else if([mobileLabel isEqualToString:@"iPhone"])
            {
                mobileNumber = ABMultiValueCopyValueAtIndex(phoneNumbers,i);
                [localDic setObject:[NSString stringWithFormat:@"%@",mobileNumber] forKey:@"Phone_iPhone"];
            }
            else if([mobileLabel isEqualToString:@"_$!<Home>!$_"])
            {
                mobileNumber = ABMultiValueCopyValueAtIndex(phoneNumbers,i);
                [localDic setObject:[NSString stringWithFormat:@"%@",mobileNumber] forKey:@"Phone_Home"];
                
            }
            else if([mobileLabel isEqualToString:@"_$!<Work>!$_"])
            {
                mobileNumber = ABMultiValueCopyValueAtIndex(phoneNumbers,i);
                [localDic setObject:[NSString stringWithFormat:@"%@",mobileNumber] forKey:@"Phone_Work"];
                break;
            }
            
        }
        
        
        
        //get EmailId
        ABMutableMultiValueRef EmailIds = ABRecordCopyValue(ref, kABPersonEmailProperty);
        CFTypeRef EmailId;
        NSString *EmailLabel;
        
        
        for (CFIndex i = 0; i < ABMultiValueGetCount(EmailIds); i++) {
            EmailLabel=[NSString stringWithFormat:@"%@",ABMultiValueCopyLabelAtIndex(EmailIds,i)];
            
            if([EmailLabel isEqualToString:@"_$!<Home>!$_"])
            {
                EmailId = ABMultiValueCopyValueAtIndex(EmailIds,i);
                [localDic setObject:[NSString stringWithFormat:@"%@",EmailId] forKey:@"Email_Home"];
                
            }
            else if([EmailLabel isEqualToString:@"_$!<Work>!$_"])
            {
                EmailId = ABMultiValueCopyValueAtIndex(EmailIds,i);
                [localDic setObject:[NSString stringWithFormat:@"%@",EmailId] forKey:@"Email_Work"];
                break;
            }
        }
        
        
        //get Social Datas..
        ABMultiValueRef SocialProperty = ABRecordCopyValue(ref, kABPersonSocialProfileProperty);
        
        for (CFIndex i = 0; i < ABMultiValueGetCount(SocialProperty); i++) 
        {
            
            CFDictionaryRef dict = ABMultiValueCopyValueAtIndex(SocialProperty, i);
            NSString *service = [NSString stringWithFormat:@"%@",CFDictionaryGetValue(dict, @"service")];
           // NSLog(@"%@",dict);
            if([service isEqualToString:@"twitter"])
                [localDic setObject:[NSString stringWithFormat:@"%@",CFDictionaryGetValue(dict, @"username")] forKey:@"twitter_uname"];
            else if([service isEqualToString:@"facebook"])
                [localDic setObject:[NSString stringWithFormat:@"%@",CFDictionaryGetValue(dict, @"username")] forKey:@"facebook_uname"];
            else if([service isEqualToString:@"flickr"])
                [localDic setObject:[NSString stringWithFormat:@"%@",CFDictionaryGetValue(dict, @"username")] forKey:@"flickr_uname"];
            else if([service isEqualToString:@"linkedin"])
                [localDic setObject:[NSString stringWithFormat:@"%@",CFDictionaryGetValue(dict, @"username")] forKey:@"linkedin_uname"];
            
            CFRelease(dict);
        }
        
        //Get Address Detail Info..
        
        ABMultiValueRef addressProperty = ABRecordCopyValue(ref, kABPersonAddressProperty);
        
        for (CFIndex i = 0; i < ABMultiValueGetCount(addressProperty); i++) 
        {
            
            CFDictionaryRef dict = ABMultiValueCopyValueAtIndex(addressProperty, i);
            
            CFStringRef myCountryCode = CFDictionaryGetValue(dict, kABPersonAddressCountryCodeKey);
            [localDic setObject:[NSString stringWithFormat:@"%@",myCountryCode] forKey:@"Country_Code"];
            
            CFRelease(dict);
        }
        
        [menuArray addObject:localDic];
        [localDic release];
    }
   // NSLog(@"menuArray  %@",menuArray);
    
    NSDictionary *data = [NSDictionary dictionaryWithObjectsAndKeys:menuArray,@"contacts_info", nil];
    NSError *writeError = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:data options:NSJSONWritingPrettyPrinted error:&writeError];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
   // NSLog(@"JSON Output: %@", jsonString);
    
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:@"YOUR REQUEST URL"]];
    //NSLog(@"request..%@",request);
    NSURLConnection *connection = [[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];
    [request release];
    [jsonString release];
    if (connection) {
       // NSLog(@"connection established");
    }
    
}

No comments:

Post a Comment