iOS开发中判断网络连接的操作步骤如下:
1、添加framework,SystemConfiguration.framework;
2、倒入文件:
#import <SystemConfiguration/SystemConfiguration.h> #include <netdb.h>
3、判断方法:
- (BOOL) connectedToNetwork { // Create zero addy 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) { return NO; } BOOL isReachable = ((flags & kSCNetworkFlagsReachable) != 0); BOOL needsConnection = ((flags & kSCNetworkFlagsConnectionRequired) != 0); return (isReachable && !needsConnection) ? YES : NO; }
来自《The iPhone Developer's Cookbook》
最新版本的SDK下载地址:https://developers.google.com/mobile-ads-sdk/download#downloadios
SDK使用的开发指南地址:https://developers.google.com/mobile-ads-sdk/docs/ios/fundamentals#incorporating
最近要更新项目中的AdMob SDK,本来认为直接替换就可以了,但是却出现了一些错误。产生问题的原因是AdMob提供的SDK升级后,对应的开发指南却没有更新。从一个测试项目来开,按照开发指南添加SDK和相关代码后,错误如下所示:
根据问题详细描述,经过调查需要添加另外的两个库,分别是SdSupport.framework和StoreKit.framework,添加后对项目的重新编译就可以通过了!
至此,看似问题已经解决,但是运行时出现了强制退出。出现异常:libc++abi.dylib: terminating with uncaught exception of type NSException
问题解决方法是:在TARGETS-Build Settings-Other Linker Flags属性添加-ObjC属性即可!
运行效果如图:
开发环境:Xcode5.0+OS X10.8.5
多说一句:出错的原因可能是我们做了什么事情,或我们没做本应该做的事情!上述问题的解决是建立在特定情境中,希望对你有所帮助!:)
《The iPhone Developer's Cookbook》
没什么好说的,直接上代码:
1、UIDevice+Reachability.h:
#import <UIKit/UIKit.h> @interface UIDevice (Reachability) + (BOOL) networkAvailable; + (BOOL) activeWLAN; + (BOOL) activeWWAN; @end
2、UIDevice+Reachability.m:
#import <SystemConfiguration/SystemConfiguration.h> #include <arpa/inet.h> #include <netdb.h> #include <net/if.h> #include <ifaddrs.h> #import "UIDevice-Reachability.h" @implementation UIDevice (Reachability) SCNetworkConnectionFlags connectionFlags; + (NSString *) localWiFiIPAddress { BOOL success; struct ifaddrs * addrs; const struct ifaddrs * cursor; success = getifaddrs(&addrs) == 0; if (success) { cursor = addrs; while (cursor != NULL) { // the second test keeps from picking up the loopback address if (cursor->ifa_addr->sa_family == AF_INET && (cursor->ifa_flags & IFF_LOOPBACK) == 0) { NSString *name = [NSString stringWithUTF8String:cursor->ifa_name]; if ([name isEqualToString:@"en0"]) // Wi-Fi adapter return [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)cursor->ifa_addr)->sin_addr)]; } cursor = cursor->ifa_next; } freeifaddrs(addrs); } return nil; } #pragma mark Checking Connections + (void) pingReachabilityInternal { BOOL ignoresAdHocWiFi = NO; struct sockaddr_in ipAddress; bzero(&ipAddress, sizeof(ipAddress)); ipAddress.sin_len = sizeof(ipAddress); ipAddress.sin_family = AF_INET; ipAddress.sin_addr.s_addr = htonl(ignoresAdHocWiFi ? INADDR_ANY : IN_LINKLOCALNETNUM); // Recover reachability flags SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (struct sockaddr *)&ipAddress); BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &connectionFlags); CFRelease(defaultRouteReachability); if (!didRetrieveFlags) printf("Error. Could not recover network reachability flags\n"); } + (BOOL) networkAvailable { [self pingReachabilityInternal]; BOOL isReachable = ((connectionFlags & kSCNetworkFlagsReachable) != 0); BOOL needsConnection = ((connectionFlags & kSCNetworkFlagsConnectionRequired) != 0); return (isReachable && !needsConnection) ? YES : NO; } + (BOOL) activeWWAN { if (![self networkAvailable]) return NO; return ((connectionFlags & kSCNetworkReachabilityFlagsIsWWAN) != 0); } + (BOOL) activeWLAN { return ([UIDevice localWiFiIPAddress] != nil); } @end