简体   繁体   English

如何允许我的iOS应用程序将信息发送到服务器?

[英]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.

相关问题 如何让Mac OS X防火墙永久允许我的iOS应用程序? - How do I get the Mac OS X Firewall to permanently allow my iOS app? iOS - 允许我的应用在锁定屏幕上显示信息? - iOS - Allow my app to display information on lock screen? 如何在iOS应用中显示包含路况信息的地图? - How can I display a map with traffic information in my iOS app? 我如何只允许从我的iOS应用程序访问我的MySQL数据库? (使用webapp作为db的网关) - How do I only allow access to my MySQL database from my iOS app? (Using webapp as gateway to db) 如何使用应用程序扩展中的 IPC 将信息异步发送到 iOS 上的包含应用程序? - How can I use IPC from an app extension to asynchronously send information to the containing app on iOS? 如何允许我的用户在应用商店之外下载我的 ios 应用? - How can I allow my users to download my ios app outside the app store? 如何从python服务器发送iOS推送通知? - How I send iOS push notifications from my python server? 如何从我的 iOS 应用程序打开 Instagram 应用程序? - How do I open Instagram app from my iOS app? 如何使用Beta iOS在iPhone上运行我的应用程序? - How do I run my app on my iPhone with the Beta iOS? 在将我的第一个ios应用程序发送到Apple App Store之前,我知道/知道什么? - What shoud I do/know before send my first ios app to Apple App store?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM