> For the complete documentation index, see [llms.txt](https://developers-apps-in-toss.toss.im/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developers-apps-in-toss.toss.im/documentation/api-and-sdk-zh/common/network-environment/environment.md).

# 运行环境

### 1. 确认正在运行的平台（`getPlatformOS`)

`getPlatformOS`是用于确认当前正在运行的平台的函数。 `react-native`的 [`Platform.OS`](https://reactnative.dev/docs/0.72/platform#os) 它基于该值运行， `ios` 或 `android` 并返回其中一个字符串。

**签名**

```typescript
function getPlatformOS(): 'ios' | 'android';
```

**返回值**

* 'ios' | 'android'

  当前正在运行的平台

**示例**

```tsx
import { getPlatformOS } from '@apps-in-toss/framework';
import { Text } from 'react-native';

function Page() {
  const platform = getPlatformOS();

  return <Text>当前平台：{platform}</Text>;
}
```

***

### 2. 确认应用程序环境（`getOperationalEnvironment`)

`getOperationalEnvironment` 可通过该函数确认应用当前处于哪个发布环境（例如： `sandbox`, `toss`)中运行。如果是在 Toss 应用中运行， `'toss'`，如果是在沙盒环境中运行， `'sandbox'`会返回。

运行环境指的是应用运行的上下文，可用于判断某些功能是否可用。

**签名**

```typescript
function getOperationalEnvironment(): 'toss' | 'sandbox';
```

**返回值**

这是表示当前运行环境的字符串。

* 'toss' | 'sandbox'

  `'toss'`：正在 Toss 应用中运行。 `'sandbox'`：正在沙盒环境中运行。

**示例**

{% tabs %}
{% tab title="js" %}

```js
import { getOperationalEnvironment } from '@apps-in-toss/web-framework';

const environment = getOperationalEnvironment();
```

{% endtab %}

{% tab title="React" %}

```tsx
import { getOperationalEnvironment } from '@apps-in-toss/web-framework';
import { Text } from '@toss/tds-mobile';

function EnvironmentInfo() {
  const environment = getOperationalEnvironment();

  return <Text>{`当前运行环境是 '${environment}'。`}</Text>;
}
```

{% endtab %}

{% tab title="React Native" %}

```tsx
import { getOperationalEnvironment } from '@apps-in-toss/framework';
import { Text } from '@toss/tds-react-native';

function EnvironmentInfo() {
  const environment = getOperationalEnvironment();

  return <Text>{`当前运行环境是 '${environment}'。`}</Text>;
}
```

{% endtab %}
{% endtabs %}

有些功能可能只应在特定发布环境中提供。下面是 `sandbox` 仅在某个环境中提供特殊功能的示例。

```tsx{4,8-9}
import { View, Text } from 'react-native';
import { getOperationalEnvironment } from '@apps-in-toss/framework';

const isSandbox = getOperationalEnvironment() === 'sandbox'; // 用于确认是否为 'sandbox' 环境的变量

function Component() {
  const handlePress = () => {
    if (isSandbox) {
      // 在 'sandbox' 环境中提供的功能
    } else {
      // 在其他环境中提供的功能
    }
  };

  return <Button onPress={handlePress}>查看详情</Button>;
}
```

***

### 3. 确认设备唯一标识符（`getDeviceId`)

`getDeviceId` 该函数会以字符串形式返回正在使用设备的唯一标识符。可用于按设备保存设置或数据，或识别用户设备以记录日志和进行分析。也适合区分同一用户的多台设备。

**签名**

```typescript
function getDeviceId(): string;
```

**返回值**

* string

  这是表示设备唯一标识符的字符串。

**示例**

{% tabs %}
{% tab title="js" %}

```js
import { getDeviceId } from '@apps-in-toss/web-framework';

const deviceId = getDeviceId();
```

{% endtab %}

{% tab title="React" %}

```tsx
import { getDeviceId } from '@apps-in-toss/web-framework';
import { useState } from 'react';

const DeviceInfo = () => {
  const [deviceId, setDeviceId] = useState<string | null>(null);

  const fetchDeviceId = async () => {
    setDeviceId(getDeviceId());
  };

  return (
    <div>
      <button onClick={fetchDeviceId}>获取设备 ID</button>
      {deviceId && <p>设备 ID：{deviceId}</p>}

  );
};
```

{% endtab %}

{% tab title="React Native" %}

```tsx
import { getDeviceId } from '@apps-in-toss/framework';
import { Text } from '@toss/tds-react-native';

function MyPage() {
  const id = getDeviceId();

  return <Text>用户设备唯一标识符：{id}</Text>;
}
```

{% endtab %}
{% endtabs %}

***

### 4. 获取 scheme 值（`getSchemeUri`)

`getSchemeUri`会返回最初进入页面时的 scheme 值。不会反映因页面跳转导致的 URI 变更。

**签名**

```typescript
function getSchemeUri(): string;
```

**返回值**

* string

  会返回最初进入页面时的 scheme 值。

**示例**

```tsx
import { getSchemeUri } from '@apps-in-toss/framework';
import { Text } from 'react-native';

function MyPage() {
  const schemeUri = getSchemeUri();

  return <Text>最初进入页面时的 scheme 值：{schemeUri}</Text>;
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://developers-apps-in-toss.toss.im/documentation/api-and-sdk-zh/common/network-environment/environment.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
