IOS通讯录开发<2> 通讯录的获取及应用

这是博主在根据需求做的一个流程图,不一定适用于其他项目,就当抛砖引玉。

我用的是http://mob.com/ 的短信验证 sdk,同样有实现短信验证功能和匹配通讯录好友功能。 

这里无论是用系统 还是第三方都不是关键

sdk获取通讯录非常方便 

_addressBookData=[SMS_SDK addressBook];
    //获得本地通讯录
    for (int i=0; i<_addressBookData.count; i++)
    {
        SMS_AddressBook* person1=[_addressBookData objectAtIndex:i];
        NSString* str1=[NSString stringWithFormat:@"%@+%@",person1.name,person1.phones];
        NSString* str2=[str1 stringByAppendingString:@"#"];
        
        NSString *phonesTemp;
        if( [[person1.phones substringToIndex:3] isEqualToString:@"+86"])
        {
            phonesTemp=[person1.phones substringFromIndex:3];
        }
        else if([[person1.phones substringToIndex:1] isEqualToString:@"1"])
        {
            phonesTemp=person1.phones;
        }
        [_other addObject:str2];// other是存储所有的号码+姓名组合 
        
        if(phonesTemp!=nil)
        {
            [uploadData addObject:phonesTemp];
        }
     }
    NSLog(@"%@",uploadData);

_addressBookData作为一个数组,储存通讯里的信息,但是信息是以对象的方式存储。

以上代码是为了获取 一个纯电话号码组成的数组。目的用以上传。(后台接口是上传手机号码数组,返回手机号码在应用中对应的用户信息)

通过网络请求进行提交,得到 手机号码对应的用户信息

if ([api cacheJson]) {
        // NSDictionary *json = [api cacheJson];
        NSLog(@"—————————读取缓存—————————" );
        NSLog(@"%@",[api cacheJson]);
        downData=[[api cacheJson] objectForKey:@"userdata"];
        for (int i=0; i<downData.count; i++) {
            NSDictionary* dict1=[downData objectAtIndex:i] ;
            NSString* phone1=[dict1 objectForKey:@"UserPhone"];
            NSString* name1=[dict1 objectForKey:@"NickName"];
            for (int j=0; j<_addressBookData.count; j++)
            {
                SMS_AddressBook* person1=[_addressBookData objectAtIndex:j];
                for (int k=0; k<person1.phonesEx.count; k++)
                {
                    if ([phone1 isEqualToString:[person1.phonesEx objectAtIndex:k]])
                    {
                        if (person1.name&&name1!=[NSNull null])
                        {
                            NSString* str1=[NSString stringWithFormat:@"%@+%@",name1,person1.name];
                            NSString* str2=[str1 stringByAppendingString:@"@"];
                            [_friendsData addObject:str2];
                            
                            NSString* str3=[NSString stringWithFormat:@"%@+%@",person1.name,person1.phones];
                            NSString* str4=[str3 stringByAppendingString:@"#"];
                            
                            [_other removeObject:str4];
                            
                        }
                    }
                }
            }
        }
        
        
        NSLog(@"获取到了%zi条通讯录信息",_addressBookData.count);
        
        NSLog(@"获取到了%zi条好友信息",_friendsData.count);

这里我使用了缓存技术,增加用户体验:)

这里的代码是为了将本地通讯录 结合 服务器返回的数据 进行筛选 

分列2个section:已加入  待邀请

  NSMutableDictionary *dict = [NSMutableDictionary dictionary];
            
            
            if (_friendsData.count>0) {
                [dict setObject:_friendsData forKey:NSLocalizedString(@"已加入好友", nil)];
            }
            if (_other.count>0) {
                [dict setObject:_other forKey:NSLocalizedString(@"待邀请好友", nil)];
            }
            self.allNames = dict;<p class="p1"><span class="s1"><span style="white-space:pre">	</span>  [</span><span class="s2">self</span><span class="s1"> </span><span class="s3">resetSearch</span><span class="s1">];</span></p>

以上是针对通讯录的筛选

 -(void)resetSearch
{
    NSMutableDictionary *allNamesCopy = [self.allNames mutableDeepCopy];
    self.names = allNamesCopy;
    NSMutableArray *keyArray = [[NSMutableArray alloc] init];

    [keyArray addObject:UITableViewIndexSearch];
    [keyArray addObjectsFromArray:[[self.allNames allKeys] 
                                   sortedArrayUsingSelector:@selector(compare:)]];
    self.keys = keyArray;
}


#pragma mark Table View Data Source Methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return [keys count];

    

}

- (NSInteger)tableView:(UITableView *)tableView 

 numberOfRowsInSection:(NSInteger)section

