Build iOS App with AI Assisted Coding
Challenge: build an iOS app with the help of GPT and GitHub Copilot in two days.
I have pretty much zero knowledge coding native iOS apps in Swift. My only experience dealing with app developmnet is using React Native.
My goal is to build an iOS + Watch app that, when the iOS device is physically moved or picked up, immediately sends a message to my paired watch to alert me. I got this idea when I went to a local swimming pool. I often leave my phone in the backpack, put it on an open shelf, and go swimming. I want to know if anyone tries to take my backpack.
Here are the steps I took to build the app.
-
Start a new Xcode project using “WatchOS” template -> Watch App with New Companion iOS App.
-
Ask ChatGPT with this prompt:
"Write a simple iOS project. The goal is to use iPhone motion sensor to detect movement of the phone. If a movement is detected, send a notification to a paired apple watch."
-
I got some code from the answer, while gives me a basic idea of the library and commands that can detect motion with the accelerometer. However, I realized that the code is using an older coding style
AppDelegate
, which is not used in the new project generated by the template. I am using Xcode 14.3. -
Also, I found it too clumbersome to copy & paste between the chat.openai.com browser tab and the Xcode. So I opened the project directory with VScode, and installed the extension
DanielSanMedium.dscodegpt
-
The extension is quite convenient. I can highlight part the code, and then ask GPT questions regarding the selected lines of code. Also, I can also GPT to improve, find problems, and document the selected code.
-
I also wanted to try GitHub Copilot. Unlike the CodeGPT extension, the default way to use GitHub Copilot is to create functions. It is not as versatile.
-
I found that there are basically 2 ways to pass message from iOS app to Watch:
- Local push notification - this is the original way I imagined. I wanted the watch app to stay in the background while I swim, with the Fitness app tracking my swimming data. But later on I found that by default if the companion iOS app is in the foreground, push notification will be displayed there rather than on the watch. Furthermore, if I want the iOS app to be run on the background detecting motion, I will need to jump through more hoops and get background capabilities.
- WatchConnectivity session - only work when both iOS and Watch apps are in the foreground. For this experiment I think this is good enough.
-
Unfortunately, neither CodeGPT nor Copilot could give me a working code snippet for the WatchConnectivity session. So I went back to the basic: Google. After a few pages, I found this article: Sending Data Between watchOS and iOS App Using Watch Connectivity by Baha Abisheva that can quickly get the result I want.
-
Tada! The app is working! See the project in GitHub: 👉 https://github.com/calvincchan/PhoneWatcher
Learnings
-
Assisted coding is not super robust at the moment, but the development will be accelerated. It is only getting better and better.
-
Human vetting is still necessary.
-
“Explain the selected code” and “Check of mistakes” are super useful to spot common mistakes. It also helped me to check the Swift code that I am not familiar with.
-
Yet to try the “Create unit tests” feature in CodeGPT. I will test it in other projects.
It is a good experiment to get something working in an unfamiliar programming language. Next, I will try to use these tools on existing projects in programming languages I am familiar with.