How to check internet connection in alamofire?

46.2k views Asked by At

I am using below code for making HTTP request in server.Now I want to know whether it is connected to internet or not. Below is my code

  let request = Alamofire.request(completeURL(domainName: path), method: method, parameters: parameters, encoding: encoding.value, headers: headers)
      .responseJSON {


        let resstr = NSString(data: $0.data!, encoding: String.Encoding.utf8.rawValue)
        print("error is \(resstr)")


        if $0.result.isFailure {
          self.failure("Network")
          print("API FAILED 4")
          return
        }
        guard let result = $0.result.value else {
          self.unKnownError()
          self.failure("")
          print("API FAILED 3")

          return
        }
        self.handleSuccess(JSON(result))
    }
9

There are 9 answers

3
MAhipal Singh On
  func isConnectedToNetwork()-> 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)
        }
    }
    //Commented code only work upto iOS Swift 2.3
    //    let defaultRouteReachability = withUnsafePointer(to: &zeroAddress) {
    //
    //        SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer($0))
    //    }

    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)
}
    // Call api method
    func callApi(){
        if isConnectedToNetwork() {  // Network Connection status
            // Call your request here
        }else{
            //"Your Internet connection is not active at this time."
        }
    }
2
Umair Afzal On

If you goto NetworkReachabilityManager.swift you will see this

/// Whether the network is currently reachable. public var isReachable: Bool { return isReachableOnWWAN || isReachableOnEthernetOrWiFi }

So I have written this in my APIhandlerClass

import AlamofireNetworkActivityIndicator

private let manager = NetworkReachabilityManager(host: "www.apple.com")

func isNetworkReachable() -> Bool {
    return manager?.isReachable ?? false
}

So this tells me the status of network.

0
Nupur Sharma On

If Alamofire.upload result returns success then below is the way to check for internet availibility while uploading an image:

Alamofire.upload(multipartFormData: { multipartFormData in

                for (key,value) in parameters {
                 multipartFormData.append((value).data(using: .utf8)!, withName: key)
                }
                  multipartFormData.append(self.imageData!, withName: "image" ,fileName: "image.jpg" , mimeType: "image/jpeg")
            }, to:url)
            { (result) in

                switch result{

                case .success(let upload, _, _):

                    upload.uploadProgress(closure: { (progress) in
                     print("Upload Progress: \(progress.fractionCompleted)")

                    })

                    upload.responseJSON { response in
                        if  let statusCode = response.response?.statusCode{

                        if(statusCode == 201){
                         //internet available
                          }
                        }else{
                        //internet not available

                        }
                    }

                case .failure(let encodingError):
                    print(encodingError)

                }

            }
3
Parth Adroja On

For Swift 3/4,

In Alamofire, there is a class called NetworkReachabilityManager which can be used to observer or check if internet is available or not.

let reachabilityManager = NetworkReachabilityManager()

reachabilityManager?.startListening()
reachabilityManager?.listener = { _ in
        if let isNetworkReachable = self.reachabilityManager?.isReachable,
            isNetworkReachable == true {
            //Internet Available
        } else {
            //Internet Not Available"
        }
    }

Here, listener will get called every time when there is changes in state of internet. You can handle it as you would like.

1
Aromal Sasidharan On

Using RequestAdapter class of alamofire and throw error when no internet connectivity

class RequestInterceptor : RequestAdapter{
func adapt(_ urlRequest: URLRequest) throws -> URLRequest {

    let reachable = NetworkReachabilityManager()?.isReachable ?? false
    if !reachable{
        throw NSError.NoInternet
    }
    var nUrlRequest = urlRequest
    // modify request if needed 
    return nUrlRequest
   }
}

extension NSError  {

static func createWithLocalizedDesription(withCode code:Int = 204,localizedDescription:String) -> NSError{
    return  NSError(domain: "<your bundle id>", code:code, userInfo: [NSLocalizedDescriptionKey : localizedDescription])
}
static var NoInternet : NSError {
    return createWithLocalizedDesription(withCode: -1009,localizedDescription:"Please check your internet connection")
}

}

