Writing Tools with Apple Intelligence

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

Lesson 02: Controlling Writing Tools With UIKit

Demo 02

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 a couple more advanced ways to configure how your UITextViews work with Writing Tools. Open the starter project for Lesson Two and select CustomTextView.swift, which is the UIKit version of the cover letter drafting view.

Defining Ignored Text Ranges

Using the cover letter view as an example, here’s how you can implement a range of text Writing Tools will not attempt to modify. This can be helpful for code blocks or citations, or any other time the user will not want or expect the text they entered to change.

func textView(_ textView: UITextView, writingToolsIgnoredRangesIn
  enclosingRange: NSRange) -> [NSRange] {

}
let fullText = textView.text as NSString
let searchString = "Dear Hiring Manager,"
let range = fullText.range(of: searchString)
if range.location != NSNotFound {
  return [range]
} else {
  return []
}

Preventing Unexpected Data Loss or Changes

Next, you’ll see how to use textViewWritingToolsWillBegin() and textViewWritingToolsDidEnd() to manage app operations when Writing Tools are active.

var textWhenWritingToolsBegins = ""
func textViewWritingToolsWillBegin(_ textView: UITextView) {
  textWhenWritingToolsBegins = textView.text
}
func textViewWritingToolsDidEnd(_ textView: UITextView) {
  if textView.text != textWhenWritingToolsBegins {

  }
}
var updatedLetter = parent.coverLetter
updatedLetter.content = textView.text
parent.coverLetterManager.saveCoverLetter(updatedLetter)
print("Updated letter saved")
} else {

  textWhenWritingToolsBegins = ""
  print("No changes to save")
}
See forum comments
Cinema mode Download course materials from Github
Previous: Instruction 02 Next: Conclusion