RDSInputWiFiVC.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //
  2. // RDSInputWiFiVC.m
  3. // Temperature
  4. //
  5. // Created by RD on 2022/11/21.
  6. //
  7. #import "RDSInputWiFiVC.h"
  8. #import "NSDictionary+WiFiInfo.h"
  9. #import "RDSBindDeviceVC.h"
  10. #import <CoreLocation/CoreLocation.h>
  11. @interface RDSInputWiFiVC ()<UITextFieldDelegate,CLLocationManagerDelegate>
  12. @property (weak, nonatomic) IBOutlet UITextField *wifiAccountTF;
  13. @property (weak, nonatomic) IBOutlet UITextField *wifiPwdTF;
  14. @property (weak, nonatomic) IBOutlet UIButton *nextBtn;
  15. @property (nonatomic, strong) CLLocationManager *locationManager;
  16. @end
  17. @implementation RDSInputWiFiVC
  18. - (void)viewDidLoad {
  19. [super viewDidLoad];
  20. // Do any additional setup after loading the view from its nib.
  21. [self.locationManager startUpdatingLocation];
  22. [self p_initUI];
  23. // APP由后台进入
  24. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(p_reloadWiFi)name:UIApplicationWillEnterForegroundNotification object:nil];
  25. }
  26. - (void)viewWillAppear:(BOOL)animated{
  27. [super viewWillAppear:animated];
  28. [self p_reloadWiFi];
  29. }
  30. - (void) p_reloadWiFi{
  31. if(![self.wifiAccountTF.text isEqualToString:[NSDictionary rds_fetchSSIDInfo][@"SSID"]]){
  32. self.wifiAccountTF.text = [NSDictionary rds_fetchSSIDInfo][@"SSID"];
  33. self.wifiPwdTF.text = [kUserDefaults objectForKey:self.wifiAccountTF.text];
  34. }
  35. }
  36. #pragma mark - UI
  37. - (void)p_initUI {
  38. self.title = @"选择设备工作Wi-Fi";
  39. self.wifiAccountTF.delegate = self;
  40. self.wifiPwdTF.delegate = self;
  41. }
  42. - (IBAction)onChangeWifiClick {
  43. // 跳转系统设置
  44. NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
  45. if ([[UIApplication sharedApplication] canOpenURL:url]) {
  46. [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
  47. }
  48. }
  49. - (IBAction)onNextBtnClick:(UIButton *)sender {
  50. if ([NSDictionary rds_fetchSSIDInfo][@"SSID"] == nil) {
  51. [RDSHudShower showCenterToast:@"请把手机连接到WiFi"];
  52. return;
  53. }
  54. if (self.wifiAccountTF.text.length < 1) {
  55. [RDSHudShower showCenterToast:@"请输入WiFi信息"];
  56. return;
  57. }
  58. if (self.wifiPwdTF.text.length < 1) {
  59. [RDSHudShower showCenterToast:@"请输入WiFi信息"];
  60. return;
  61. }
  62. [self.view endEditing:YES];
  63. [kUserDefaults setObject:self.wifiPwdTF.text forKey:self.wifiAccountTF.text];
  64. RDSBindDeviceVC *vc = [[RDSBindDeviceVC alloc] init];
  65. vc.roomName = _roomName;
  66. vc.deviceID = _deviceID;
  67. vc.wifiName = self.wifiAccountTF.text;
  68. vc.pwd = self.wifiPwdTF.text;
  69. [self pushViewController:vc animated:YES];
  70. }
  71. - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
  72. [self.view endEditing:YES];
  73. }
  74. -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
  75. [self p_reloadWiFi];
  76. }
  77. -(CLLocationManager*)locationManager{
  78. if (!_locationManager) {
  79. _locationManager = [[CLLocationManager alloc] init];
  80. _locationManager.delegate = self;
  81. _locationManager.distanceFilter = kCLLocationAccuracyThreeKilometers;
  82. [_locationManager requestWhenInUseAuthorization];
  83. }
  84. return _locationManager;
  85. }
  86. @end