RAYTPlayer.m 7.8 KB

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