3 * Copyright (c) 2020 Project CHIP Authors
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #import "WifiViewController.h"
19 #import "CHIPUIViewUtils.h"
20 #import "DefaultsUtils.h"
22 @interface WifiViewController ()
23 @property (strong, nonatomic) UITextField * networkSSID;
24 @property (strong, nonatomic) UITextField * networkPassword;
25 @property (strong, nonatomic) UIButton * saveButton;
26 @property (strong, nonatomic) UIButton * clearButton;
29 @implementation WifiViewController
31 // MARK: UIViewController methods
38 UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
39 [self.view addGestureRecognizer:tap];
42 - (void)viewWillAppear:(BOOL)animated
44 [self fillNetworkConfigWithDefaults];
47 - (void)dismissKeyboard
49 [self.networkSSID resignFirstResponder];
50 [self.networkPassword resignFirstResponder];
57 self.view.backgroundColor = UIColor.whiteColor;
60 UILabel * titleLabel = [CHIPUIViewUtils addTitle:@"Network Configuration" toView:self.view];
63 UIStackView * stackView = [UIStackView new];
64 stackView.axis = UILayoutConstraintAxisVertical;
65 stackView.distribution = UIStackViewDistributionFill;
66 stackView.alignment = UIStackViewAlignmentLeading;
67 stackView.spacing = 30;
68 [self.view addSubview:stackView];
70 stackView.translatesAutoresizingMaskIntoConstraints = false;
71 [stackView.topAnchor constraintEqualToAnchor:titleLabel.bottomAnchor constant:30].active = YES;
72 [stackView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor constant:30].active = YES;
73 [stackView.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor constant:-30].active = YES;
76 UILabel * informationLabel = [UILabel new];
77 informationLabel.font = [UIFont systemFontOfSize:17 weight:UIFontWeightThin];
78 informationLabel.text = @"Please provide the home network your phone is connected to.";
79 informationLabel.numberOfLines = 0;
80 [stackView addArrangedSubview:informationLabel];
81 informationLabel.translatesAutoresizingMaskIntoConstraints = false;
84 UILabel * ssidLabel = [UILabel new];
85 ssidLabel.text = @"SSID";
86 _networkSSID = [UITextField new];
87 UIView * ssidView = [CHIPUIViewUtils viewWithLabel:ssidLabel textField:_networkSSID];
88 [stackView addArrangedSubview:ssidView];
89 ssidView.translatesAutoresizingMaskIntoConstraints = false;
90 [ssidView.trailingAnchor constraintEqualToAnchor:stackView.trailingAnchor].active = YES;
93 UILabel * passwordLabel = [UILabel new];
94 passwordLabel.text = @"Password";
95 _networkPassword = [UITextField new];
96 [_networkPassword setSecureTextEntry:YES];
97 UIView * passwordView = [CHIPUIViewUtils viewWithLabel:passwordLabel textField:_networkPassword];
98 [stackView addArrangedSubview:passwordView];
99 [passwordView.trailingAnchor constraintEqualToAnchor:stackView.trailingAnchor].active = YES;
102 _clearButton = [UIButton new];
103 [_clearButton setTitle:@"Clear" forState:UIControlStateNormal];
104 _saveButton = [UIButton new];
105 [_saveButton setTitle:@"Save" forState:UIControlStateNormal];
106 [_clearButton addTarget:self action:@selector(clearCredientials:) forControlEvents:UIControlEventTouchUpInside];
107 [_saveButton addTarget:self action:@selector(saveCredientials:) forControlEvents:UIControlEventTouchUpInside];
109 UIStackView * stackViewButtons = [CHIPUIViewUtils stackViewWithButtons:@[ _clearButton, _saveButton ]];
110 [stackView addArrangedSubview:stackViewButtons];
111 [stackViewButtons.trailingAnchor constraintEqualToAnchor:stackView.trailingAnchor].active = YES;
114 - (void)fillNetworkConfigWithDefaults
116 NSString * networkSSID = CHIPGetDomainValueForKey(kCHIPToolDefaultsDomain, kNetworkSSIDDefaultsKey);
117 if ([networkSSID length] > 0) {
118 self.networkSSID.text = networkSSID;
121 NSString * networkPassword = CHIPGetDomainValueForKey(kCHIPToolDefaultsDomain, kNetworkPasswordDefaultsKey);
122 if ([networkPassword length] > 0) {
123 self.networkPassword.text = networkPassword;
127 // MARK: Button methods
129 - (IBAction)saveCredientials:(id)sender
131 if ([self.networkSSID.text length] > 0) {
132 CHIPSetDomainValueForKey(kCHIPToolDefaultsDomain, kNetworkSSIDDefaultsKey, self.networkSSID.text);
135 if ([self.networkPassword.text length] > 0) {
136 CHIPSetDomainValueForKey(kCHIPToolDefaultsDomain, kNetworkPasswordDefaultsKey, self.networkPassword.text);
139 [self.navigationController popViewControllerAnimated:YES];
142 - (IBAction)clearCredientials:(id)sender
144 if ([self.networkSSID.text length] > 0) {
145 CHIPSetDomainValueForKey(kCHIPToolDefaultsDomain, kNetworkSSIDDefaultsKey, nil);
148 if ([self.networkPassword.text length] > 0) {
149 CHIPSetDomainValueForKey(kCHIPToolDefaultsDomain, kNetworkPasswordDefaultsKey, nil);
152 self.networkSSID.text = @"";
153 self.networkPassword.text = @"";