2013-03-01 75 views
0

当设备连接到Wi-Fi或移动数据网络时,我是否可以自动发布通知?当设备连接到无线网络时,iOS发布通知

我的想法是,当连接情况,使用这在我的应用程序委托:

[[NSNotificationCenter defaultCenter] 
postNotificationName:@"connectedToNetwork" 
object:nil]; 

,并在我的课赶上此通知:

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(doSomething) 
              name:@"connectedToNetwork" 
              object:nil]; 

我使用可达性看看设备是否连接到互联网,但这不是我想要的。当设备连接到Wi-Fi或移动网络时,我需要自动发出一些通知。我不在乎是否通过该网络访问互联网,我只需要在连接发生时收到通知。

回答

0
// allocate a reachability object 
Reachability* reach = [Reachability reachabilityWithHostname:@"www.google.com"]; 

// tell the reachability that we DONT want to be reachable on 3G/EDGE/CDMA 
reach.reachableOnWWAN = NO; 

// here we set up a NSNotification observer. The Reachability that caused the notification 
// is passed in the object parameter 
[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(reachabilityChanged:) 
              name:kReachabilityChangedNotification 
              object:nil]; 

[reach startNotifier] 
相关问题