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