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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
//
//  EZWifiInfoViewController.m
//  EZOpenSDKDemo
//
//  Created by DeJohn Dong on 15/10/29.
//  Copyright © 2015年 Ezviz. All rights reserved.
//
 
#import "EZWifiInfoViewController.h"
#import <SystemConfiguration/CaptiveNetwork.h>
#import "DDKit.h"
#import "EZWifiConfigViewController.h"
#import "EZAPWiFiConfigViewController.h"
 
#define WIFI_PREFROOT_URL @"prefs:root=WIFI"
#define WIFI_IOS10_WIFI_URL @"App-Prefs:root=WIFI"
 
@interface EZWifiInfoViewController ()
 
@property (nonatomic, weak) IBOutlet UIButton *nextButton;
@property (nonatomic, weak) IBOutlet UILabel *tipsLabel;
@property (nonatomic, weak) IBOutlet UITextField *nameTextField;
@property (nonatomic, weak) IBOutlet UITextField *passwordTextField;
@property (nonatomic, weak) IBOutlet UILabel *nameLabel;
@property (nonatomic, weak) IBOutlet UILabel *passwordLabel;
 
 
@end
 
@implementation EZWifiInfoViewController
 
- (void)dealloc
{
    
}
 
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = NSLocalizedString(@"wifi_connect_wifi_title", @"第二步,连接WiFi");
    
    self.nameTextField.leftView = self.nameLabel;
    self.nameTextField.leftViewMode = UITextFieldViewModeAlways;
    self.nameTextField.enabled = NO;
    
    self.passwordTextField.leftView = self.passwordLabel;
    self.passwordTextField.leftViewMode = UITextFieldViewModeAlways;
 
    [self.nameTextField dd_addCornerRadius:4.0f lineColor:[UIColor lightGrayColor]];
    [self.passwordTextField dd_addCornerRadius:4.0f lineColor:[UIColor lightGrayColor]];
    [self.nextButton dd_addCornerRadius:19.0 lineColor:[UIColor dd_hexStringToColor:@"0x1b9ee2"]];
}
 
- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self addNotifications];
}
 
- (void) viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self removeNotifications];
}
 
- (void) viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    [self getWiFiName];
    
    [self checkWiFi];
}
 
- (void)addNotifications
{
    [self removeNotifications];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(applicationDidBecomeActive)
                                                 name:UIApplicationDidBecomeActiveNotification
                                               object:nil];
}
 
- (void)removeNotifications
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
 
- (void) getWiFiName
{
    NSArray *interfaces = CFBridgingRelease(CNCopySupportedInterfaces());
    for (NSString *ifnam in interfaces)
    {
        NSDictionary *info = CFBridgingRelease(CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam));
        self.nameTextField.text = info[@"SSID"];
        break;
    }
}
 
- (void) checkWiFi
{
    if (!self.nameTextField.text || self.nameTextField.text.length <= 0)
    {
        self.nextButton.enabled = NO;
        [self showJumpSettingTip];
    }
    else
    {
        self.nextButton.enabled = YES;
    }
}
 
- (void) showJumpSettingTip
{
    UIAlertController *alertController =
        [UIAlertController alertControllerWithTitle:NSLocalizedString(@"alert_title",@"提示")
                                            message:NSLocalizedString(@"wifi_info_no_wifi_msg", @"未连接Wi-Fi,请设置合适的Wi-Fi")
                                     preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"cancel",@"取消")
                                                           style:UIAlertActionStyleCancel
                                                         handler:nil];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"wifi_info_setting",@"设置")
                                                       style:UIAlertActionStyleDefault
                                                     handler:^(UIAlertAction * _Nonnull action) {
                                                         NSString *urlStr = [[[UIDevice currentDevice] systemVersion] floatValue] >= 10.0?WIFI_IOS10_WIFI_URL:WIFI_PREFROOT_URL;
        
                                                         NSURL * url = [NSURL URLWithString:urlStr];
                                                         if([[UIApplication sharedApplication] canOpenURL:url])
                                                         {
                                                             [[UIApplication sharedApplication] openURL:url];
                                                         }
                                                     }];
    [alertController addAction:cancelAction];
    [alertController addAction:okAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
}
 
- (void) showModeSelect
{
    UIAlertController *alertController =
    [UIAlertController alertControllerWithTitle:nil
                                        message:NSLocalizedString(@"wifi_mode_select_msg", @"请选择配网模式")
                                 preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *normalAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"wifi_mode_normal",@"一般配网")
                                                           style:UIAlertActionStyleDefault
                                                         handler:^(UIAlertAction * _Nonnull action) {
                                                             [self performSegueWithIdentifier:@"go2WifiConfig" sender:nil];
                                                         }];
    
    UIAlertAction *soundAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"wifi_mode_sound",@"声波配网")
                                                           style:UIAlertActionStyleDefault
                                                         handler:^(UIAlertAction * _Nonnull action) {
                                                             [self performSegueWithIdentifier:@"go2WifiConfig" sender:nil];
                                                         }];
    
    UIAlertAction *apAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"wifi_mode_ap",@"热点配网")
                                                       style:UIAlertActionStyleDefault
                                                     handler:^(UIAlertAction * _Nonnull action) {
                                                         [self performSegueWithIdentifier:@"go2APWifiConfig" sender:nil];
                                                     }];
    
    UIAlertAction *lineAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"wifi_mode_line",@"有线连接")
                                                         style:UIAlertActionStyleDefault
                                                       handler:^(UIAlertAction * _Nonnull action) {
                                                           [self.navigationController dismissViewControllerAnimated:YES completion:nil];
                                                     }];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"cancel",@"取消")
                                                           style:UIAlertActionStyleCancel
                                                         handler:^(UIAlertAction * _Nonnull action) {
                                                         }];
    
    if (self.supportSmartMode)
    {
        [alertController addAction:normalAction];
    }
    
    if (self.supportSoundMode)
    {
        [alertController addAction:soundAction];
    }
    
    if (self.supportApMode)
    {
        [alertController addAction:apAction];
    }
    
    [alertController addAction:lineAction];
    
    [alertController addAction:cancelAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
}
 
#pragma mark - notification
 
- (void) applicationDidBecomeActive
{
    [self getWiFiName];
    
    [self checkWiFi];
}
 
 
#pragma mark - Navigation
 
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    if ([[segue destinationViewController] isKindOfClass:[EZWifiConfigViewController class]]) {
        EZWifiConfigViewController *vc = (EZWifiConfigViewController *)[segue destinationViewController];
        vc.ssid = self.nameTextField.text;
        vc.password = self.passwordTextField.text;
        vc.supportSmartMode = self.supportSmartMode;
        vc.supportSoundMode = self.supportSoundMode;
    }
    
    if ([[segue destinationViewController] isKindOfClass:[EZAPWiFiConfigViewController class]]) {
        EZAPWiFiConfigViewController *vc = (EZAPWiFiConfigViewController *)[segue destinationViewController];
        vc.ssid = self.nameTextField.text;
        vc.password = self.passwordTextField.text;
    } 
}
 
- (IBAction)nextAction:(id)sender
{
    //热点配网
    [self performSegueWithIdentifier:@"go2APWifiConfig" sender:nil];
 
//    [self showModeSelect];
}
 
@end