iOS 接入场景
临境直播观看 SDK 的接入可以通过两种场景;
一、原生场景:直接从 App 原生进入直播观看;
二、H5 嵌入场景:从嵌套在 webview 的 H5 中,进入直播观看;
原生场景
原生场景接入方式
NSMutableDictionary *params = [NSMutableDictionary dictionary];
[params setValue:@"" forKey:kLivePullSDKeyRoomCode];//直播室roomCode 必传
[params setValue:@"" forKey:kLivePullSDKeyUserId];//用户userId 必传
[params setValue:@"" forKey:kLivePullSDKeyUserName];//用户昵称 必传
[params setValue:@"" forKey:kLivePullSDKeyAvatar];//用户头像
[params setValue:@"" forKey:kLivePullSDKeyPassword];//观看密码
[params setValue:@"" forKey:kLivePullSDKeyLoginName];//产线用户登陆名称
[params setValue:@"" forKey:kLivePullSDKeyUserPoint];//用户积分
[params setValue:@(YES) forKey:kLivePullSDKeyCannotEditUserInfo];//是否允许编辑个人信息(业务线及外部使用传Yes )
[[YXTLivePullSDK getInstance] loginChatRoomWithParam:params inNav:当前的navigationController];
H5嵌入场景
H5嵌入场景接入方式
H5嵌入方式通过拦截协议:yxtthirdapp://biz.util.open?param=…” 响应进入直播室跳转并获取直播信息
// 添加自定义userAgent
[webView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id _Nullable result, NSError * _Nullable error) {
// 生成自定义userAgent
NSString *userAgent = result;
NSString *userAgentDefault = @"";
NSArray<NSString *> *components = [userAgent componentsSeparatedByString:@";yxtapp/"];
if (components.count < 2 ) {
userAgentDefault = userAgent;
}
NSString *customUserAgent = [userAgentDefault stringByAppendingFormat:@";%@", @"XuanLiveThirdPlatform/00000"];
// 设置自定义userAgent
if (@available(iOS 9.0, *)) {
webView.customUserAgent = customUserAgent;
} else {
[webView setValue:customUserAgent forKey:@"applicationNameForUserAgent"];
}
// 设置用于拦截协议的代理
webView.navigationDelegate = self;
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://m2-qida.yunxuetang.com.cn/l/#index"]];
// 加载H5页面
[webView loadRequest:request];
}];
// 拦截协议
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
NSString *string = [navigationAction.request.URL.absoluteString stringByRemovingPercentEncoding];
if ([string hasPrefix:@"yxtthirdapp://"]) {
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\{[^}]+\\}" options:NSRegularExpressionCaseInsensitive error:nil];
NSTextCheckingResult *result = [regex firstMatchInString:string options:0 range:NSMakeRange(0, string.length)];
NSRange range = result.range;
string = [string substringWithRange:range];
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
// scheme为yxtthirdapp://并且携带参数中name为yxt_app_live_open是进入直播室的协议
NSString *name = dic[@"name"];
if ([name isEqualToString:@"yxt_app_live_open"]) {
[self openLiveWithInfo:dic];
}
}
decisionHandler(WKNavigationActionPolicyAllow);
}
// 设置参数进入直播室
- (void)openLiveWithInfo:(NSDictionary *)info {
NSMutableDictionary *params = [NSMutableDictionary dictionary];
[params setValue:info[@"roomid"] forKey:kLivePullSDKeyRoomCode];//直播室roomCode 必传
[params setValue:@"" forKey:kLivePullSDKeyUserId];//用户userId 必传
[params setValue:@"" forKey:kLivePullSDKeyUserName];//用户昵称 必传
[params setValue:@"" forKey:kLivePullSDKeyAvatar];//用户头像
[params setValue:info[@"password"] forKey:kLivePullSDKeyPassword];//观看密码
[params setValue:@"" forKey:kLivePullSDKeyLoginName];//产线用户登陆名称
[params setValue:@(YES) forKey:kLivePullSDKeyCannotEditUserInfo];//是否允许编辑个人信息(业务线及外部使用传Yes )
// 获取积分
// NSString *host = @"https://api-qida.yunxuetang.cn/v1/qdl/"; // production
NSString *host = @"https://devinner.yunxuetang.com.cn/qidaliveapi/v1/"; // developer
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"%@live/getvalidpoints"]];
[[NSURLSession.sharedSession dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
[params setValue:dic[@"validPoints"] forKey:kLivePullSDKeyUserPoint];
// 进入直播室
[[YXTLivePullSDK getInstance] loginChatRoomWithParam:params inNav:当前的navigationController];
}] resume];
}
关于作者
江苏云学堂网络科技有限公司开发人员,如有问题请发送邮件至 yll@yxt.com。