小秋的家

          home

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            1 Posts :: 91 Stories :: 17 Comments :: 0 Trackbacks

          //在程序的啟動處,開啟通知

          - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

          {

            //.....

          //開啟網絡狀況的監聽

          [[NSNotificationCenter defaultCenteraddObserver:self

                   selector:@selector(reachabilityChanged:)

               namekReachabilityChangedNotification

             objectnil];

          hostReach = [[Reachability reachabilityWithHostName:@"www.google.com"retain];//可以以多種形式初始化

          [hostReach startNotifier];  //開始監聽,會啟動一個run loop

                  [self updateInterfaceWithReachabilityhostReach];

            //.....

          }


          // 連接改變

          - (void) reachabilityChanged: (NSNotification* )note

          {

          Reachability* curReach = [note object];

          NSParameterAssert([curReach isKindOfClass: [Reachability class]]);

          [self updateInterfaceWithReachability: curReach];

          }


          //處理連接改變后的情況

          - (void) updateInterfaceWithReachability: (Reachability*) curReach

          {

              //對連接改變做出響應的處理動作。

                  NetworkStatus status = [curReach currentReachabilityStatus];

              

          if (status == NotReachable) {  //沒有連接到網絡就彈出提實況

          UIAlertView *alert = [[UIAlertView allocinitWithTitle:@"My App Name"

                                        message:@"NotReachable"

                                        delegate:nil

                                        cancelButtonTitle:@"YES" otherButtonTitles:nil];

                                        [alert show];

                                        [alert release];

          }

          }

          posted on 2012-12-05 11:05 棋劍小秋 閱讀(243) 評論(0)  編輯  收藏 所屬分類: iPhone
          主站蜘蛛池模板: 逊克县| 威信县| 南丰县| 成武县| 会同县| 渝中区| 紫金县| 牟定县| 安远县| 思茅市| 西畴县| 金昌市| 化隆| 商都县| 河曲县| 孙吴县| 盐池县| 元阳县| 宜州市| 昭平县| 漳州市| 城口县| 罗山县| 新民市| 视频| 信阳市| 秦皇岛市| 会东县| 闵行区| 九龙坡区| 广宗县| 大竹县| 灵武市| 兴和县| 林甸县| 崇义县| 息烽县| 万宁市| 金川县| 江津市| 永川市|