In the last tutorial, we looked at ChatGPT chat conversations and added a simple user interface to a SwiftUI app. Today we will look at integrating DALL-E into your SwiftUI app, using the image generations endpoint that allows you to provide a prompt and generate one to ten images. The app will be similar to what was created yesterday. The difference is that instead of receiving an answer in text form, we’ll receive it as a URL that can then be processed to display on the view.
[Read more…]ChatGPT Integration: Building a SwiftUI Chatbot for Your iPhone App
In the past year, ChatGPT has brought AI to the limelight. ChatGPT 3 was the model in use when I first heard of it last year. Just like it did with me, It has captured the interest of many people and businesses.
ChatGPT can be used for many things, such as finding answers to daily questions, solving maths problems, writing blog posts, and writing code, to name a few.
In this tutorial, we’ll connect a SwiftUI app to OpenAI’s ChatGPT API to provide a chatbot for your mobile app. The tutorial will briefly cover how to create a simple user interface and connect to OpenAI’s API to receive answers to questions you ask within the app.
The application here is just a chatbot, but you can use what you learn here to add value to any kind of app such as a note-taking app where a user can write some notes and then use AI to either add, summarise or correct what has been written.
OpenAI’s API is very similar to many other APIs on the internet in that you create an HTTP request, such as a POST request, add the bearer to the header, add items to the body, and then post it. The API then passes back a result that contains the answer you need.
[Read more…]How to add a Map to a SwiftUI App
Many iPhone apps use maps to display helpful information to the user, often showing the current location. Uses include maps for navigation, finding nearby places, adding location to notes, reminders, and so on. This short tutorial will teach us two ways to add a map to an app.
Using MapKit
The MapKit framework makes adding a map to your app simple. You can first begin by importing MapKit into your view as follows:
import MapKit

How to Use Local Notifications
There are two categories of notifications on iOS devices. One type is a push notification sent by a server, and the other is a local notification scheduled on-device that appears at a particular time or location.
This tutorial focuses on local notifications. An example of a local notification can be seen in your calendar. You add an appointment, set it to remind you 15 minutes before, and 15 minutes before the meeting begins, you see a notification on your lock screen. Let’s take a look at how these work.
[Read more…]The SwiftUI App Life Cycle
When Xcode 12 was launched in 2020 it brought with it a new app life cycle for SwiftUI applications. Prior to this, SwiftUI based apps were using AppDelegate.swift and SceneDelegate.swift.
Creating a new app can be done with this code:
import SwiftUI
@main
struct MyNewApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
Apple Sample Code for Three-Column Layout in SwiftUI
For the last few weeks, I have been making plans for a new iPad/iPhone, and potentially macOS application. I haven’t used SwiftUI much in development so wasn’t sure how best to sort out the navigation on both mobile devices.
The app I am building, which is in very early stages, is for note-taking and meeting planning. My plan was to use a two-column layout, much like master/detail, but it seems that one good option is the three-column layout that has a menu on the far left column, a list view in the centre column, and then details on the rest of the view, as seen below.

Unable to use SwiftUI Preview due to Insufficient System Resources
If you are getting an error saying that you cannot preview this file (while using SwiftUI), then perhaps like my experience, a reboot might fix it. That’s the short version to possibly get you going before reading on.
I created a new project today and switched the default ContentView.swift to use an iPad Pro 11 inch for the preview. An error appeared in right pane saying “Cannot preview in this file – Unable to boot device due to insufficient system resources”. Clicking the diagnostics button provided the following:
HumanReadableNSError: Unable to boot device due to insufficient system resources.
Please see Simulator Help for information on adjusting resource limits.
NSPOSIXErrorDomain (67):
==NSLocalizedFailureReason: The current system settings are not sufficient to allow booting additional simulators: maxFiles: 12288, openFiles: 11669, enforcedFilesBuffer: 1868
As mentioned, a reboot fixed the issue. It seems that Apple last recognised this happening back in 2017 in the Simulator Help section on the Apple help site that gives some recommendations on how to modify the number of processes and number of open files.
What is a Swift Class
We recently wrote about Swift structs. In this tutorial, we’ll look at classes. Classes and structures have a lot in common, although there are some important differences. Let’s take a look at what a class is and what you use them for.
- A class is initialised as an object.
- You need to create an initialiser for your class.
- Classes can also inherit from other classes which bring to your class the abilities and properties of the other class. You can also override the default behaviour of a method of the parent class.
- Unlike structs which are passed around by value, classes are passed by reference.
What is a Swift Struct
A struct, or structure, is a custom data type in Swift that allows you to add many properties to it. If you think of a regular string, you can store just one string in the variable, as follows:
var firstName = "Matthew"
What if we wanted to add Matthews height:
var firstName = "Matthew"
var height = 190
This rapidly becomes complicated because we now have two variables to manage and think about. Imagine if we needed to add their last name, and so on, and then have the need to pass this information somewhere else within the program. This is one benefit of using a struct.
[Read more…]SwiftUI Text View
The basic template for SwiftUI View begins with a simple Text view:
struct ContentView: View {
var body: some View {
Text("Hello World!")
}
}
When a Text view is returned with no attributes, it is put it in the middle of the view.
[Read more…]- « Previous Page
- 1
- 2
- 3
- 4
- …
- 8
- Next Page »