QQ小程序 打開 App

2020-07-03 14:51 更新

此功能需要用戶主動(dòng)觸發(fā)才能打開 APP,所以不由 API 來(lái)調(diào)用,需要用 open-type 的值設(shè)置為 launchApp 的 <button> 組件的點(diǎn)擊來(lái)觸發(fā)。 當(dāng)小程序從 APP 分享消息卡片的場(chǎng)景打開(場(chǎng)景值 1036)或 移動(dòng)應(yīng)用打開(場(chǎng)景值 1069),小程序會(huì)獲得打開 APP 的能力,此時(shí)用戶點(diǎn)擊按鈕可以打開分享該小程序卡片/拉起該小程序的 APP。即小程序不能打開任意 APP,只能 跳回 APP。 在一個(gè)小程序的生命周期內(nèi),只有在特定條件下,才具有打開 APP 的能力。 當(dāng)小程序從 1069 和 1036 場(chǎng)景打開時(shí),可以打開 APP。 當(dāng)小程序從非 1036 和 1069 的場(chǎng)景值打開時(shí),會(huì)在小程序框架內(nèi)部會(huì)管理的一個(gè)狀態(tài),為 true 則可以打開 APP,為 false 則不可以打開 APP。這個(gè)狀態(tài)的維護(hù)遵循以下規(guī)則:

  • 當(dāng)小程序從 App 分享消息卡片(場(chǎng)景值1036) 或 移動(dòng)應(yīng)用打開(場(chǎng)景值 1069) 打開時(shí),該狀態(tài)置為 true。
  • 當(dāng)小程序從以下場(chǎng)景打開時(shí),保持上一次打開小程序時(shí)打開 App 能力的狀態(tài):

  1. 從其他小程序返回小程序(場(chǎng)景值1038)時(shí)
  2. 從QQ主界面-下拉列表(場(chǎng)景值1091)時(shí)
    • 當(dāng)小程序從非以上場(chǎng)景打開時(shí),不具有打開 APP 的能力,該狀態(tài)置為 false。

使用方法

小程序端

需要將 <button>組件 open-type 的值設(shè)置為 launchApp。如果需要在打開 APP 時(shí)向 APP 傳遞參數(shù),可以設(shè)置 app-parameter 為要傳遞的參數(shù)。通過(guò) binderror 可以監(jiān)聽打開 APP 的錯(cuò)誤事件。 開發(fā)者需要在button組件中填充下列參數(shù),才能正常使用:

  1. app-packagename:應(yīng)用的包名 (安卓)

  1. app-bundleid:應(yīng)用的bundleid (iOS)

  1. app-connect-id:QQ互聯(lián)中的AppID

小程序代碼示例

<button open-type="launchApp" app-packagename="com.tencent.qq" app-bundleid="com.tencent.qq" app-connect-id="101010101" binderror="launchAppError">
  打開APP
</button>

Page({
  launchAppError(e) {
    console.log(e.detail.errMsg)
  }
})

error 事件參數(shù)說(shuō)明

|值|說(shuō)明| |invalid scene|調(diào)用場(chǎng)景不正確,即此時(shí)的小程序不具備打開 APP 的能力。|

app 端

APP 需要接入 互聯(lián)sdk。 文檔請(qǐng)參考 (https://wiki.connect.qq.com)

iOS端

app-parameter獲取方法如下: 在UIApplicationDelegate的實(shí)現(xiàn)中添加如下代碼

- (BOOL)application:(UIApplication *)app
            openURL:(NSURL *)url
            options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
    // 判斷url.host
    if ([url.host isEqualToString:@"maLaunchApp"]) {
        // 獲取appParameter
        NSURLComponents *components = [[NSURLComponents alloc] initWithString:url.absoluteString];
        [components.queryItems enumerateObjectsUsingBlock:^(NSURLQueryItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([obj.name isEqualToString:@"appParameter"]) {
                // 傳遞到客戶端的appParameter經(jīng)過(guò)url encode
                NSString *encodedValue = obj.value;
                // TODO: 將encodedValue 通過(guò)標(biāo)準(zhǔn)url decode還原
            }
        }];

        
    }

    
    return YES;
}

Android端 app-parameter獲取方法如下: 如果app-parameter的參數(shù)含有”path“,則通過(guò)Intent.ACTION_VIEW + path內(nèi)容(Uri)的方式啟動(dòng)app。path為app端可識(shí)別的內(nèi)容即可打開對(duì)應(yīng)的頁(yè)面。

  <button open-type="launchApp" 
    app-packagename="com.tencent.qq" 
    app-bundleid="com.tencent.qq" 
    app-connect-id="101010101"
    app-parameter="{'path':'Uri'}"(此處app-parameter參數(shù)要求為JSON格式)
    binderror="launchAppError">
  打開APP
</button>

如果app-parameter的參數(shù)不含有”path“,則通過(guò)Intent.ACTION_MAIN的方式啟動(dòng)app。參數(shù)在主activity的getIntent()中獲取,key為“appParameter”,value透?jìng)鱝pp-parameter內(nèi)容。

  <button open-type="launchApp" 
    app-packagename="com.tencent.qq" 
    app-bundleid="com.tencent.qq" 
    app-connect-id="101010101"
    app-parameter="test:123"
    binderror="launchAppError">
  打開APP
</button>
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)