I am making a paint app with Xcode.
I am creating it with reference to various articles, but I can not solve the error when displaying PKToolPicker
Thanks for your advice.

] (90619e6eb94acede426b856d9fc115ac.jpeg)

Error message
'shared (for :)' was deprecated in iOS 14.0: Create individual instances instead.

Corresponding source code
import UIKit
import PencilKit
class ViewController: UIViewController {
    override func viewDidLoad () {
        super.viewDidLoad ()
        // Three Lines of Code Add 3 lines of code here
        let canvas = PKCanvasView (frame: view.frame)
        view.addSubview (canvas)
        canvas.tool = PKInkingTool (.pen, color: .black, width: 30)
        canvas.drawingPolicy = .anyInput
         // PKToolPicker: Tool palette that can be moved by dragging (problem)
        if let window = UIApplication.shared.windows.first {
            if let toolPicker = PKToolPicker.shared (for: window) {
                toolPicker.addObserver (canvas)
                toolPicker.setVisible (true, forFirstResponder: canvas)
                canvas.becomeFirstResponder ()

What I tried

It seems that the PKToolPicker.shared part does not support ios14.

let toolPicker = PKToolPicker.init ()
toolPicker.addObserver (canvas)
toolPicker.setVisible (true, forFirstResponder: canvas)
canvas.becomeFirstResponder ()

I tried changing it like this, but I couldn't build it.


・ MacOS: 10.15.7 ・ Xcode: 12.1 ・ iOS: 14.1

  • Answer # 1

    As the error message indicates. You can use this initializer.

    Then you will get an error again.
    Even in that case, the error message is as shown, so it will be like this in the end.

    if #available (iOS 14.0, *) {
        let p = PKToolPicker ()
    } else {
        let p = PKToolPicker.shared (for: yourWindow)

  • Answer # 2

    I solved it like this

    Add the following to the canvasViewDrawingDidChange method

    toolPicker.addObserver (canvas)
    toolPicker.setVisible (true, forFirstResponder: canvas1)
    canvas.becomeFirstResponder ()