iOS 接入场景

临境直播观看 SDK 的接入可以通过两种场景;
一、原生场景:直接从 App 原生进入直播观看;
二、H5 嵌入场景:从嵌套在 webview 的 H5 中,进入直播观看;

原生场景

原生场景接入方式

  1. NSMutableDictionary *params = [NSMutableDictionary dictionary];
  2. [params setValue:@"" forKey:kLivePullSDKeyRoomCode];//直播室roomCode 必传
  3. [params setValue:@"" forKey:kLivePullSDKeyUserId];//用户userId 必传
  4. [params setValue:@"" forKey:kLivePullSDKeyUserName];//用户昵称 必传
  5. [params setValue:@"" forKey:kLivePullSDKeyAvatar];//用户头像
  6. [params setValue:@"" forKey:kLivePullSDKeyPassword];//观看密码
  7. [params setValue:@"" forKey:kLivePullSDKeyLoginName];//产线用户登陆名称
  8. [params setValue:@"" forKey:kLivePullSDKeyUserPoint];//用户积分
  9. [params setValue:@(YES) forKey:kLivePullSDKeyCannotEditUserInfo];//是否允许编辑个人信息(业务线及外部使用传Yes
  10. [[YXTLivePullSDK getInstance] loginChatRoomWithParam:params inNav:当前的navigationController];

H5嵌入场景

H5嵌入场景接入方式

H5嵌入方式通过拦截协议:yxtthirdapp://biz.util.open?param=…” 响应进入直播室跳转并获取直播信息

  1. // 添加自定义userAgent
  2. [webView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id _Nullable result, NSError * _Nullable error) {
  3. // 生成自定义userAgent
  4. NSString *userAgent = result;
  5. NSString *userAgentDefault = @"";
  6. NSArray<NSString *> *components = [userAgent componentsSeparatedByString:@";yxtapp/"];
  7. if (components.count < 2 ) {
  8. userAgentDefault = userAgent;
  9. }
  10. NSString *customUserAgent = [userAgentDefault stringByAppendingFormat:@";%@", @"XuanLiveThirdPlatform/00000"];
  11. // 设置自定义userAgent
  12. if (@available(iOS 9.0, *)) {
  13. webView.customUserAgent = customUserAgent;
  14. } else {
  15. [webView setValue:customUserAgent forKey:@"applicationNameForUserAgent"];
  16. }
  17. // 设置用于拦截协议的代理
  18. webView.navigationDelegate = self;
  19. NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://m2-qida.yunxuetang.com.cn/l/#index"]];
  20. // 加载H5页面
  21. [webView loadRequest:request];
  22. }];
  23. // 拦截协议
  24. - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
  25. NSString *string = [navigationAction.request.URL.absoluteString stringByRemovingPercentEncoding];
  26. if ([string hasPrefix:@"yxtthirdapp://"]) {
  27. NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\{[^}]+\\}" options:NSRegularExpressionCaseInsensitive error:nil];
  28. NSTextCheckingResult *result = [regex firstMatchInString:string options:0 range:NSMakeRange(0, string.length)];
  29. NSRange range = result.range;
  30. string = [string substringWithRange:range];
  31. NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
  32. NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
  33. // schemeyxtthirdapp://并且携带参数中nameyxt_app_live_open是进入直播室的协议
  34. NSString *name = dic[@"name"];
  35. if ([name isEqualToString:@"yxt_app_live_open"]) {
  36. [self openLiveWithInfo:dic];
  37. }
  38. }
  39. decisionHandler(WKNavigationActionPolicyAllow);
  40. }
  41. // 设置参数进入直播室
  42. - (void)openLiveWithInfo:(NSDictionary *)info {
  43. NSMutableDictionary *params = [NSMutableDictionary dictionary];
  44. [params setValue:info[@"roomid"] forKey:kLivePullSDKeyRoomCode];//直播室roomCode 必传
  45. [params setValue:@"" forKey:kLivePullSDKeyUserId];//用户userId 必传
  46. [params setValue:@"" forKey:kLivePullSDKeyUserName];//用户昵称 必传
  47. [params setValue:@"" forKey:kLivePullSDKeyAvatar];//用户头像
  48. [params setValue:info[@"password"] forKey:kLivePullSDKeyPassword];//观看密码
  49. [params setValue:@"" forKey:kLivePullSDKeyLoginName];//产线用户登陆名称
  50. [params setValue:@(YES) forKey:kLivePullSDKeyCannotEditUserInfo];//是否允许编辑个人信息(业务线及外部使用传Yes
  51. // 获取积分
  52. // NSString *host = @"https://api-qida.yunxuetang.cn/v1/qdl/"; // production
  53. NSString *host = @"https://devinner.yunxuetang.com.cn/qidaliveapi/v1/"; // developer
  54. NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"%@live/getvalidpoints"]];
  55. [[NSURLSession.sharedSession dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
  56. NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
  57. [params setValue:dic[@"validPoints"] forKey:kLivePullSDKeyUserPoint];
  58. // 进入直播室
  59. [[YXTLivePullSDK getInstance] loginChatRoomWithParam:params inNav:当前的navigationController];
  60. }] resume];
  61. }

关于作者

江苏云学堂网络科技有限公司开发人员,如有问题请发送邮件至 yll@yxt.com