//显示网络连接状态,即在状态栏上显示一个旋转图标
UIApplication *app = [UIApplication sharedApplication];
app.networkActivityIndicatorVisible = YES;
1. 需要导入需要的框架 SystemConfiguration的框架
2. 使用的头文件,包括所需要的网络处理相关的头文件
#import <sys/socket.h>
#import <netinet/in.h>
#import
<arpa/inet.h>
#import <netdb.h>
#import
<SystemConfiguration/SCNetworkReachability.h>
3.添加函数
+ (BOOL)connectionIsLink{
struct sockaddr_in zeroAddress;
bzero(&zeroAddress, sizeof(zeroAddress));
zeroAddress.sin_len = sizeof(zeroAddress);
zeroAddress.sin_family = AF_INET;
// Recover reachability flags
SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
SCNetworkReachabilityFlags flags;
BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
CFRelease(defaultRouteReachability);
if (!didRetrieveFlags)
{
NSLog(@"Error. Could not recover network reachability flags");
return NO;
}
BOOL isReachable = flags & kSCNetworkFlagsReachable;
BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired;
return (isReachable && !needsConnection) ? YES : NO;
}