Apple Intelligence launched with iOS 18, and along with that, several tools that can be integrated into your app. These include:
- Writing Tools
- Image Playground
- Genmoji
- Siri with App Intents
In this tutorial we’ll look at Writing Tools which brings Apple Intelligence to your app. However, to dig deeper into using Writing Tools, you’ll need to use a UIKit view. We’ll look at that in the next tutorial. For now, lets take a look at this VERY brief tutorial as writing tools in SwiftUI is limited to what is baked in.
Apple has added support to some text views, so if you use a TextEditor, for example, you can access writing tools in the normal way and proofread, rewrite, or ask for suggestions for the text you have entered.
import SwiftUI
struct ContentView: View {
@State private var note = ""
var body: some View {
VStack {
Text("AI-Powered Notes")
.font(.title)
.bold()
.padding(.bottom)
TextEditor(text: $note)
.writingToolsBehavior(.automatic)
.frame(height: 200)
.padding()
.overlay(
RoundedRectangle(cornerRadius: 10)
.stroke(Color.gray, lineWidth: 1)
)
Spacer()
}
.padding()
}
}
#Preview {
ContentView()
}
In this simple example above, line 14 is sets the behaviour. You can actually remove this, as mentioned above, and still access writing tools in the TextEditor view.
A look at the documentation shows us what writingToolsBehaviour does. There are currently four properties available which are automatic, complete, disabled, and limited.
The automatic option gives an “appropriate editing experience” based on the context. This might remove some of the features.
The complete option gives the full inline editing experience where possible.
The disabled option removes the writing tools menu item when you tap
The limited option limites the experience if possible.
Feel free to switch around and see what happens with each option.
Apple only allows writingToolsBehaviour to be used in SwiftUI. If you want more control over the writing tools, you’ll need to use UIKit. This allows you to finetune writing tools as well as use the delegate methods available in UIKit apps to be notified when writing tools are busy at work so that you can either put up a progress meter, or perhaps hold off saving a document while changes are being calculated by AI.
As mentioned, this is a really quick tutorial as SwiftUI’s support for writing tools is very limited. Next time we’ll look at what can be done when using UIKit, perhaps in a SwiftUI app.
Leave a Reply
You must be logged in to post a comment.