Writing Tools with Apple Intelligence

Oct 16 2024 · Swift 6, iOS 18.1, Xcode 16.1

Lesson 02: Controlling Writing Tools With UIKit

Demo 01

Episode complete

Play next episode

Next

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In this demo, you’ll see how to configure Writing Tools behavior and output in a sample app. Open the CoverCraft project in this lesson’s starter folder.

CustomTextView(text: $coverLetterText, coverLetter: coverLetter)

// TextEditor(text: $coverLetterText)
// .writingToolsBehavior(WritingToolsBehavior.complete)
// .scrollContentBackground(.hidden)
// .background(Color(UIColor.systemGray5))
// .cornerRadius(8)
// .frame(height: 300)
// .padding(.horizontal)

Setting Writing Tools Behavior

Next, open CustomTextView.swift. Add the following line just before the return statement in makeUIView():

textView.writingToolsBehavior = .none

Requesting the Writing Tools Output

Using UIKit, you can also specify which kinds of results Writing Tools returns. This is done via the allowedWritingToolsResultOptions property. To see this in action, first undo what you did in the last section. Inside makeUIView(), set writingToolsBehavior to .complete:

textView.writingToolsBehavior = .complete
textView.allowedWritingToolsResultOptions = [.plainText, .richText]

Requesting the Writing Tools Experience in a Web View

Like with text views, you can request a preferred version of the tools for the system to display. To do this for a web view, you’ll need to create a WKWebViewConfiguration object, on which you’ll set the same writingToolsBehavior property that’s used on UIKit text views and also available as a view modifier in SwiftUI.

func makeUIView(context: Context) -> WKWebView {

}
let configuration = WKWebViewConfiguration()
if #available(iOS 18.0, *) {
  configuration.writingToolsBehavior = .complete
}
  let webView = WKWebView(frame: .zero, configuration: configuration)
  return webView

Reading the Current Writing Tools Behavior

You can also read the current Writing Tools behavior in web and UIKit text views. Start by making sure the logic inside the availability check in makeUIView() looks like this. It might be different depending on where you left in experimenting in the previous step:

if #available(iOS 18.0, *) {
  configuration.writingToolsBehavior = .complete

}
let currentBehavior = configuration.writingToolsBehavior
  let behaviorName = [
    UIWritingToolsBehavior.none: "none",
    UIWritingToolsBehavior.default: "default",
    UIWritingToolsBehavior.complete: "complete",
    UIWritingToolsBehavior.limited: "limited"
  ][currentBehavior] ?? "unknown"
print("Current Writing Tools Behavior: \(behaviorName)")
See forum comments
Cinema mode Download course materials from Github
Previous: Instruction 01 Next: Instruction 02