动态获取React Native应用程序的'url'

问题描述 投票:0回答:1

如果我们有一个使用 Expo 构建的 React Native 应用程序,当通过

npx expo start
启动应用程序时,我们会在终端中看到以下内容: enter image description here

上面写着:“Metro waiting on exp://192.168.100.8:8081”。这个“url”取决于我们当前连接的网络,并且可以更改。

有什么方法可以通过expo动态获取这个'url'吗?例如,如果我想创建一个显示此“url”的屏幕,我将如何获取此值?

(我在引号中说“url”,因为据我所知,它在技术上不是一个 url}

这可以在expo的源代码中找到,行:

Log.log(printItem(chalk`Metro waiting on {underline ${nativeRuntimeUrl}}`));

我想要 nativeRuntimeUrl 在我的 RN 应用程序中返回什么。

react-native expo
1个回答
0
投票

expo-cli 中有一些关于“url”的选项 - 输入 npx expo start --help 来查看它们

Info
    Start a local dev server for the app

  Usage
    $ npx expo start <dir>

  Options
    <dir>                           Directory of the Expo project. Default: Current working directory
    -a, --android                   Open on a connected Android device
    -i, --ios                       Open in an iOS simulator
    -w, --web                       Open in a web browser
    
    -d, --dev-client                Launch in a custom native app
    -g, --go                        Launch in Expo Go
    
    -c, --clear                     Clear the bundler cache
    --max-workers <number>          Maximum number of tasks to allow Metro to spawn
    --no-dev                        Bundle in production mode
    --minify                        Minify JavaScript
    
    -m, --host <string>             Dev server hosting type. Default: lan
                                    lan: Use the local network
                                    tunnel: Use any network by tunnel through ngrok
                                    localhost: Connect to the dev server over localhost
    --tunnel                        Same as --host tunnel
    --lan                           Same as --host lan
    --localhost                     Same as --host localhost
    
    --offline                       Skip network requests and use anonymous manifest signatures
    --https                         Start the dev server with https protocol
    --scheme <scheme>               Custom URI protocol to use when launching an app
    -p, --port <number>             Port to start the dev server on (does not apply to web or tunnel). Default: 8081

    --private-key-path <path>       Path to private key for code signing. Default: "private-key.pem" in the same directory as the certificate specified by the expo-updates configuration in app.json.
    -h, --help                      Usage info

https://docs.expo.dev/more/expo-cli/#tunneling

© www.soinside.com 2019 - 2024. All rights reserved.