{

    if ([keys count] == 0)

    {

        return 0;

    }

    

    NSString *key = [keys objectAtIndex:section];

    NSArray *nameSection = [names objectForKey:key];

    return [nameSection count];

}- (UITableViewCell *)tableView:(UITableView *)tableView

         cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    NSUInteger section = [indexPath section];

    NSString *key = [keys objectAtIndex:section];

    NSArray *nameSection = [names objectForKey:key];

    

    



    NSString* str1 = [nameSection objectAtIndex:indexPath.row];

    NSString* newStr1=[str1 substringFromIndex:(str1.length-1)];

    

    NSRange range=[str1 rangeOfString:@"+"];

    NSString* str2=[str1 substringFromIndex:range.location];

    NSString* phone=[str2 stringByReplacingOccurrencesOfString:@"+" withString:@""];

    NSString *cccc = [phone substringToIndex:[phone length] - 1];

    NSString* name=[str1 substringToIndex:range.location];

    

    static NSString *CellWithIdentifier = @"CustomCellIdentifier";

    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellWithIdentifier];

    if (cell == nil)

    {

        cell=[[CustomCell alloc] init];

        cell.delegate = self;

    }

    if ([newStr1 isEqualToString:@"@"])

    {

        UIButton* btn=cell.btn;

        [btn setTitle:NSLocalizedString(@"添加", nil) forState:UIControlStateNormal];

        cell.nameDesc=[NSString stringWithFormat:@"手机联系人:%@",cccc];

       

        NSLog(@"%@",cccc);

        NSString *imgurl;

        for(int i=0;i<downData.count;i++)

        {

             if([name isEqualToString:[downData[i] objectForKey:@"NickName"]])

            {

                NSLog(@"%@",downData[i]);

                imgurl=[downData[i]objectForKey:@"UserImage"];

                if(![imgurl isEqualToString:@"0"])

                {

                [cell.imageView sd_setImageWithURL:[NSURL URLWithString:imgurl]];

                }

                else

                {

                     cell.image=[UIImage imageNamed:@"chatListCellHead@2x"];

                }

                people = [m_IMMANAGER getSomeOneInfo:[downData[i] objectForKey:@"UserID"]];

                

                if ([self didBuddyExist:[downData[i] objectForKey:@"UserID"]] && people)

                {

                    [btn setTitle:NSLocalizedString(@"聊天", nil) forState:UIControlStateNormal];

                }

                



            }

        }

        

    }

    

    if ([newStr1 isEqualToString:@"#"])

    {

        UIButton* btn=cell.btn;

        [btn setTitle:NSLocalizedString(@"邀请", nil) forState:UIControlStateNormal];

        cell.nameDesc=[NSString stringWithFormat:@"%@",cccc];

        cell.nameDescLabel.hidden=YES;

        cell.image=[UIImage imageNamed:@"chatListCellHead@2x"];

        

    }

    if(![name isEqualToString:@"(null)"])

    {

    cell.name=name;

    }

    else

    {

     cell.name=@"未知好友";

    }

    cell.index = (int)indexPath.row;

    cell.section = (int)[indexPath section];

    return cell;

}

- (NSString *)tableView:(UITableView *)tableView
titleForHeaderInSection:(NSInteger)section
{
    if ([keys count] == 0)
        return nil;

    NSString *key = [keys objectAtIndex:section];
    if (key == UITableViewIndexSearch)
        return nil;
    
    return key;
}

#pragma mark Table View Delegate Methods
- (NSIndexPath *)tableView:(UITableView *)tableView 
  willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [search resignFirstResponder];
    search.text = @"";
    isSearching = NO;
    [tableView reloadData];
    return indexPath;
}
- (NSInteger)tableView:(UITableView *)tableView 
sectionForSectionIndexTitle:(NSString *)title 
               atIndex:(NSInteger)index
{
    NSString *key = [keys objectAtIndex:index];
    if (key == UITableViewIndexSearch)
    {
        [tableView setContentOffset:CGPointZero animated:NO];
        return NSNotFound;
    }
    else return index;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSUInteger section = [indexPath section];
    NSString *key = [keys objectAtIndex:section];
    NSArray *nameSection = [names objectForKey:key];
    NSString* str1 = [nameSection objectAtIndex:indexPath.row];
    NSRange range=[str1 rangeOfString:@"+"];
    
    NSString* str2=[str1 substringFromIndex:range.location];
    NSString* areaCode=[str2 stringByReplacingOccurrencesOfString:@"+" withString:@""];
    NSString* countryName=[str1 substringToIndex:range.location];
    NSLog(@"%@ %@",countryName,areaCode);
}

主要思路是 存在的好友 以数组对象里的 @ #来区分号码的状态

猜你喜欢

转载自blog.csdn.net/w250130255/article/details/44117023