2

I am creating an application, which implements MultipeerConnectivity framework and therefore I would like to check user's internet connection TYPE before allowing him to proceed. In other words, my question is:

Is there a way in iOS to check wether user connected to the internet via Cellular or WiFi? I am sure there is but I could not find it.

Notice: I am not asking how to check wether user is connected to the Internet or not(I know how to do that). Thank you in advance.

EBDOKUM
  • 1,696
  • 3
  • 15
  • 33

2 Answers2

3

This one is not swift, but I'm sure you'll find a swift variant.

See the Reachability class, which you can find at Apple as example and can tweak as you want. Chances are high you are already using it already to detect if you are connected or not.

From this class, you can ask the connection status:

Reachability *reachability = [Reachability reachabilityForInternetConnection];
[reachability startNotifier];

NetworkStatus status = [reachability currentReachabilityStatus];

if(status == NotReachable) 
{
    //No internet
}
else if (status == ReachableViaWiFi)
{
    //WiFi
}
else if (status == ReachableViaWWAN) 
{
    //3G
}

See iOS Detect 3G or WiFi

Community
  • 1
  • 1
Jelle De Laender
  • 577
  • 3
  • 16
  • Tip: it's better to flag a question as duplicate rather than post a copy of another answer. The question was tagged Swift anyway, not Objective-C. Thank you. – Eric Aya Jan 10 '16 at 11:22
  • That's why I didn't flag it as duplicate, as I'm given the asker a direction where he can find something and which direction he can look into, as this solution works as well in Swift, with small adaptions ;) – Jelle De Laender Jan 10 '16 at 11:30
  • Then the right move would be to adapt it in Swift yourself, it would make a nice answer. :) As it is, it is off-topic because of the wrong language - even if, yes, I know, it addresses the question... It's not to be pedantic, just that if we begin to "answer to Java questions in Python", we're doomed. :p – Eric Aya Jan 10 '16 at 11:33
0
class func hasConnectivity() -> Bool
 {

 let reachability: Reachability = 

Reachability.reachabilityForInternetConnection()

  let networkStatus: Int = reachability.currentReachabilityStatus().rawValue

  return networkStatus != 0
}


=====================================

import Foundation
import SystemConfiguration

public class Reachability {

    class func isConnectedToNetwork() -> Bool {

        var zeroAddress = sockaddr_in(sin_len: 0, sin_family: 0, sin_port: 0, sin_addr: in_addr(s_addr: 0), sin_zero: (0, 0, 0, 0, 0, 0, 0, 0))
        zeroAddress.sin_len = UInt8(sizeofValue(zeroAddress))
        zeroAddress.sin_family = sa_family_t(AF_INET)

        let defaultRouteReachability = withUnsafePointer(&zeroAddress) {
            SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer($0)).takeRetainedValue()
        }

        var flags: SCNetworkReachabilityFlags = 0
        if SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags) == 0 {
            return false
        }

        let isReachable = (flags & UInt32(kSCNetworkFlagsReachable)) != 0
        let needsConnection = (flags & UInt32(kSCNetworkFlagsConnectionRequired)) != 0

        return isReachable && !needsConnection
    }

}

=> Here is Post it helps to you:-

https://github.com/ashleymills/Reachability.swift

EBDOKUM
  • 1,696
  • 3
  • 15
  • 33
Akash
  • 461
  • 2
  • 14
  • That code (apparently from http://stackoverflow.com/a/25774420/1187415) does not compile with Swift 2. See e.g. http://stackoverflow.com/a/25623647/1187415 for an update. – Martin R Jan 10 '16 at 11:44
  • It does not answer my question, does it? I mean, I even added the "Notice" and you still answered something I asked not to answer with – EBDOKUM Jan 10 '16 at 11:59
  • sorry for i was busy some work thats why i could not replay: and i had not try in swift 2.0 so i can not say more about swift 2.0 – Akash Jan 10 '16 at 12:02
  • It's irrelevant thus I flagged it – EBDOKUM Jan 10 '16 at 13:26
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - [From Review](/review/low-quality-posts/10840257) – devpro Jan 10 '16 at 17:35
  • @devpro Why not? How can you be sure? I see it as a not very useful answer because lots of explanations or missing. Mostly, why it answers the question and what the crucial parts of the code are. – NoDataDumpNoContribution Jan 10 '16 at 18:22