2011-04-15 6 views
4

이것은 일반적인 문제이지만 안드로이드와 아이폰에서 나에게 특히 영향을 미치고 있습니다 : 사용자와 전화 번호가 주어지면 저장 및 전화 걸기에 실제로 유용한 전화 번호를 어떻게 정규화 할 수 있습니까?사용자의 주소록에서 전화 번호를 정규화하기위한 전략은 무엇입니까?

  • 7 자리 미국 번호 (555-1212)
  • 10 자리 미국 번호 (210-555-1212)
  • 국제 : 사용자는 형태의 자신의 주소록에있는 전화 번호를 가질 수있다 +와 번호 (+ 46-555-1212)
  • 국내가 아닌 미국 전체 번호 (123-555-1212)
  • 국내가 아닌 미국립니다 번호 (555-1212)

사용자에 대해 아는 것들 submitti NG이 번호 :

  • IP 주소 어쩌면
  • 전화 번호
  • 어쩌면 선택한 국가
  • 어쩌면 선택한 지역은

이 힘든 것 같아 문제 - 나는 분명히 더 많은 정보를 얻기 위해 사용자에게 묻고 싶지 않다. 정말해야하지만,이 데이터는 꽤 신뢰할 수 있어야합니다. 여기서 재사용 할 수있는 최선의 방법이 있습니까?

답변

1

IOS

좋아, 이것은 단지 당신에게 도움이 될 수 있습니다. 그렇게되기를 바랍니다.

내 앱에서 필자는 어떻게 든 연락처로부터 전화 번호를 알아야했습니다. 그래서 문제는 당신이 설명했듯이 - 다양한 문자들 (*)과 국가 코드없이 - 일 수 있습니다.

그래서 - 나는 ABPeoplePickerNavigationController를 사용하여 연락처를 얻을 수 사실 수와 수에서 얻을 - 기능을 사용하여 국가 코드 : 또한

- (void)saveContactPhone:(NSString *) mContactPhone 
{ 
    if(mContactPhone && [mContactPhone length]) 
    { 
     if ([mContactPhone rangeOfString:@"+"].location != NSNotFound)//this means number includes country code. 
     { 
      NSString * mCCodeString = @""; 

      BOOL mFound = FALSE; 

      for(int i = 0; i<[mContactPhone length]; i++) // check number for any obvious country code. 
      { 
       if(mFound == TRUE) 
       { 
        break; 
       } 

       mCCodeString = [mContactPhone substringToIndex:i]; 

       mCCodeString = [[mCCodeString componentsSeparatedByCharactersInSet: 
       [[NSCharacterSet decimalDigitCharacterSet] invertedSet]] 
       componentsJoinedByString:@""]; 

       if([mCCodeString intValue] != 1)//because This is US/CA 
       { 
        for(int j = 0; j<[pickerViewElementArray_ count]; j++) 
        { 
         if([[pickerViewElementArray_ objectAtIndex:j] intValue] == [mCCodeString intValue]) 
         { 
          mFound = TRUE; 

          //we got ourselves final telephone number 
          //and we got country code!! 

          mContactPhone = [mContactPhone substringFromIndex:i]; 

          break; 
         } 
        } 
       } 

      } 

      if(mFound == FALSE)//If no luck finding a match - lets try again, but til index 2. (find if it is +1) 
      { 
       mCCodeString = [mContactPhone substringToIndex:2]; 

       mCCodeString = [[mCCodeString componentsSeparatedByCharactersInSet: 
       [[NSCharacterSet decimalDigitCharacterSet] invertedSet]] 
       componentsJoinedByString:@""]; 

       mContactPhone = [mContactPhone substringFromIndex:1]; 

       for(int i = 0; i<[pickerViewElementArray_ count]; i++) 
       { 
        if([[pickerViewElementArray_ objectAtIndex:i] intValue] == [mCCodeString intValue]) 
        { 
         //we found it!! Its +1!!!! 

         mFound = TRUE; 

         break; 
        } 
       } 
      } 
     } 
    } 

    mContactPhone = [[mContactPhone componentsSeparatedByCharactersInSet: 
      [[NSCharacterSet decimalDigitCharacterSet] invertedSet]] 
      componentsJoinedByString:@""]; 
} 

당신이 국가 코드 배열을 필요 :

NSArray *pickerViewElementArray_ = [NSArray arrayWithObjects: 
    @"93", 
    @"355", 
    @"213", 
    @"1684", 
    @"376", 
    @"244", 
    .... 
: 예를 들어,

누군가를 돕는 희망!

관련 문제