Swift 快捷中心&Siri的处理

在iOS16以后Swift增加了AppShortcutsProvider的协议,在开发APP中可以在快捷指令中增加针对APP的操作,可以在搜索中搜索, 也可以通过Siri进行操作
具体效果展示


IMG_6906.jpg

具体的代码参考

import Foundation
import AppIntents
import SwiftUI
struct AppShortcuts: AppShortcutsProvider {
    @available(iOS 16.0, *)
    /// The color the system uses to display the App Shortcuts in the Shortcuts app.
    static var shortcutTileColor = ShortcutTileColor.grayBlue
    
    @available(iOS 16.0, *)
    static var appShortcuts: [AppShortcut] {
     
        AppShortcut(
            intent: MyInputViewIntent(),
            phrases: [
                "Meditate with \(.applicationName)",
            ],
            shortTitle: "输入数据",
            systemImageName: "square.and.arrow.up.circle.fill"
        )
  
        AppShortcut(
            intent: MyDialogIntent(),
            phrases: ["StartMeditationIntent a \(.applicationName)"],
            shortTitle: "展示弹框",
            systemImageName: "wallet.pass"
        )
        
        AppShortcut(
            intent: MyCustomViewIntent(),
            phrases: [
                "打开自定义视图展示\(.applicationName)",
            ]
        )
    }
}

其中自定义主色调, 以及背景的颜色,需要在Assets 中声明颜色的主题
然后再info文件中增加对应的配置

<key>CFBundleIcons</key>
    <dict>
        <key>CFBundlePrimaryIcon</key>
        <dict>
            <key>NSAppIconActionTintColorName</key>
            <string>AccentColor</string>
            <key>NSAppIconComplementingColorNames</key>
            <array>
                <string>ComplementingGradientColor1</string>
                <string>ComplementingGradientColor2</string>
            </array>
        </dict>
    </dict>
    <key>INAlternativeAppNames</key>
    <array>
        <dict>
            <key>INAlternativeAppName</key>
            <string>Trails</string>
            <key>INAlternativeAppNamePronunciationHint</key>
            <string>Tray ls</string>
        </dict>
    </array>

AccentColor
ComplementingGradientColor1
ComplementingGradientColor2
是在Assets声明的颜色主题

快捷指令有很多类型,都是要继承AppIntent

import AppIntents
struct MyDialogIntent: AppIntent {
    static var title = LocalizedStringResource("打开弹框")
    @MainActor
    func perform() async throws -> some IntentResult & ProvidesDialog{
        print("MyDialogIntent这里可以处理对应的逻辑")
        return .result(dialog: "Okay, MyDialogIntent.")
    }
}

可以参考官方Demo
https://developer.apple.com/videos/play/wwdc2022/10169
https://developer.apple.com/videos/play/wwdc2022/10170

IMG_6907.PNG
IMG_6908.PNG
IMG_6909.PNG
IMG_6910.PNG
IMG_6912.PNG
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容