Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CountryPicker/CountryPicker.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@

@protocol CountryPickerDelegate <UIPickerViewDelegate>

- (void)countryPicker:(CountryPicker *)picker didSelectCountryWithName:(NSString *)name code:(NSString *)code;
- (void)countryPicker:(CountryPicker *)picker didSelectCountryWithName:(NSString *)name code:(NSString *)code dailCode:(NSString *)dailCode;

@end

Expand Down
65 changes: 57 additions & 8 deletions CountryPicker/CountryPicker.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
#pragma GCC diagnostic ignored "-Wgnu"


#define LangId @"en_US" //change Localized country from here for Egypt set LangId as @"ar_EG"


#import <Availability.h>
#if !__has_feature(objc_arc)
#error This class requires automatic reference counting
Expand All @@ -53,7 +56,9 @@ @interface CountryPicker () <UIPickerViewDelegate, UIPickerViewDataSource>
@end


@implementation CountryPicker
@implementation CountryPicker{
}
static NSDictionary *dialCode;

//doesn't use _ prefix to avoid name clash with superclass
@synthesize delegate;
Expand All @@ -75,9 +80,23 @@ + (NSArray *)countryCodes
{
_countryCodes = [[[self countryCodesByName] objectsForKeys:[self countryNames] notFoundMarker:@""] copy];
}

return _countryCodes;
}

+ (NSDictionary *)diallingCodes
{


static dispatch_once_t onceToken;

dispatch_once (&onceToken, ^{
// Do some work that happens once
NSString * plistPath = [[NSBundle mainBundle] pathForResource:@"DiallingCodes" ofType:@"plist"];
dialCode = [NSDictionary dictionaryWithContentsOfFile:plistPath];
});

return dialCode;
}
+ (NSDictionary *)countryNamesByCode
{
static NSDictionary *_countryNamesByCode = nil;
Expand All @@ -87,13 +106,13 @@ + (NSDictionary *)countryNamesByCode
for (NSString *code in [NSLocale ISOCountryCodes])
{
NSString *countryName = [[NSLocale currentLocale] displayNameForKey:NSLocaleCountryCode value:code];

//workaround for simulator bug
if (!countryName)
{
countryName = [[NSLocale localeWithLocaleIdentifier:@"en_US"] displayNameForKey:NSLocaleCountryCode value:code];
countryName = [[NSLocale localeWithLocaleIdentifier:LangId] displayNameForKey:NSLocaleCountryCode value:code];
}

namesByCode[code] = countryName ?: code;
}
_countryNamesByCode = [namesByCode copy];
Expand All @@ -111,6 +130,7 @@ + (NSDictionary *)countryCodesByName
for (NSString *code in countryNamesByCode)
{
codesByName[countryNamesByCode[code]] = code;

}
_countryCodesByName = [codesByName copy];
}
Expand All @@ -136,6 +156,7 @@ - (instancetype)initWithCoder:(NSCoder *)aDecoder
{
if ((self = [super initWithCoder:aDecoder]))
{
dialCode = [[NSDictionary alloc]init];
[self setUp];
}
return self;
Expand Down Expand Up @@ -165,7 +186,11 @@ - (NSString *)selectedCountryCode
NSUInteger index = (NSUInteger)[self selectedRowInComponent:0];
return [[self class] countryCodes][index];
}

- (NSString *)selectedDailCountryCode
{
NSUInteger index = (NSUInteger)[self selectedRowInComponent:0];
return [[self class] countryCodes][index];
}
- (void)setSelectedCountryName:(NSString *)countryName animated:(BOOL)animated
{
NSUInteger index = [[[self class] countryNames] indexOfObject:countryName];
Expand Down Expand Up @@ -235,14 +260,34 @@ - (UIView *)pickerView:(__unused UIPickerView *)pickerView viewForRow:(NSInteger
}
[view addSubview:label];

UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(150, 3, 245, 24)];
label2.backgroundColor = [UIColor clearColor];
label2.tag = 3;
[view addSubview:label2];


UIImageView *flagView = [[UIImageView alloc] initWithFrame:CGRectMake(3, 3, 24, 24)];
flagView.contentMode = UIViewContentModeScaleAspectFit;
flagView.tag = 2;
[view addSubview:flagView];


}

((UILabel *)[view viewWithTag:1]).text = [[self class] countryNames][(NSUInteger)row];
NSString *imagePath = [NSString stringWithFormat:@"CountryPicker.bundle/%@", [[self class] countryCodes][(NSUInteger) row]];
<<<<<<< HEAD
((UIImageView *)[view viewWithTag:2]).image = [UIImage imageNamed:imagePath];
NSString *test =[[self class] countryCodes][(NSUInteger)row];
NSString *code = [[[self class] diallingCodes] objectForKey:[test lowercaseString]];
((UILabel *)[view viewWithTag:3]).text = code;






=======
UIImage *image;
if ([[UIImage class] respondsToSelector:@selector(imageNamed:inBundle:compatibleWithTraitCollection:)])
image = [UIImage imageNamed:imagePath inBundle:[NSBundle bundleForClass:[CountryPicker class]] compatibleWithTraitCollection:nil];
Expand All @@ -251,6 +296,7 @@ - (UIView *)pickerView:(__unused UIPickerView *)pickerView viewForRow:(NSInteger
((UIImageView *)[view viewWithTag:2]).image = image;


>>>>>>> nicklockwood/master
return view;
}

Expand All @@ -259,7 +305,10 @@ - (void)pickerView:(__unused UIPickerView *)pickerView
inComponent:(__unused NSInteger)component
{
__strong id<CountryPickerDelegate> strongDelegate = delegate;
[strongDelegate countryPicker:self didSelectCountryWithName:self.selectedCountryName code:self.selectedCountryCode];
NSString *test =[[self class] countryCodes][(NSUInteger)row];
NSString *code = [[[self class] diallingCodes] objectForKey:[test lowercaseString]];

[strongDelegate countryPicker:self didSelectCountryWithName:self.selectedCountryName code:self.selectedCountryCode dailCode:code];
}

@end
Loading