简体   繁体   中英

How can I check if my React-Native app is running in a web broswer vs running in an ios/android app?

Basically what the title says. I am aware of the Platform component ( https://reactnative.dev/docs/platform-specific-code ) which can be used to check what device the app is running on. Although, from my understanding, if the app is running in a web browser on ios/android, Platform wouldn't identify if it's running as an app or in a browser. Can I maybe do something like document == 'undefined (true if browser, false if app)? I appreciate any guidance.

I'm using expo by the way.

import { Platform } from 'react-native';

if (Platform.OS === 'ios') {
  // do something for ios
} else if (Platform.OS === 'android') {
  // other thing for android
} else if (Platform.OS === 'web') {
  // it's on web!
} else {
  // you probably won't end up here unless you support another platform!
}

I would recommend following the full tutorial provided at the start of the Expo docs, the Handling platform differences section covers this and you may learn other valuable information from the other sections.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM