RAYTPlayer.m 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. //
  2. // RAYTPlayer.m
  3. // iSales-UWAVER
  4. //
  5. // Created by Rui Zhang on 11/26/19.
  6. // Copyright © 2019 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "RAYTPlayer.h"
  9. #import "const.h"
  10. #import "RAUtils.h"
  11. @interface RAYTPlayer ()
  12. @end
  13. @implementation RAYTPlayer
  14. - (instancetype)initWithFrame:(CGRect)frame
  15. {
  16. self = [super initWithFrame:frame];
  17. if (self) {
  18. [self initWKWebView];
  19. [self initMessageHandler];
  20. }
  21. return self;
  22. }
  23. - (instancetype)initWithCoder:(NSCoder *)coder
  24. {
  25. self = [super initWithCoder:coder];
  26. if (self) {
  27. [self initWKWebView];
  28. [self initMessageHandler];
  29. }
  30. return self;
  31. }
  32. - (void)layoutSubviews {
  33. [super layoutSubviews];
  34. self.wkwebView.frame = self.bounds;
  35. }
  36. -(void) initWKWebView
  37. {
  38. WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
  39. WKPreferences *preferences = [WKPreferences new];
  40. preferences.javaScriptCanOpenWindowsAutomatically = false;
  41. // preferences.minimumFontSize = 40.0;
  42. configuration.mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeAll;
  43. configuration.allowsInlineMediaPlayback = true;
  44. configuration.allowsPictureInPictureMediaPlayback=false;
  45. configuration.preferences = preferences;
  46. _wkwebView = [[WKWebView alloc] initWithFrame:self.bounds configuration:configuration];
  47. self.wkwebView.hidden=YES;
  48. // self.wkwebView.UIDelegate = self;
  49. self.wkwebView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
  50. self.wkwebView.navigationDelegate = self;
  51. self.wkwebView.scrollView.bounces = false;
  52. [self addSubview:self.wkwebView];
  53. // __weak typeof(self) weakSelf = self;
  54. // [self.wkwebView.scrollView setEmptyContentViewBackgroupColor:UIColor.whiteColor];
  55. // self.wkwebView.scrollView.emptyView = [RAEmptyView emptyViewWithTitle:@"can not reach the server.\ntap to reload" clickHandler:^(id sender) {
  56. // [weakSelf LoadFromURL:weakSelf.url];
  57. // }];
  58. // self.firstLoad = YES;
  59. }
  60. -(void)LoadWithVid:(NSString *)vid
  61. {
  62. if(self.wkwebView==nil)
  63. return;
  64. NSString * replacement = [NSString stringWithFormat:@"src=\"https://www.youtube.com/embed/%@\"",vid];
  65. NSError *error;
  66. NSString *htmlStr = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"ytplayer" ofType:@"html"]encoding:NSUTF8StringEncoding error: & error];
  67. htmlStr=[htmlStr stringByReplacingOccurrencesOfString:@"src=replacement" withString:replacement];
  68. // NSString *htmlStr1=@"abc def ggg";
  69. // [htmlStr stringByReplacingOccurrencesOfString:@"src=replacement" withString:replacement];
  70. // NSString* new_url = [NSString stringWithFormat:@"https://www.youtube.com/embed/%@?playsinline=1",vid];
  71. // [self.wkwebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:new_url]]];
  72. [self.wkwebView loadHTMLString:htmlStr baseURL:nil];
  73. self.wkwebView.hidden=NO;
  74. }
  75. -(void)LoadFromURL:(NSString *)vid
  76. {
  77. if(self.wkwebView==nil)
  78. return;
  79. NSString* url = [NSString stringWithFormat:@"https://www.youtube.com/embed/%@?playsinline=1",vid];
  80. NSURL* U=[NSURL URLWithString:url];
  81. NSURLRequest* request=[[NSURLRequest alloc]initWithURL:U cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:30.0f];
  82. [self.wkwebView loadRequest:request];
  83. }
  84. -(void) initMessageHandler
  85. {
  86. }
  87. -(void)destroyMessageHandler
  88. {
  89. }
  90. - (void)dealloc
  91. {
  92. [self destroyMessageHandler];
  93. }
  94. #pragma mark - Setter
  95. - (void)setContentInsetAdjustmentBehavior:(UIScrollViewContentInsetAdjustmentBehavior)contentInsetAdjustmentBehavior {
  96. self.wkwebView.scrollView.contentInsetAdjustmentBehavior = contentInsetAdjustmentBehavior;
  97. }
  98. #pragma mark - player
  99. //- (void)stringFromEvaluatingJavaScript:(NSString *)jsToExecute {
  100. // // return [self.wkwebView stringByEvaluatingJavaScriptFromString:jsToExecute];
  101. //
  102. //
  103. //[self.wkwebView evaluateJavaScript:jsToExecute completionHandler:^(id _Nullable result, NSError * _Nullable error) {
  104. // DebugLog(@"%@----%@",result, error);
  105. // }];
  106. //
  107. //}
  108. - (void)stopVideo {
  109. [self.wkwebView reload];
  110. }
  111. #pragma mark - Utils
  112. - (NSString *)adjustNilString:(NSString *)str {
  113. if (str) {
  114. return str;
  115. }
  116. return @"";
  117. }
  118. //-(UIViewController*) getViewController
  119. //{
  120. // for (UIView* next = [self superview]; next; next = next.superview) {
  121. // UIResponder* nextResponder = [next nextResponder];
  122. // if ([nextResponder isKindOfClass:[UIViewController class]]) {
  123. // return (UIViewController*)nextResponder;
  124. // }
  125. // }
  126. // return nil;
  127. //}
  128. #pragma mark - WKNavigationDelegate
  129. - (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation {
  130. DebugLog(@"%s",__func__);
  131. }
  132. - (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler {
  133. DebugLog(@"%s",__func__);
  134. //1)获取trust object
  135. SecTrustRef trust = challenge.protectionSpace.serverTrust;
  136. // SecTrustResultType result;
  137. //2)SecTrustEvaluate对trust进行验证
  138. // OSStatus status = SecTrustEvaluate(trust, &result);
  139. // CFArrayRef defaultPolicies = NULL;
  140. // SecTrustCopyPolicies(trust, &defaultPolicies);
  141. // DebugLog(@"default policies: %@",(__bridge id)defaultPolicies);
  142. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  143. CFErrorRef cfError = nil;
  144. bool btrust = SecTrustEvaluateWithError(trust, &cfError);
  145. NSMutableArray *policies = [NSMutableArray array];
  146. [policies addObject:(__bridge_transfer id)SecPolicyCreateBasicX509()];
  147. SecTrustSetPolicies(trust, (__bridge CFArrayRef)policies);
  148. // if (status == errSecSuccess && (result == kSecTrustResultProceed || result == kSecTrustResultUnspecified))
  149. if(btrust&&cfError == nil)
  150. {
  151. //3)验证成功,生成NSURLCredential凭证cred,告知challenge的sender使用这个凭证来继续连接
  152. NSURLCredential *cred = [NSURLCredential credentialForTrust:trust];
  153. [challenge.sender useCredential:cred forAuthenticationChallenge:challenge];
  154. if (completionHandler) {
  155. completionHandler(NSURLSessionAuthChallengeUseCredential,cred); // 使用证书
  156. }
  157. } else {
  158. [challenge.sender cancelAuthenticationChallenge:challenge];
  159. //4)验证失败,取消这次验证流程
  160. if (completionHandler) {
  161. completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge,nil); // 忽略证书
  162. }
  163. }
  164. });
  165. }
  166. - (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error
  167. {
  168. DebugLog(@"%@",error);
  169. }
  170. //禁止链接跳转
  171. - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
  172. DebugLog(@"%s",__func__);
  173. DebugLog(@"navigationAction = %@, \r!!!!!!!!!!!!!!!!!!!!!!\n request = %@",navigationAction,navigationAction.request.URL);
  174. // NSURLRequest * request = navigationAction.request;
  175. return decisionHandler(WKNavigationActionPolicyAllow);
  176. // if ([request.URL.host isEqual: self.originURL.host]) {
  177. // return decisionHandler(WKNavigationActionPolicyAllow);
  178. // } else if ([request.URL.scheme isEqual:@"ytplayer"]) {
  179. // [self notifyDelegateOfYouTubeCallbackUrl:request.URL];
  180. // return decisionHandler(WKNavigationActionPolicyCancel);
  181. // } else if ([request.URL.scheme isEqual: @"http"] || [request.URL.scheme isEqual:@"https"]) {
  182. // if( [self handleHttpNavigationToUrl:request.URL])
  183. // return decisionHandler(WKNavigationActionPolicyAllow);
  184. // else
  185. // return decisionHandler(WKNavigationActionPolicyCancel);
  186. // }
  187. // return decisionHandler(WKNavigationActionPolicyAllow);
  188. }
  189. @end