// // RAYTPlayer.m // iSales-UWAVER // // Created by Rui Zhang on 11/26/19. // Copyright © 2019 United Software Applications, Inc. All rights reserved. // #import "RAYTPlayer.h" @interface RAYTPlayer () //@property (strong, nonatomic) WKWebView *wkwebView; //@property (nonatomic,strong) NSDictionary *currentParams; //@property (nonatomic,weak) RAProgressHUD *hud; //@property (nonatomic,copy) NSString *url; //@property (nonatomic,assign) BOOL firstLoad; @end @implementation RAYTPlayer - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self initWKWebView]; [self initMessageHandler]; } return self; } - (instancetype)initWithCoder:(NSCoder *)coder { self = [super initWithCoder:coder]; if (self) { [self initWKWebView]; [self initMessageHandler]; } return self; } - (void)layoutSubviews { [super layoutSubviews]; self.wkwebView.frame = self.bounds; } -(void) initWKWebView { WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init]; WKPreferences *preferences = [WKPreferences new]; preferences.javaScriptCanOpenWindowsAutomatically = false; // preferences.minimumFontSize = 40.0; configuration.mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeAll; configuration.allowsInlineMediaPlayback = true; configuration.allowsPictureInPictureMediaPlayback=false; configuration.preferences = preferences; _wkwebView = [[WKWebView alloc] initWithFrame:self.bounds configuration:configuration]; self.wkwebView.hidden=YES; // self.wkwebView.UIDelegate = self; self.wkwebView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight); self.wkwebView.navigationDelegate = self; self.wkwebView.scrollView.bounces = false; [self addSubview:self.wkwebView]; // __weak typeof(self) weakSelf = self; // [self.wkwebView.scrollView setEmptyContentViewBackgroupColor:UIColor.whiteColor]; // self.wkwebView.scrollView.emptyView = [RAEmptyView emptyViewWithTitle:@"can not reach the server.\ntap to reload" clickHandler:^(id sender) { // [weakSelf LoadFromURL:weakSelf.url]; // }]; // self.firstLoad = YES; } -(void)LoadWithVid:(NSString *)vid { if(self.wkwebView==nil) return; NSString * replacement = [NSString stringWithFormat:@"src=\"https://www.youtube.com/embed/%@\"",vid]; NSError *error; NSString *htmlStr = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"ytplayer" ofType:@"html"]encoding:NSUTF8StringEncoding error: & error]; htmlStr=[htmlStr stringByReplacingOccurrencesOfString:@"src=replacement" withString:replacement]; // NSString *htmlStr1=@"abc def ggg"; // [htmlStr stringByReplacingOccurrencesOfString:@"src=replacement" withString:replacement]; // NSString* new_url = [NSString stringWithFormat:@"https://www.youtube.com/embed/%@?playsinline=1",vid]; // [self.wkwebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:new_url]]]; [self.wkwebView loadHTMLString:htmlStr baseURL:nil]; self.wkwebView.hidden=NO; } -(void)LoadFromURL:(NSString *)vid { if(self.wkwebView==nil) return; NSString* url = [NSString stringWithFormat:@"https://www.youtube.com/embed/%@?playsinline=1",vid]; NSURL* U=[NSURL URLWithString:url]; NSURLRequest* request=[[NSURLRequest alloc]initWithURL:U cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:30.0f]; [self.wkwebView loadRequest:request]; } -(void) initMessageHandler { // [self.wkwebView.configuration.userContentController addScriptMessageHandler:self name:@"CamScan"]; // [self.wkwebView.configuration.userContentController addScriptMessageHandler:self name:@"Location"]; // [self.wkwebView.configuration.userContentController addScriptMessageHandler:self name:@"Share"]; // [self.wkwebView.configuration.userContentController addScriptMessageHandler:self name:@"JumpTo"]; // [self.wkwebView.configuration.userContentController addScriptMessageHandler:self name:@"ViewImage"]; // [self.wkwebView.configuration.userContentController addScriptMessageHandler:self name:@"PostDialog"]; // [self.wkwebView.configuration.userContentController addScriptMessageHandler:self name:@"Photo"]; // [self.wkwebView.configuration.userContentController addScriptMessageHandler:self name:@"Email"]; // [self.wkwebView.configuration.userContentController addScriptMessageHandler:self name:@"Preview"]; // [self.wkwebView.configuration.userContentController addScriptMessageHandler:self name:@"Navigation"]; // [self.wkwebView.configuration.userContentController addScriptMessageHandler:self name:@"DatetimePicker"]; // //// [self.wkwebView.configuration.userContentController addScriptMessageHandler:self name:@"UpdateBadge"]; // // [self.wkwebView.configuration.userContentController addScriptMessageHandler:self name:@"Signature"]; // [self.wkwebView.configuration.userContentController addScriptMessageHandler:self name:@"SelectContact"]; // [self.wkwebView.configuration.userContentController addScriptMessageHandler:self name:@"requestApplicationInfo"]; // [self.wkwebView.configuration.userContentController addScriptMessageHandler:self name:@"cleanCache"]; // // [self.wkwebView.configuration.userContentController addScriptMessageHandler:self name:@"login"]; // [self.wkwebView.configuration.userContentController addScriptMessageHandler:self name:@"logout"]; // [self.wkwebView.configuration.userContentController addScriptMessageHandler:self name:@"UpdateBadge"]; // [self.wkwebView.configuration.userContentController addScriptMessageHandler:self name:@"QueryLaunchNotification"]; // // // } -(void)destroyMessageHandler { // [self.wkwebView.configuration.userContentController removeScriptMessageHandlerForName:@"CamScan"]; // [self.wkwebView.configuration.userContentController removeScriptMessageHandlerForName:@"Location"]; // [self.wkwebView.configuration.userContentController removeScriptMessageHandlerForName:@"Share"]; // [self.wkwebView.configuration.userContentController removeScriptMessageHandlerForName:@"JumpTo"]; // [self.wkwebView.configuration.userContentController removeScriptMessageHandlerForName:@"ViewImage"]; // [self.wkwebView.configuration.userContentController removeScriptMessageHandlerForName:@"PostDialog"]; // [self.wkwebView.configuration.userContentController removeScriptMessageHandlerForName:@"Photo"]; // [self.wkwebView.configuration.userContentController removeScriptMessageHandlerForName:@"Email"]; // [self.wkwebView.configuration.userContentController removeScriptMessageHandlerForName:@"Preview"]; // [self.wkwebView.configuration.userContentController removeScriptMessageHandlerForName:@"Navigation"]; // [self.wkwebView.configuration.userContentController removeScriptMessageHandlerForName:@"DatetimePicker"]; // [self.wkwebView.configuration.userContentController removeScriptMessageHandlerForName:@"Signature"]; // [self.wkwebView.configuration.userContentController removeScriptMessageHandlerForName:@"SelectContact"]; // [self.wkwebView.configuration.userContentController removeScriptMessageHandlerForName:@"requestApplicationInfo"]; // [self.wkwebView.configuration.userContentController removeScriptMessageHandlerForName:@"cleanCache"]; // // [self.wkwebView.configuration.userContentController removeScriptMessageHandlerForName:@"login"]; // [self.wkwebView.configuration.userContentController removeScriptMessageHandlerForName:@"logout"]; // [self.wkwebView.configuration.userContentController removeScriptMessageHandlerForName:@"UpdateBadge"]; // [self.wkwebView.configuration.userContentController removeScriptMessageHandlerForName:@"QueryLaunchNotification"]; } - (void)dealloc { [self destroyMessageHandler]; } #pragma mark - Setter - (void)setContentInsetAdjustmentBehavior:(UIScrollViewContentInsetAdjustmentBehavior)contentInsetAdjustmentBehavior { self.wkwebView.scrollView.contentInsetAdjustmentBehavior = contentInsetAdjustmentBehavior; } // //#pragma mark - Private // //- (UIViewController *)ra_viewController { // return [RAUtils getViewController:self]; //} // //- (void)ra_openPhotoLibrary:(NSDictionary *)params { // // UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; // imagePicker.delegate = self; // imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; // imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical; // imagePicker.allowsEditing = YES; // [self.ra_viewController presentViewController:imagePicker animated:YES completion:nil]; // self.currentParams = params; //} // //- (void)ra_openCamera:(NSDictionary *)params { // // RACameraViewController *cameraVC = [RACameraViewController viewControllerFromStoryboard]; // cameraVC.completion = ^(UIImage *image) { // // image = [UIImage img_compress:image kbsize:1024]; // NSData *data = UIImageJPEGRepresentation(image, 1.0); //// NSString *name = NSUUID.UUID.UUIDString; //// NSString *path = [NSTemporaryDirectory() stringByAppendingPathComponent:name]; //// [data writeToFile:path atomically:NO]; // // NSString *imageSource = [NSString stringWithFormat:@"data:image/jpg;base64,%@",[data base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed]]; // // [self returnToWebPage:params value:imageSource]; // }; // // cameraVC.fromVC = self.ra_viewController; // [self.ra_viewController.navigationController pushViewController:cameraVC animated:YES]; //} // //- (void)ra_openQRCodeScanner:(NSDictionary *)params { // // RAQRCodeScannerViewController *scanVC = [RAQRCodeScannerViewController viewControllerFromStoryboard]; // // scanVC.QRCodeViewControllerDidCompletion = ^(RAQRCodeScannerViewController *qrcodeViewController, NSString *value) { // // [self returnToWebPage:params value:value]; // [qrcodeViewController.navigationController popViewControllerAnimated:YES]; // }; // // scanVC.QRCodeViewControllerDidCanceled = ^(RAQRCodeScannerViewController *qrcodeViewController) { // // [qrcodeViewController.navigationController popViewControllerAnimated:YES]; // }; // // [self.ra_viewController.navigationController pushViewController:scanVC animated:YES]; //} // // //- (void)ra_previewDocument:(NSDictionary *)params { // /** // cc = "test1@abc.com,test2@abc.com"; // content = content; // receipt = "test@abc.com"; // subject = "email subject"; // url = "http://10.0.0.4:8080/backend_test/test.pdf"; // */ // // NSString *urlStr = [params objectForKey:@"url"]; //// urlStr = @"http://192.168.0.130/ios.pdf"; // if (urlStr) { // // RAProgressHUD *hud = [RAProgressHUD showHUDOnView:self.ra_viewController.view]; // // // 先下载下来,否则QuickLook分享要崩溃 // dispatch_async(dispatch_get_global_queue(0, 0), ^{ // // NSString *tmpDir = NSTemporaryDirectory(); // // [RADataProvider GET_DownloadFile:urlStr toFolder:tmpDir progressHandler:^(NSURLSessionTask *task, double progress) { // // NSLog(@"download %@ progress %f", urlStr.lastPathComponent, progress); // // } completionHandler:^(NSMutableDictionary *result) { // // dispatch_async(dispatch_get_main_queue(), ^{ // // [hud dismiss:^{ // // int resultCode = [[result objectForKey:@"result"] intValue]; // if (resultCode == RESULT_TRUE) { // // NSString *path = [result objectForKey:@"path"]; // // RAPreviewController *vc = [RAPreviewController ra_previewControllerWithURL:[NSURL fileURLWithPath:path]]; // [self.ra_viewController presentViewController:vc animated:YES completion:nil]; // // } else { // // NSString *msg = [result objectForKey:@"msg"]; // [RAUtils message_alert:msg title:NSLocalizedString(@"warning_title", nil) controller:self.ra_viewController]; // // } // // }]; // // }); // // }]; // }); // // } //} // //- (void)ra_mail:(NSDictionary *)params { // // /** // // cc = "test1@abc.com,test2@abc.com"; // content = content; // receipt = "test@abc.com"; // subject = "email subject"; // // */ // MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init]; // // if(mc==nil) // { // return; // } // // NSString *subject = [params objectForKey:@"subject"]; // NSString *receipt = [params objectForKey:@"receipt"]; // NSString *content = [params objectForKey:@"content"]; // NSString *cc = [params objectForKey:@"cc"]; // // mc.mailComposeDelegate = self; // [mc setSubject:subject]; // // // [mc setToRecipients:[receipt componentsSeparatedByString:@","]]; // [mc setCcRecipients:[cc componentsSeparatedByString:@","]]; // // if(!content) // content=@""; // // NSString* htmlbody = [NSString stringWithFormat:@"
%@
",content]; // // [mc setMessageBody:htmlbody isHTML:YES]; // // //// NSString* filename =self.filename; //// //// NSString *filePath = self.url; //// NSData *data = [NSData dataWithContentsOfFile:filePath]; //// //// // if(self.attachment_name.length>0) //// // filename = self.attachment_name; //// [mc addAttachmentData:data mimeType:@"application/pdf" fileName:filename]; // // [self.ra_viewController presentViewController:mc animated:YES completion:nil]; // self.currentParams = params; //} // //- (void)ra_share:(NSDictionary *)params { // // // // // 设置分享内容 // //// NSString *content = [params objectForKey:@"content"]; //// NSString *imageURL = [params objectForKey:@"image"]; //// NSString *title = [params objectForKey:@"title"]; // NSString *url = [params objectForKey:@"url"]; // // NSArray *activityItems = @[[NSURL URLWithString:url]]; // // // 服务类型控制器 // UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:@[]]; // // _activityViewController = activityViewController; // // activityViewController.excludedActivityTypes = @[UIActivityTypeMail]; // activityViewController.modalInPopover = true; // // // 选中分享类型 // [activityViewController setCompletionWithItemsHandler:^(NSString * __nullable activityType, BOOL completed, NSArray * __nullable returnedItems, NSError * __nullable activityError){ // // 显示选中的分享类型 // NSLog(@"act type %@",activityType); // //// if (completed) { //// NSLog(@"ok"); //// }else { //// NSLog(@"not ok"); //// } // NSMutableDictionary *shareResult = [NSMutableDictionary dictionary]; // if (!activityType) { // activityType = @"cancel"; // } // shareResult[@"to"] = [activityType componentsSeparatedByString:@"."].lastObject; // shareResult[@"completed"] = @(completed); // if (activityError) { // shareResult[@"error"] = activityError.localizedDescription; // } // // [self returnToWebPage:params value:[RAConvertor dict2string:shareResult]]; // // }]; // // [self.ra_viewController presentViewController:activityViewController animated:YES completion:nil]; //} // //- (void)ra_viewPhotos:(NSDictionary *)params { // // NSArray *urlArr = [params objectForKey:@"images"]; // NSUInteger index = [[params objectForKey:@"index"] integerValue]; // // NSMutableArray *mArr = [NSMutableArray array]; // for (NSString *url in urlArr) { // // RAPhotoModel *model = [RAPhotoModel new]; // model.url = url; // // [mArr addObject:model]; // } // // // RAPhotoPreviewController *previewController = [RAPhotoPreviewController ra_photoPreviewControllerWithPhotoItems:mArr offset:index]; // // [self.ra_viewController presentViewController:previewController animated:YES completion:nil]; //} // //- (void)ra_navigate:(NSDictionary *)params { // // NSString *name = [params objectForKey:@"name"]; // NSString *addr = [params objectForKey:@"address"]; // // [RAMapNavigateHandler ra_navigate:addr withTitle:name viewController:self.ra_viewController]; //} // //- (void)ra_signature:(NSDictionary *)params { // // __weak typeof(self) weakSelf = self; // SignatureViewController *signatureVC = [SignatureViewController ra_signatureViewControllerWithCompletion:^(UIImage *image) { // // NSData *data = UIImageJPEGRepresentation(image, 1.0); // // NSString *imageSource = [NSString stringWithFormat:@"data:image/jpg;base64,%@",[data base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed]]; // // [weakSelf returnToWebPage:params value:imageSource]; // // }]; // // [self.ra_viewController.navigationController pushViewController:signatureVC animated:NO]; //} // //- (void)ra_dateTimePicker:(NSDictionary *)params { // // NSString *type = [params objectForKey:@"type"]; // NSString *value = [params objectForKey:@"value"]; // NSString *title = [params objectForKey:@"title"]; // // NSString *format = @"MM/dd/yyyy"; // UIDatePickerMode mode = UIDatePickerModeDate; // //// type = @"dateTime"; //// value = @"09/22/1028 14:44:24"; // // if ([type isEqualToString:@"date"]) { // // } else if ([type isEqualToString:@"time"]) { // // format = @"HH:mm:ss"; // mode = UIDatePickerModeTime; // // } else if ([type isEqualToString:@"dateTime"]) { // // format = @"MM/dd/yyyy HH:mm:ss"; // mode = UIDatePickerModeDateAndTime; // } // // NSDateFormatter *dateFormatter = [NSDateFormatter new]; // dateFormatter.dateFormat = format; // NSDate *date = [dateFormatter dateFromString:value]; // // __weak typeof(self) weakSelf = self; // [RADatePickerViewController presentDatePicker:self.ra_viewController title:title dateMode:mode defaultDate:date withSetBlk:^(NSDate *date) { // // NSString *dateStr = [dateFormatter stringFromDate:date]; // [weakSelf returnToWebPage:params value:dateStr]; // }]; //} // //- (void)ra_selectContact:(NSDictionary *)params { // // __weak typeof(self) weakSelf = self; // [[RAContactHelper defaultHelper] showContactPickerByViewController:self.ra_viewController withComplete:^(BOOL canceled, NSArray