Created
August 29, 2013 09:47
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 can you tell me where can i find the countrycode api's list?
@somasekar try this one https://restcountries.eu/
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you could have used countrycode api's:
https://countrycode.org/api/countryCode/countryMenu