Skip to content

Resolving PhoneNumberKit Errors in Flutter iOS Builds

Problem Statement

When building a Flutter app for iOS, developers may encounter these critical errors related to the PhoneNumberKit library:

  1. Cannot find type 'PhoneNumberKit' in scope: This compilation error occurs when Xcode fails to recognize the PhoneNumberKit dependency during the build process.

  2. Extra argument 'phoneNumberKit' in call: This error indicates a mismatch between function signatures and actual arguments passed when using PhoneNumberKit APIs.

These issues typically appear after adding PhoneNumberKit to a Flutter project, despite following installation documentation. The core problem stems from version mismatches and improper pod cache management, where:

  • The iOS build system can't locate the library
  • API versions may conflict between expected and actual implementations
  • Build artifacts from previous installations cause conflicts

Solution: Version Pinning and Clean Rebuild

The most reliable solution involves pinning a compatible PhoneNumberKit version and performing a complete rebuild of your project dependencies. Follow these steps:

1. Update Podfile Configuration

Modify your ios/Podfile to specify a compatible PhoneNumberKit version:

ruby
target 'Runner' do
  use_frameworks!
  use_modular_headers!

  # Add this line with the recommended version
  pod 'PhoneNumberKit', '~> 3.7.6'

  flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
end

Key changes made:

  • Added explicit version constraint ~> 3.7.6
  • Ensured use_frameworks! is present for Swift libraries
  • Confirmed modular headers are enabled with use_modular_headers!

Why version 3.7.6?

This specific version resolves both errors:

  • Includes necessary type definitions to resolve Cannot find type errors
  • Maintains API compatibility to prevent Extra argument errors

2. Execute Clean Rebuild Commands

Run these terminal commands in sequence from your project root:

bash
# Remove iOS build artifacts and pod cache
rm -rf ios/Pods ios/Podfile.lock

# Update CocoaPods repository
pod install --repo-update

# Reset Flutter build environment
flutter clean

# Rebuild iOS project
flutter build ios --no-cache

Critical Clean Order

Execute these commands exactly in sequence. Skipping steps or changing the order may leave conflicting artifacts in your build environment.

Explanation of the Solution

This approach works by addressing multiple potential failure points:

  1. Version Conflicts
    Pinning ~> 3.7.6 ensures compatibility with Flutter's iOS build system. Newer versions may contain API changes that haven't been validated with Flutter.

  2. Cache Corruption
    Removing Pods/ and Podfile.lock eliminates corrupted or outdated dependency artifacts that cause Cannot find type errors.

  3. API Mismatch Resolution
    The specific version 3.7.6 maintains consistent method signatures that prevent Extra argument errors during calls.

  4. Complete Environment Reset
    The flutter clean command clears outdated build artifacts that might reference older library versions.

Compatibility Note

This solution has proven effective with these environment versions:

  • Flutter 3.29+
  • CocoaPods 1.15.0+
  • Xcode 16.0+

Verification Steps

Confirm the solution worked by:

  1. Opening Xcode Workspace:

    bash
    open ios/Runner.xcworkspace
  2. Checking PhoneNumberKit import:
    Add this test code in any Swift file:

    swift
    import PhoneNumberKit
    
    func testPhoneNumberKit() {
        let phoneNumberKit = PhoneNumberKit()
        print("Library loaded successfully")
    }
  3. Building the project:
    Press ⌘ + B in Xcode and verify a clean build

Alternative Troubleshooting

If issues persist, consider these additional measures:

Reinitialize iOS Project

bash
flutter create --platforms ios .

Verify Swift Compiler Settings

  1. Open Runner.xcworkspace in Xcode
  2. Go to Build Settings > Swift Compiler - General
  3. Confirm PhoneNumberKit appears in "Install Objective-C Compatibility Header" list

Additional Considerations

  • Version Flexibility: Once confirmed working, you can cautiously update to newer versions using semantic versioning:

    ruby
    pod 'PhoneNumberKit', '~> 3.7'  # Allows patch updates
  • Dependency Conflicts: If using other native libraries, check for PhoneNumberKit conflicts with:

    bash
    pod outdated
    pod deintegrate
  • Long-term Maintenance: Monitor Flutter and CocoaPods releases for breaking changes that might require version updates.

This comprehensive approach resolves the immediate PhoneNumberKit errors while establishing a stable foundation for future updates. By explicitly managing versions and build artifacts, you'll maintain consistent iOS builds across your development environment and CI systems.