[英]How do I allow my iOS app to send information to a server?
I have an unactivated iPhone so no cellular data, but it is WiFi capable. 我有一个未激活的iPhone,所以没有蜂窝数据,但它具有WiFi功能。 I can access Safari, slack, etc. no problem. 我可以访问Safari,Slack等。 I am making an app that is trying to send a JSON package to a server and await the servers response. 我正在制作一个尝试将JSON包发送到服务器并等待服务器响应的应用程序。 But it seems that the app I am making is not even touching the Internet. 但似乎我正在制作的应用程序甚至都没有触及互联网。
This code will return true: (google.com is just an example url) 这段代码将返回true:(google.com只是一个示例网址)
func isInternetAvailable() -> Bool
{
var zeroAddress = sockaddr_in()
zeroAddress.sin_len = UInt8(MemoryLayout.size(ofValue: zeroAddress))
zeroAddress.sin_family = sa_family_t(AF_INET)
let defaultRouteReachability = withUnsafePointer(to: &zeroAddress) {
$0.withMemoryRebound(to: sockaddr.self, capacity: 1) {zeroSockAddress in
SCNetworkReachabilityCreateWithAddress(nil, zeroSockAddress)
}
}
var flags = SCNetworkReachabilityFlags()
if !SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags) {
return false
}
let isReachable = (flags.rawValue & UInt32(kSCNetworkFlagsReachable)) != 0
let needsConnection = (flags.rawValue & UInt32(kSCNetworkFlagsConnectionRequired)) != 0
return (isReachable && !needsConnection)
}
While this code will return false: 虽然此代码将返回false:
func checkInternet(flag:Bool, completionHandler:@escaping (_ internet:Bool) -> Void)
{
UIApplication.shared.isNetworkActivityIndicatorVisible = true
let url = NSURL(string: "http://www.google.com/")
let request = NSMutableURLRequest(url: url! as URL)
request.httpMethod = "POST"
request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringLocalAndRemoteCacheData
request.timeoutInterval = 10.0
NSURLConnection.sendAsynchronousRequest(request as URLRequest, queue:OperationQueue.main, completionHandler:
{(response: URLResponse?, data: Data?, error: Error?) -> Void in
UIApplication.shared.isNetworkActivityIndicatorVisible = false
let rsp = response as! HTTPURLResponse?
self.OutputTextField.text = self.OutputTextField.text + ", \(rsp)"
completionHandler(rsp?.statusCode == 200)
} as! (URLResponse?, Data?, Error?) -> Void )
}
I have tried many methods to send the JSON package, but most just crash the app or give no response. 我尝试了许多发送JSON包的方法,但是大多数方法只是使应用程序崩溃或不响应。 I put in this code to demonstrate what the app is doing in regards to the Internet connectivity. 我输入此代码来演示应用程序在Internet连接方面正在做什么。 I have changed the Info.plist to allow arbitrary loads and to use WiFi. 我更改了Info.plist,以允许任意负载并使用WiFi。 Anything to get this to work would be helpful. 使此工作正常进行的任何事情都会有所帮助。
So there were several things I did wrong. 所以我做错了几件事。 The first was I was updating the UI in a background thread causing crashes. 首先是我正在后台线程中更新UI,从而导致崩溃。 Pertaining to the server connection, right click on info.plist and edit it as an xml document and add the site exceptions that way. 与服务器连接有关,右键单击info.plist并将其编辑为xml文档,然后以这种方式添加站点例外。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.