Cocoaã®æ¥ã : [iOS] SCNetworkReachabilityGetFlags ã®ãããã¯ã®ä»¶
以å、Cocoaã®æ¥ã : ãããã¯ã¼ã¯æ¥ç¶ç¶æ³ãç¥ã ã¨ããããã°ãæ¸ãã。
ãã®å¾ããã£ããã¨ãç´¹ä»ãã。
SCNetworkReachabilityGetFlags ããããã¯ãã件
ãããã¯ã¼ã¯æ¥ç¶ç¶æ³ãåå¾ãã SCNetworkReachabilityGetFlagsé¢æ° ãå¦çä¸ã«ãããã¯ãã¦ãã¾ããã¨ãããã£ã。次ã®ç°å¢ã§ãããã¯ããç¾è±¡ã確èªã§ãã。
・iPhone 3GS/iOS4.2
・3GS→× WiFi→◯ ※ãã©ã¤ãã¢ã¼ã+WiFiæå¹
・WiFiã«ã¼ã¿→× ※ã¤ã³ã¿ã¼ãããéæ¥ç¶(å åç·æ)
ã¤ã¾ã iPhone 㧠WiFiæ¥ç¶ãã¦ããã、ãã®æ¥ç¶å ã® WiFiã«ã¼ã¿ããå ã®ã¤ã³ã¿ã¼ãããã«æ¥ç¶ãã¦ããªã。ãã®ç°å¢ã§é¢æ°ãå¼ã¶ã¨ããã§å¦çãåæ¢ã、60ç§å¾ã«ã¢ããªã±ã¼ã·ã§ã³ã®åä½ãåéãã。60ç§ãå¾ ããã«ã¢ããªã±ã¼ã·ã§ã³ãè½ã¡ãã±ã¼ã¹ããã£ã(ã¯ã©ãã·ã¥ãã°ãã SCNetworkReachabilityGetFlags ãåå ã§ãããã¨ãåãã£ã¦ãã)。
ã¿ã¤ã ã¢ã¦ãã¯å®æ¸¬å¤ã§60ç§。å¤æ´ã§ããããªé¢æ°ã¯è¦å½ãããªã。
ãã®ãã¨ããSCNetworkReachability系㮠API ãå©ç¨ããå ´åã¯、SCNetworkReachabilityGetFlags ã¯ä½¿ãã、ã³ã¼ã«ããã¯é¢æ°ã使ã£ãéåæå®è£ ãè¯ãã¨æããã。
ãµã³ãã«ããã°ã©ã NetworkRechable ã®æ¹è¯
ä»åã®æ å ±ãå ã«ä»¥åä½æãããµã³ãã«ããã°ã©ã ãæ¹è¯ãã。SCNetworkReachabilityGetFlags ã®ä½¿ç¨ããã㦠SCNetworkReachabilitySetCallback ã使ã£ãéåææ¹å¼ã«å ¨é¢çã«æ¸ãæãã。
ã¤ã³ã¿ã¼ãã§ã¤ã¹ã¯ãã。
#import <Foundation/Foundation.h> #import <SystemConfiguration/SystemConfiguration.h> typedef enum { kNetworkReachableUninitialization = 0, kNetworkReachableNon, kNetworkReachableWiFi, kNetworkReachableWWAN } NetworkReachabilityConnectionMode; #define NetworkReachabilityChangedNotification @"NetworkReachabilityChangedNotification" @interface NetworkReachability : NSObject { SCNetworkReachabilityRef reachability_; NetworkReachabilityConnectionMode connectionMode_; } + (NetworkReachability*)networkReachabilityWithHostname:(NSString *)hostname; - (NetworkReachabilityConnectionMode)connectionMode; - (NSString*)connectionModeString; @endå®è£ ã¯ãã¤ã³ãã ã。
// åæå - (id)initWithHostname:(NSString*)hostname { if (self = [super init]) { reachability_= SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, [hostname UTF8String]); connectionMode_ = kNetworkReachableUninitialization; [self startNotifier_]; } return self; } // ã³ã¼ã«ããã¯è¨å® - (BOOL)startNotifier_ { BOOL ret = NO; SCNetworkReachabilityContext context = {0, self, NULL, NULL, NULL}; if(SCNetworkReachabilitySetCallback(reachability_, ReachabilityCallback_, &context)) { if(SCNetworkReachabilityScheduleWithRunLoop( reachability_, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode)) { ret = YES; } } return ret; } // ã³ã¼ã«ããã¯å static void ReachabilityCallback_(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void* info) { NSAutoreleasePool* myPool = [[NSAutoreleasePool alloc] init]; NetworkReachability* noteObject = (NetworkReachability*)info; [noteObject updateConnectionModeWithFlags_:flags]; [[NSNotificationCenter defaultCenter] postNotificationName:NetworkReachabilityChangedNotification object:noteObject]; [myPool release]; }å é¨çã« SCNetworkReachabilitySetCallback ã使ã、ã³ã¼ã«ããã¯ãããã¿ã¤ãã³ã°ã§ connectionMode_ ãæ´æ°ãã。ã³ã¼ã«ããã¯ã¯æ¥ç¶ç¶æ³ã«å¤åããã£ãã¨ãã«å¼ã³åºããã(ä¾ãã°、3G→WiFiã、WiFi→OFF)。
å©ç¨ã¤ã¡ã¼ã¸
NetworkReachability* networkReachability = [NetworkReachability networkReachabilityWithHostname:@"www.google.com"]; NetworkReachabilityConnectionMode mode = [networkReachability connectionMode];ä½åº¦ã使ãå ´åã¯ã¤ã³ã¹ã¿ã³ã¹ãã¨ã£ã¦ããã¦ä½¿ãåãã°è¯ã。
æ¥ç¶ç¶æ³ãå¤åããæã«éç¥ãåãåãããå ´å㯠NetworkReachabilityChangedNotification ãç£è¦ããã°è¯ã。
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:NetworkReachabilityChangedNotification object: nil]; - (void) reachabilityChanged: (NSNotification* )note { NSLog(@"changed:%@", note); [self updateStatus]; }
åè
ååã®ã¿ã¤ã ã©ã°
ã³ã¼ã«ããã¯ã使ã£ãéåæå¼ã®å ´å、æ¥ç¶ç¶æ³ãå¤åããæã®éç¥ã«è¥å¹²ã®ã¿ã¤ã ã©ã°ããã(é·ãã¯ãªã〜ï¼ç§æªæº)。å¤ãã®å ´ååé¡ãªããã¢ããªã±ã¼ã·ã§ã³èµ·åæã®æåã®å¦çã§ãã®æ¥ç¶ç¶æ³ã使ãå ´åã¯åé¡ã«ãªããã¨ããã。å®éç°¡åãªã¢ããªã±ã¼ã·ã§ã³ã§ã¯æåã®ç»é¢ã表示ãããå¾ã«æåã®å¤æ´éç¥ãå±ãã®ã§、æåã®ç»é¢ã§ãããã¯ã¼ã¯ã®æ¥ç¶ç¶æ³ã確èªãã¦ããã¢ã¯ã·ã§ã³ãèµ·ããã¨ããå¦çã®å ´åãã®ã¿ã¤ã ã©ã°ãåé¡ã«ãªã。éåæå¦çã ããæ¬è³ªçãªè§£æ±ºæ¹æ³ã¯ç¡ãã®ã§、NetworkReachabilityChangedNotification ã®å°çãå¾ ã£ã¦ããå¦çãè¡ãã、ååã®ã¿æ¥ç¶ç¶æ³ã¯ç¡è¦ãããããªå¯¾å¦ãå¿ è¦ã«ãªã。å ã®ãµã³ãã«ã§ã¯ãã®æ¥ç¶ç¶æ³ã決ã¾ããªãç¶æ (ããªãã¡æåã®éç¥ãæ¥ãã§ã®ç¶æ )ã¨ã㦠kNetworkReachableUninitialization ã¨ããåæå¤ãè¨å®ãã¦ãã。å©ç¨å´ã¯ -[NetworkReachability connectionMode] ã§ãã®å¤ã帰ã£ã¦ããå ´å、ãã®ã¿ã¤ã ã©ã°ä¸ã§ãããã¨ãå¤æã§ããã®ã§、ãããå ã«ãªãããã®å¯¾å¦ãè¡ãæ¹æ³ãèãããã。SCNetworkReachabilityCreateWithName()ã§æå®ãããã¹ã
SCNetworkReachability ã¤ã³ã¹ã¿ã³ã¹ãä½æããæã«ä½¿ã SCNetworkReachabilityCreateWithName é¢æ°ã¸æ¸¡ããã¹ãåã¯ç¾å®ã«åå¨ãã¦ãããã¯ã¼ã¯ã§å°éå¯è½ãªãã®ãæå®ããå¿ è¦ããã。試ãã« dummy.dummy.dummy ã®ãããªé©å½ãªãã¹ãåã渡ããã¨ãã、常ã«æ¥ç¶ä¸è½ç¶æ ã¨ãªã£ã¦ãã¾ã£ã(å½ããåã¨ããã°å½ããåã ã...)。ã½ã¼ã¹ã³ã¼ã
GitHubããã©ãã。
NetworkReachable at 2010-12-13 from xcatsan/iOS-Sample-Code - GitHub
åèæ å ±
SCNetworkReachability Reference
Responses
Leave a Response