Skip to content

Instantly share code, notes, and snippets.

@donjordano
Created August 29, 2013 09:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save donjordano/6376170 to your computer and use it in GitHub Desktop.
Save donjordano/6376170 to your computer and use it in GitHub Desktop.
All country codes and names retrieve from: http://countrycode.org/ and written in plist. For parsing HTML I'm using this: https://github.com/topfunky/hpple
NSURL *url = [NSURL URLWithString:@"http://countrycode.org/"];
NSData * data = [NSData dataWithContentsOfURL:url];
TFHpple * doc = [[TFHpple alloc] initWithHTMLData:data];
NSArray *ar = [doc searchWithXPathQuery:@"/html/body/div/div[2]/div/div[2]/table/tbody"];
TFHppleElement *element = [ar objectAtIndex:0];
NSArray *children = [element children];
NSMutableDictionary *countryCodes = [[NSMutableDictionary alloc] init];
for (TFHppleElement *el in children) {
for (TFHppleElement *elm in [el children]) {
TFHppleElement *countryElement = [[[[el children] objectAtIndex:0] children] objectAtIndex:1];
TFHppleElement *codeElement = [[el children] objectAtIndex:4];
NSString *code = [[codeElement text] stringByReplacingOccurrencesOfString:@"\u00a0" withString:@""];
NSString *codeNW = [code stringByReplacingOccurrencesOfString:@" " withString:@""];
NSString *codePlus = [NSString stringWithFormat:@"+%@", codeNW];
[countryCodes setObject:codePlus forKey:[countryElement text]];
}
}
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Countries.plist"];
// write plist to disk
[countryCodes writeToFile:path atomically:YES];
@ayox
Copy link

ayox commented Jun 6, 2016

you could have used countrycode api's:
https://countrycode.org/api/countryCode/countryMenu

@Somasekar-N
Copy link

ayox can you tell me where can i find the countrycode api's list?

@mechanicalobject
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment