forked from iimgal/StudyiOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetConnViewController.m
More file actions
143 lines (116 loc) · 4.3 KB
/
Copy pathNetConnViewController.m
File metadata and controls
143 lines (116 loc) · 4.3 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//
// NetConnViewController.m
// StudyiOS
//
// Created by on 11-10-24.
// Copyright (c) 2011年 ZhangYiCheng. All rights reserved.
//
#import "NetConnViewController.h"
#import<SystemConfiguration/SystemConfiguration.h>
#import<netdb.h>
#import "Reachability.h"
@implementation NetConnViewController
@synthesize textField;
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidUnload
{
[self setTextField:nil];
[super viewDidUnload];
}
- (void)viewDidLoad
{
// 增加Code按钮,可跳转至教学页面
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"Code" style:UIBarButtonItemStyleBordered target:self action:@selector(code)];
self.navigationItem.rightBarButtonItem = item;
[super viewDidLoad];
}
- (IBAction)isConn:(id)sender {
struct sockaddr_in zeroAddress;
bzero(&zeroAddress, sizeof(zeroAddress));
zeroAddress.sin_len = sizeof(zeroAddress);
zeroAddress.sin_family = AF_INET;
SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
SCNetworkReachabilityFlags flags;
BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
CFRelease(defaultRouteReachability);
if (!didRetrieveFlags) {
printf("Error. Count not recover network reachability flags\n");
}
BOOL isReachable = flags & kSCNetworkFlagsReachable;
BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired;
NSString *result;
if ((isReachable && !needsConnection) ? YES : NO) {
result = @"Connection Successed!!!";
}else {
result = @"Connection Faild!!!";
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"TestConnection"
message:result
delegate:self
cancelButtonTitle:@"OK,I Know"
otherButtonTitles:nil];
[alert show];
}
- (IBAction)connType:(id)sender {
NSString *connectionKind;
// 测试连接可用性
Reachability *hostReach = [Reachability reachabilityWithHostName:@"www.baidu.com"];
// 判断连接类型
switch ([hostReach currentReachabilityStatus]) {
case NotReachable:
connectionKind = @"没有网络链接";
break;
case ReachableViaWiFi:
connectionKind = @"当前使用的网络类型是WIFI";
break;
case ReachableViaWWAN:
connectionKind = @"当前使用的网络链接类型是WWAN(3G)";
break;
default:
break;
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"网络链接类型"
message:connectionKind
delegate:self
cancelButtonTitle:@"知道了,谢谢"
otherButtonTitles:nil];
[alert show];
}
- (IBAction)isSiteConn:(id)sender {
[self.textField resignFirstResponder];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
NSString *message;
Reachability *hostReach = [Reachability reachabilityWithHostName:self.textField.text];
if ([hostReach currentReachabilityStatus] == NotReachable) {
message = @"连接失败";
}else {
message = @"连接成功";
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"结果"
message:message
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
//[Reachability hostAvailable:@"www.google.com"]
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.textField resignFirstResponder];
}
// 跳转至教学页面
- (void)code
{
CodeViewController *controller = [[CodeViewController alloc] initWithNibName:@"CodeViewController" bundle:nil];
NSString *name = [NSString stringWithUTF8String:object_getClassName(self)];
controller.className = name;
[self.navigationController pushViewController:controller animated:YES];
}
@end