DeviceCheckView.m 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. //
  2. // DeviceCheckView.m
  3. // KulexiuForTeacher
  4. //
  5. // Created by Kyle on 2022/3/21.
  6. //
  7. #import "DeviceCheckView.h"
  8. #import <UIView+Animation.h>
  9. #import "AudioRecordManager.h"
  10. #import "AudioPlayManager.h"
  11. #import "Reachability.h"
  12. #import <UserNotifications/UserNotifications.h>
  13. @interface DeviceCheckView ()<AudioRecordManagerDelegate>
  14. @property (weak, nonatomic) IBOutlet UIImageView *TopImageView;
  15. @property (weak, nonatomic) IBOutlet UIButton *oneButton;
  16. @property (weak, nonatomic) IBOutlet UIView *oneButtonView;
  17. @property (weak, nonatomic) IBOutlet UIView *twoButtonView;
  18. @property (weak, nonatomic) IBOutlet UIButton *twoPreButton;
  19. @property (weak, nonatomic) IBOutlet UIButton *twoNextButton;
  20. @property (weak, nonatomic) IBOutlet UILabel *titleLabel;
  21. @property (weak, nonatomic) IBOutlet UILabel *descLabel;
  22. @property (weak, nonatomic) IBOutlet UILabel *followLabel;
  23. @property (nonatomic, assign) NSInteger currentIndex;
  24. @property (nonatomic, assign) BOOL isRecording;
  25. @property (nonatomic, strong) AudioRecordManager *recordManager; //音频录音管理
  26. @property (nonatomic, strong) AudioPlayManager *playManager; // 音频播放
  27. @end
  28. @implementation DeviceCheckView
  29. - (void)awakeFromNib {
  30. [super awakeFromNib];
  31. [self resetView];
  32. }
  33. - (void)resetView {
  34. self.currentIndex = 0;
  35. self.isRecording = NO;
  36. }
  37. - (void)setCurrentIndex:(NSInteger)currentIndex {
  38. _currentIndex = currentIndex;
  39. switch (currentIndex) {
  40. case 0:
  41. {
  42. [self.TopImageView setImage:[UIImage imageNamed:@"check_device"]];
  43. self.twoButtonView.hidden = YES;
  44. [self.titleLabel setText:@"设备检测"];
  45. [self.descLabel setText:@"为确保您能正常上课,现在进行声音测试"];
  46. [self.followLabel setText:@""];
  47. [self.oneButton setTitle:@"下一步" forState:UIControlStateNormal];
  48. }
  49. break;
  50. case 1: // 话筒
  51. {
  52. [self.TopImageView setImage:[UIImage imageNamed:@"check_microphone"]];
  53. [self.titleLabel setText:@"手机话筒检测"];
  54. [self.descLabel setText:@"请按录音并念出下方文字"];
  55. [self.followLabel setText:@"“我正在测试话筒与扬声器”"];
  56. [self.oneButton setTitle:@"下一步" forState:UIControlStateNormal];
  57. }
  58. break;
  59. case 2: // 扬声器
  60. {
  61. [self.TopImageView setImage:[UIImage imageNamed:@"check_loudspeaker"]];
  62. [self.titleLabel setText:@"扬声器检测"];
  63. [self.descLabel setText:@"录音内容播放中请听是否有声音"];
  64. [self.followLabel setText:@""];
  65. self.twoButtonView.hidden = NO;
  66. [self.twoPreButton setTitle:@"无声音" forState:UIControlStateNormal];
  67. [self.twoNextButton setTitle:@"有声音" forState:UIControlStateNormal];
  68. }
  69. break;
  70. case 3: // 设备检测成功
  71. {
  72. [self.TopImageView setImage:[UIImage imageNamed:@"check_audioSuccess"]];
  73. [self.titleLabel setText:@"设备检测成功"];
  74. [self.descLabel setText:@"恭喜您!话筒与扬声器测试已通过"];
  75. [self.followLabel setText:@""];
  76. self.twoButtonView.hidden = YES;
  77. [self.oneButton setTitle:@"下一步" forState:UIControlStateNormal];
  78. }
  79. break;
  80. case 4: // 网络
  81. {
  82. [self.TopImageView setImage:[UIImage imageNamed:@"check_networking"]];
  83. [self.titleLabel setText:@"网络检测中"];
  84. [self.descLabel setText:@"检测中..."];
  85. [self.followLabel setText:@""];
  86. self.twoButtonView.hidden = YES;
  87. [self.oneButton setTitle:@"下一步" forState:UIControlStateNormal];
  88. }
  89. break;
  90. case 5: // 通知权限
  91. {
  92. [self.TopImageView setImage:[UIImage imageNamed:@"check_notification"]];
  93. [self.titleLabel setText:@"通知权限"];
  94. [self.descLabel setText:@"通知权限已开启,再也不会错过上课提醒了"];
  95. [self.followLabel setText:@""];
  96. self.twoButtonView.hidden = YES;
  97. [self.oneButton setTitle:@"完成测试" forState:UIControlStateNormal];
  98. }
  99. break;
  100. default:
  101. break;
  102. }
  103. }
  104. + (instancetype)shareInstance {
  105. DeviceCheckView *view = [[[NSBundle mainBundle] loadNibNamed:@"DeviceCheckView" owner:nil options:nil] firstObject];
  106. view.frame = CGRectMake(0, 0, KPortraitWidth, KPortraitHeight);
  107. return view;
  108. }
  109. - (void)showAlert {
  110. [self resetView];
  111. [[UIApplication sharedApplication].keyWindow addSubview:self];
  112. [self setPopAnimation];
  113. }
  114. - (IBAction)cancleAction:(id)sender {
  115. // 销毁播放 和录音
  116. [self.recordManager ks_deleteRecording];
  117. [self.playManager stopMusic];
  118. [self removeFromSuperview];
  119. }
  120. - (void)hiddenView {
  121. [self removeFromSuperview];
  122. }
  123. #pragma mark ---- one button action
  124. - (IBAction)oneButtonAction:(UIButton *)sender {
  125. if (self.currentIndex == 5) {
  126. [self hiddenView];
  127. }
  128. else if (self.currentIndex == 0) {
  129. self.currentIndex++;
  130. }
  131. else if (self.currentIndex == 1) {
  132. if (_isRecording == NO) { // 如果没有开始录音
  133. _isRecording = YES;
  134. [sender setTitle:@"停止录音" forState:UIControlStateNormal];
  135. [self.recordManager ks_record];
  136. }
  137. else {
  138. _isRecording = NO;
  139. [self.recordManager ks_stop];
  140. NSURL *fileUrl = [self.recordManager getSavePath];
  141. self.playManager = [AudioPlayManager shareInstanceWithFileUrl:fileUrl];
  142. [self.playManager playMusic];
  143. self.currentIndex++;
  144. }
  145. }
  146. else if (self.currentIndex == 3) {
  147. [self.recordManager ks_deleteRecording];
  148. [self.playManager stopMusic];
  149. [self checkNetWorkStatus];
  150. }
  151. else if (self.currentIndex == 4) {
  152. [self checkNotificationStatus];
  153. }
  154. }
  155. - (void)checkNotificationStatus {
  156. if ([self isAllowedNotification]) { // 通知开启了
  157. self.currentIndex++;
  158. }
  159. else {
  160. self.currentIndex++;
  161. [self.descLabel setText:@"通知权限未开启,可能错过上课提醒哦! 快去开启通知权限吧~"];
  162. self.twoButtonView.hidden = NO;
  163. [self.twoPreButton setTitle:@"完成测试" forState:UIControlStateNormal];
  164. [self.twoNextButton setTitle:@"去设置" forState:UIControlStateNormal];
  165. }
  166. }
  167. - (BOOL)isAllowedNotification {
  168. UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings];
  169. if (UIUserNotificationTypeNone == setting.types) {
  170. return NO;
  171. }else{
  172. return YES;
  173. }
  174. }
  175. #pragma mark --- two view aciton
  176. - (IBAction)preAction:(UIButton *)sender {
  177. if (self.currentIndex == 2) { // 点击无声音按钮
  178. [self.titleLabel setText:@"扬声器故障"];
  179. self.twoPreButton.userInteractionEnabled = NO;
  180. self.twoNextButton.userInteractionEnabled = NO;
  181. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  182. [self checkNetWorkStatus];
  183. self.twoPreButton.userInteractionEnabled = YES;
  184. self.twoNextButton.userInteractionEnabled = YES;
  185. });
  186. }
  187. else if (self.currentIndex == 5) {
  188. [self hiddenView];
  189. }
  190. }
  191. - (IBAction)nextAction:(UIButton *)sender {
  192. if (self.currentIndex == 2) {
  193. self.currentIndex++;
  194. }
  195. else if (self.currentIndex == 5) {
  196. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:nil];
  197. }
  198. }
  199. /**
  200. * 获得录音机管理
  201. *
  202. * @return 录音机对象
  203. */
  204. - (AudioRecordManager *)recordManager {
  205. if (!_recordManager) {
  206. _recordManager = [AudioRecordManager shareManager];
  207. _recordManager.delegate = self;
  208. }
  209. return _recordManager;
  210. }
  211. - (void)checkNetWorkStatus {
  212. self.currentIndex = 4;
  213. if ([self checkNetworkAvaiable]) {
  214. [self.titleLabel setText:@"网络监测成功"];
  215. [self.descLabel setText:@"您当前的网络状态良好"];
  216. }
  217. else {
  218. [self.titleLabel setText:@"网络检测失败"];
  219. [self.descLabel setText:@"您当前网络不顺畅"];
  220. }
  221. }
  222. - (BOOL)checkNetworkAvaiable {
  223. BOOL isExistenceNetwork = YES;
  224. Reachability *reach = [Reachability reachabilityWithHostName:@"www.apple.com"];
  225. switch ([reach currentReachabilityStatus]) {
  226. case NotReachable:
  227. isExistenceNetwork = NO;
  228. //NSLog(@"notReachable");
  229. break;
  230. case ReachableViaWiFi:
  231. isExistenceNetwork = YES;
  232. //NSLog(@"WIFI");
  233. break;
  234. case ReachableViaWWAN:
  235. isExistenceNetwork = YES;
  236. //NSLog(@"3G");
  237. break;
  238. }
  239. return isExistenceNetwork;
  240. }
  241. /*
  242. // Only override drawRect: if you perform custom drawing.
  243. // An empty implementation adversely affects performance during animation.
  244. - (void)drawRect:(CGRect)rect {
  245. // Drawing code
  246. }
  247. */
  248. @end