使用 xcode 访问本地 IP 地址

Accessing local ip address using xcode

我正在尝试访问本地 IP 地址,以便我可以 change/update 本地 raspberry pi 上的信息。即我使用以下内容:

https://github.com/Jopyth/MMM-Remote-Control

我将我的 plist 更新为如下所示:

<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
        <key>CFBundleDevelopmentRegion</key>
        <string>en</string>
        <key>CFBundleExecutable</key>
        <string>$(EXECUTABLE_NAME)</string>
        <key>CFBundleIdentifier</key>
        <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
        <key>CFBundleInfoDictionaryVersion</key>
        <string>6.0</string>
        <key>CFBundleName</key>
        <string>$(PRODUCT_NAME)</string>
        <key>CFBundlePackageType</key>
        <string>APPL</string>
        <key>CFBundleShortVersionString</key>
        <string>1.0</string>
        <key>CFBundleVersion</key>
        <string>1</string>
        <key>LSApplicationCategoryType</key>
        <string></string>
        <key>LSRequiresIPhoneOS</key>
        <true/>
        <key>NSAppTransportSecurity</key>
        <dict>
            <key>NSExceptionDomains</key>
            <dict>
                <key>http://192.168.0.40</key>  // need to add your domain name of webservice
                <dict>
                    <key>NSIncludesSubdomains</key>
                    <true/>
                    <key>NSExceptionAllowsInsecureHTTPLoads</key>
                    <true/>
                    <key>NSExceptionRequiresForwardSecrecy</key>
                    <true/>
                    <key>NSExceptionMinimumTLSVersion</key>
                    <string>TLSv1.2</string>
                    <key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
                    <false/>
                    <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
                    <true/>
                    <key>NSThirdPartyExceptionMinimumTLSVersion</key>
                    <string>TLSv1.2</string>
                    <key>NSRequiresCertificateTransparency</key>
                    <false/>
                </dict>
            </dict>
        </dict>
        <key>NSTemporaryExceptionMinimumTLSVersion</key>
        <string>TLSv1.1</string>
        <key>UILaunchStoryboardName</key>
        <string>LaunchScreen</string>
        <key>UIMainStoryboardFile</key>
        <string>Main</string>
        <key>UIRequiredDeviceCapabilities</key>
        <array>
            <string>armv7</string>
        </array>
        <key>UISupportedInterfaceOrientations</key>
        <array>
            <string>UIInterfaceOrientationPortrait</string>
            <string>UIInterfaceOrientationLandscapeLeft</string>
            <string>UIInterfaceOrientationLandscapeRight</string>
        </array>
        <key>UISupportedInterfaceOrientations~ipad</key>
        <array>
            <string>UIInterfaceOrientationPortrait</string>
            <string>UIInterfaceOrientationPortraitUpsideDown</string>
            <string>UIInterfaceOrientationLandscapeLeft</string>
            <string>UIInterfaceOrientationLandscapeRight</string>
        </array>
    </dict>
    </plist>

但在 sim 卡或设备上加载时,我仍然得到一个空白的,尽管是黑色的页面。

我正在制作最基本的应用程序,以便我兄弟在家时可以访问他的魔镜来更新它,但我似乎看不到本地 IP 地址。任何帮助都会很棒。

实际上,真正的问题是 ATS 例外 domains 不适用于 IP 地址。它仅适用于 域名

选项:

选项 1 - 禁用 ATS

如果您要将应用程序提交到应用程序商店,则完全关闭 ATS 不是一个好主意 - 如果您希望在 Apple 开始执行该规则时提交到应用程序商店,那么这样做会让您证明该决定是正确的.通常我不建议将此作为 SO 的选项。但是,考虑到您在其中放置了一个硬编码的 IP 地址,我怀疑这是为特定用例创建的一次性应用程序,您无需提交给 Apple 进行审核(您可能会直接在设备上安装使用开发配置文件)。如果是这样的话,我会使用 Info.plist 中的 NSAllowsArbitraryLoads 键完全禁用 ATS。

这将禁用所有 ATS 保护,但如果您的应用仅通过 HTTPS 与本地网络实体通信,应用传输安全无论如何都不会保护任何内容。

选项 2 - 使用 NSAllowsLocalNetworking

如果您将 NSAllowsArbitraryLoadsNSAllowsLocalNetworking 都放在 Info.plist 中,在 iOS 9 中,您将完全禁用 ATS,但在 iOS 10 中(第一个支持 NSAllowsLocalNetworking 设置的版本),iOS 将忽略 NSAllowsArbitraryLoads 并且仅在本地网络调用时禁用 ATS。这会更安全,但如果您只拨打本地网络电话,我会禁用 ATS(选项 1)。

选项 3 - xip.io

如果您想保持 ATS 开启,其他人已成功使用 xip.io 服务将本地 IP 地址 "convert" 转换为域名。因此,您可以将 xip.io 添加到异常域,将 NSIncludesSubdomains 的子值设置为 true。然后,当您连接到您的域时,您将连接到 192.168.0.40 而不是连接到 192.168.0.40.xip.io