Bỏ qua, đến nội dung

launchApp ​

Khởi chạy app với option cấu hình quyền và xoá trạng thái.

Mở một app trên device đích (Android hoặc iOS). Mặc định, command này dừng app đang chạy trước khi mở lại.

Tham số ​

Command launchApp nhận các tham số sau trong một map:

ParameterMô tả
appIdKhông bắt buộc. Package name (Android) hoặc bundle ID (iOS) của app cần mở. Nếu không chỉ định, Astra Scout mở app đang test bằng appId khai báo ở đầu file YAML.
clearStateKhông bắt buộc. Nếu true, xoá trạng thái của app trước khi mở.
clearKeychainKhông bắt buộc. Nếu true, xoá toàn bộ iOS Keychain.
stopAppKhông bắt buộc. Nếu false, command đưa app đang chạy nền lên foreground mà không khởi động lại. Mặc định true.
permissionsKhông bắt buộc. Map các quyền cần cấp, từ chối hoặc bỏ đặt. Mặc định tất cả quyền đều được cho phép.
argumentsKhông bắt buộc. Map các cặp key-value truyền cho app dưới dạng launch argument. Hỗ trợ giá trị string, boolean, double và integer.

Ví dụ sử dụng ​

Mở một app ​

Để mở app đang test:

yaml
- launchApp

Để mở một app khác theo ID:

yaml
- launchApp: com.example.app

Mở với trạng thái sạch ​

Để xoá toàn bộ dữ liệu app trước khi mở, dùng clearState: true:

yaml
- launchApp:
    appId: "com.example.app"
    clearState: true
    clearKeychain: true # Tùy chọn

Quản lý app đang chạy ​

Để đưa app đang chạy nền lên foreground mà không khởi động lại:

yaml
- launchApp:
    stopApp: false

Để khởi động lại app đang chạy, chạy launchApp không tham số, vì stopApp mặc định là true:

yaml
- launchApp

Đặt quyền khi mở app ​

Để từ chối tất cả quyền:

yaml
- launchApp:
    permissions: 
      all: deny

Để đặt các quyền cụ thể, thêm từng quyền riêng:

yaml
- launchApp:
    permissions:
        notifications: unset
        android.permission.ACCESS_FINE_LOCATION: deny

Truyền launch argument ​

Ví dụ này truyền các argument thuộc nhiều kiểu khác nhau cho app khi mở.

yaml
- launchApp:
    appId: "com.example.app"
    arguments: 
       foo: "This is a string"
       isFooEnabled: false
       fooValue: 3.24
       fooInt: 3

Nhận launch argument ​

Các ví dụ sau cho thấy cách nhận các argument được truyền bằng command launchApp trong code app của bạn.

Android ​

kotlin
intent.extras?.getBoolean("isFooEnabled")?.let {
    // Do something with isFooEnabled
}

intent.extras?.getString("foo")?.let {
    // Do something with foo
}

iOS ​

swift
if ProcessInfo.processInfo.arguments.contains("-isFooEnabled") {
    // Do something with isFooEnabled
}

// By default all the values received here would be string
let standardDefaultsDict = UserDefaults.standard.dictionaryRepresentation()
let foo = (standardDefaultsDict["foo"] as? String) ?? "defaultValue"

React Native ​

typescript
import { LaunchArguments } from 'react-native-launch-arguments'

export const isFooEnabled = () => {
  try {
    const foo = LaunchArguments.value().isFooEnabled
    return !!foo
  } catch (e) {
    return false
  }
}

Flutter ​

dart
import 'package:flutter_launch_arguments/flutter_launch_arguments.dart';

Future<void> getArguments() async {
  final fla = FlutterLaunchArguments();

  final foo = await fla.getString('foo');
  final isFooEnabled = await fla.getBool('isFooEnabled');
  final fooValue = await fla.getDouble('fooValue');
  final fooInt = await fla.getInt('fooInt');
}

Tìm hiểu cách dùng permissions trên app iOS và Android trong flow.