Categories
Mobile Programming

Convert a number into words in a couple of lines of Swift

https://giphy.com/gifs/whoa-hd-tim-and-eric-xT0xeJpnrWC4XWblEk

Converting a number into its word form — for example, converting 1,234,456 into one million, two hundred and thirty-four thousand, four hundred and fifty-six — used to be a really painful exercise in programming. These days, there are libraries that save you from having to do this, and in the case of Swift, this functionality is built into its standard library in the form of the NumberFormatter class. Not only will it convert numbers into words, but it will also do so for many languages!

Open a Swift playground in Xcode and enter the following code:

// Swift

let formatter = NumberFormatter()
formatter.numberStyle = .spellOut
let number = 87654
let spelledOutNumber = formatter.string(for: number)!
print("\(number) spelled out is \(spelledOutNumber).")

Run the playground code, and you’ll see this:

87654 spelled out is eighty-seven thousand six hundred fifty-four.

Having come from the world of C, where you format strings using printf() and formatting strings, and later from other languages where you use whatever formatting method its string class provides, I’ve ignored most of Swift’s classes that derive from Formatter — with one notable exception: DateFormatter, which is indispensable when working with dates and times.

I’m now looking for an excuse to use this capability.

As I typed “DateFormatter” a couple of paragraphs above, I remembered that DateFormatter had a locale property. It’s for ensuring that any dates you present are in the correct form for the locale:

I wondered:

  • Does NumberFormatter have a locale property?
  • What happens if I changed it to something other than my system’s default of US English?

So I changed the code in my playground to the following:

// Swift

let formatter = NumberFormatter()
formatter.numberStyle = .spellOut
formatter.locale = Locale(identifier: "fil_PH")
let number = 87654
let spelledOutNumber = formatter.string(for: number)!
print("\(number) spelled out in Filipino is \(spelledOutNumber).")

I ran the code and saw this…

87654 spelled out in Filipino is walóng pû’t pitóng libó’t anim na daán at limáng pû’t ápat.

…and my response was “Ay nako!” (translation: OMG!)

How about Korean?

// Swift

let formatter = NumberFormatter()
formatter.numberStyle = .spellOut
formatter.locale = Locale(identifier: "ko_KR")
let number = 87654
let spelledOutNumber = formatter.string(for: number)!
print("\(number) spelled out in Korean is \(spelledOutNumber).")

The output:

87654 spelled out in Korean is 팔만 칠천육백오십사.

My response: 세상에 (“Sesange!”, which is pretty much Korean for OMG!)

Try it out!  You might find this list of iOS locale string identifiers useful.

Tampa Bay Apple Coding Meetup (Tampa, FL) | Meetup

Speaking of Swift development, I’m restarting the Tampa Bay Apple Coding Meetup in the next few weeks. Watch this space, and if you’re interested, join the meetup group!

Categories
Mobile Programming

How to sort import statements in Android Studio

When you do native Android development, you use import statements. A lot of import statements. Even the “Hello World”-ish template app that Android Studio generates when you select Empty Compose Activity contains 11 import statements:

Worse still, you end up adding more import statements over time, as you add more code. It’s easy to end up with a screenful or more of import statements, which makes them more difficult to manage.

How I used to sort imports in Android Studio

I used to keep import statements organized by occasionally rearranging them into alphabetical order by hand. This soon became annoying, so I automated the process with a simple Jupyter Notebook. Using this Notebook, I’d copy the import statements…

…and then paste them into a variable, which would then be processed with a Python one-liner:

# Python
print('\n'.join(sorted(import_lines.splitlines())))

Here’s screenshot showing how I last used my import-organizing Jupyter Notebook:

After running it, I’d copy the one-liner’s output and paste it back into my Android code. Problem solved!

How to sort imports in Android Studio

I’ve only recently found out about the Optimize Imports option in Android Studio (and many other JetBrains IDEs), and it does a better job that my Jupyter Notebook, which simply puts the imports in alphabetical order. It also:

  • Deletes duplicate import lines (I should update my script to do the same), and
  • consolidates imports when possible.

Categories
Mobile Programming

Learn how to add Auth0 login/logout to Android apps

“Add login/logout to Android apps,” featuring the screens from the demo app for the tutorial.

Android is the most popular operating system in the world and runs on more devices than any other OS. With 3 billion users and 70% of the mobile OS market share, you want to write apps for Android! And sooner or later, you’ll need to write an Android app that requires the user to log in and log out.

This video and written tutorial — starring and written by Yours Truly — shows you how to do it with Auth0 by Okta.

Here’s the video…

…here’s the article…

Screenshot from the article “Get Started with Android Authentication Using Kotlin”.
Tap to go to the article.

…and here’s the Github repository containing both the starter and the completed projects for the tutorial.

I hope you find them helpful, and if you have any questions, feel free to email me or ask in the comments!

Categories
Meetups Mobile Programming Tampa Bay

Tampa Bay Apple Coding Meetup needs a place for its sessions!

