[Xcode12] Share extension に変化はあるか → ありませんでした

     
⌛️ < 1 min.

Xcode12 がダウンロードできるようになっていたので、Xcode11 が作るテンプレートコードとの差異があるかチェック

App extension – Share に変化は?

App extension 向けのターゲットを追加した時に作成されるテンプレートのコードの差異をチェックしてみました。

App extension を追加するには、ベースのプロジェクトが必要です。Xcode11 までは、iOS向けプロジェクト、MacOS向けプロジェクトとそれぞれ作成し、その後、Targetを追加する必要がありました。

Xcode12 からは、iOS/MacOS 個別ではなく、Multiplatform というプロジェクトが選択できるようになっていたので、共通のプロジェクトを作成して、iOS向け、MacOS向けそれぞれに App extension のターゲットを追加しました。

iOS向けは変化なし

iOS向けには、SLComposeServiceViewController を継承する ShareViewController が作られていました。これは、Xcode11 が生成していたコードと同じです。

コード


class ShareViewController: SLComposeServiceViewController {

    override func isContentValid() -> Bool {
        // Do validation of contentText and/or NSExtensionContext attachments here
        return true
    }

    override func didSelectPost() {
        // This is called after the user selects Post. Do the upload of contentText and/or NSExtensionContext attachments.
    
        // Inform the host that we're done, so it un-blocks its UI. Note: Alternatively you could call super's -didSelectPost, which will similarly complete the extension context.
        self.extensionContext!.completeRequest(returningItems: [], completionHandler: nil)
    }

    override func configurationItems() -> [Any]! {
        // To add configuration options via table cells at the bottom of the sheet, return an array of SLComposeSheetConfigurationItem here.
        return []
    }

}

MacOS向けも変化なし

NSViewController を継承する ShareViewController が作られていました。これは、Xcode11 が生成していたコードと同じです。

コード


class ShareViewController: NSViewController {

    override var nibName: NSNib.Name? {
        return NSNib.Name("ShareViewController")
    }

    override func loadView() {
        super.loadView()
    
        // Insert code here to customize the view
        let item = self.extensionContext!.inputItems[0] as! NSExtensionItem
        if let attachments = item.attachments {
            NSLog("Attachments = %@", attachments as NSArray)
        } else {
            NSLog("No Attachments")
        }
    }

    @IBAction func send(_ sender: AnyObject?) {
        let outputItem = NSExtensionItem()
        // Complete implementation by setting the appropriate value on the output item
    
        let outputItems = [outputItem]
        self.extensionContext!.completeRequest(returningItems: outputItems, completionHandler: nil)
}

    @IBAction func cancel(_ sender: AnyObject?) {
        let cancelError = NSError(domain: NSCocoaErrorDomain, code: NSUserCancelledError, userInfo: nil)
        self.extensionContext!.cancelRequest(withError: cancelError)
    }

}

まとめ:App extension 向けに変更は見当たらない

WWDC2020 では、SwiftUI に結構大きな追加が説明されていますが、App extension という観点からは、大きな変化はまだ訪れていないようです。

説明は以上です。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です