关于Reachability的类倒入并添加SystemConfiguration.framework
/判断当前的网络是3g还是wifi
-(NSString*)GetCurrntNet
{
NSString* result;
Reachability *r = [ReachabilityreachabilityWithHostName:@"www.apple.com"];
switch ([r currentReachabilityStatus]) {
caseNotReachable:// 没有网络连接
result=nil;
break;
caseReachableViaWWAN:// 使用3G网络
result=@"3g";
break;
caseReachableViaWiFi:// 使用WiFi网络
result=@"wifi";
break;
}
return result;
}
// 在应用程序启动的时候加入网络的检测
@classReachability;
@interfaceMyAppDelegate: NSObject<UIApplicationDelegate>
{ Reachability*hostReach;}
@end
// MyAppDelegate.m
- (void)reachabilityChanged:(NSNotification*)note{
Reachability* curReach= [note object];
NSParameterAssert([curReach isKindOfClass: [Reachabilityclass]]);
NetworkStatusstatus= [curReach currentReachabilityStatus];
if(status == NotReachable) {
UIAlertView*alert= [[UIAlertViewalloc] initWithTitle:@"AppName""
message:@"NotReachable"delegate:nil
cancelButtonTitle:@"YES"otherButtonTitles:nil];
[alert show];
[alert release];
}}
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// ...
// 监测网络情况
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];
hostReach = [[Reachability reachabilityWithHostName:@"www.google.com"] retain];
[hostReach startNotifer];
// ...}