Check for internet connection with Swift

前端 未结 21 1889
别跟我提以往
别跟我提以往 2020-11-22 05:59

When I try to check for an internet connection on my iPhone I get a bunch of errors. Can anyone help me to fix this?

The code:

import Foundation
impo         


        
21条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-22 06:21

    If you are using Alamofire, you can do something like this:

    let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
    configuration.timeoutIntervalForRequest = 15 //Set timeouts in sec
    configuration.timeoutIntervalForResource = 15
    
    let alamoFireManager = Alamofire.Manager(configuration:configuration)
    alamoFireManager?.request(.GET, "https://yourURL.com", parameters: headers, encoding: .URL)
                         .validate()
                                  .responseJSON { response in
    
                                    if let error = response.result.error {
                                       switch error.code{
                                        case -1001:
                                            print("Slow connection")
                                            return
                                        case -1009:
                                            print("No Connection!")
                                            return
                                        default: break
                                        }
                                    }
    

提交回复
热议问题