Skip to main content

Privacy Management

1 Privacy Permission Check​

1.1 Interface Prototype​

See Host Privacy Policy Access for details

func setPrivacyChecker(checker: TJPrivacyCheckerListener?)

2 Host Privacy Policy Access​

2.1 Function Introduction​

To standardize the behavior of developers when invoking system capabilities, such as camera, Bluetooth, sensors, etc., the host provides an asynchronous call interface for permission checking to third-party access platforms. Third-party platforms can check or guide users to grant relevant permissions through this interface.

2.2 Access Instructions​

1. Implement the `TJPrivacyCheckerListener` interface, where the possible values of `methodName` are shown in the next section Related APIs section.

public protocol TJPrivacyCheckerListener {
/// Requests permissions for system capabilities, such as camera, Bluetooth, sensors, etc.
/// - Parameters:
/// - methodName: The name of the method requiring permission validation
/// - callback: Callback with the validation result
func requirePrivacy(methodName: String, callback: @escaping TJRequirePrivacyCallback)
}
  1. TJPrivacyCheckerListener callback TJRequirePrivacyCallback and TJPrivacyResult are defined as follows, parameters:

    1. methodName is the passed methodName;

    2. passed indicates whether the inspection has passed;

    3. errorNo and errorMessage will be returned to the relevant call in the js.

public typealias TJRequirePrivacyCallback = (String, TJPrivacyResult) -> Void

public class TJPrivacyResult {

public init(passed: Bool, errNo: Int, errMsg: String) {
self.passed = passed
errorNo = errNo
errorMessage = errMsg
}

func isPassed() -> Bool {
passed
}

func getErrorNo() -> Int {
errorNo
}

func getErrorMessage() -> String {
errorMessage
}

private var passed: Bool
private var errorNo: Int
private var errorMessage: String
}
  • Pass in a custom PrivacyChecker via RuntimeGameHandle .
let gameHandle: RuntimeGameHandle
gameHandle.setPrivacyChecker(new RandomPrivacyChecker())
Information TypemethodNameDescription
MicrophonestartRecorderManagerRecorderManager.start
CamerastartCameraManagertj.createCamera
BluetoothopenBluetoothAdapter, createBLEPeripheralServertj.openBluetoothAdapter, tj.createBLEPeripheralServer
Gyroscope SensorstartGyroscopetj.startGyroscope
Direction SensorstartDeviceMotionListeningtj.startDeviceMotionListening
Magnetic Field SensorstartCompasstj.startCompass
Acceleration SensorstartAccelerometertj.startAccelerometer

4 Example​

A PrivacyChecker that returns random results.

class RandomPrivacyChecker: TJPrivacyCheckerListener {
private func randomResult() -> PrivacyResult {
let random = Int.random(in: 0...9)
if random < 5 {
return PrivacyResult(granted: true, errorCode: 0, errorMessage: "privacy granted")
} else {
return PrivacyResult(granted: false, errorCode: 1, errorMessage: "privacy denied")
}
}

func requirePrivacy(methodName: String, callback: TJRequirePrivacyCallback?) {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
if let callback = callback {
let result = self.randomResult()
print("requirePrivacy: \(methodName) \(result.errorMessage)")
callback.onPrivacyResult(methodName, result)
}
}
}
}