抓娃娃版1127

MainViewController.m 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. //
  2. // MainViewController.m
  3. // OpenLive
  4. //
  5. // Created by GongYuhua on 2016/9/12.
  6. // Copyright © 2016年 Agora. All rights reserved.
  7. //
  8. #import "MainViewController.h"
  9. #import "SettingsViewController.h"
  10. #import "LiveRoomViewController.h"
  11. #import "MyInterfaceController.h"
  12. #import "OLglobal.h"
  13. #import <PgyUpdate/PgyUpdateManager.h>
  14. //设置倒计时相关
  15. #define PGY_APP_ID @"c9f036f976eb6ccd2e0350418c31bc98"
  16. #define Func NSLog(@"func :%s",__FUNCTION__);
  17. @interface MainViewController () <SettingsVCDelegate, LiveRoomVCDelegate, UITextFieldDelegate>
  18. //断开重连
  19. @property (weak, nonatomic) IBOutlet UISwitch *reconnectionSwitch;
  20. //@property (weak, nonatomic) IBOutlet UILabel *status;
  21. //隔天重连
  22. @property (weak, nonatomic) IBOutlet UILabel *versionCode;
  23. @property (weak, nonatomic) IBOutlet UISwitch *reconnectionNextDaySwitch;
  24. @property (weak, nonatomic) IBOutlet UILabel *build;
  25. @property (weak, nonatomic) IBOutlet UITextField *uidTextField;
  26. @property (weak, nonatomic) IBOutlet UITextField *roomNameTextField;
  27. @property (weak, nonatomic) IBOutlet UIView *popoverSourceView;
  28. @property (assign, nonatomic) AgoraRtcVideoProfile videoProfile;
  29. @property(nonatomic,strong) MyInterfaceController *inter;
  30. //倒计时进入相关
  31. @property (assign, nonatomic) int secondsCountDown; //倒计时总时长
  32. @property (strong, nonatomic) NSTimer *countDownTimer;
  33. @property (nonatomic,copy) NSString *secondsCountDownString;
  34. @property (strong, nonatomic) UIAlertController *alertCon;
  35. @property (assign, nonatomic) BOOL GoNext;
  36. @end
  37. @implementation MainViewController
  38. -(void)getVersionCode{
  39. NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
  40. NSString *version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
  41. self.versionCode.text=[NSString stringWithFormat:@"当前版本号:%@",version];
  42. self.versionCode.font=[UIFont systemFontOfSize:14.0f];
  43. self.versionCode.textColor=[UIColor redColor];
  44. }
  45. -(void)buildCode{
  46. NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
  47. NSString *build = [infoDictionary objectForKey:@"CFBundleVersion"];
  48. self.build.text=[NSString stringWithFormat:@"当前build号:%@",build];
  49. self.build.font=[UIFont systemFontOfSize:14.0f];
  50. self.build.textColor=[UIColor greenColor];
  51. }
  52. -(MyInterfaceController *)inter{
  53. if (!_inter) {
  54. _inter=[[MyInterfaceController alloc]init];
  55. }
  56. return _inter;
  57. }
  58. #pragma mark 加载视图
  59. //xib加载完后
  60. -(void)awakeFromNib{
  61. [super awakeFromNib];
  62. }
  63. - (void)viewDidLoad {
  64. [super viewDidLoad];
  65. [self setConfiguration];
  66. self.videoProfile = AgoraRtc_VideoProfile_480P;
  67. [self inter];
  68. [self userAdd];
  69. [self buildCode];
  70. [self getVersionCode];
  71. //启动基本SDK
  72. // [[PgyManager sharedPgyManager] startManagerWithAppId:PGY_APP_ID];
  73. // [[PgyUpdateManager sharedPgyManager] startManagerWithAppId:PGY_APP_ID];
  74. }
  75. -(void)userAdd{
  76. NSString *rootName;
  77. NSString *UID;
  78. rootName=[[NSUserDefaults standardUserDefaults] objectForKey:@"RootName"];
  79. UID=[[NSUserDefaults standardUserDefaults] objectForKey:@"UID"];
  80. if (nil!=rootName) {
  81. self.roomNameTextField.text=rootName;
  82. }
  83. if (nil!=UID) {
  84. self.uidTextField.text=UID;
  85. }
  86. }
  87. #pragma mark 基本设置
  88. -(void)setConfiguration{
  89. self.uidTextField.keyboardType=UIKeyboardTypeNumberPad;
  90. self.uidTextField.returnKeyType=UIReturnKeyNext;
  91. self.roomNameTextField.returnKeyType=UIReturnKeyNext;
  92. }
  93. #pragma mark 下一页按钮
  94. - (IBAction)nextPageGo:(id)sender {
  95. //消失弹窗
  96. if (![self.alertCon isKindOfClass:[NSNull class]]) {
  97. [self.alertCon dismissViewControllerAnimated:YES completion:^{
  98. NSLog(@"消失弹窗");
  99. }];
  100. }
  101. //首先判断text是否有数据
  102. if ([self.roomNameTextField.text isEqualToString:@""] || [self.uidTextField.text isEqualToString:@""]) {
  103. [self showAlertCon];
  104. return;
  105. }else{
  106. [[NSUserDefaults standardUserDefaults] setObject:self.roomNameTextField.text forKey:@"RootName"];
  107. [[NSUserDefaults standardUserDefaults] setObject:self.uidTextField.text forKey:@"UID"];
  108. [[NSUserDefaults standardUserDefaults]synchronize];
  109. //判断是否是自动进入,判断switch
  110. if (sender==nil) {
  111. //自动进入
  112. if(self.GoNext==YES){
  113. [self joinWithRole:AgoraRtc_ClientRole_Broadcaster];
  114. }
  115. }else{
  116. [self joinWithRole:AgoraRtc_ClientRole_Broadcaster];
  117. }
  118. }
  119. }
  120. - (void)showAlertCon{
  121. UIAlertController *sheet = [UIAlertController alertControllerWithTitle:@"提示" message:@"有内容没有输入" preferredStyle:UIAlertControllerStyleAlert];
  122. UIAlertAction *broadcaster = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  123. }];
  124. [sheet addAction:broadcaster];
  125. [self presentViewController:sheet animated:YES completion:nil];
  126. NSLog(@"不用了");
  127. }
  128. #pragma mark storyborad页面跳转时调用
  129. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  130. NSString *segueId = segue.identifier;
  131. if ([segueId isEqualToString:@"mainToSettings"]) {
  132. SettingsViewController *settingsVC = segue.destinationViewController;
  133. settingsVC.videoProfile = self.videoProfile;
  134. settingsVC.delegate = self;
  135. } else if ([segueId isEqualToString:@"mainToLive"]) {
  136. LiveRoomViewController *liveVC = segue.destinationViewController;
  137. liveVC.roomName = self.roomNameTextField.text;
  138. liveVC.reconnectionNextDay=self.reconnectionNextDaySwitch.on;
  139. //未解决 有符号转换无符号long
  140. liveVC.uid = [self.uidTextField.text longLongValue];
  141. liveVC.videoProfile = self.videoProfile;
  142. liveVC.clientRole = [sender integerValue];
  143. liveVC.delegate = self;
  144. }
  145. }
  146. #pragma mark 模态跳转页面
  147. - (void)joinWithRole:(AgoraRtcClientRole)role {
  148. [self performSegueWithIdentifier:@"mainToLive" sender:@(role)];
  149. }
  150. #pragma mark -取消第一响应
  151. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  152. {
  153. [self.roomNameTextField resignFirstResponder];
  154. [self.uidTextField resignFirstResponder];
  155. }
  156. #pragma mark SettingsVCDelegate
  157. - (void)settingsVC:(SettingsViewController *)settingsVC didSelectProfile:(AgoraRtcVideoProfile)profile {
  158. self.videoProfile = profile;
  159. //返回页面
  160. [self dismissViewControllerAnimated:YES completion:nil];
  161. }
  162. #pragma mark LiveRoomVCDelegate
  163. - (void)liveVCNeedClose:(LiveRoomViewController *)liveVC {
  164. [self.navigationController popViewControllerAnimated:YES];
  165. //弹框显示,默认一分钟后重新进入界面
  166. if(self.reconnectionSwitch.on==YES){
  167. NSLog(@"正在重新连接");
  168. //弹窗出现倒计时 1分钟
  169. [self reconnectionTimer];
  170. }
  171. }
  172. //倒计时重连
  173. -(void)reconnectionTimer{
  174. Func
  175. self.GoNext=YES;
  176. _secondsCountDown = 3;//60秒倒计时
  177. _secondsCountDownString=[NSString stringWithFormat:@"\n%@",@(self.secondsCountDown)];
  178. //弹窗
  179. _alertCon=[UIAlertController alertControllerWithTitle:@"倒计时" message:self.secondsCountDownString preferredStyle:UIAlertControllerStyleAlert];
  180. NSLog(@"curr %@",NSThread.currentThread);
  181. UIAlertAction *alertAct1=[UIAlertAction actionWithTitle:@"停止进入" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  182. self.GoNext=NO;
  183. NSLog(@"点击了停止进入");
  184. [self.countDownTimer invalidate];
  185. }];
  186. [self.alertCon addAction:alertAct1];
  187. [self presentViewController:self.alertCon animated:YES completion:^{
  188. NSLog(@"弹窗");
  189. }];
  190. //开启倒计时模式
  191. _countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES];
  192. }
  193. //倒计时循环调用函数
  194. -(void)timeFireMethod{
  195. //倒计时-1
  196. NSLog(@"倒计时进入");
  197. self.secondsCountDown--;
  198. //修改倒计时标签现实内容
  199. NSLog(@"%i ",self.secondsCountDown);
  200. self.secondsCountDownString=[NSString stringWithFormat:@"\n%i",self.secondsCountDown];
  201. self.alertCon.message=self.secondsCountDownString;
  202. NSLog(@"3:%@",self.secondsCountDownString);
  203. //当倒计时到0时,做需要的操作,比如验证码过期不能提交
  204. if(self.secondsCountDown==0){
  205. [self.countDownTimer invalidate];
  206. self.countDownTimer=nil;
  207. self.secondsCountDownString=[NSString stringWithFormat:@"\n正在进入..."];
  208. self.alertCon.message=self.secondsCountDownString;
  209. //调用进入函数
  210. if (self.reconnectionSwitch.on==YES) {
  211. [self performSelector:@selector(nextPageGo:) withObject:nil afterDelay:1.0f];
  212. }
  213. }
  214. }
  215. #pragma mark -textFielddelegate
  216. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  217. [textField resignFirstResponder];
  218. return YES;
  219. }
  220. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
  221. if (textField==self.uidTextField) {//uid只允许输入数字,只能是整数
  222. if (range.length==1) {
  223. return YES;
  224. }
  225. if ([@"0123456789" rangeOfString:string].location == NSNotFound) {
  226. return NO;
  227. }
  228. if([textField.text length]>10){
  229. return NO;
  230. }
  231. }
  232. return YES;
  233. }
  234. - (IBAction)updateZWW:(id)sender {
  235. [[PgyUpdateManager sharedPgyManager] startManagerWithAppId:@"c9f036f976eb6ccd2e0350418c31bc98"]; // 请将 PGY_APP_ID 换成应用的 App Key
  236. [[PgyUpdateManager sharedPgyManager] checkUpdate];
  237. }
  238. - (void)didReceiveMemoryWarning {
  239. [super didReceiveMemoryWarning];
  240. // Dispose of any resources that can be recreated.
  241. NSLog(@"main内存不够了");
  242. }
  243. @end