码迷,mamicode.com
首页 > 移动开发 > 详细

iOS开发——网络编程Swift篇&(一)网络监测

时间:2015-06-07 23:09:33      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:

网络监测

 

 1 enum ReachabilityType {
 2     case WWAN,
 3     WiFi,
 4     NotConnected
 5 }
 6 
 7 public class Reachability {
 8     
 9     /**
10     :see: Original post - http://www.chrisdanielson.com/2009/07/22/iphone-network-connectivity-test-example/
11     */
12     class func isConnectedToNetwork() -> Bool {
13         
14         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))
15         zeroAddress.sin_len = UInt8(sizeofValue(zeroAddress))
16         zeroAddress.sin_family = sa_family_t(AF_INET)
17         
18         let defaultRouteReachability = withUnsafePointer(&zeroAddress) {
19             SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer($0)).takeRetainedValue()
20         }
21         
22         var flags: SCNetworkReachabilityFlags = 0
23         if SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags) == 0 {
24             return false
25         }
26         
27         let isReachable = (flags & UInt32(kSCNetworkFlagsReachable)) != 0
28         let needsConnection = (flags & UInt32(kSCNetworkFlagsConnectionRequired)) != 0
29         
30         return (isReachable && !needsConnection) ? true : false
31     }
32     
33     class func isConnectedToNetworkOfType() -> ReachabilityType {
34         
35         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))
36         zeroAddress.sin_len = UInt8(sizeofValue(zeroAddress))
37         zeroAddress.sin_family = sa_family_t(AF_INET)
38         
39         
40         
41         let defaultRouteReachability = withUnsafePointer(&zeroAddress) {
42             SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer($0)).takeRetainedValue()
43         }
44         
45         var flags: SCNetworkReachabilityFlags = 0
46         if SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags) == 0 {
47             return .NotConnected
48         }
49         
50         let isReachable = (flags & UInt32(kSCNetworkFlagsReachable)) != 0
51         let isWWAN = (flags & UInt32(kSCNetworkReachabilityFlagsIsWWAN)) != 0
52         //let isWifI = (flags & UInt32(kSCNetworkReachabilityFlagsReachable)) != 0
53         
54         if(isReachable && isWWAN){
55             return .WWAN
56         }
57         if(isReachable && !isWWAN){
58             return .WiFi
59         }
60         
61         return .NotConnected
62         //let needsConnection = (flags & UInt32(kSCNetworkFlagsConnectionRequired)) != 0
63         
64         //return (isReachable && !needsConnection) ? true : false
65     }

 使用:

 1     
 2     // MARK: - 检测网络连接情况
 3     @IBOutlet var statusLabel: UILabel!
 4     @IBOutlet var typeLabel: UILabel!
 5     
 6     @IBAction func checkButtonUpinside(sender: UIButton) {
 7         
 8         if Reachability.isConnectedToNetwork() {
 9             statusLabel.text = "网络连接:可用"
10         } else {
11             statusLabel.text = "网络连接:不可用"
12         }
13         
14         let statusType = Reachability.isConnectedToNetworkOfType()
15         switch statusType{
16         case .WWAN:
17             typeLabel.text = "连接类型:移动网络"
18         case .WiFi:
19             typeLabel.text = "连接类型:WiFi"
20             
21         case .NotConnected:
22             typeLabel.text = "连接类型:没有网络连接"
23         }
24     }

 

 
 

iOS开发——网络编程Swift篇&(一)网络监测

标签:

原文地址:http://www.cnblogs.com/iCocos/p/4559601.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!