2016-06-20 4 views
3

In meiner App überprüfe ich, dass, wenn mobile Daten aus dem Popup ist wie Ihre Datenverbindung überprüfen. Dafür schreibe ich diesen CodeWie kann ich überprüfen, ob mobile Daten oder WLAN aktiviert oder deaktiviert sind? ios swift

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(kCFAllocatorDefault, UnsafePointer($0)) 
     } 

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

     let isReachable = flags == .Reachable 
     let needsConnection = flags == .ConnectionRequired 

     return isReachable && !needsConnection 

    } 
} 

aber durch diesen Code seine einzige Kontrolle, dass Wifi verbunden ist oder nicht. aber wenn ich versuche mit 3g mobile daten zeigt es mir immer, dass deine mobilen daten nicht verbunden sind. also wie kann ich das lösen?

+0

Mit Dies funktioniert: [link] (https://github.com/ashleymills/Reachability .swift) – DadoZolic

+0

Dies funktioniert. während ich über WLAN verbunden bin. aber es funktioniert nicht, während ich über mobile Daten verbunden @ DadoZolic –

Antwort

2

Wenn Sie Super-Klasse in Ihrer Anwendung verwenden, dann unter Code

override func viewWillAppear(animated: Bool) 
    { 
     super.viewWillAppear(animated) 


     NSNotificationCenter.defaultCenter().addObserver(self, selector: "ShowNetConnectivity", name: SHOW_NO_INTERNET_CONNECTION, object: nil) 

     NSNotificationCenter.defaultCenter().addObserver(self, selector: "DisssmissConnectivity", name: DISMISS_INTERNET_CONNECTION, object: nil) 


     let reachability = SCNetworkReachabilityCreateWithName(nil, host)! 
     SCNetworkReachabilitySetCallback(reachability, { (_, flags, _) in 
      print(flags.rawValue) 

      if (flags.rawValue == NotReachable.rawValue && flags.rawValue != ReachableViaWiFi.rawValue && flags.rawValue != ReachableViaWWAN.rawValue) 
      { 
       if(isConnectionAvailable == true) 
       { 
        let nc = NSNotificationCenter.defaultCenter() 
        nc.postNotificationName(SHOW_NO_INTERNET_CONNECTION, object: nil) 
       } 
      }else 
      { 
       if(isConnectionAvailable == false) 
       { 
        let nc = NSNotificationCenter.defaultCenter() 
        nc.postNotificationName(DISMISS_INTERNET_CONNECTION, object: nil) 
       } 
      } 

      }, &context) 

     SCNetworkReachabilityScheduleWithRunLoop(reachability, CFRunLoopGetMain(), kCFRunLoopCommonModes) 
    } 

    override func viewWillDisappear(animated: Bool) 
    { 
     super.viewWillDisappear(animated) 
     NSNotificationCenter.defaultCenter().removeObserver(self, name: SHOW_NO_INTERNET_CONNECTION, object: nil) 
     NSNotificationCenter.defaultCenter().removeObserver(self, name: DISMISS_INTERNET_CONNECTION, object: nil) 
    } 
4

Versuchen Sie, wie unten Code:

Objekt der Erreichbarkeits Klasse erstellen wie,

var internetReachability = Reachability() 

Jetzt unten schreiben Code in viewDidLoad(),

NSNotificationCenter.defaultCenter().addObserver(self, selector: "reachabilityChanged:", name: kReachabilityChangedNotification, object: nil) 
    self.internetReachability = Reachability.reachabilityForInternetConnection() 
    self.internetReachability.startNotifier() 
    self.updateInterfaceWithReachability(self.internetReachability) 

Nun erstellen Funktion reachabilty von Wi-Fi sowie Datenpaket zu überprüfen,

func updateInterfaceWithReachability(reachability: Reachability) 
{ 
    let netStatus : NetworkStatus = reachability.currentReachabilityStatus() 
    switch (netStatus.rawValue) 
    { 
    case NotReachable.rawValue: 
     print("offline") 
     break 
    case ReachableViaWWAN.rawValue: 
     print("online") 
     break 
    case ReachableViaWiFi.rawValue: 
     print("online") 
     break 
    default : 
     print("offline") 
     break 
    } 
} 

// Rechability update status 
func reachabilityChanged(sender : NSNotification!) 
{ 
    let curReach : Reachability = sender.object as! Reachability 
    self.updateInterfaceWithReachability(curReach) 
} 

Hope this Ihnen helfen.

+0

sollte ich die gleiche Erreichbarkeit() -Klasse verwenden? –

+0

Ja. Hast du diese Bibliothek? –

+0

Nein, ich benutze es, indem ich öffentliche Klasse mache –

1

Hier ist, wie ich es mache und es funktioniert für mich. In meinem viewDidLoad:

do { 
     reachability = try Reachability.reachabilityForInternetConnection() 
    } catch { 
     print("Unable to create Reachability") 
     return 
    } 

    NSNotificationCenter.defaultCenter().addObserver(self, 
                selector: #selector(MainViewController.reachabilityChanged(_:)), 
                name: ReachabilityChangedNotification, 
                object: reachability) 

    do { 
     try reachability.startNotifier() 
    } catch { 
     print("This is not working.") 
     return 
    } 

Und die reachabilityChanged

func reachabilityChanged(note: NSNotification) { 

    let reachability = note.object as! Reachability 

    if reachability.isReachable() { 
     if reachability.isReachableViaWiFi() { 
      print("Reachable via WiFi") 
     } else { 
      print("Reachable via Cellular") 
     } 
    } else { 
     showNoConnectionAlert() 
     print("Not reachable") 
    } 
} 

dieses für mich Reachability