iOS Question B4A PhoneNumberUtils for B4I

fbritop

Active Member
Licensed User
Longtime User
I have the following code in B4A

B4X:
Dim n As String="5699126532"
Dim r As Reflector
n=(r.RunStaticMethod("android.telephony.PhoneNumberUtils", "formatNumber", Array As Object("+" & n, Null), Array As String("java.lang.String", "java.lang.String")))

Which gives me a correct output:
For 56991296532 it gets 56 9 9129 6532
For 5491133954455 it gets 54 9 11 3395-4455

Both outputs are correct, as it is the way each country formats the number. Is there any similar function I can achieve this results in B4I?

Thanks
 

fbritop

Active Member
Licensed User
Longtime User
Thanks

I´m aware of that old thread on mine, but as you always tell us. "Start a new thread for this question"... was strictly followed.
1598614846589.png


Anyway, I have that function at it works OK, the only thing is that you need to be updating the lib every while. But the end question in not looking if the number is valid, but format the output (according to the country code +56/+54 for example.

I remember have given a try to libPhoneNumber some time ago, but I could not resolve how to get the formatting done (I´m almost close to null when it comes to OBJC

Thanks Again
 
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
For example, you want to format international number.

1) Start B4I, create a new "Default" project. Set your bundle id and add to Project Attributes
B4X:
    #CertificateFile : your.cer    ' <- Change
    #ProvisionFile   : your.mobileprovision ' <- Change
  
    #AdditionalLib: libPhoneNumberiOS.framework.swift.3

2) Add following code to the bottom of the module
B4X:
#if OBJC
#import <libPhoneNumberiOS/libPhoneNumberiOS.h>
- (NSString*) PhoneInternational: (NSString*) Number : (NSString*) DefaultRegion
   {
   NBPhoneNumberUtil *phoneUtil = [[NBPhoneNumberUtil alloc] init];
   NSError *anError = nil;
   NSString *answer = nil;
   NBPhoneNumber *myNumber = [phoneUtil parse: Number defaultRegion: DefaultRegion  error: &anError];
   if (anError == nil) { answer = [phoneUtil format: myNumber numberFormat: NBEPhoneNumberFormatINTERNATIONAL error: &anError]; };
   if (anError != nil) { answer = [anError localizedDescription]; };
   return answer;
  }
#End If

3) Add call to Application_Start subroutine
B4X:
Dim nme As NativeObject = Me
Log (nme.RunMethod ("PhoneInternational::", Array ("+5491133954455", "")).AsString)

Run on iPhone. The result (in Log) will be +54 9 11 3395-4455

You can specify national number also (without + and country code), but in this case you must specify a region:
B4X:
Log (nme.RunMethod ("PhoneInternational::", Array ("0111533954455", "AR")).AsString)

If you want output as national number, replace NBEPhoneNumberFormatINTERNATIONAL to NBEPhoneNumberFormatNATIONAL.
In this case output will be 011 15-3395-4455
 
Last edited:
Upvote 0
Top