您的位置:首页 > 理论基础 > 计算机网络

IOS开发中如何检测网络连接

2015-12-03 10:28 573 查看
1)将**SystemConfiguration**framework添加到项目中。

2)添加Reachability.h和**Reachability.m**githubhttps://github.com/tonymillion/Reachability

3)实现方法

#import "Reachability.h"

// Add this to the interface in the .m file of your view controller
@interface MyViewController ()
{
Reachability *internetReachableFoo;
}
@end


实现方法

// Checks if we have an internet connection or not
- (void)testInternetConnection
{
internetReachableFoo = [Reachability reachabilityWithHostname:@"www.google.com"];

// Internet is reachable
internetReachableFoo.reachableBlock = ^(Reachability*reach)
{
// Update the UI on the main thread
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"Yayyy, we have the interwebs!");
});
};

// Internet is not reachable
internetReachableFoo.unreachableBlock = ^(Reachability*reach)
{
// Update the UI on the main thread
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"Someone broke the internet :(");
});
};

[internetReachableFoo startNotifier];
}


StackOverFlow
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: