gernot, German
@gernot@mas.to avatar

Checking and formatting phone numbers for app signup in #swiftUI. How hard can it be?

(Turns out: Hard. And I suspected that… Still sucks though.)

uliwitness,
@uliwitness@chaos.social avatar

@gernot At least Apple has a built-in formatter for those these days… imagine having to maintain the German area code table…

gernot,
@gernot@mas.to avatar

@uliwitness I don’t need String <-> Beautiful String. I need String <-> Structured Phone Number Data, and I need this done in a way that let’s the user edit the country prefix in a separate SwiftUI Picker. And did I mention that we only support a subset of all country prefixes?

gernot,
@gernot@mas.to avatar

@uliwitness Oh, and we need it foolproof, so someone entering a number who’s never heard of country prefixes can cope.

I am currently playing around with „PhoneNumberKit“, https://github.com/marmelroy/PhoneNumberKit but as good as their parsing is: They have never heard of SwiftUI.

ctietze,
@ctietze@mastodon.social avatar

@gernot @uliwitness If the parser is good enough, sounds like a somewhat simple transformation using a view model (name used intentionally and properly this time), though?

gernot,
@gernot@mas.to avatar

@ctietze @uliwitness Nope. Because I cannot generate the structured data with anything but the parser…

ctietze,
@ctietze@mastodon.social avatar

@gernot @uliwitness Can you explain?

It looks to me like it can parse "round trip" values, i.e.
String→PhoneNumber→String→PhoneNumber

You can have it parse any String input,

get a structured PhoneNumber value,
(display this in the UI),

encode that into a String (with the .e164 strategy, the other one uses a dictionary),

then parse that again.

If the UI starts with empty fields, and the user pastes +1234567 into a text field, that should work to kick-start things?

gernot,
@gernot@mas.to avatar

@ctietze @uliwitness Say a User has +49 123 1234. Changes the prefix to +43. What to do then? (PhoneNumber.countrycode is not mutable, PhoneNumber has no initializers exept being returned from a formatter)

ctietze,
@ctietze@mastodon.social avatar

@gernot @uliwitness Oh dear, didn't notice the immutability! That is awkward.

You can initialize from dictionary or JSON though:
https://github.com/marmelroy/PhoneNumberKit/blob/7678318c0c5b88051b9e26fd86b7af6484463339/PhoneNumberKit/PhoneNumber%2BCodable.swift

Since that's possible, the same invariant protections should be available in a public initializer IMHO. PR Should be trivial

gernot,
@gernot@mas.to avatar

@ctietze @uliwitness Yeah, mutating the stuct via codable is giving me severe headaches.

gernot,
@gernot@mas.to avatar

@ctietze @uliwitness There are other super crazy design decisions. PartialFormatter class that doesn’t conform to the Formatter protocol. The fact that their actual PhoneNumberFormatter does, but needs a „generatedPhoneNumber = true“, because its default mode is NOT doing formatting to PhoneNumber (and it silently fails in SwiftUI Textfields). And there is a Class called „Formatter“ in the framework that is not Foundations’ Formatter. (1/2)

gernot,
@gernot@mas.to avatar

And yeah, you can format using an instance of the PhoneNumberKit class.

It’s a madhouse. (2/2)

gernot,
@gernot@mas.to avatar

@ctietze @uliwitness Replacing the +49 with +43 on the String is NOT what I want, because avoiding this stuff is why I want to use PhoneNumberKit in the first place.

  • All
  • Subscribed
  • Moderated
  • Favorites
  • SwiftUI
  • DreamBathrooms
  • ngwrru68w68
  • modclub
  • magazineikmin
  • thenastyranch
  • rosin
  • khanakhh
  • InstantRegret
  • Youngstown
  • slotface
  • Durango
  • kavyap
  • mdbf
  • GTA5RPClips
  • JUstTest
  • ethstaker
  • normalnudes
  • tester
  • osvaldo12
  • everett
  • cubers
  • tacticalgear
  • anitta
  • provamag3
  • Leos
  • cisconetworking
  • megavids
  • lostlight
  • All magazines