natintosh / intl_phone_number_input

MIT License
166 stars 517 forks source link

Crashes on IOS #286

Open Hwan-seok opened 3 years ago

Hwan-seok commented 3 years ago

Describe the bug The application crashes when calling PhoneNumber.getRegionInfoFromPhoneNumber Sorry because I cannot reproduce it but the firebase crash report says as follows.

Package version 0.7.0+2

Flutter version 2.5.3

To Reproduce call

await PhoneNumber.getRegionInfoFromPhoneNumber(
  "phone_number",
  "82",
);

Expected behavior A clear and concise description of what you expected to happen.

Screenshots If applicable, add screenshots to help explain your problem.

Targeted platforms (please complete the following information):

Additional context This is the crash report

Crashed: com.apple.main-thread
0  libphonenumber_plugin          0x9b6c specialized SwiftLibphonenumberPlugin.getRegionInfo(call:result:) + 85 (SwiftLibphonenumberPlugin.swift:85)
1  libphonenumber_plugin          0xaee4 specialized SwiftLibphonenumberPlugin.handle(_:result:) + 528 (<compiler-generated>:528)
2  libphonenumber_plugin          0x80f8 @objc SwiftLibphonenumberPlugin.handle(_:result:) + 72 (<compiler-generated>:72)
3  Flutter                        0x4ecdc0 (누락)
4  Flutter                        0x3ff34 (누락)
5  Flutter                        0x348db4 (누락)
6  Flutter                        0x2ee068 (누락)
7  Flutter                        0x2f1098 (누락)
8  CoreFoundation                 0xae268 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 32
9  CoreFoundation                 0x32c40 __CFRunLoopDoTimer + 1076
10 CoreFoundation                 0x2d43c __CFRunLoopDoTimers + 328
11 CoreFoundation                 0xbc58 __CFRunLoopRun + 1944
12 CoreFoundation                 0x1f3b8 CFRunLoopRunSpecific + 600
13 GraphicsServices               0x138c GSEventRunModal + 164
14 UIKitCore                      0x5196a8 -[UIApplication _run] + 1100
15 UIKitCore                      0x2987f4 UIApplicationMain + 2092
16 Runner                         0x75d4 main + 6 (AppDelegate.swift:6)
17 ???                            0x1014c5a24 (누락)
tudor07 commented 2 years ago

I ran into the same issue for +1000002405

jsmeke commented 2 years ago

Same issue here

dvanphu commented 2 years ago

I ran into the same issue for +123456789

mehdico commented 2 years ago

Same issue with latest version (0.7.1)

mingzew commented 2 years ago

Bumping this

praharshbhatt commented 1 year ago

Still face this issue on the latest version (0.7.1)

tudor07 commented 1 year ago

Found another one: +1 945-894-9194. What's worse is that Android allows this phone number but iOS crashes.

praharshbhatt commented 1 year ago

This is what I am doing as a temporary fix for now:

Using phone_number, I validate the phone number before initializing intl_phone_number_input.

import 'package:phone_number/phone_number.dart' as PhoneNumberValidator;

...

      PhoneNumberValidator.PhoneNumber validatedPhoneNumber =
          await PhoneNumberValidator.PhoneNumberUtil()
              .parse("+1 945-894-9194");
      phoneNumber = PhoneNumber(
          phoneNumber: validatedPhoneNumber.nationalNumber,
          dialCode: validatedPhoneNumber.countryCode,
          isoCode: validatedPhoneNumber.regionCode);
darkstarx commented 1 year ago

@natintosh can you fix the issue, please? This is a regression. The problem is in the libphonenumber_plugin Here:

    func getRegionInfo(call: FlutterMethodCall, result: @escaping FlutterResult) {
        let arguments = call.arguments as! Dictionary<String, Any>
        let phoneNumber = arguments["phoneNumber"] as! String
        let isoCode = arguments["isoCode"] as! String  //<<< HERE the bug #1

        do {

            let p: PhoneNumber = try phoneNumberKit.parse(phoneNumber, withRegion: isoCode.uppercased(), ignoreType: true)

            let regionCode: String? = phoneNumberKit.getRegionCode(of: p)
            let countryCode: String? = phoneNumberKit.mainCountry(forCode: p.countryCode)  //<<< HERE the bug #2
            let formattedNumber: String? = phoneNumberKit.format(p, toType: PhoneNumberFormat.national)

            let data : Dictionary<String, String?> = ["isoCode": regionCode, "regionCode" : countryCode, "formattedPhoneNumber" : formattedNumber]

            result(data)

The fix of the bug #1:

        var isoCode = arguments["isoCode"] as! String
        if (isoCode.isEmpty) {
            isoCode = PhoneNumberKit.defaultRegionCode()
        }

This will fix this issue.

The fix of the bug #2: let countryCode: String = String(p.countryCode) This will fix an issue with formatting of the input number. Now if you input the number, it contains digital country code (like +1), but it should not contain.

darkstarx commented 1 year ago

Oh, I see that it's already fixed in the libphonenumber_plugin: 0.3.1, so @natintosh could you just update the dependency, please.