神秘网络问题与帖子。 Wifi Works,Cell没有

时间:2017-04-26 18:00:37

标签: ios swift post

当我的实际设备在3G或4G上时,我一直在发送超时请求。但是,当我在wifi上时,服务器端会立即收到邮件请求。

这是我的代码,涉及发布请求。我得到了Optional("The request timed out.")。我不认为会话长度应该是一个问题,因为它会在很长一段时间后超时,并且正在发送的数据只是一个带密码的用户名。有什么想法吗?

我运行一个烧瓶服务器,但我不认为这是相关的,所以我没有包含它的代码。

       if let jsonData = try? JSONSerialization.data(withJSONObject: dict, options: .prettyPrinted) {
            let url = NSURL(string: base_address + taglocation_address)!
            let request = NSMutableURLRequest(url: url as URL)
            request.httpMethod = "POST"
            request.addValue("application/json", forHTTPHeaderField: "Content-Type")
            request.httpBody = jsonData
            print("starting task")
            let task = URLSession.shared.dataTask(with: request as URLRequest){ data,response,error in
                if error != nil{
                    self.statusLabel.text = error?.localizedDescription
                    print(error?.localizedDescription)
                    return
                }

                do {
                    let json = try JSONSerialization.jsonObject(with: data!, options:  JSONSerialization.ReadingOptions.allowFragments) as? NSDictionary
                    print("doing something")
                    if let responseJSON = json as? [String: String] {
                        print(responseJSON)
                        self.statusLabel.text = responseJSON["status"]

                    }

                } catch let error as NSError {
                    self.statusLabel.text = error.localizedDescription
                    print(error)
                }
            }
            task.resume()
        }

1 个答案:

答案 0 :(得分:1)

NSMutableURLRequest对象中,尝试添加allowsCellularAccess属性并将其设置为true。请参阅下面的代码。

 if let jsonData = try? JSONSerialization.data(withJSONObject: dict, options: .prettyPrinted) {
            let url = NSURL(string: base_address + taglocation_address)!
            let request = NSMutableURLRequest(url: url as URL)
            request.httpMethod = "POST"
            request.addValue("application/json", forHTTPHeaderField: "Content-Type")
            request.httpBody = jsonData
            request.allowsCellularAccess=true //Add this line
            print("starting task")
            let task = URLSession.shared.dataTask(with: request as URLRequest){ data,response,error in
                if error != nil{
                    self.statusLabel.text = error?.localizedDescription
                    print(error?.localizedDescription)
                    return
                }

                do {
                    let json = try JSONSerialization.jsonObject(with: data!, options:  JSONSerialization.ReadingOptions.allowFragments) as? NSDictionary
                    print("doing something")
                    if let responseJSON = json as? [String: String] {
                        print(responseJSON)
                        self.statusLabel.text = responseJSON["status"]

                    }

                } catch let error as NSError {
                    self.statusLabel.text = error.localizedDescription
                    print(error)
                }
            }
            task.resume()
        }