自定义三方语音技能界面

更新时间:2024-08-07 02:44:19下载pdf

目前,三方语音技能业务包里,带界面的三方语音开放技能有 Amazon AlexaGoogle Assistant

自定义三方语音技能界面 自定义三方语音技能界面

授权流程

完整的授权流程包含 4 个界面:授权前、授权确认、授权完成和解绑授权。

自定义三方语音技能界面 自定义三方语音技能界面

自定义三方语音技能界面 自定义三方语音技能界面

考虑到部分用户需要自定义 UI 元素,涂鸦特意新增了配置插件,来满足要求。下文将介绍该插件,并编写一个简单的 Demo 来实现 UI 元素的自定义。

接口

enum ThingVoiceServiceType {
  case amazonAlexa
  case googleHome
}

public protocol ThingValueAddedServicePlugAPIProtocol {

    optional func preAuthorizationIcon(_ type: ThingVoiceServiceType) -> UIImage?
    optional func preAuthorizationImage(_ type: ThingVoiceServiceType) -> UIImage?
    optional func preAuthorizationDetail(_ type: ThingVoiceServiceType) -> String?

    optional func authorizationConfirmIcon(_ type: ThingVoiceServiceType) -> UIImage?
    optional func authorizationConfirmAppName(_ type: ThingVoiceServiceType) -> String?
    optional func authorizationConfirmTitle(_ type: ThingVoiceServiceType) -> String?
    optional func authorizationConfirmTipsTitle(_ type: ThingVoiceServiceType) -> String?
    optional func authorizationConfirmTipsDetail(_ type: ThingVoiceServiceType) -> String?

    optional func didAuthorizationIcon(_ type: ThingVoiceServiceType) -> UIImage?
    optional func didAuthorizationTitle(_ type: ThingVoiceServiceType) -> String?
    optional func didAuthorizationTipsTitle(_ type: ThingVoiceServiceType) -> String?
    optional func didAuthorizationTipsDetail(_ type: ThingVoiceServiceType) -> String?

    optional func authorizationUnbindIcon(_ type: ThingVoiceServiceType) -> UIImage?
    optional func authorizationUnbindTitle(_ type: ThingVoiceServiceType) -> String?
    optional func authorizationUnbindTipsTitle(_ type: ThingVoiceServiceType) -> String?
    optional func authorizationUnbindTipsDetails(_ type: ThingVoiceServiceType) -> [String]?
}

授权前

授权前界面可以配置 3 个 UI 元素。

自定义三方语音技能界面
    optional func preAuthorizationIcon(_ type: ThingVoiceServiceType) -> UIImage?
    optional func preAuthorizationImage(_ type: ThingVoiceServiceType) -> UIImage?
    optional func preAuthorizationDetail(_ type: ThingVoiceServiceType) -> String?

授权确认

授权确认界面可以配置 5 个 UI 元素。

自定义三方语音技能界面
    optional func authorizationConfirmIcon(_ type: ThingVoiceServiceType) -> UIImage?
    optional func authorizationConfirmAppName(_ type: ThingVoiceServiceType) -> String?
    optional func authorizationConfirmTitle(_ type: ThingVoiceServiceType) -> String?
    optional func authorizationConfirmTipsTitle(_ type: ThingVoiceServiceType) -> String?
    optional func authorizationConfirmTipsDetail(_ type: ThingVoiceServiceType) -> String?

授权完成

授权完成界面可以配置 4 个 UI 元素。

自定义三方语音技能界面
    optional func didAuthorizationIcon(_ type: ThingVoiceServiceType) -> UIImage?
    optional func didAuthorizationTitle(_ type: ThingVoiceServiceType) -> String?
    optional func didAuthorizationTipsTitle(_ type: ThingVoiceServiceType) -> String?
    optional func didAuthorizationTipsDetail(_ type: ThingVoiceServiceType) -> String?

解绑授权

授权解绑界面可以配置 4 个 UI 元素。

自定义三方语音技能界面
    optional func authorizationUnbindIcon(_ type: ThingVoiceServiceType) -> UIImage?
    optional func authorizationUnbindTitle(_ type: ThingVoiceServiceType) -> String?
    optional func authorizationUnbindTipsTitle(_ type: ThingVoiceServiceType) -> String?
    optional func authorizationUnbindTipsDetails(_ type: ThingVoiceServiceType) -> [String]?

自定义

  • 定义一个新的类,通过该类实现 ThingValueAddedServicePlugAPIProtocol 协议:

    import ThingValueAddedServicePlugAPI
    
    class ThingValueAddedServicePlugAPIProtocolImpl: NSObject {
    }
    
    extension ThingValueAddedServicePlugAPIProtocolImpl: ThingValueAddedServicePlugAPIProtocol {
    }
    
  • 注册服务:

    ThingSmartBizCore.sharedInstance().registerService(ThingValueAddedServicePlugAPIProtocol.self, with: ThingValueAddedServicePlugAPIProtocolImpl.self)
    
  • 按需实现方法:

    extension ThingValueAddedServicePlugAPIProtocolImpl: ThingValueAddedServicePlugAPIProtocol {
    
        func preAuthorizationIcon(_ type: ThingVoiceServiceType) -> UIImage? {
            if (type == .amazonAlexa) {
                return UIImage(named: "icon");
            }else{
                return UIImage(named: "bannerBanner");
            }
        }
    
        func preAuthorizationImage(_ type: ThingVoiceServiceType) -> UIImage? {
            if (type == .amazonAlexa) {
                return UIImage(named: "banner");
            }else{
                return UIImage(named: "iconGoogle");
            }
        }
    
        func preAuthorizationDetail(_ type: ThingVoiceServiceType) -> String? {
            if (type == .amazonAlexa) {
                return "preAuthorizationDetail alexa";
            }else{
                return "preAuthorizationDetail google";
            }
        }
    
        func authorizationConfirmIcon(_ type: ThingVoiceServiceType) -> UIImage? {
            if (type == .amazonAlexa) {
                return UIImage(named: "icon");
            }else{
                return UIImage(named: "iconGoogle");
            }
        }
    
        func authorizationConfirmAppName(_ type: ThingVoiceServiceType) -> String? {
            if (type == .amazonAlexa) {
                return "alexa";
            }else{
                return "google";
            }
        }
    
        func authorizationConfirmTitle(_ type: ThingVoiceServiceType) -> String? {
            if (type == .amazonAlexa) {
                return "alexa";
            }else{
                return "google";
            }
        }
    
        func authorizationConfirmTipsTitle(_ type: ThingVoiceServiceType) -> String? {
            if (type == .amazonAlexa) {
                return "alexa tips";
            }else{
                return "google tips";
            }
        }
    
        func authorizationConfirmTipsDetail(_ type: ThingVoiceServiceType) -> String? {
            if (type == .amazonAlexa) {
                return "alexa detail";
            }else{
                return "google detail";
            }
        }
    
        func didAuthorizationIcon(_ type: ThingVoiceServiceType) -> UIImage? {
            if (type == .amazonAlexa) {
                return UIImage(named: "icon");
            }else{
                return UIImage(named: "iconGoogle");
            }
        }
    
        func didAuthorizationTitle(_ type: ThingVoiceServiceType) -> String? {
            if (type == .amazonAlexa) {
                return "alexa title";
            }else{
                return "google title";
            }
        }
    
        func didAuthorizationTipsTitle(_ type: ThingVoiceServiceType) -> String? {
            if (type == .amazonAlexa) {
                return "alexa tips title";
            }else{
                return "google tips title";
            }
        }
    
        func didAuthorizationTipsDetail(_ type: ThingVoiceServiceType) -> String? {
            if (type == .amazonAlexa) {
                return "alexa tips detail";
            }else{
                return "google tips detail";
            }
        }
    
        func authorizationUnbindIcon(_ type: ThingVoiceServiceType) -> UIImage? {
            if (type == .amazonAlexa) {
                return UIImage(named: "icon");
            }else{
                return UIImage(named: "iconGoogle");
            }
        }
    
        func authorizationUnbindTitle(_ type: ThingVoiceServiceType) -> String? {
            if (type == .amazonAlexa) {
                return "alexa";
            }else{
                return "google";
            }
        }
    
        func authorizationUnbindTipsTitle(_ type: ThingVoiceServiceType) -> String? {
            if (type == .amazonAlexa) {
                return "alexaTipsTitle";
            }else{
                return "googleTipsTitle";
            }
        }
    
        func authorizationUnbindTipsDetails(_ type: ThingVoiceServiceType) -> [String]? {
            if (type == .amazonAlexa) {
                return ["authorizationUnbindTipsDetails"];
            }else{
                return ["authorizationUnbindTipsDetails", "authorizationUnbindTipsDetails"];
            }
        }
    }