I’m ready to starting holding sessions of Tampa Bay Apple Coding Meetup, but I’m missing one thing: a place to hold them!

Prior to the pandemic, I’d been doing monthly meetups where I’d walk Tampa Bay’s developers through “code along with me” tutorials where I’d show them how to write native iOS apps in Swift such as:

  • A simple text editor
  • Pomodoro timer
  • “Magic 8-ball”
  • Weather app with geolocation
  • “Frogger” / “Crossy Road”-style game
  • An augmented reality app similar to the IKEA furniture app
  • A machine learning app that can identify specific images

It’s time to bring these meetups back!

I need a place in the Tampa Bay area where I can lead a group through an exercise where we get together and build an iPhone, iPad, Apple Watch, Apple TV, Mac, or augmented reality app together. If you’d like to host such an event or know someone or some organization that would be willing to do this, please let me know via email.

Categories
Meetups Mobile Programming Tampa Bay

Tampa’s iOS meetup is coming back as Tampa Bay Apple Coding!

I’m bringing back the Tampa iOS Meetup in 2023 under a new name: Tampa Bay Apple Coding Meetup. If you’re in the Tampa Bay area and want to learn how to program Apple devices in Swift, this meetup will be for you!

I’ve got a fair bit of experience showing people how to build applications for Apple devices and technologies, from doing presentations on coding augmented reality apps for the iPhone…

…to co-authoring the book on iOS development…

…to writing all sorts of apps:

These meetups will follow my usual modus operandi:

  • We’ll define a simple app that we want to make for the iPhone/iPad, Apple Watch, Apple TV, or other Apple device.
  • Then we’ll look at the tools and techniques that will allow us to create that app.
  • And finally, you’ll code along with me as we build the app together. You’ll leave the meetup with either a complete app, or at least a part of the app that you can continue working on.

The goal is to help you learn coding or sharpen your skills by building apps for the preferred devices of the digerati!

I’m currently working on getting a space for the first meetup of Tampa Bay Apple Coding in January — watch this space for announcements!

To find out more, see the Tampa Bay Apple Coding meetup page.

Categories
Mobile Programming

Add Auth0 authentication to your Jetpack Compose Android apps!

My newest article on the Auth0 Developer Blog is a two-part that covers adding authentication to Android apps built using the Jetpack Compose UI framework!

Here are the two parts:

  1. Jetpack Compose Basics: An introduction to Jetpack Compose through building a single-screen app with composable functions and managing state the Jetpack Compose Way.
  2. Adding authentication: Taking the app from part one and adding Auth0 authentication.

I’m working on the companion video for the article as I write this — keep an eye out for it!

Categories
Current Events Mobile Programming

A mobile dev’s perspective on that old Vine code

Of the many ideas for Twitter that Elon Musk has been posting for public reaction since taking over, the best one — an easy choice, as most of the ones he’s tweeted run the gamut from dismal to disastrous — is resurrecting Vine, the proto-TikTok short video service where you could share incredibly short (six-second) looping videos:

Vine started as its own company in June 2012, Twitter acquired it in October 2012, and its first official release was in January 2013. Twitter announced that it would discontinue Vine in October 2016 and disabled all uploads.

If Vine could be turned into a TikTok competitor as YouTuber Jimmy “MrBeast” Donaldson suggests, it could help counter TikTok’s serious security issues and help give Twitter a much-needed image boost.

Here’s the problem:

Musk has asked engineers to look at the old code. This would be a sensible thing if we were dealing with a hardware, mechanical, or physical artifact, but Vine is software. Even more challenging is the fact that it’s mobile software. That was first released in 2012 and last updated in 2016.

As software developer and product manager Sara Beykpour put it (and on Twitter, no less!):

An actual book from the early Vine era.

I have no inside knowledge of Vine, but I’d be willing to bet that the Android and iOS Vine apps were probably written as native apps. React Native wasn’t out until 2015, and every other cross-platform solution at the time (including Xamarin) wouldn’t have been up to the task.

In 2012, when Vine began, that would mean:

  • On Android: writing the app in Java using Eclipse (the stable 1.0 version of Android Studio wouldn’t come out until December 2014).
  • On iOS: writing the app in Objective-C, quite possibly using NIBs instead of storyboards.

Simply put — ancient stuff, at least by the standards of mobile development. While there are still some Java-based Android and Objective-C-based iOS projects out there, the majority of the top apps in the stores are written in either Kotlin or Swift.

It would be a good idea to have a handful of developers look over the old Vine code for an audit. There’s a chance that there might be a few useful ideas in there, possibly in feature switches that never got turned on. However, that old code will probably be useless as a starting point to build on in the short timeframe that Musk suggests for the relaunch of Vine.

In case you were wondering…

…the flagship phones of 2016, when Vine was active, were the Samsung Galaxy S7 on the Android side, which came with Android 6 (a.k.a. “Marshmallow”, a.k.a. API 23)

…and on the iOS side, it was the iPhone 7, which came with iOS 10.