ios - wifi关闭时GDCWebserver serverURL为空
<p><p>我没有从其他网络访问。我只是想为混合应用程序提供文件。 </p>
<p>当我使用以下代码连接到 wifi 网络时,只需运行基本服务器即可:</p>
<pre><code>let webServer = GCDWebServer()
webServer.addDefaultHandlerForMethod("GET", requestClass: GCDWebServerRequest.self, processBlock: {request in
return GCDWebServerDataResponse(HTML:"<html><body><p>Hello World</p></body></html>")
})
webServer.startWithPort(8080, bonjourName: "GCD Web Server")
print("Visit \(webServer.serverURL) in your web browser")
</code></pre>
<p>日志:</p>
<blockquote>
<p>Visit <a href="http://192.168.1.132:8080/" rel="noreferrer noopener nofollow">http://192.168.1.132:8080/</a> in your web browser</p>
</blockquote>
<p>当我放弃 wifi 并启动应用程序和服务器时,我得到:</p>
<blockquote>
<p>GCDWebServer started on port 8080 and reachable at (null)</p>
</blockquote></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>无论连接类型如何(wifi 或 3g/4g),尝试启动服务器绑定(bind)到 localhost</p>
<pre><code>do {
try webServer.start(options: [
"Port": 8080,
"BindToLocalhost": true
])
} catch {
// handle error
}
</code></pre>
<p>另外,如果 <code>Arbitrary Loads</code> 被禁用(可能),请将“localhost”添加到 info.plist 中的 <code>NSExceptionDomains</code></p>
<pre><code><key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>localhost</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>
<key>NSAllowsArbitraryLoads</key>
<false/>
</dict>
</code></pre>
<p> <a href="/image/1VtVs.png" rel="noreferrer noopener nofollow"><img src="/image/1VtVs.png" alt="enter image description here"/></a> </p></p>
<p style="font-size: 20px;">关于ios - wifi关闭时GDCWebserver serverURL为空,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/38386461/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/38386461/
</a>
</p>
页:
[1]