iOS知识点总结
1.监测网络状态:
- (void)checkNetwork { __block NSString *tips; _reachiabilityManager = [AFNetworkReachabilityManager manager]; [_reachiabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) { switch (status) { case -1:{ tips = @"未知网络错误"; break; } case 0:{ tips = @"暂无网络,请检查网络"; break; } case 1:{ tips = @"当前连接的数据流量"; break; } case 2:{ tips = @"当前连接的是wifi"; break; } default: break; } if (tips.length) { // 提示 } }]; [_reachiabilityManager startMonitoring]; }
2.进入直播页面判断权限:
// 判断是否是模拟器 if ([[UIDevice currentDevice] isSimulator]) { [self showNoticeMessage:@"请使用真机运行"]; return NO; } // 判断是否有摄像头 if(![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){ [self showNoticeMessage:@"您的设备没有摄像头或者相关的驱动, 不能进行直播"]; return NO; } // 判断是否有摄像头权限 AVAuthorizationStatus authorizationStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]; if (authorizationStatus == AVAuthorizationStatusRestricted|| authorizationStatus == AVAuthorizationStatusDenied) { [self showNoticeMessage:@"app需要访问您的摄像头。\n请启用摄像头-设置/隐私/摄像头"]; return NO; } // 开启麦克风权限 AVAudioSession *audioSession = [AVAudioSession sharedInstance]; if ([audioSession respondsToSelector:@selector(requestRecordPermission:)]) { [audioSession performSelector:@selector(requestRecordPermission:) withObject:^(BOOL granted) { if (granted) { return YES; } else { [self showNoticeMessage:@"app需要访问您的麦克风。\n请启用麦克风-设置/隐私/麦克风"]; return NO; } }]; }
3.直播使用的第三方:
视频播放:IJKMediaFramework
弹幕:BarrageRenderer
推流:LFLiveKit
源码学习: https://github.com/SunLiner/MiaowShow
4.Cell中的倒计时:
/// 每秒执行一次 - (void)countDownWithPER_SECBlock:(void (^)())PER_SECBlock { if (_timer==nil) { dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue); dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行 dispatch_source_set_event_handler(_timer, ^{ dispatch_async(dispatch_get_main_queue(), ^{ PER_SECBlock(); }); }); dispatch_resume(_timer); } }
5.搜索时设置“搜索高亮”:
// 设置搜索高亮 if (model.searchKeyWord.length > 0) { // 从全部文字中找到特殊的文字 NSRange range = [model.titleStr rangeOfString:model.searchKeyWord]; if (range.location != NSNotFound && range.length > 0) { NSMutableAttributedString *muAttStr = [[NSMutableAttributedString alloc] initWithString:model.titleStr]; [muAttStr addAttribute:NSForegroundColorAttributeName value:BLUE_COLOR range:range]; self.titleLabel.attributedText = muAttStr; } }