Now set the adapter to Alamofire Session Manager

let sessionManager = Alamofire.SessionManager(configuration: configuration)

sessionManager.adapter = RequestInterceptor()

Now each time when You create Alamofire Request, catch the error in DataResponse. This mechanism will act common to all request

1
MAhipal Singh On

Swift 2.3

Alamofire.request(.POST, url).responseJSON { response in
switch response.result {
    case .Success(let json):
        // internet works.  
    case .Failure(let error):

        if let err = error as? NSURLError where err == .NotConnectedToInternet {
            // no internet connection
        } else {
            // other failures
        }
    }
}

Swift 3.0

  Alamofire.upload(multipartFormData: { multipartFormData in
    }, to: URL, method: .post,headers: nil,
       encodingCompletion:  { (result) in
        switch result {

        case .success( _, _, _): break

        case .failure(let encodingError ):
            print(encodingError)

            if let err = encodingError as? URLError, err.code == .notConnectedToInternet {
                // no internet connection
                print(err)
            } else {
                // other failures
            }

        }
    })

Using NetworkReachabilityManager

let networkReachabilityManager = Alamofire.NetworkReachabilityManager(host: "www.apple.com")

func checkForReachability() {
    self.networkReachabilityManager?.listener = { status in
        print("Network Status: \(status)")
        switch status {
        case .notReachable:
            //Show error here (no internet connection)
        case .reachable(_), .unknown:
            //Hide error here
        }
    }

    self.networkReachabilityManager?.startListening()
}

//How to Use : Just call below function in required class
if checkForReachability() {
   print("connected with network")
}
1
Eoin Norris On

In general if you can get the internet offline information from the actual call, its better than reachability. You can be certain that the actual API call has failed because the internet is down. If you test for reachability before you call an API and it fails then all you know is that when the test was done the internet was offline ( or Apple was down), you don't know that when you make the call the internet will be offline. You might think it is a matter of milliseconds after the reachability call returns, or you retrieved the stored value, but thats in fact non deterministic. The OS might have scheduled some arbitrary number of threads before reachability returns its values in its closure, or updates whatever global you are storing.

And reachability has historically had bugs in its own code.

This isn't to say that you shouldn't use alamofire's NetworkReachabilityManager to change your UI, listen to it and update all the UI components.

But if you have reason to call an API, at that API layer the test for reachability is redundant, or possibly will cause some subtle bugs.

0
Lukasz D On

In Alamofire 5 when error is of type AFError you can switch it like that:

 case .failure(let error):  // error is type of AFError
                    if error.isSessionTaskError {
                      // seems that one only get triggered when no internet 
                        print("Session task error")
                        print(error.localizedDescription)
                        }
                    switch error.responseCode {
                    case 401:
                        print("401 token invalid or expired")
                    default:
                        print("Error : \(error.responseCode ?? 0)")
                        print(error.localizedDescription)
                    }
                    print("All other errors:\(error)")
                case .finished:
                    break
                }
4
abhimuralidharan On

For swift 5 and Alamofire 5.4.4 ,I created a swift class called Connectivity . Use NetworkReachabilityManager class from Alamofire and configure the isConnectedToInternet() method as per your need.

import Foundation
import Alamofire

class Connectivity {
    class func isConnectedToInternet() -> Bool {
        return NetworkReachabilityManager()?.isReachable ?? false
    }
}

Usage:

if Connectivity.isConnectedToInternet() {
        print("Yes! internet is available.")
        // do some tasks..
 }

EDIT: Since swift is encouraging computed properties, you can change the above function like:

import Foundation
import Alamofire
class Connectivity {
    class var isConnectedToInternet:Bool {
        return NetworkReachabilityManager()?.isReachable ?? false
    }
}

and use it like:

if Connectivity.isConnectedToInternet {
        print("Yes! internet is available.")
        // do some tasks..
 }