AppleWalker

Walker Softwareのサポートページです。 Mac iPhoneなどApple系のことを書いていきたいと思います。 訪問してくれた、みなさんよろしくね。(^^)ニコ

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
12



応援しよう!
banner3.jpg
 
 
 
アクセス数
 
 
Twitter
 
 
 
 
 
 
Apple-Style Plus
 
 
ApplePeople
 
 
 
 
 
 
 
 
全記事表示リンク
 

[開発Tips] WiFiか3Gか判定する方法

[iPhone][SDK][Apple]

iphone_sdk.png

開発のTipsです。

しかし、いろいろ書いてるけど少しは皆様のお役に立ててるん
でしょうか?コメントいただけると嬉しいです。

今回は、アップルのサンプルを利用してネットワークの判定を
する方法です。
自分のアプリに組み込んでるんで、説明しやすいので。(^^)

まず、アップルのサイトから、Reachabilityというサンプルを
ゲットして下さい。これ便利です。
あまり難しいことは考えなくても使えます。
えーと、その中からReachability.hとReachability.mを使いたい
プロジェクトにドラッグ&ドロップしてください。
その際に、ダイアログの一番上にあるデスティネーションなんたら
かんたらにチェックを入れてください。
そうすれば、そのプロジェクトのフォルダにもコピーされます。
あと、SystemConfiguration.frameworkを追加してください。

で、サンプルコードです。
興味あるかたは、Read Moreからどうぞ...

#import "Reachability.h"

@interface HogeViewController : UIViewController {
  IBOutlet UILabel *netStatus;
  NetworkStatus internetConnectionStatus;
}

@property(nonatomic, assign) UILabel *netStatus;
@property NetworkStatus internetConnectionStatus;

- (void)updateNetStatus;
- (void)reachabilityChanged:(NSNotification *)note;

@end

続いて .mファイルです。

#import "Reachability.h"

@implementation HogeViewController

@synthesize internetConnectionStatus, netStatus;

- (void)viewDidLoad {
  [[NSNotificationCenter defaultCenter] addObserver:self
    selector:@selector(reachabilityChanged:)
    name:@"kNetworkReachabilityChangedNotification"
    object:nil];
  [self updateNetStatus];
  [super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated {
  [self updateNetStatus];
  [super viewWillAppear:animated];
}

- (void)dealloc {
  [[NSNotificationCenter defaultCenter] removeObserver:self
   name:@"kNetworkReachabilityChangedNotification"
   object:nil];
  [super dealloc];
}

- (void)reachabilityChanged:(NSNotification *)note {
  [self updateNetStatus];
}

- (void)updateNetStatus {
  self.internetConnectionStatus = [[Reachability sharedReachability] internetConnectionStatus];

if (self.internetConnectionStatus == NotReachable) {
   netStatus.text = @"Not Connection";
 } else if (self.internetConnectionStatus == ReachableViaCarrierDataNetwork) {
   netStatus.text = @"3G Mode";
 } else if (self.internetConnectionStatus == ReachableViaWiFiNetwork) {
   netStatus.text = @"WiFi Mode";
 }
}

これだけです。
緑の部分が定数ですので判定に使えます。
あと、Reachabilityのサンプルを見ればわかるのですが、ローカルの
判定やホストの判定もできます。便利ですね。

最近、何を書いていいかわからなくなってるWalkerです。
ブログは難しいですね。日記っていったら、そうなんですが...
やはり見てくれる人のことを考えると構えてしまいます。(^^)

皆様のお役に立ててれば幸いです。
でわ?

関連記事


Comments

Leave a Comment


Body