Test iOS phone number input contracts on a cloud Mac

Test iOS phone number input contracts on a cloud Mac

Just before release, the sign-up screen accepts a number locally but reports a format error on another Mac test machine. Before blaming the simulator, inspect the input: pasted text may contain a nonbreaking space, or the validator may treat full-width digits as ordinary digits. Turning phone number rules into an executable input contract makes the problem easier to isolate than repeatedly clicking the sign-up button.

Define what the tests actually prove

This article validates just one storage format: an ASCII plus sign followed by up to fifteen ASCII digits, with a nonzero first digit. This is a structural gate for international number representation, not a complete numbering-plan validator.

Passing the structural check does not establish that the country calling code is assigned, that the number's length meets local rules, or that the number can receive verification messages. The interface must distinguish “invalid format” from “verification incomplete” rather than using one success state for both stages.

A correctly formatted number, a routable number, and a number held by the user are three different conclusions. Test names and error messages should express them separately; a single Boolean cannot represent every business state.

This approach is useful for investigating input differences across machines. It is not a standalone solution for parsing international phone numbers. If the product accepts local formats, add a separate parsing layer with region context instead of piling on more regular expressions.

Establish a minimal runner on a cloud Mac

First, run these two commands in Terminal to confirm that the selected developer tools are available, and save their output with the test results. You can check the string rules without launching a simulator.

xcrun swift --version
xcodebuild -version

Save the code below as PhoneContract.swift and run xcrun swift PhoneContract.swift. It depends only on the standard library, throws an error on failure, and exits with a nonzero status. The short numbers in the example are synthetic data, not examples of actual assignments.

For team regression runs, record the source commit, toolchain version, and fixture revision. Do not rely on a tester's current keyboard layout or grab ad hoc samples from the clipboard; either makes the same failure difficult to reproduce.

Enforce a strict storage boundary with byte checks

The implementation deliberately checks UTF-8 bytes rather than using a broad “is this a digit character?” test. Unicode includes several kinds of digit characters, but they fall outside the ASCII range this contract allows.

enum PhoneError: Error {
    case invalidShape
}

func canonicalPhone(_ input: String) throws -> String {
    let bytes = Array(input.utf8)
    guard (2...16).contains(bytes.count),
          bytes[0] == 43,
          (49...57).contains(bytes[1]),
          bytes.dropFirst().allSatisfy({ (48...57).contains($0) })
    else {
        throw PhoneError.invalidShape
    }
    return input
}

func accepts(_ input: String) -> Bool {
    do {
        _ = try canonicalPhone(input)
        return true
    } catch {
        return false
    }
}

struct FixtureFailure: Error {
    let label: String
}

let valid = [
    "+1",
    "+1234",
    "+" + String(repeating: "1", count: 15)
]

let invalid = [
    "",
    "+",
    "1234",
    "+0123",
    "+" + String(repeating: "1", count: 16),
    "+1234",
    "+1234",
    "+١٢٣٤",
    "+12 34",
    "+12\u{00A0}34",
    "+12\u{200B}34",
    "+1234\n",
    "+1234x9",
    "+12\u{0000}34"
]

for (index, value) in valid.enumerated() {
    guard accepts(value) else {
        throw FixtureFailure(label: "valid-\(index)")
    }
    guard try canonicalPhone(value) == value else {
        throw FixtureFailure(label: "roundtrip-\(index)")
    }
}

for (index, value) in invalid.enumerated() {
    guard !accepts(value) else {
        throw FixtureFailure(label: "invalid-\(index)")
    }
}

Accepting +1 here makes the boundary explicit: this function does not claim to know the minimum length of an assignable number. If the business rules require region-specific length checks, perform them in the next layer and maintain separate test data.

Production entry points must also limit the size of the raw request. This example copies the entire byte array, so do not use it directly on unbounded external text. A request-body size budget and field-structure validation are two independent lines of defense.

Separate UI cleanup from strict validation

Do not simply delete every nondigit character. That can silently splice extensions, letters, or malformed input into a different number, turning a visible rejection into the successful storage of an incorrect identifier.

If the interface allows spaces, parentheses, or hyphens, specify exactly which characters are allowed and how they are converted, and show the final value that will be submitted. A change in display formatting must not, by itself, overwrite the stored value with the formatted text.

Add at least these three groups of UI assertions:

  • Pasting text containing spaces produces the cleanup result defined by the product rules; disallowed characters trigger visible feedback.
  • Running cleanup again on an already-cleaned value leaves it unchanged, so the number does not change with every edit.
  • Extensions go into a separate field rather than being merged with the main number; the original input remains editable after a failed submission.

Do not use real contacts as test account data. Failure logs should contain only fixture identifiers, error types, and lengths—not complete phone numbers used by the application—so regression artifacts do not become copies of personal information.

Integrate with the iOS project and define the acceptance gate

Once the standalone script passes, put the same rules in the project's shared business-logic module and move these test vectors into unit tests. The script proves only the example implementation; it cannot replace tests of the application's actual call paths.

Add two end-to-end checks in the simulator: the canonical value submitted by the interface matches the network request field, and the screen does not continue to show a verified state when the backend rejects the number. Keyboard type is an input convenience, not a barrier against pasting, autofill, or programmatic assignment.

The final gate must satisfy all of these conditions: valid input round-trips unchanged, invalid characters are rejected, the fifteen-digit and sixteen-digit boundaries are clearly distinguished, cleanup is idempotent, extensions remain separate, and errors do not expose phone numbers. When results differ across machines, compare the raw bytes and toolchains first, then inspect the UI path.

Capture failing samples as escaped literals and commit them to the repository. The next time the team changes Mac test machines or adjusts an input component, it can determine whether the difference comes from the rules, the display layer, or the backend—without relying on someone to paste the same unusual character again.

Frequently asked questions

Does passing an E.164 structure check prove a number exists?

No. Checking the plus sign, digits and length does not establish that a country code is assigned, the number is reachable or the user controls it.

Why not store a phone number as an integer?

A phone number is an identifier, not a quantity. Integer conversion loses representation details such as the plus sign. Store the normalized number as a string and any extension separately.

Validate your regression tests in a dedicated environment

Need an extra machine for a short release regression cycle? Rent a dedicated physical Mac mini—not a virtual machine. Teams running at full capacity year-round or only lightweight unit tests may be better off buying their own machine or using a shared environment.

Rent a dedicated Mac for testing

Try one regression cycle to see if the environment is right for you

Need extra capacity for a release window? Start with a short rental. If you need full-time capacity year-round or only run lightweight unit tests, buying your own machine or using a shared environment may be a better fit.