MOBILE

Swift for PHP Developers

Recently, I've been tasked with building an audio player app from scratch. The app needs to be fast, responsive, and have a clean user interface. At first, I reached for React Native, but I quickly realized that when it comes to having a native feel, nothing beats building a native app.

Some of the setbacks I encountered with React Native were lock screen controls and background audio playback issues. Sure there are packages/settings to address these issues, but they are not as seamless as I would like. After a while it seemed like I was fighting the framework more than building the app.

I decided to give Swift a try. I've always been interested in iOS development, but I never had the time to dive into it. I was pleasantly surprised at how easy it was to pick up Swift. In addition, for this app in particular all audio controls, background audio playback, and lock screen controls were built in. Not to mention Carplay and Airplay support took a few lines of code.

#Similarities to PHP

Swift has a lot of similarities to PHP. The syntax is clean and easy to read. The language is strongly typed, but it's not as strict as Java or C#. Swift has a lot of built-in functions that make it easy to work with arrays, dictionaries, and strings.

#SwiftUI

My favorite part of Swift is SwiftUI. SwiftUI is a declarative framework that makes it easy to build user interfaces. Think about like laravel blade components, but for iOS. Below is how easy it is to add a airplay button to your app:

1import SwiftUI
2 
3 // struct to add airplay button
4struct AirPlayButton: UIViewRepresentable {
5 func makeUIView(context: Context) -> AVRoutePickerView {
6 let routePicker = AVRoutePickerView()
7 routePicker.tintColor = .white
8 routePicker.activeTintColor = .red
9 return routePicker
10 }
11 
12 func updateUIView(_ uiView: AVRoutePickerView, context: Context) {}
13}
14 
15// add airplay button to your view
16struct ContentView: View {
17 var body: some View {
18 VStack {
19 AirPlayButton()
20 }
21 }
22}

VStack and HStack are fundamental layout containers in SwiftUI that help arrange views in vertical and horizontal orientations respectively. Let me break them down:

1// Arranges views vertically from top to bottom
2VStack {
3 Text("First Item")
4 Text("Second Item")
5 Text("Third Item")
6}
7 
8// Arranges views horizontally from left to right
9HStack {
10 Text("Left")
11 Text("Middle")
12 Text("Right")
13}

Now lets put it together. Below is an example of using VStack and HStack together.

1import SwiftUI
2 
3struct ContentView: View {
4 var body: some View {
5 VStack(alignment: .leading, spacing: 20) {
6 Text("Header")
7 .font(.title)
8 
9 HStack(spacing: 10) {
10 Image(systemName: "star.fill")
11 Image(systemName: "star.fill")
12 Image(systemName: "star.fill")
13 Image(systemName: "star.fill")
14 Image(systemName: "star.leadinghalf.filled")
15 }
16 
17 HStack(spacing: 10) {
18 Text("Rating")
19 Text("4.5")
20 }
21 
22 Text("Description")
23 .font(.body)
24 }
25 }
26}

#Getting Started

Installing Xcode is the first step to start building iOS apps with Swift. Xcode is the official IDE for iOS development, and it's available for free on the Mac App Store. Once you have Xcode installed, you can start a new project and choose the SwiftUI App template. This template will give you a basic app structure with a ContentView.swift file that you can start editing right away.

Pro Tip: If you're coming from PHP, you might find the Swift Playground app useful. It's a great way to experiment with Swift code without having to create a full Xcode project.

Another Pro Tip: Install the Copilot extension for Xcode. It's a great way to speed up your development process by generating code snippets for you.

#Final Thoughts

This is not to say that React Native is bad. It's a great framework for building cross-platform apps, and it has a large community and ecosystem. However, in this particular case, I needed the app to have a native feel and seamless integration with iOS features. Swift and SwiftUI provided that for me.

If this particular app weren't soo audio focused, I would have probably stuck with React Native. But for now, I'm happy with my decision to switch to Swift.