forked from iziz/libPhoneNumber-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppDelegate.m
348 lines (268 loc) · 13.7 KB
/
AppDelegate.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
//
// AppDelegate.m
// MetadataGenerator
//
// Created by zen.isis on 26/11/2016.
// Copyright © 2016 ohtalk.me. All rights reserved.
//
#import "AppDelegate.h"
@import libPhoneNumberiOS;
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor blackColor];
[self.window makeKeyAndVisible];
[self.window setRootViewController:[[UIViewController alloc] init]];
[self testWithRealData];
//[self testWithGCD];
//[self testForGetSupportedRegions];
return YES;
}
- (void)testForGetSupportedRegions
{
NBPhoneNumberUtil *phoneUtil = [NBPhoneNumberUtil sharedInstance];
NSLog(@"%@", [phoneUtil getSupportedRegions]);
}
- (void)testWithRealData
{
NBAsYouTypeFormatter *formatter = [[NBAsYouTypeFormatter alloc] initWithRegionCode:@"US"];
NSLog(@"%@ (%@)", [formatter inputDigit:@"2"], formatter.isSuccessfulFormatting ? @"Y":@"N");
NSLog(@"%@ (%@)", [formatter inputDigit:@"1"], formatter.isSuccessfulFormatting ? @"Y":@"N");
NSLog(@"%@ (%@)", [formatter inputDigit:@"2"], formatter.isSuccessfulFormatting ? @"Y":@"N");
NSLog(@"%@ (%@)", [formatter inputDigit:@"5"], formatter.isSuccessfulFormatting ? @"Y":@"N");
NSLog(@"%@ (%@)", [formatter inputDigit:@"5"], formatter.isSuccessfulFormatting ? @"Y":@"N");
NSLog(@"%@ (%@)", [formatter inputDigit:@"5"], formatter.isSuccessfulFormatting ? @"Y":@"N");
NSLog(@"%@ (%@)", [formatter inputDigit:@"5"], formatter.isSuccessfulFormatting ? @"Y":@"N");
NSLog(@"%@ (%@)", [formatter inputDigit:@"5"], formatter.isSuccessfulFormatting ? @"Y":@"N");
NSLog(@"%@ (%@)", [formatter inputDigit:@"5"], formatter.isSuccessfulFormatting ? @"Y":@"N");
NSLog(@"%@ (%@)", [formatter inputDigit:@"5"], formatter.isSuccessfulFormatting ? @"Y":@"N");
NSLog(@"%@ (%@)", [formatter inputDigit:@"5"], formatter.isSuccessfulFormatting ? @"Y":@"N");
NBAsYouTypeFormatter *f = [[NBAsYouTypeFormatter alloc] initWithRegionCode:@"US"];
NSLog(@"%@", [f inputDigit:@"6"]); // "6"
NSLog(@"%@", [f inputDigit:@"5"]); // "65"
NSLog(@"%@", [f inputDigit:@"0"]); // "650"
NSLog(@"%@", [f inputDigit:@"2"]); // "650 2"
NSLog(@"%@", [f inputDigit:@"5"]); // "650 25"
NSLog(@"%@", [f inputDigit:@"3"]); // "650 253"
// Note this is how a US local number (without area code) should be formatted.
NSLog(@"%@", [f inputDigit:@"2"]); // "650 2532"
NSLog(@"%@", [f inputDigit:@"2"]); // "650 253 22"
NSLog(@"%@", [f inputDigit:@"2"]); // "650 253 222"
NSLog(@"%@", [f inputDigit:@"2"]); // "650 253 2222"
// Can remove last digit
NSLog(@"%@", [f removeLastDigit]); // "650 253 222"
NSLog(@"%@", [f inputString:@"16502532222"]); // 1 650 253 2222
// Unit test for isValidNumber is failing some valid numbers. #7
NBPhoneNumberUtil *phoneUtil = [NBPhoneNumberUtil sharedInstance];
{
NSError *error = nil;
NBPhoneNumber *phoneNumberUS = [phoneUtil parse:@"(366) 522-8999" defaultRegion:@"US" error:&error];
if (error) {
NSLog(@"err [%@]", [error localizedDescription]);
}
NSLog(@"- isValidNumber [%@]", [phoneUtil isValidNumber:phoneNumberUS] ? @"YES" : @"NO");
NSLog(@"- isPossibleNumber [%@]", [phoneUtil isPossibleNumber:phoneNumberUS error:&error] ? @"YES" : @"NO");
NSLog(@"- getRegionCodeForNumber [%@]", [phoneUtil getRegionCodeForNumber:phoneNumberUS]);
}
{
NSError *error = nil;
NBPhoneNumber *phoneNumberUS = [phoneUtil parse:@"14155552671" defaultRegion:@"US" error:&error];
if (error) {
NSLog(@"err [%@]", [error localizedDescription]);
}
NSLog(@"- isValidNumber [%@]", [phoneUtil isValidNumber:phoneNumberUS] ? @"YES" : @"NO");
NSLog(@"- isPossibleNumber [%@]", [phoneUtil isPossibleNumber:phoneNumberUS error:&error] ? @"YES" : @"NO");
NSLog(@"- getRegionCodeForNumber [%@]", [phoneUtil getRegionCodeForNumber:phoneNumberUS]);
}
NSLog(@"- - - - -");
{
NSError *error = nil;
NBPhoneNumber *phoneNumberZZ = [phoneUtil parse:@"+84 74 883313" defaultRegion:NB_UNKNOWN_REGION error:&error];
if (error) {
NSLog(@"err [%@]", [error localizedDescription]);
}
NSLog(@"- isValidNumber [%@]", [phoneUtil isValidNumber:phoneNumberZZ] ? @"YES" : @"NO");
NSLog(@"- isPossibleNumber [%@]", [phoneUtil isPossibleNumber:phoneNumberZZ error:&error] ? @"YES" : @"NO");
NSLog(@"- getRegionCodeForNumber [%@]", [phoneUtil getRegionCodeForNumber:phoneNumberZZ]);
}
NSLog(@"- - - - - GB / +923406171134");
// I can't validate pakistani numbers #58
{
NSError *error = nil;
NBPhoneNumber *phoneNumberUS = [phoneUtil parse:@"+923406171134" defaultRegion:@"GB" error:&error];
if (error) {
NSLog(@"err [%@]", [error localizedDescription]);
}
NSLog(@"- isValidNumber [%@]", [phoneUtil isValidNumber:phoneNumberUS] ? @"YES" : @"NO");
NSLog(@"- isPossibleNumber [%@]", [phoneUtil isPossibleNumber:phoneNumberUS error:&error] ? @"YES" : @"NO");
NSLog(@"- getRegionCodeForNumber [%@]", [phoneUtil getRegionCodeForNumber:phoneNumberUS]);
}
// finnish phone number don't get recognised #59
{
NSString *nationalNumber = @"";
NSNumber *countryCode = [phoneUtil extractCountryCode:@"+358401493292" nationalNumber:&nationalNumber];
NSLog(@"- %@ %@", countryCode, nationalNumber);
NBPhoneNumber *phonePN = [phoneUtil parse:@"+358401493292" defaultRegion:@"FIN" error:nil];
NSLog(@"- %@", [phoneUtil format:phonePN numberFormat:NBEPhoneNumberFormatINTERNATIONAL error:nil]);
}
NSLog(@"-------------- customTest");
NSError *anError = nil;
NBPhoneNumber *myNumber = [phoneUtil parse:@"+8617196622520," defaultRegion:nil error:&anError];
if (anError == nil)
{
NSLog(@"isValidPhoneNumber ? [%@]", [phoneUtil isValidNumber:myNumber] ? @"YES":@"NO");
NSLog(@"E164 : %@", [phoneUtil format:myNumber numberFormat:NBEPhoneNumberFormatE164 error:&anError]);
NSLog(@"INTERNATIONAL : %@", [phoneUtil format:myNumber numberFormat:NBEPhoneNumberFormatINTERNATIONAL error:&anError]);
NSLog(@"NATIONAL : %@", [phoneUtil format:myNumber numberFormat:NBEPhoneNumberFormatNATIONAL error:&anError]);
NSLog(@"RFC3966 : %@", [phoneUtil format:myNumber numberFormat:NBEPhoneNumberFormatRFC3966 error:&anError]);
}
else
{
NSLog(@"Error : %@", [anError localizedDescription]);
}
NSLog (@"extractCountryCode [%@]", [phoneUtil extractCountryCode:@"823213123123" nationalNumber:nil]);
NSString *res = nil;
NSNumber *dRes = [phoneUtil extractCountryCode:@"823213123123" nationalNumber:&res];
NSLog (@"extractCountryCode [%@] [%@]", dRes, res);
NBAsYouTypeFormatter *fr = [[NBAsYouTypeFormatter alloc] initWithRegionCode:@"FR"];
NSLog(@"%@", [fr inputDigit:@"6"]);
NSLog(@"%@", [fr inputDigit:@"7"]);
NSLog(@"%@", [fr inputDigit:@"8"]);
NSLog(@"%@", [fr inputDigit:@"9"]);
NSLog(@"%@", [fr inputDigit:@"0"]);
NSLog(@"%@", [fr inputDigit:@"1"]);
// Note this is how a US local number (without area code) should be formatted.
NSLog(@"%@", [fr inputDigit:@"2"]);
NSLog(@"%@", [fr inputDigit:@"3"]);
NSLog(@"%@", [fr inputDigit:@"4"]);
NSLog(@"%@", [fr inputString:@"0678901234"]); // 6 78 90 12 34 // it Should be like this
// JP Test
NBPhoneNumber *phoneJP = [phoneUtil parse:@"0066-33-132432" defaultRegion:@"JP" error:nil];
NSLog(@"- %@", [phoneUtil format:phoneJP numberFormat:NBEPhoneNumberFormatINTERNATIONAL error:nil]);
NSLog(@"- isValidNumber %@", [phoneUtil isValidNumber:phoneJP] ? @"Y":@"N");
NSLog(@"- isPossibleNumber %@", [phoneUtil isPossibleNumber:phoneJP] ? @"Y":@"N");
}
- (NSString *)stringWithRandomNumber
{
NSString *numbers = @"0123456789";
NSMutableString *randomString = [NSMutableString stringWithCapacity:11];
for (int i=0; i<12; i++) {
[randomString appendFormat: @"%C", [numbers characterAtIndex: arc4random() % [numbers length]]];
}
return randomString;
}
- (NSString *)randomRegion
{
NBMetadataHelper *aHelper = [[NBMetadataHelper alloc] init];
NSDictionary *metadata = [[aHelper getAllMetadata] objectAtIndex:(arc4random() % [aHelper getAllMetadata].count)];
if (metadata) {
NSString *region = [metadata objectForKey:@"code"];
if (region) {
return region;
}
}
return nil;
}
- (void)testForMultithread
{
NBPhoneNumberUtil *aUtil = [NBPhoneNumberUtil sharedInstance];
NSString *testRegion = [self randomRegion];
if (!testRegion) {
return;
}
NSLog(@"- START [%@]", testRegion);
dispatch_async(dispatch_get_main_queue(), ^{
NSError *error = nil;
for (int i=0; i<10000; i++) {
NBPhoneNumber *phoneNumber = [aUtil parse:[self stringWithRandomNumber] defaultRegion:testRegion error:&error];
if (error && ![error.domain isEqualToString:@"INVALID_COUNTRY_CODE"]) {
NSLog(@"error [%@]", [error localizedDescription]);
}
if (!error) {
error = nil;
[aUtil format:phoneNumber numberFormat:NBEPhoneNumberFormatINTERNATIONAL error:&error];
}
error = nil;
}
NSLog(@"OK [%@]", testRegion);
});
}
- (void)testWithGCD
{
for (int i = 0; i<100; i++) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
[self testForMultithread];
});
}
}
- (void)testForExtraDatas
{
NBMetadataHelper *helper = [[NBMetadataHelper alloc] init];
NSArray *arrayData = [helper getAllMetadata];
if (arrayData && arrayData.count > 0) {
NSLog(@"Log sample metadata [%@]", [arrayData firstObject]);
} else {
NSLog(@"Fail to extract meta data");
}
}
- (void)testCarrierRegion
{
NSLog(@"testCarrierRegion %@", [self getPhoneNumberFormatted:@"1234567890"]);
}
- (NSString *)getPhoneNumberFormatted:(NSString *)phoneNumber
{
NBPhoneNumberUtil *phoneUtil = [NBPhoneNumberUtil sharedInstance];
NSString *retValue;
NBPhoneNumber *phoneNumberFormatted = [phoneUtil parseWithPhoneCarrierRegion:phoneNumber error:nil];
retValue = [phoneUtil format:phoneNumberFormatted numberFormat:NBEPhoneNumberFormatRFC3966 error:nil];
return retValue;
}
// FIXME: This unit test ALWAYS FAIL ... until google libPhoneNumber fix this issue
- (void)testAustriaNationalNumberParsing
{
NBPhoneNumberUtil *phoneUtil = [NBPhoneNumberUtil sharedInstance];
NSError *anError = nil;
NSString *internationalNumberForInput = @"436606545646";
NSString *nationalNumberForExpect = @"6606545646";
NSString *defaultRegion = @"AT";
NBPhoneNumber *phoneNumber = [phoneUtil parse:internationalNumberForInput defaultRegion:defaultRegion error:&anError];
NSString *nationalNumberForActual = [NSString stringWithFormat:@"%@", phoneNumber.nationalNumber];
// ALWAYS FAIL need fix "google libPhoneNumber"
NSLog(nationalNumberForExpect, nationalNumberForActual);
}
- (void)testForiOS7
{
NBPhoneNumberUtil *phoneUtil = [NBPhoneNumberUtil sharedInstance];
NSError *anError = nil;
NBPhoneNumber *myNumber = [phoneUtil parse:@"0174 2340XXX" defaultRegion:@"DE" error:&anError];
if (anError == nil) {
NSLog(@"isValidPhoneNumber ? [%@]", [phoneUtil isValidNumber:myNumber] ? @"YES":@"NO");
NSLog(@"E164 : %@", [phoneUtil format:myNumber numberFormat:NBEPhoneNumberFormatE164 error:&anError]);
NSLog(@"INTERNATIONAL : %@", [phoneUtil format:myNumber numberFormat:NBEPhoneNumberFormatINTERNATIONAL error:&anError]);
NSLog(@"NATIONAL : %@", [phoneUtil format:myNumber numberFormat:NBEPhoneNumberFormatNATIONAL error:&anError]);
NSLog(@"RFC3966 : %@", [phoneUtil format:myNumber numberFormat:NBEPhoneNumberFormatRFC3966 error:&anError]);
} else {
NSLog(@"Error : %@", [anError localizedDescription]);
}
}
- (void)applicationWillResignActive:(UIApplication *)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end