Native iOS Country Picker - ios

I'm developing an app with an custom Registration Flow, where the user should set his nationality or his home Country.
My question is, if it's possible to get this information via an native ViewController like ABPeoplePickerNavigationController for contacts.
Should look like this (iPhone 6, iOS 8.x, contacts App 'select a Country'):
Thanks

It is very easy to make your own Country Picker.
You can get the country list(country codes) easily from NSLocale,
NSArray *countryArray = [NSLocale ISOCountryCodes];
Also you will get the country name list using displayNameForKey method,
NSLocale *locale = [NSLocale currentLocale];
NSMutableArray *sortedCountryArray = [[NSMutableArray alloc] init];
for (NSString *countryCode in countryArray) {
NSString *displayNameString = [locale displayNameForKey:NSLocaleCountryCode value:countryCode];
[sortedCountryArray addObject:displayNameString];
}
[sortedCountryArray sortUsingSelector:#selector(localizedCompare:)];
Just use the sortedCountryArray for populating tableView in your picker class.

I think you have to make your own one using modal view controller.
Alternatively, you can use this one, which works quite well.

No there isn't a built-in picker for countries. Same about the classic picture reader, its the stuff you have to make yourself.
Thankfully, it's rather easy to make and find, wether you want someone else's (lockwood is a very realiable programmer, i'm sure his CountryPicker is great).
Or you can go the harder way and make one yourself from scratch.
Note that if you have all the country codes (BE, FR, EN, NL, PT, etc.) you can find the localized name with iOS.
What I did in my code was importing a massive static dictionary of all country codes, and simply use their localized names in the tableview.
This is how I got all the codes (and international prefixes, because my app needs that).
- (NSMutableDictionary*)dialCodeDict{
NSMutableDictionary *dialCodeDict = [[NSMutableDictionary alloc]initWithObjectsAndKeys:
#"972", #"IL",
#"93", #"AF",
#"355", #"AL",
#"213", #"DZ",
#"1", #"AS",
#"376", #"AD",
#"244", #"AO",
#"1", #"AI",
#"1", #"AG",
#"54", #"AR",
#"374", #"AM",
#"297", #"AW",
#"61", #"AU",
#"43", #"AT",
#"994", #"AZ",
#"1", #"BS",
#"973", #"BH",
#"880", #"BD",
#"1", #"BB",
#"375", #"BY",
#"32", #"BE",
#"501", #"BZ",
#"229", #"BJ",
#"1", #"BM", #"975", #"BT",
#"387", #"BA", #"267", #"BW", #"55", #"BR", #"246", #"IO",
#"359", #"BG", #"226", #"BF", #"257", #"BI", #"855", #"KH",
#"237", #"CM", #"1", #"CA", #"238", #"CV", #"345", #"KY",
#"236", #"CF", #"235", #"TD", #"56", #"CL", #"86", #"CN",
#"61", #"CX", #"57", #"CO", #"269", #"KM", #"242", #"CG",
#"682", #"CK", #"506", #"CR", #"385", #"HR", #"53", #"CU",
#"537", #"CY", #"420", #"CZ", #"45", #"DK", #"253", #"DJ",
#"1", #"DM", #"1", #"DO", #"593", #"EC", #"20", #"EG",
#"503", #"SV", #"240", #"GQ", #"291", #"ER", #"372", #"EE",
#"251", #"ET", #"298", #"FO", #"679", #"FJ", #"358", #"FI",
#"33", #"FR", #"594", #"GF", #"689", #"PF", #"241", #"GA",
#"220", #"GM", #"995", #"GE", #"49", #"DE", #"233", #"GH",
#"350", #"GI", #"30", #"GR", #"299", #"GL", #"1", #"GD",
#"590", #"GP", #"1", #"GU", #"502", #"GT", #"224", #"GN",
#"245", #"GW", #"595", #"GY", #"509", #"HT", #"504", #"HN",
#"36", #"HU", #"354", #"IS", #"91", #"IN", #"62", #"ID",
#"964", #"IQ", #"353", #"IE", #"972", #"IL", #"39", #"IT",
#"1", #"JM", #"81", #"JP", #"962", #"JO", #"77", #"KZ",
#"254", #"KE", #"686", #"KI", #"965", #"KW", #"996", #"KG",
#"371", #"LV", #"961", #"LB", #"266", #"LS", #"231", #"LR",
#"423", #"LI", #"370", #"LT", #"352", #"LU", #"261", #"MG",
#"265", #"MW", #"60", #"MY", #"960", #"MV", #"223", #"ML",
#"356", #"MT", #"692", #"MH", #"596", #"MQ", #"222", #"MR",
#"230", #"MU", #"262", #"YT", #"52", #"MX", #"377", #"MC",
#"976", #"MN", #"382", #"ME", #"1", #"MS", #"212", #"MA",
#"95", #"MM", #"264", #"NA", #"674", #"NR", #"977", #"NP",
#"31", #"NL", #"599", #"AN", #"687", #"NC", #"64", #"NZ",
#"505", #"NI", #"227", #"NE", #"234", #"NG", #"683", #"NU",
#"672", #"NF", #"1", #"MP", #"47", #"NO", #"968", #"OM",
#"92", #"PK", #"680", #"PW", #"507", #"PA", #"675", #"PG",
#"595", #"PY", #"51", #"PE", #"63", #"PH", #"48", #"PL",
#"351", #"PT", #"1", #"PR", #"974", #"QA", #"40", #"RO",
#"250", #"RW", #"685", #"WS", #"378", #"SM", #"966", #"SA",
#"221", #"SN", #"381", #"RS", #"248", #"SC", #"232", #"SL",
#"65", #"SG", #"421", #"SK", #"386", #"SI", #"677", #"SB",
#"27", #"ZA", #"500", #"GS", #"34", #"ES", #"94", #"LK",
#"249", #"SD", #"597", #"SR", #"268", #"SZ", #"46", #"SE",
#"41", #"CH", #"992", #"TJ", #"66", #"TH", #"228", #"TG",
#"690", #"TK", #"676", #"TO", #"1", #"TT", #"216", #"TN",
#"90", #"TR", #"993", #"TM", #"1", #"TC", #"688", #"TV",
#"256", #"UG", #"380", #"UA", #"971", #"AE", #"44", #"GB",
#"1", #"US", #"598", #"UY", #"998", #"UZ", #"678", #"VU",
#"681", #"WF", #"967", #"YE", #"260", #"ZM", #"263", #"ZW",
#"591", #"BO", #"673", #"BN", #"61", #"CC", #"243", #"CD",
#"225", #"CI", #"500", #"FK", #"44", #"GG", #"379", #"VA",
#"852", #"HK", #"98", #"IR", #"44", #"IM", #"44", #"JE",
#"850", #"KP", #"82", #"KR", #"856", #"LA", #"218", #"LY",
#"853", #"MO", #"389", #"MK", #"691", #"FM", #"373", #"MD",
#"258", #"MZ", #"970", #"PS", #"872", #"PN", #"262", #"RE",
#"7", #"RU", #"590", #"BL", #"290", #"SH", #"1", #"KN",
#"1", #"LC", #"590", #"MF", #"508", #"PM", #"1", #"VC",
#"239", #"ST", #"252", #"SO", #"47", #"SJ", #"963",
#"SY",#"886",
#"TW", #"255",
#"TZ", #"670",
#"TL",#"58",
#"VE",#"84",
#"VN",
#"284", #"VG",
#"340", #"VI",
#"678",#"VU",
#"681",#"WF",
#"685",#"WS",
#"967",#"YE",
#"262",#"YT",
#"27",#"ZA",
#"260",#"ZM",
#"263",#"ZW",
nil];
return dialCodeDict;
}
And then I did this :
- (void)viewDidLoad {
_dialCodes = [self dialCodeDict];
[self setCodes];
}
- (void)setCodes{
CTTelephonyNetworkInfo *network_Info = [CTTelephonyNetworkInfo new];
CTCarrier *carrier = network_Info.subscriberCellularProvider;
NSArray *keys = [_dialCodes allKeys];
NSArray *values = [_dialCodes allValues];
_countries = [[NSMutableArray alloc]initWithCapacity:[_dialCodes count]];
NSString *baseCountry = [_dialCodes objectForKey:carrier.isoCountryCode.uppercaseString];
for (int i = 0; i < [_dialCodes count] ; i++){
Country *c = [[Country alloc]init];
c.isoC = [keys objectAtIndex:i];
c.dialC = [values objectAtIndex:i];
c.nameC = [[NSLocale currentLocale] displayNameForKey:NSLocaleCountryCode value:c.isoC];
c.isLocal = NO;
if ([baseCountry isEqualToString:[values objectAtIndex:i]]){
c.isLocal = YES;
_myCountry = c;
}
[_countries addObject:c];
}
[_countries sortUsingComparator:^(Country *firstObject, Country *secondObject) {
return [firstObject.nameC caseInsensitiveCompare:secondObject.nameC];
}];
[self.tableView reloadData];
idx = [NSIndexPath indexPathForRow:[_countries indexOfObject:_myCountry] inSection:0];
[self.tableView selectRowAtIndexPath:idx animated:YES scrollPosition:UITableViewScrollPositionMiddle];
}
I think there is too much code for what you need but, what this does is fill a tableview of all countries and align the view to the current country (if the user has a sim card or an NSLocale)
This is how i designed the tableview :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = #"InternationalCell";
CustomCellInternationalTableViewCell *cell;
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil){
[tableView registerNib:[UINib nibWithNibName:#"CustomCellInternationalTableViewCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
cell = [[CustomCellInternationalTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}else{
cell.lbCountry.text = [[_countries objectAtIndex:indexPath.row]nameC];
cell.lbCode.text = [NSString stringWithFormat:#"+%#",[[_countries objectAtIndex:indexPath.row]dialC]];
if ([[_countries objectAtIndex:indexPath.row]isLocal] == YES){
cell.contentView.backgroundColor = FlatGray;
}else{
cell.contentView.backgroundColor = ClearColor;
}
}
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [_countries count];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
_myCountry = [_countries objectAtIndex:indexPath.row];
self.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self dismissViewControllerAnimated:YES completion:nil];
}
Again, there are some lines that i could remove because this is straight from my code, and because it's not well commented (my bad :D) I don't remember which would be important to you and which wouldn't ! But i'll re-read and edit if necessary.

Here is a country picker I made as a cocoapod.
Usage is very simple:
class ViewController: UIViewController, MRCountryPickerDelegate {
#IBOutlet weak var countryPicker: MRCountryPicker!
#IBOutlet weak var countryName: UILabel!
#IBOutlet weak var countryCode: UILabel!
#IBOutlet weak var countryFlag: UIImageView!
#IBOutlet weak var phoneCode: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
countryPicker.countryPickerDelegate = self
countryPicker.showPhoneNumbers = true
countryPicker.setCountry("SI")
}
func countryPhoneCodePicker(picker: SwiftCountryPicker, didSelectCountryWithName name: String, countryCode: String, phoneCode: String, flag: UIImage) {
self.countryName.text = name
self.countryCode.text = countryCode
self.phoneCode.text = phoneCode
self.countryFlag.image = flag
}
}

Related

Getting data from a services Objective c

I have got a small problem with the below service data
{
"DataTable": {
"DataList1": {
"StudentName": "Rakesh",
"StudentId": "13",
"StudentAge": "19",
"StudentAddress": "NewYork",
},
"DataList2": [{
"TeacherName": "Abhinav",
"TeacherId": "309",
"TeacherAge": "34",
"TeacherAddress": "NewYork",
}]
}
}
i Can get the data from DataList1 and cannot know how to get the data from DataList2. Below is the code what i have tried. Please help to find out the solution. Thanks in Advance
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[jsonArray removeAllObjects];
NSString *responseString = [[NSString alloc] initWithData:self.responseData encoding:NSUTF8StringEncoding];
self.responseData = nil;
NSMutableDictionary *sdf = [(NSDictionary*)[responseString JSONValue] objectForKey:#"DataTable"];
NSMutableArray * myArray = [[NSMutableArray alloc] init];
if (([(NSString*)sdf isEqual: [NSNull null]])) {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Information" message:#"Currently there are no Data" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert setTag:1];
[alert show];
}else {
[myArray addObject:[sdf objectForKey:#"DataList1"]];
jsonArray=[myArray mutableCopy];
refilldict=[jsonArray objectAtIndex:0];
NSArray *keys = [refilldict allKeys];
for(int p=0 ; p<[keys count]; p++ )
{
NSString *value=[refilldict objectForKey:[keys objectAtIndex:p]];
if ([value isEqual: [NSNull null]]||[value length]==0) {
[refilldict setValue:#"" forKey:[keys objectAtIndex:p]];
}
}
lblStudentName.text = [refilldict objectForKey:#"StudentName"];
lblStudentId.text = [refilldict objectForKey:#"StudentId"];
lblStudentAge.text = [refilldict objectForKey:#"StudentAge"];
lblStudentAddress.text = [refilldict objectForKey:#"StudentAddress"];
}
self.navigationController.navigationBar.userInteractionEnabled = YES;
[HUD hide:YES];
[HUD removeFromSuperview];
HUD=nil;
}
Please use the bellow code and pass your Initial Json dictionary in it.
-(void)parseJsonData:(NSDictionary *)jsonDictionary{
for(int i=0;i<[jsonDictionary allKeys].count;i++){
NSString *keyName = [[jsonDictionary allKeys] objectAtIndex:i];
id objValue = [jsonDictionary objectForKey:keyName];
if([objValue isKindOfClass:[NSArray class]]){
NSArray *dataList2Array = (NSArray *)objValue;
NSLog(#"DataList2 Is :--%#",dataList2Array);
}
else {
NSDictionary *dataList1 = (NSDictionary *)objValue;
NSLog(#"DataList1 Is :--%#",dataList1);
}
}
}
you can get easily like
[myArray addObject:[sdf objectForKey:#"DataList1"]];
its started with array of dictionary , so you need to store your second object to array and take from index.
NSArray *temp = [sdf objectForKey:#"DataList2"];
if(temp.count >0)
{
lblTeacherName.text = temp[0][#"TeacherName"];
lblTeacherId.text = temp[0][#"TeacherId"];
lblTeacherAge.text = temp[0][#"TeacherAge"];
lblTeacherAddress.text = temp[0][#"TeacherAddress"];
}

returning a nsdictionary from a method? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
#import <Foundation/Foundation.h>
#interface GenerarPassword: NSObject
{
int password;
}
#property int password;
-(NSString*) GenerarValor:(NSString*) key;
-(NSDictionary*) getDiccionario;
-(int) password;
//-(NSArray*)generarlistaletras:(int)numero;
-(int) generarClave;
-(void) printPassword;
-(void)setPassword:(int)nuevoValor;
/*
-(int) generarNumeroAleatorio;
-(NSArray *) generarListadeUsuarios;
*/
#end
#import <Foundation/Foundation.h>
#include <stdlib.h>
#include "GenerarPassword.h"
#implementation GenerarPassword
-(NSDictionary*) getDiccionario
{
NSDictionary* m_Dict =[NSDictionary dictionaryWithObjectsAndKeys:
#"1", #"Dx",
#"2", #"Om",
#"3", #"Al",
#"4", #"Dx",
#"5", #"Je",
#"6", #"Ko",
#"7", #"Ke",
#"8", #"Fi",
#"9", #"Re",
#"10", #"Me",
#"11", #"Mu",
#"12", #"Ra",
#"13", #"Lu",
#"14", #"Lo",
#"15", #"Ka"];
return m_Dict;
}
-(int) password
{
return password;
}
-(void) setPassword:(int)nuevoValor
{
password = nuevoValor;
}
-(void) printPassword
{
NSLog(#"%d",password);
}
/*Se genera la clave numérica*/
-(int) generarClave
{
srand(time(0));
int r = rand() %(9999-1000+1) +1000;
return r;
}
//- (NSArray*) generarlistaletras:(int)numero
//{
// return nil;
//}
//Esta función Genera el valor Aleatorio
-(NSString*) GenerarValor:(NSString*) key
{
NSString *valor = [[self generarDiccionario] valueForKey: key];
return valor;
}
// return (generarDiccionario().get(key))
// lista = [0,0,0]
//lista[random.randrange(0,3)] = [GenerarValor(numero)]
//for i in range(len(lista)):
// if lista[i] == 0:
// lista[i] = [GenerarValor(random.randrange(11,20))]
// return lista
#end
int main(int argc, char const *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
GenerarPassword *Generar1 = [[GenerarPassword alloc]init];
int clave = [Generar1 generarClave];
[Generar1 setPassword: clave];
NSDictionary* dict = [Generar1 getDiccionario];
NSLog(#"%a",[Generar1 GenerarValor: #"3"]);
[pool drain];
return 0;
}
I am really desperate, i just want to use the dictionary but at the compiling time i get a few warnings and when i run the program i get the following error: ": Uncaught exception NSInvalidArgumentException, reason: Can not determine type information for -[GenerarPassword (null)]
"
I see missing nil at the end of the object/key list:
NSDictionary* m_Dict =[NSDictionary dictionaryWithObjectsAndKeys:
#"1", #"Dx",
#"2", #"Om",
#"3", #"Al",
#"4", #"Dx",
#"5", #"Je",
#"6", #"Ko",
#"7", #"Ke",
#"8", #"Fi",
#"9", #"Re",
#"10", #"Me",
#"11", #"Mu",
#"12", #"Ra",
#"13", #"Lu",
#"14", #"Lo",
#"15", #"Ka",
nil];
return m_Dict;
}
and generarDiccionario method not declared (I don't see any method matching this name). Did you mean getDiccionario?
-(NSString*) GenerarValor:(NSString*) key
{
NSString *valor = [[self getDiccionario] valueForKey: key];
return valor;
}
On my environment, NSInvalidArgumentException didn't appeared. But there are some mistakes to fix.
On the method 'dictionaryWithObjectsAndKeys', you must first write the object , then the key, nil for last.
NSDictionary *m_Dict = [NSDictionary dictionaryWithObjectsAndKeys:
#"Dx", #"1",
#"Om", #"2",
#"Al", #"3",
#"Dx", #"4",
#"Je", #"5",
#"Ko", #"6",
#"Ke", #"7",
#"Fi", #"8",
#"Re", #"9",
#"Me", #"10",
#"Mu", #"11",
#"Ra", #"12",
#"Lu", #"13",
#"Lo", #"14",
#"Ka", #"15",
nil];
There is no method 'generarDiccionario'. Do you mean 'getDiccionario'?
- (NSString *)GenerarValor:(NSString *)key
{
//NSString *valor = [[self generarDiccionario] valueForKey: key]; <-Not found
NSString *valor = [[self getDiccionario] valueForKey: key]; //Do you mean this?
return valor;
}
The format identifier is '%#' for NSString*.
NSLog(#"%a",[Generar1 GenerarValor: #"3"]); //Error
NSLog(#"%#",[Generar1 GenerarValor: #"3"]);
Perhaps it will work after fix them.

Convert Phone Number to International with Country Code iOS

I have an app that reads in the user's contact list, but I need to convert each number to the International Equivalent with the Country Code. For example, if a number is 07777777777, then the result would be +447777777777, or if a number was from Turkey and it was 0090, it would replace it with +90.
- (NSString *)ConvertNumberToInternational:(NSString *)number {
if(number != nil) {
if(number.length > 0) {
NSLocale *locale = [NSLocale currentLocale];
NSString *countryCode = [locale objectForKey: NSLocaleCountryCode];
NSDictionary *dict = [self dictCountryCodes];
number = [number stringByReplacingOccurrencesOfString:#" " withString:#""];
if([[number substringToIndex:1] isEqualToString:#"0"] && ![[number substringToIndex:2] isEqualToString:#"00"]) {
number = [NSString stringWithFormat:#"+44%#", [number stringByReplacingCharactersInRange:[number rangeOfString:#"0"] withString:#""]];
}
if([[number substringToIndex:2] isEqualToString:#"00"]) {
number = [NSString stringWithFormat:#"+%#%#", [dict objectForKey:countryCode], [number substringFromIndex:2]];
}
number = [number stringByReplacingOccurrencesOfString:#" " withString:#""];
}
}
return number;
}
-(NSDictionary *)dictCountryCodes{
NSDictionary *dictCodes = [NSDictionary dictionaryWithObjectsAndKeys:
#"93", #"AF",#"20",#"EG", #"355", #"AL", #"213", #"DZ", #"1", #"AS",
#"376", #"AD", #"244", #"AO", #"1", #"AI", #"1", #"AG",
#"54", #"AR", #"374", #"AM", #"297", #"AW", #"61", #"AU",
#"43", #"AT", #"994", #"AZ", #"1", #"BS", #"973", #"BH",
#"880", #"BD", #"1", #"BB", #"375", #"BY", #"32", #"BE",
#"501", #"BZ", #"229", #"BJ", #"1", #"BM", #"975", #"BT",
#"387", #"BA", #"267", #"BW", #"55", #"BR", #"246", #"IO",
#"359", #"BG", #"226", #"BF", #"257", #"BI", #"855", #"KH",
#"237", #"CM", #"1", #"CA", #"238", #"CV", #"345", #"KY",
#"236", #"CF", #"235", #"TD", #"56", #"CL", #"86", #"CN",
#"61", #"CX", #"57", #"CO", #"269", #"KM", #"242", #"CG",
#"682", #"CK", #"506", #"CR", #"385", #"HR", #"53", #"CU",
#"537", #"CY", #"420", #"CZ", #"45", #"DK", #"253", #"DJ",
#"1", #"DM", #"1", #"DO", #"593", #"EC", #"20", #"EG",
#"503", #"SV", #"240", #"GQ", #"291", #"ER", #"372", #"EE",
#"251", #"ET", #"298", #"FO", #"679", #"FJ", #"358", #"FI",
#"33", #"FR", #"594", #"GF", #"689", #"PF", #"241", #"GA",
#"220", #"GM", #"995", #"GE", #"49", #"DE", #"233", #"GH",
#"350", #"GI", #"30", #"GR", #"299", #"GL", #"1", #"GD",
#"590", #"GP", #"1", #"GU", #"502", #"GT", #"224", #"GN",
#"245", #"GW", #"595", #"GY", #"509", #"HT", #"504", #"HN",
#"36", #"HU", #"354", #"IS", #"91", #"IN", #"62", #"ID",
#"964", #"IQ", #"353", #"IE", #"972", #"IL", #"39", #"IT",
#"1", #"JM", #"81", #"JP", #"962", #"JO", #"77", #"KZ",
#"254", #"KE", #"686", #"KI", #"965", #"KW", #"996", #"KG",
#"371", #"LV", #"961", #"LB", #"266", #"LS", #"231", #"LR",
#"423", #"LI", #"370", #"LT", #"352", #"LU", #"261", #"MG",
#"265", #"MW", #"60", #"MY", #"960", #"MV", #"223", #"ML",
#"356", #"MT", #"692", #"MH", #"596", #"MQ", #"222", #"MR",
#"230", #"MU", #"262", #"YT", #"52", #"MX", #"377", #"MC",
#"976", #"MN", #"382", #"ME", #"1", #"MS", #"212", #"MA",
#"95", #"MM", #"264", #"NA", #"674", #"NR", #"977", #"NP",
#"31", #"NL", #"599", #"AN", #"687", #"NC", #"64", #"NZ",
#"505", #"NI", #"227", #"NE", #"234", #"NG", #"683", #"NU",
#"672", #"NF", #"1", #"MP", #"47", #"NO", #"968", #"OM",
#"92", #"PK", #"680", #"PW", #"507", #"PA", #"675", #"PG",
#"595", #"PY", #"51", #"PE", #"63", #"PH", #"48", #"PL",
#"351", #"PT", #"1", #"PR", #"974", #"QA", #"40", #"RO",
#"250", #"RW", #"685", #"WS", #"378", #"SM", #"966", #"SA",
#"221", #"SN", #"381", #"RS", #"248", #"SC", #"232", #"SL",
#"65", #"SG", #"421", #"SK", #"386", #"SI", #"677", #"SB",
#"27", #"ZA", #"500", #"GS", #"34", #"ES", #"94", #"LK",
#"249", #"SD", #"597", #"SR", #"268", #"SZ", #"46", #"SE",
#"41", #"CH", #"992", #"TJ", #"66", #"TH", #"228", #"TG",
#"690", #"TK", #"676", #"TO", #"1", #"TT", #"216", #"TN",
#"90", #"TR", #"993", #"TM", #"1", #"TC", #"688", #"TV",
#"256", #"UG", #"380", #"UA", #"971", #"AE", #"44", #"GB",
#"1", #"US", #"598", #"UY", #"998", #"UZ", #"678", #"VU",
#"681", #"WF", #"967", #"YE", #"260", #"ZM", #"263", #"ZW",
#"591", #"BO", #"673", #"BN", #"61", #"CC", #"243", #"CD",
#"225", #"CI", #"500", #"FK", #"44", #"GG", #"379", #"VA",
#"852", #"HK", #"98", #"IR", #"44", #"IM", #"44", #"JE",
#"850", #"KP", #"82", #"KR", #"856", #"LA", #"218", #"LY",
#"853", #"MO", #"389", #"MK", #"691", #"FM", #"373", #"MD",
#"258", #"MZ", #"970", #"PS", #"872", #"PN", #"262", #"RE",
#"7", #"RU", #"590", #"BL", #"290", #"SH", #"1", #"KN",
#"1", #"LC", #"590", #"MF", #"508", #"PM", #"1", #"VC",
#"239", #"ST", #"252", #"SO", #"47", #"SJ", #"963", #"SY",
#"886", #"TW", #"255", #"TZ", #"670", #"TL", #"58", #"VE",
#"84", #"VN", #"1", #"VG", #"1", #"VI", nil];
return dictCodes;
}
The above code does not work, as I am just using the user's current locale. I need to know the locale of the phone number. Any help would be appreciated!
so you assume just 0 is the iphone region always and 00 is the international number 'prefix' –
#import <Foundation/Foundation.h>
#interface T : NSObject
#end
#implementation T
- (NSString *)convertNumberToInternational:(NSString *)number {
if(number != nil) {
if(number.length > 0) {
NSLocale *locale = [NSLocale currentLocale];
NSString *countryCode = [locale objectForKey: NSLocaleCountryCode];
NSDictionary *dict = [self dictCountryCodes];
NSString *localNumberCode = dict[countryCode];
number = [number stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if([[number substringToIndex:1] isEqualToString:#"0"] && ![[number substringToIndex:2] isEqualToString:#"00"]) {
number = [NSString stringWithFormat:#"+%#%#", localNumberCode, [number substringFromIndex:1]];
}
else if([[number substringToIndex:2] isEqualToString:#"00"]) {
number = [NSString stringWithFormat:#"+%#", [number substringFromIndex:2]];
}
}
}
return number;
}
-(NSDictionary *)dictCountryCodes{
NSDictionary *dictCodes = [NSDictionary dictionaryWithObjectsAndKeys:
#"93", #"AF",#"20",#"EG", #"355", #"AL", #"213", #"DZ", #"1", #"AS",
#"376", #"AD", #"244", #"AO", #"1", #"AI", #"1", #"AG",
#"54", #"AR", #"374", #"AM", #"297", #"AW", #"61", #"AU",
#"43", #"AT", #"994", #"AZ", #"1", #"BS", #"973", #"BH",
#"880", #"BD", #"1", #"BB", #"375", #"BY", #"32", #"BE",
#"501", #"BZ", #"229", #"BJ", #"1", #"BM", #"975", #"BT",
#"387", #"BA", #"267", #"BW", #"55", #"BR", #"246", #"IO",
#"359", #"BG", #"226", #"BF", #"257", #"BI", #"855", #"KH",
#"237", #"CM", #"1", #"CA", #"238", #"CV", #"345", #"KY",
#"236", #"CF", #"235", #"TD", #"56", #"CL", #"86", #"CN",
#"61", #"CX", #"57", #"CO", #"269", #"KM", #"242", #"CG",
#"682", #"CK", #"506", #"CR", #"385", #"HR", #"53", #"CU",
#"537", #"CY", #"420", #"CZ", #"45", #"DK", #"253", #"DJ",
#"1", #"DM", #"1", #"DO", #"593", #"EC", #"20", #"EG",
#"503", #"SV", #"240", #"GQ", #"291", #"ER", #"372", #"EE",
#"251", #"ET", #"298", #"FO", #"679", #"FJ", #"358", #"FI",
#"33", #"FR", #"594", #"GF", #"689", #"PF", #"241", #"GA",
#"220", #"GM", #"995", #"GE", #"49", #"DE", #"233", #"GH",
#"350", #"GI", #"30", #"GR", #"299", #"GL", #"1", #"GD",
#"590", #"GP", #"1", #"GU", #"502", #"GT", #"224", #"GN",
#"245", #"GW", #"595", #"GY", #"509", #"HT", #"504", #"HN",
#"36", #"HU", #"354", #"IS", #"91", #"IN", #"62", #"ID",
#"964", #"IQ", #"353", #"IE", #"972", #"IL", #"39", #"IT",
#"1", #"JM", #"81", #"JP", #"962", #"JO", #"77", #"KZ",
#"254", #"KE", #"686", #"KI", #"965", #"KW", #"996", #"KG",
#"371", #"LV", #"961", #"LB", #"266", #"LS", #"231", #"LR",
#"423", #"LI", #"370", #"LT", #"352", #"LU", #"261", #"MG",
#"265", #"MW", #"60", #"MY", #"960", #"MV", #"223", #"ML",
#"356", #"MT", #"692", #"MH", #"596", #"MQ", #"222", #"MR",
#"230", #"MU", #"262", #"YT", #"52", #"MX", #"377", #"MC",
#"976", #"MN", #"382", #"ME", #"1", #"MS", #"212", #"MA",
#"95", #"MM", #"264", #"NA", #"674", #"NR", #"977", #"NP",
#"31", #"NL", #"599", #"AN", #"687", #"NC", #"64", #"NZ",
#"505", #"NI", #"227", #"NE", #"234", #"NG", #"683", #"NU",
#"672", #"NF", #"1", #"MP", #"47", #"NO", #"968", #"OM",
#"92", #"PK", #"680", #"PW", #"507", #"PA", #"675", #"PG",
#"595", #"PY", #"51", #"PE", #"63", #"PH", #"48", #"PL",
#"351", #"PT", #"1", #"PR", #"974", #"QA", #"40", #"RO",
#"250", #"RW", #"685", #"WS", #"378", #"SM", #"966", #"SA",
#"221", #"SN", #"381", #"RS", #"248", #"SC", #"232", #"SL",
#"65", #"SG", #"421", #"SK", #"386", #"SI", #"677", #"SB",
#"27", #"ZA", #"500", #"GS", #"34", #"ES", #"94", #"LK",
#"249", #"SD", #"597", #"SR", #"268", #"SZ", #"46", #"SE",
#"41", #"CH", #"992", #"TJ", #"66", #"TH", #"228", #"TG",
#"690", #"TK", #"676", #"TO", #"1", #"TT", #"216", #"TN",
#"90", #"TR", #"993", #"TM", #"1", #"TC", #"688", #"TV",
#"256", #"UG", #"380", #"UA", #"971", #"AE", #"44", #"GB",
#"1", #"US", #"598", #"UY", #"998", #"UZ", #"678", #"VU",
#"681", #"WF", #"967", #"YE", #"260", #"ZM", #"263", #"ZW",
#"591", #"BO", #"673", #"BN", #"61", #"CC", #"243", #"CD",
#"225", #"CI", #"500", #"FK", #"44", #"GG", #"379", #"VA",
#"852", #"HK", #"98", #"IR", #"44", #"IM", #"44", #"JE",
#"850", #"KP", #"82", #"KR", #"856", #"LA", #"218", #"LY",
#"853", #"MO", #"389", #"MK", #"691", #"FM", #"373", #"MD",
#"258", #"MZ", #"970", #"PS", #"872", #"PN", #"262", #"RE",
#"7", #"RU", #"590", #"BL", #"290", #"SH", #"1", #"KN",
#"1", #"LC", #"590", #"MF", #"508", #"PM", #"1", #"VC",
#"239", #"ST", #"252", #"SO", #"47", #"SJ", #"963", #"SY",
#"886", #"TW", #"255", #"TZ", #"670", #"TL", #"58", #"VE",
#"84", #"VN", #"1", #"VG", #"1", #"VI", nil];
return dictCodes;
}
#end
int main(int argc, char *argv[]) {
#autoreleasepool {
T*t = [[T alloc] init];
NSLog(#"%#", [t convertNumberToInternational:#"05135897"]);
NSLog(#"%#", [t convertNumberToInternational:#"00995135897"]);
}
}
Probably you could use NSFormatter class.
Validate the total length of the telephone number. (Which will be entered and read as NSString)
There may be few things you may require to do like, skip the first part i.e. 0.

How to extract cell value/text from UITableView?

For some reasons, I have to extract a cell's text or value from a UITableView. I have the following table method:
- (void)loadCollection_Information
{
_collection_InformationCellData = [[NSMutableArray alloc] init];
NSMutableArray *cells_1 = [[NSMutableArray alloc] init];
NSDictionary *cellContainer_1_1 = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:#"Brett Whiteley", #"Author", #"", #"", #"", #"", nil] forKeys:[NSArray arrayWithObjects:#"Text", #"Detail Text", #"Image", #"Text Color", #"Detail Text Color", #"Accessory", nil]];
[cells_1 addObject:cellContainer_1_1];
NSDictionary *cellContainer_1_2 = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:#"1976", #"Year", #"", #"", #"", #"", nil]
forKeys:[NSArray arrayWithObjects:#"Text", #"Detail Text", #"Image", #"Text Color", #"Detail Text Color", #"Accessory", nil]];
[cells_1 addObject:cellContainer_1_2];
NSDictionary *cellContainer_1_3 = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:#"Canvas, Oil", #"Materials", #"", #"", #"", #"", nil] forKeys:[NSArray arrayWithObjects:#"Text", #"Detail Text", #"Image", #"Text Color", #"Detail Text Color", #"Accessory", nil]];
[cells_1 addObject:cellContainer_1_3];
NSDictionary *cellContainer_1_4 = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:#"Social Comment & Hard Edged Abstraction", #"Style", #"", #"", #"", #"", nil]
forKeys:[NSArray arrayWithObjects:#"Text", #"Detail Text", #"Image", #"Text Color", #"Detail Text Color", #"Accessory", nil]];
[cells_1 addObject:cellContainer_1_4];
NSDictionary *cellContainer_1_5 = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:#"182.0 high * 200.0 wide cm", #"Size", #"", #"", #"", #"", nil]
forKeys:[NSArray arrayWithObjects:#"Text", #"Detail Text", #"Image", #"Text Color", #"Detail Text Color", #"Accessory", nil]];
[cells_1 addObject:cellContainer_1_5];
NSDictionary *sectionContainer_1 = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:#"Collection Information", cells_1, #"", nil]
forKeys:[NSArray arrayWithObjects:#"Title", #"Cells", #"Footer Title", nil]];
[_collection_InformationCellData addObject:sectionContainer_1];
_collection_InformationSelectedRow = 0;
_collection_InformationSelectedSection = 0;
_collection_InformationShowHeader = YES;
[_collection_Information setEditing:NO];
[_collection_Information reloadData];
}
and didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (tableView == _collection_Information) {
if (indexPath.section == 0 && indexPath.row == 0) {
UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"Brett_WhiteleyViewController"];
[self.navigationController pushViewController:controller animated:YES];
}
if (indexPath.section == 0 && indexPath.row == 1) {
UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"Year_1960s_70sViewController"];
[self.navigationController pushViewController:controller animated:YES];
}
if (indexPath.section == 0 && indexPath.row == 2) {
UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"Canvas_and_OilViewController"];
[self.navigationController pushViewController:controller animated:YES];
}
if (indexPath.section == 0 && indexPath.row == 3) {
UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"SC_and_HE_AbstractionViewController"];
[self.navigationController pushViewController:controller animated:YES];
}
}
//Get the Dictionary Object at index 0, to check out the object.
NSLog(#"%#",[_collection_InformationCellData objectAtIndex:0]);
//Store the Objects from dictionary in temp Array.
NSArray *temp = [[_collection_InformationCellData objectAtIndex:indexPath.section] valueForKey:#"Cells"];
//Get the object based on Row Selection in UITableView.
NSLog(#"%#",[[temp objectAtIndex: indexPath.row] valueForKey:#"Text"]);
}
I need to extract the NSStrings such as: Brett Whiteley, 1976, Canvas, Oil, Social Comment & Hard Edged Abstraction separately, and put it into follow code in another method:
-(NSString *)maxmsg
{
NSString *msg;
int maxint=-1;
RecommendationData *obj = [RecommendationData getInstance];
if(obj.author<=5 && obj.year<=5 && obj.material<=5 && obj.style<=5)
{
msg=#"nothing";
}
else if( obj.author >= obj.year && obj.author >= obj.material && obj.author >= obj.style)
{
maxint=obj.author;
//the code for placing the value
obj.maxValue= (place value here);
msg=#"It seems that you are interested in the author of this collection.";
}
else if(obj.year >= obj.author && obj.year >= obj.material && obj.year >= obj.style)
{
maxint=obj.year;
//the code for placing the value
obj.maxValue= (place value here);
msg=#"It seems that you are interested in this period of the collection.";
}
else
{
msg=#"equal";
}
return msg;
}
Can anyone tell me how to do it?
To receive "Brett Whiteley" from another method you can call
_collection_InformationCellData[ CellContainerIndex ][ #"Cells" ][ 0 ][ "Text" ]
but you need to save somewhere/to know the correct CellContainerIndex. May be it is just 0 in your case?
Here is the way to perform it :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//Get the Dictionary Object at index 0, to check out the object.
NSLog(#"%#",[_collection_InformationCellData objectAtIndex:0]);
//Store the Objects from dictionary in temp Array.
NSArray *temp = [[_collection_InformationCellData objectAtIndex:indexPath.section] valueForKey:#"Cells"];
//Get the object based on Row Selection in UITableView.
NSLog(#"%#",[[temp objectAtIndex: indexPath.row] valueForKey:#"Text"]);
}
You need to access the Array using Index and getting the values using key from that object.

GDataXML get child node

There is my XML data:
<item>
<title>The Powerful Academician Able to Reduce Tobacco Hazards</title>
<link>http://china15min.com/2013/03/26/the-powerful-academician-able-to-reduce-tobacco-hazards/</link>
<comments>http://china15min.com/2013/03/26/the-powerful-academician-able-to-reduce-tobacco-hazards/#comments</comments>
<pubDate>Tue, 26 Mar 2013 08:43:37 +0000</pubDate>
<dc:creator>Panda Walking</dc:creator>
<category><![CDATA[Economics]]></category>
<category><![CDATA[Academician of Chinese Engineering Academy]]></category>
<category><![CDATA[China Tobacco Control Association]]></category>
<category><![CDATA[China's tobacco industry]]></category>
<category><![CDATA[CORESTA]]></category>
<category><![CDATA[low-tar cigarettes in China]]></category>
<category><![CDATA[signatory country of the World Health Assembly on Tobacco Control Framework Convention]]></category>
<category><![CDATA[smokers in China]]></category>
<category><![CDATA[Xie Jian Ping]]></category>
<category><![CDATA[Yunnan]]></category>
<guid isPermaLink="false">http://china15min.com/?p=1891</guid>
<description><![CDATA[During the two sessions, the deputies of NPC are obliged to answer journalists’ questions, which may include special topics besides national affairs.  For example, why is the “tobacco academician” able to keep his position unswayed despite strong opposition from various... Read More ›<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=china15min.com&blog=37468365&post=1891&subd=china15min&ref=&feed=1" width="1" height="1" />]]></description>
<wfw:commentRss>http://china15min.com/2013/03/26/the-powerful-academician-able-to-reduce-tobacco-hazards/feed/</wfw:commentRss>
<slash:comments>0</slash:comments>
<media:content url="http://1.gravatar.com/avatar/ad06eed181b094ac3022d4507d38c2b7?s=96&d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&r=G" medium="image">
<media:title type="html">china15min</media:title>
</media:content>
<media:content url="http://china15min.files.wordpress.com/2013/03/xiejinping.jpg" medium="image">
<media:title type="html">xiejinping</media:title>
</media:content>
</item>
With this method I can get "link" , "title" and "pubDate" node,
- (void)parseRss:(GDataXMLElement *)rootElement entries:(NSMutableArray *)entries {
NSArray *channels = [rootElement elementsForName:#"channel"];
for (GDataXMLElement *channel in channels) {
NSString *blogTitle = [channel valueForChild:#"title"];
NSArray *items = [channel elementsForName:#"item"];
for (GDataXMLElement *item in items) {
NSString *articleTitle = [item valueForChild:#"title"];
NSString *articleUrl = [item valueForChild:#"link"];
NSString *articleDateString = [item valueForChild:#"pubDate"];
NSDate *articleDate = [NSDate dateFromInternetDateTimeString:articleDateString formatHint:DateFormatHintRFC822];
BQWRSSEntry *entry = [[BQWRSSEntry alloc] initWithBlogTitle:blogTitle
articleTitle:articleTitle
articleUrl:articleUrl
articleDate:articleDate];
[entries addObject:entry];
}
}
}
And I use the same method to get the "description", but the result string is null.
How can I get the "description" and the second "media:content url" url string "http://china15min.files.wordpress.com/2013/03/xiejinping.jpg"
Please help me and thank you very much.
I don't know why you can't retrieve the description. You can get the value easily like this:
NSString *description = [item valueForChild:#"description"];
Otherwise to retrieve the second "media:content url" url string follow this:
NSArray *mediaContents = [item elementsForName:#"media:content"];
if ([mediaContents count] > 1)
{
GDataXMLElement *media2 = [mediaContents objectAtIndex:1];
GDataXMLNode *urlString = [media2 attributeForName:#"url"];
}
The complete code should looks like that:
- (void)parseRss:(GDataXMLElement *)rootElement entries:(NSMutableArray *)entries {
NSArray *channels = [rootElement elementsForName:#"channel"];
for (GDataXMLElement *channel in channels) {
NSString *blogTitle = [channel valueForChild:#"title"];
NSArray *items = [channel elementsForName:#"item"];
for (GDataXMLElement *item in items) {
NSString *articleTitle = [item valueForChild:#"title"];
NSString *articleUrl = [item valueForChild:#"link"];
NSString *articleDateString = [item valueForChild:#"pubDate"];
NSString *description = [item valueForChild:#"description"];
NSArray *mediaContents = [item elementsForName:#"media:content"];
if ([mediaContents count] > 1)
{
GDataXMLElement *media2 = [mediaContents objectAtIndex:1];
GDataXMLNode *urlString = [media2 attributeForName:#"url"];
}
BQWRSSEntry *entry = [[BQWRSSEntry alloc] initWithBlogTitle:blogTitle
articleTitle:articleTitle
articleUrl:articleUrl
articleDate:articleDate];
[entries addObject:entry];
}
}
}

Resources