How to get the user's country calling code in iOS?

后端 未结 13 2026
终归单人心
终归单人心 2020-12-23 13:24

I am developing an iOS app in which the user enters their mobile number. How do I get their country calling code? For example, if a user is in India, then +91 s

13条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-23 13:26

        NSLocale *currentLocale = [NSLocale currentLocale];  // get the current locale.
        NSString *countryCode = [currentLocale objectForKey:NSLocaleCountryCode];
    
        // get country code, e.g. ES (Spain), FR (France), etc.
        NSLog(@"country code is:%@",countryCode);
        NSString*lower=[countryCode lowercaseString];
        NSString *path = [[NSBundle mainBundle] pathForResource:@"DiallingCodes" ofType:@"plist"];
        NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
        NSMutableDictionary *_dictCountry=[[NSMutableDictionary alloc]init];
        NSMutableArray *_CodeArray=[[NSMutableArray alloc]init];
        [_CodeArray addObject:dict];
    
        _dictCountry = [_CodeArray objectAtIndex:0];
    
    
        NSString*Country_code=[NSString stringWithFormat:@"+%@",[_dictCountry objectForKey:lower]];
    
    
        contactTextField.text=Country_code;
    

提交回复
热议问题