Blog

How To Use ChatGPT for Coding iOS Apps

You may already know about ChatGPT, a free, open-source artificial intelligence large language model (LLM) from OpenAI. You may even wonder if tools like this pose a threat to your job as a developer. But, if you haven’t yet explored how ChatGPT could help you code, you’re missing opportunities to save time that you could be spending on more exciting projects! 

In this blog post, we’ll look into specific examples of how you can use ChatGPT for coding iOS apps. Let’s get into it!

So, what is ChatGPT?

I decided that a great way to learn about what ChatGPT can do would be to ask it! So I wrote this prompt:

1.png

What interested me most about this generated description is it doesn’t mention how useful it can be for coding. That’s a significant oversight, in my opinion, as I have already begun to find ways to use it in my development work. Let’s explore some basic examples.

Using ChatGPT and SwiftUI for basic iOS development

I wanted to build a view at the top of an iOS screen with a close button and a title. I decided to use SwiftUI and ChatGPT to quickly generate a fully working piece of code, and provide a quick generic explanation of the code (a coding best practice).

2.png

And here’s the generated code description from ChatGPT:

In this example, the view uses a VStack to stack the close button and title horizontally at the top of the view, and a HStack to position the close button to the left and the title to the right. The close button is implemented as a button with an action that can be filled with the code to close the view. The title is implemented as a text view with the font set to the system’s title font. The entire HStack is given some padding to add some space between the elements and the edges of the screen.”

If you add this code to a SwiftUI project, you’ll see this:

3.png

It may not look like much, but consider for a moment that you didn’t have to write that basic code yourself!

Using ChatGPT and SwiftUI to customize basic iOS code

In the example above, you might have noticed that the view generated by the code isn’t positioned at the top of the iOS screen (where you would likely want it to be). If you review the code, you notice that ChatGPT did leave the comment, “Other content goes here.” If you add a Spacer() in that section of the code, you can easily move the view to the top.

But you can use ChatGPT to further customize your code, too. Let’s say that you want to build a SwiftUI view named “ExampleView” with a close red button of size 56×56 on the left. You also want a view title that is centered, in font size 32px, and bolded, appearing at the top of the screen.

All you have to do is tell ChatGPT what you want, like this:

4.png
5.png
6.png

Even though ChatGPT wasn’t able to get the view title centered, you didn’t have to write any of that basic code! Now you can make a quick adjustment, adding the two extra lines of code you need to get the centered title. 

Using ChatGPT to speed research for and customize simple code

Now that we’ve used ChatGPT to help write some simple iOS code in SwiftUI, let’s consider what else it can do for us. Imagine that you’re starting a SwiftUI project; you don’t have much time, and you’re not recalling all the possible modifiers you might need. 

You might spend precious time reviewing tutorials or searching for “how to use an HStack with an Image object StackOverflow SwiftUI” in various places. You might try testing some different code to see if any gets you all or almost all of what you need. You might even read the documentation!

What I have found, however, is that ChatGPT can save you a lot of time in situations like this. You can simply prompt ChatGPT to “generate a view with 5 images side by side each with a size equal to 48×48 and corner radius equal to 24 and the superview background equal to pink.”

Here’s an example:

7.png
8.png

This generated code can be the start of a custom view with buttons in the middle of the screen. 

Now, you can continue customizing the code to get exactly what you want. While the original code uses a placeholder for the image name (“image1”, “image2”, etc…), I instructed ChatGPT to replace it with “bitcoin.” In the example below, you’ll see that ChatGPT is smart enough to know that the next prompt you make references the previous prompt. 

9.png
10.png

You can continue customizing the code using more and more refined prompts until the screen meets your requirements. 

For example, I used this process to build separate components, each with specific adjustments such as shadows, sizes, and colors. Then, I combined all the subviews into a single view to create the full screen.

So far, I’ve only shared examples of building the UI because, while business rules are very specific, the most common ones are pretty simple to write. 

I haven’t tried creating the SwiftUI ViewModel logic using ChatGPT. It might be faster to write a simple close-screen function in the ViewModel than describe what you want to ChatGPT, but it might be interesting to see what it can do. 

Using ChatGPT to quickly find missing capabilities

I recently used ChatGPT to help find a missing capability needed to add credit cards to the Apple Wallet from within an app. Everything in the code was set up, but when I tried adding the card, the entry point to the wallet wasn’t shown. A quick Google search indicated a missing capability in the provisioning profile, which I set up. But when I tried adding the card to the wallet, it failed. 

After another search in Google and Apple documentation, I discovered yet another missing capability but, despite an afternoon of research, I couldn’t confirm the missing capability’s name.

That’s when I remembered I could use ChatGPT:

11.png

Reviewing the possibilities noted by ChatGPT, I identified the correct capability. I asked the administrator of the Apple Developer portal to turn it on. And then everything worked!

Making room for ChatGPT among your developer tools

My initial explorations with ChatGPT have confirmed that it should have a place in my tool set (and a special place in my browser, between the hundreds of tabs I usually have open). While ChatGPT may not be ready to handle more advanced programming tasks, the current version works well enough to help developers of all skill levels start a project configuration or the first lines of code. From there, success will likely depend on how well you write prompts for ChatGPT and how much time you can spend working to customize the code to meet your requirements. I expect this powerful tool will save me time so I can focus on more complex tasks, and I am looking forward to learning more about all that it can do.

Ready to be Unstoppable? Partner with Gorilla Logic, and you can be.

TALK TO OUR SALES TEAM