RAYTPlayer.m 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393
  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. //@property (strong, nonatomic) WKWebView *wkwebView;
  11. //@property (nonatomic,strong) NSDictionary *currentParams;
  12. //@property (nonatomic,weak) RAProgressHUD *hud;
  13. //@property (nonatomic,copy) NSString *url;
  14. //@property (nonatomic,assign) BOOL firstLoad;
  15. @end
  16. @implementation RAYTPlayer
  17. - (instancetype)initWithFrame:(CGRect)frame
  18. {
  19. self = [super initWithFrame:frame];
  20. if (self) {
  21. [self initWKWebView];
  22. [self initMessageHandler];
  23. }
  24. return self;
  25. }
  26. - (instancetype)initWithCoder:(NSCoder *)coder
  27. {
  28. self = [super initWithCoder:coder];
  29. if (self) {
  30. [self initWKWebView];
  31. [self initMessageHandler];
  32. }
  33. return self;
  34. }
  35. - (void)layoutSubviews {
  36. [super layoutSubviews];
  37. self.wkwebView.frame = self.bounds;
  38. }
  39. -(void) initWKWebView
  40. {
  41. WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
  42. WKPreferences *preferences = [WKPreferences new];
  43. preferences.javaScriptCanOpenWindowsAutomatically = false;
  44. // preferences.minimumFontSize = 40.0;
  45. configuration.mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeAll;
  46. configuration.allowsInlineMediaPlayback = true;
  47. configuration.allowsPictureInPictureMediaPlayback=false;
  48. configuration.preferences = preferences;
  49. _wkwebView = [[WKWebView alloc] initWithFrame:self.bounds configuration:configuration];
  50. self.wkwebView.hidden=YES;
  51. // self.wkwebView.UIDelegate = self;
  52. self.wkwebView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
  53. self.wkwebView.navigationDelegate = self;
  54. self.wkwebView.scrollView.bounces = false;
  55. [self addSubview:self.wkwebView];
  56. // __weak typeof(self) weakSelf = self;
  57. // [self.wkwebView.scrollView setEmptyContentViewBackgroupColor:UIColor.whiteColor];
  58. // self.wkwebView.scrollView.emptyView = [RAEmptyView emptyViewWithTitle:@"can not reach the server.\ntap to reload" clickHandler:^(id sender) {
  59. // [weakSelf LoadFromURL:weakSelf.url];
  60. // }];
  61. // self.firstLoad = YES;
  62. }
  63. -(void)LoadWithVid:(NSString *)vid
  64. {
  65. if(self.wkwebView==nil)
  66. return;
  67. NSString * replacement = [NSString stringWithFormat:@"src=\"https://www.youtube.com/embed/%@\"",vid];
  68. NSError *error;
  69. NSString *htmlStr = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"ytplayer" ofType:@"html"]encoding:NSUTF8StringEncoding error: & error];
  70. htmlStr=[htmlStr stringByReplacingOccurrencesOfString:@"src=replacement" withString:replacement];
  71. // NSString *htmlStr1=@"abc def ggg";
  72. // [htmlStr stringByReplacingOccurrencesOfString:@"src=replacement" withString:replacement];
  73. // NSString* new_url = [NSString stringWithFormat:@"https://www.youtube.com/embed/%@?playsinline=1",vid];
  74. // [self.wkwebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:new_url]]];
  75. [self.wkwebView loadHTMLString:htmlStr baseURL:nil];
  76. self.wkwebView.hidden=NO;
  77. }
  78. -(void)LoadFromURL:(NSString *)vid
  79. {
  80. if(self.wkwebView==nil)
  81. return;
  82. NSString* url = [NSString stringWithFormat:@"https://www.youtube.com/embed/%@?playsinline=1",vid];
  83. NSURL* U=[NSURL URLWithString:url];
  84. NSURLRequest* request=[[NSURLRequest alloc]initWithURL:U cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:30.0f];
  85. [self.wkwebView loadRequest:request];
  86. }
  87. -(void) initMessageHandler
  88. {
  89. // [self.wkwebView.configuration.userContentController addScriptMessageHandler:self name:@"CamScan"];
  90. // [self.wkwebView.configuration.userContentController addScriptMessageHandler:self name:@"Location"];
  91. // [self.wkwebView.configuration.userContentController addScriptMessageHandler:self name:@"Share"];
  92. // [self.wkwebView.configuration.userContentController addScriptMessageHandler:self name:@"JumpTo"];
  93. // [self.wkwebView.configuration.userContentController addScriptMessageHandler:self name:@"ViewImage"];
  94. // [self.wkwebView.configuration.userContentController addScriptMessageHandler:self name:@"PostDialog"];
  95. // [self.wkwebView.configuration.userContentController addScriptMessageHandler:self name:@"Photo"];
  96. // [self.wkwebView.configuration.userContentController addScriptMessageHandler:self name:@"Email"];
  97. // [self.wkwebView.configuration.userContentController addScriptMessageHandler:self name:@"Preview"];
  98. // [self.wkwebView.configuration.userContentController addScriptMessageHandler:self name:@"Navigation"];
  99. // [self.wkwebView.configuration.userContentController addScriptMessageHandler:self name:@"DatetimePicker"];
  100. //
  101. //// [self.wkwebView.configuration.userContentController addScriptMessageHandler:self name:@"UpdateBadge"];
  102. //
  103. // [self.wkwebView.configuration.userContentController addScriptMessageHandler:self name:@"Signature"];
  104. // [self.wkwebView.configuration.userContentController addScriptMessageHandler:self name:@"SelectContact"];
  105. // [self.wkwebView.configuration.userContentController addScriptMessageHandler:self name:@"requestApplicationInfo"];
  106. // [self.wkwebView.configuration.userContentController addScriptMessageHandler:self name:@"cleanCache"];
  107. //
  108. // [self.wkwebView.configuration.userContentController addScriptMessageHandler:self name:@"login"];
  109. // [self.wkwebView.configuration.userContentController addScriptMessageHandler:self name:@"logout"];
  110. // [self.wkwebView.configuration.userContentController addScriptMessageHandler:self name:@"UpdateBadge"];
  111. // [self.wkwebView.configuration.userContentController addScriptMessageHandler:self name:@"QueryLaunchNotification"];
  112. //
  113. //
  114. //
  115. }
  116. -(void)destroyMessageHandler
  117. {
  118. // [self.wkwebView.configuration.userContentController removeScriptMessageHandlerForName:@"CamScan"];
  119. // [self.wkwebView.configuration.userContentController removeScriptMessageHandlerForName:@"Location"];
  120. // [self.wkwebView.configuration.userContentController removeScriptMessageHandlerForName:@"Share"];
  121. // [self.wkwebView.configuration.userContentController removeScriptMessageHandlerForName:@"JumpTo"];
  122. // [self.wkwebView.configuration.userContentController removeScriptMessageHandlerForName:@"ViewImage"];
  123. // [self.wkwebView.configuration.userContentController removeScriptMessageHandlerForName:@"PostDialog"];
  124. // [self.wkwebView.configuration.userContentController removeScriptMessageHandlerForName:@"Photo"];
  125. // [self.wkwebView.configuration.userContentController removeScriptMessageHandlerForName:@"Email"];
  126. // [self.wkwebView.configuration.userContentController removeScriptMessageHandlerForName:@"Preview"];
  127. // [self.wkwebView.configuration.userContentController removeScriptMessageHandlerForName:@"Navigation"];
  128. // [self.wkwebView.configuration.userContentController removeScriptMessageHandlerForName:@"DatetimePicker"];
  129. // [self.wkwebView.configuration.userContentController removeScriptMessageHandlerForName:@"Signature"];
  130. // [self.wkwebView.configuration.userContentController removeScriptMessageHandlerForName:@"SelectContact"];
  131. // [self.wkwebView.configuration.userContentController removeScriptMessageHandlerForName:@"requestApplicationInfo"];
  132. // [self.wkwebView.configuration.userContentController removeScriptMessageHandlerForName:@"cleanCache"];
  133. //
  134. // [self.wkwebView.configuration.userContentController removeScriptMessageHandlerForName:@"login"];
  135. // [self.wkwebView.configuration.userContentController removeScriptMessageHandlerForName:@"logout"];
  136. // [self.wkwebView.configuration.userContentController removeScriptMessageHandlerForName:@"UpdateBadge"];
  137. // [self.wkwebView.configuration.userContentController removeScriptMessageHandlerForName:@"QueryLaunchNotification"];
  138. }
  139. - (void)dealloc
  140. {
  141. [self destroyMessageHandler];
  142. }
  143. #pragma mark - Setter
  144. - (void)setContentInsetAdjustmentBehavior:(UIScrollViewContentInsetAdjustmentBehavior)contentInsetAdjustmentBehavior {
  145. self.wkwebView.scrollView.contentInsetAdjustmentBehavior = contentInsetAdjustmentBehavior;
  146. }
  147. //
  148. //#pragma mark - Private
  149. //
  150. //- (UIViewController *)ra_viewController {
  151. // return [RAUtils getViewController:self];
  152. //}
  153. //
  154. //- (void)ra_openPhotoLibrary:(NSDictionary *)params {
  155. //
  156. // UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
  157. // imagePicker.delegate = self;
  158. // imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  159. // imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
  160. // imagePicker.allowsEditing = YES;
  161. // [self.ra_viewController presentViewController:imagePicker animated:YES completion:nil];
  162. // self.currentParams = params;
  163. //}
  164. //
  165. //- (void)ra_openCamera:(NSDictionary *)params {
  166. //
  167. // RACameraViewController *cameraVC = [RACameraViewController viewControllerFromStoryboard];
  168. // cameraVC.completion = ^(UIImage *image) {
  169. //
  170. // image = [UIImage img_compress:image kbsize:1024];
  171. // NSData *data = UIImageJPEGRepresentation(image, 1.0);
  172. //// NSString *name = NSUUID.UUID.UUIDString;
  173. //// NSString *path = [NSTemporaryDirectory() stringByAppendingPathComponent:name];
  174. //// [data writeToFile:path atomically:NO];
  175. //
  176. // NSString *imageSource = [NSString stringWithFormat:@"data:image/jpg;base64,%@",[data base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed]];
  177. //
  178. // [self returnToWebPage:params value:imageSource];
  179. // };
  180. //
  181. // cameraVC.fromVC = self.ra_viewController;
  182. // [self.ra_viewController.navigationController pushViewController:cameraVC animated:YES];
  183. //}
  184. //
  185. //- (void)ra_openQRCodeScanner:(NSDictionary *)params {
  186. //
  187. // RAQRCodeScannerViewController *scanVC = [RAQRCodeScannerViewController viewControllerFromStoryboard];
  188. //
  189. // scanVC.QRCodeViewControllerDidCompletion = ^(RAQRCodeScannerViewController *qrcodeViewController, NSString *value) {
  190. //
  191. // [self returnToWebPage:params value:value];
  192. // [qrcodeViewController.navigationController popViewControllerAnimated:YES];
  193. // };
  194. //
  195. // scanVC.QRCodeViewControllerDidCanceled = ^(RAQRCodeScannerViewController *qrcodeViewController) {
  196. //
  197. // [qrcodeViewController.navigationController popViewControllerAnimated:YES];
  198. // };
  199. //
  200. // [self.ra_viewController.navigationController pushViewController:scanVC animated:YES];
  201. //}
  202. //
  203. //
  204. //- (void)ra_previewDocument:(NSDictionary *)params {
  205. // /**
  206. // cc = "test1@abc.com,test2@abc.com";
  207. // content = content;
  208. // receipt = "test@abc.com";
  209. // subject = "email subject";
  210. // url = "http://10.0.0.4:8080/backend_test/test.pdf";
  211. // */
  212. //
  213. // NSString *urlStr = [params objectForKey:@"url"];
  214. //// urlStr = @"http://192.168.0.130/ios.pdf";
  215. // if (urlStr) {
  216. //
  217. // RAProgressHUD *hud = [RAProgressHUD showHUDOnView:self.ra_viewController.view];
  218. //
  219. // // 先下载下来,否则QuickLook分享要崩溃
  220. // dispatch_async(dispatch_get_global_queue(0, 0), ^{
  221. //
  222. // NSString *tmpDir = NSTemporaryDirectory();
  223. //
  224. // [RADataProvider GET_DownloadFile:urlStr toFolder:tmpDir progressHandler:^(NSURLSessionTask *task, double progress) {
  225. //
  226. // NSLog(@"download %@ progress %f", urlStr.lastPathComponent, progress);
  227. //
  228. // } completionHandler:^(NSMutableDictionary *result) {
  229. //
  230. // dispatch_async(dispatch_get_main_queue(), ^{
  231. //
  232. // [hud dismiss:^{
  233. //
  234. // int resultCode = [[result objectForKey:@"result"] intValue];
  235. // if (resultCode == RESULT_TRUE) {
  236. //
  237. // NSString *path = [result objectForKey:@"path"];
  238. //
  239. // RAPreviewController *vc = [RAPreviewController ra_previewControllerWithURL:[NSURL fileURLWithPath:path]];
  240. // [self.ra_viewController presentViewController:vc animated:YES completion:nil];
  241. //
  242. // } else {
  243. //
  244. // NSString *msg = [result objectForKey:@"msg"];
  245. // [RAUtils message_alert:msg title:NSLocalizedString(@"warning_title", nil) controller:self.ra_viewController];
  246. //
  247. // }
  248. //
  249. // }];
  250. //
  251. // });
  252. //
  253. // }];
  254. // });
  255. //
  256. // }
  257. //}
  258. //
  259. //- (void)ra_mail:(NSDictionary *)params {
  260. //
  261. // /**
  262. //
  263. // cc = "test1@abc.com,test2@abc.com";
  264. // content = content;
  265. // receipt = "test@abc.com";
  266. // subject = "email subject";
  267. //
  268. // */
  269. // MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
  270. //
  271. // if(mc==nil)
  272. // {
  273. // return;
  274. // }
  275. //
  276. // NSString *subject = [params objectForKey:@"subject"];
  277. // NSString *receipt = [params objectForKey:@"receipt"];
  278. // NSString *content = [params objectForKey:@"content"];
  279. // NSString *cc = [params objectForKey:@"cc"];
  280. //
  281. // mc.mailComposeDelegate = self;
  282. // [mc setSubject:subject];
  283. //
  284. //
  285. // [mc setToRecipients:[receipt componentsSeparatedByString:@","]];
  286. // [mc setCcRecipients:[cc componentsSeparatedByString:@","]];
  287. //
  288. // if(!content)
  289. // content=@"";
  290. //
  291. // NSString* htmlbody = [NSString stringWithFormat:@"<html><p>%@</p></html>",content];
  292. //
  293. // [mc setMessageBody:htmlbody isHTML:YES];
  294. //
  295. //
  296. //// NSString* filename =self.filename;
  297. ////
  298. //// NSString *filePath = self.url;
  299. //// NSData *data = [NSData dataWithContentsOfFile:filePath];
  300. ////
  301. //// // if(self.attachment_name.length>0)
  302. //// // filename = self.attachment_name;
  303. //// [mc addAttachmentData:data mimeType:@"application/pdf" fileName:filename];
  304. //
  305. // [self.ra_viewController presentViewController:mc animated:YES completion:nil];
  306. // self.currentParams = params;
  307. //}
  308. //
  309. //- (void)ra_share:(NSDictionary *)params {
  310. //
  311. //
  312. //
  313. // // 设置分享内容
  314. //
  315. //// NSString *content = [params objectForKey:@"content"];
  316. //// NSString *imageURL = [params objectForKey:@"image"];
  317. //// NSString *title = [params objectForKey:@"title"];
  318. // NSString *url = [params objectForKey:@"url"];
  319. //
  320. // NSArray *activityItems = @[[NSURL URLWithString:url]];
  321. //
  322. // // 服务类型控制器
  323. // UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:@[]];
  324. // // _activityViewController = activityViewController;
  325. // // activityViewController.excludedActivityTypes = @[UIActivityTypeMail];
  326. // activityViewController.modalInPopover = true;
  327. //
  328. // // 选中分享类型
  329. // [activityViewController setCompletionWithItemsHandler:^(NSString * __nullable activityType, BOOL completed, NSArray * __nullable returnedItems, NSError * __nullable activityError){
  330. // // 显示选中的分享类型
  331. // NSLog(@"act type %@",activityType);
  332. //
  333. //// if (completed) {
  334. //// NSLog(@"ok");
  335. //// }else {
  336. //// NSLog(@"not ok");
  337. //// }
  338. // NSMutableDictionary *shareResult = [NSMutableDictionary dictionary];
  339. // if (!activityType) {
  340. // activityType = @"cancel";
  341. // }
  342. // shareResult[@"to"] = [activityType componentsSeparatedByString:@"."].lastObject;
  343. // shareResult[@"completed"] = @(completed);
  344. // if (activityError) {
  345. // shareResult[@"error"] = activityError.localizedDescription;
  346. // }
  347. //
  348. // [self returnToWebPage:params value:[RAConvertor dict2string:shareResult]];
  349. //
  350. // }];
  351. //
  352. // [self.ra_viewController presentViewController:activityViewController animated:YES completion:nil];
  353. //}
  354. //
  355. //- (void)ra_viewPhotos:(NSDictionary *)params {
  356. //
  357. // NSArray *urlArr = [params objectForKey:@"images"];
  358. // NSUInteger index = [[params objectForKey:@"index"] integerValue];
  359. //
  360. // NSMutableArray *mArr = [NSMutableArray array];
  361. // for (NSString *url in urlArr) {
  362. //
  363. // RAPhotoModel *model = [RAPhotoModel new];
  364. // model.url = url;
  365. //
  366. // [mArr addObject:model];
  367. // }
  368. //
  369. //
  370. // RAPhotoPreviewController *previewController = [RAPhotoPreviewController ra_photoPreviewControllerWithPhotoItems:mArr offset:index];
  371. //
  372. // [self.ra_viewController presentViewController:previewController animated:YES completion:nil];
  373. //}
  374. //
  375. //- (void)ra_navigate:(NSDictionary *)params {
  376. //
  377. // NSString *name = [params objectForKey:@"name"];
  378. // NSString *addr = [params objectForKey:@"address"];
  379. //
  380. // [RAMapNavigateHandler ra_navigate:addr withTitle:name viewController:self.ra_viewController];
  381. //}
  382. //
  383. //- (void)ra_signature:(NSDictionary *)params {
  384. //
  385. // __weak typeof(self) weakSelf = self;
  386. // SignatureViewController *signatureVC = [SignatureViewController ra_signatureViewControllerWithCompletion:^(UIImage *image) {
  387. //
  388. // NSData *data = UIImageJPEGRepresentation(image, 1.0);
  389. //
  390. // NSString *imageSource = [NSString stringWithFormat:@"data:image/jpg;base64,%@",[data base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed]];
  391. //
  392. // [weakSelf returnToWebPage:params value:imageSource];
  393. //
  394. // }];
  395. //
  396. // [self.ra_viewController.navigationController pushViewController:signatureVC animated:NO];
  397. //}
  398. //
  399. //- (void)ra_dateTimePicker:(NSDictionary *)params {
  400. //
  401. // NSString *type = [params objectForKey:@"type"];
  402. // NSString *value = [params objectForKey:@"value"];
  403. // NSString *title = [params objectForKey:@"title"];
  404. //
  405. // NSString *format = @"MM/dd/yyyy";
  406. // UIDatePickerMode mode = UIDatePickerModeDate;
  407. //
  408. //// type = @"dateTime";
  409. //// value = @"09/22/1028 14:44:24";
  410. //
  411. // if ([type isEqualToString:@"date"]) {
  412. //
  413. // } else if ([type isEqualToString:@"time"]) {
  414. //
  415. // format = @"HH:mm:ss";
  416. // mode = UIDatePickerModeTime;
  417. //
  418. // } else if ([type isEqualToString:@"dateTime"]) {
  419. //
  420. // format = @"MM/dd/yyyy HH:mm:ss";
  421. // mode = UIDatePickerModeDateAndTime;
  422. // }
  423. //
  424. // NSDateFormatter *dateFormatter = [NSDateFormatter new];
  425. // dateFormatter.dateFormat = format;
  426. // NSDate *date = [dateFormatter dateFromString:value];
  427. //
  428. // __weak typeof(self) weakSelf = self;
  429. // [RADatePickerViewController presentDatePicker:self.ra_viewController title:title dateMode:mode defaultDate:date withSetBlk:^(NSDate *date) {
  430. //
  431. // NSString *dateStr = [dateFormatter stringFromDate:date];
  432. // [weakSelf returnToWebPage:params value:dateStr];
  433. // }];
  434. //}
  435. //
  436. //- (void)ra_selectContact:(NSDictionary *)params {
  437. //
  438. // __weak typeof(self) weakSelf = self;
  439. // [[RAContactHelper defaultHelper] showContactPickerByViewController:self.ra_viewController withComplete:^(BOOL canceled, NSArray<CNContact *> *contacts) {
  440. //
  441. // if (!canceled) {
  442. //
  443. // NSMutableArray *contactArray = [NSMutableArray array];
  444. // for (CNContact *contact in contacts) {
  445. //
  446. // NSMutableDictionary *contactDictionary = [NSMutableDictionary dictionary];
  447. // NSLog(@"=========================================================");
  448. // // givenName middleName familyName
  449. // NSString *givenName = [self adjustNilString:contact.givenName];
  450. // NSString *middleName = [self adjustNilString:contact.middleName];
  451. // NSString *familyName = [self adjustNilString:contact.familyName];
  452. // NSString *name = [NSString stringWithFormat:@"%@ %@ %@", givenName, middleName, familyName];
  453. //
  454. // NSLog(@"=======================Contact Name=======================");
  455. // NSLog(@"%@",name);
  456. // contactDictionary[@"name"] = name;
  457. //
  458. // // organizationName departmentName jobTitle
  459. // NSString *organizationName = [self adjustNilString:contact.organizationName];
  460. // NSString *departmentName = [self adjustNilString:contact.departmentName];
  461. // NSString *jobTitle = [self adjustNilString:contact.jobTitle];
  462. // NSString *job = [NSString stringWithFormat:@"%@ %@ %@", organizationName, departmentName, jobTitle];
  463. //
  464. // NSLog(@"=======================Contact Job=======================");
  465. // NSLog(@"%@",job);
  466. // contactDictionary[@"job"] = @[job];
  467. //
  468. // // phoneNumbers emailAddresses postalAddresses urlAddresses
  469. //
  470. // NSMutableArray *phoneArray = [NSMutableArray array];
  471. // NSMutableArray *faxArray = [NSMutableArray array];
  472. // NSLog(@"=======================Phone Number=======================");
  473. // NSArray<CNLabeledValue<CNPhoneNumber*>*> *phoneNumbers = contact.phoneNumbers;
  474. // for (CNLabeledValue<CNPhoneNumber*> *labeledValue in phoneNumbers) {
  475. //
  476. // CNPhoneNumber *phoneNumber = labeledValue.value;
  477. // NSString *label = [CNLabeledValue localizedStringForLabel:labeledValue.label];
  478. // NSLog(@"%@ %@", label, phoneNumber.stringValue);
  479. //
  480. // if ([labeledValue.label.lowercaseString containsString:@"fax"]) {
  481. // [faxArray addObject:phoneNumber.stringValue];
  482. // } else {
  483. // [phoneArray addObject:phoneNumber.stringValue];
  484. // }
  485. // }
  486. // contactDictionary[@"phone"] = phoneArray;
  487. // contactDictionary[@"fax"] = faxArray;
  488. //
  489. // NSMutableArray *emailArray = [NSMutableArray array];
  490. // NSLog(@"=======================Email Address=======================");
  491. // NSArray<CNLabeledValue<NSString*>*> *emailAddresses = contact.emailAddresses;
  492. // for (CNLabeledValue<NSString*> *labeledValue in emailAddresses) {
  493. //
  494. // NSString *email = labeledValue.value;
  495. // NSString *label = [CNLabeledValue localizedStringForLabel:labeledValue.label];
  496. // NSLog(@"%@ %@", label, email);
  497. // [emailArray addObject:email];
  498. // }
  499. // contactDictionary[@"email"] = emailArray;
  500. //
  501. // NSMutableArray *addressArray = [NSMutableArray array];
  502. // NSLog(@"=======================Postal Number=======================");
  503. // NSArray<CNLabeledValue<CNPostalAddress*>*> *postalAddresses = contact.postalAddresses;
  504. // for (CNLabeledValue<CNPostalAddress*> *labeledValue in postalAddresses) {
  505. //
  506. // NSString *label = [CNLabeledValue localizedStringForLabel:labeledValue.label];
  507. // CNPostalAddress *postalAddress = labeledValue.value;
  508. // NSString *postalCode = [self adjustNilString:postalAddress.postalCode];
  509. // NSString *country = [self adjustNilString:postalAddress.country];
  510. // NSString *state = [self adjustNilString:postalAddress.state];
  511. // NSString *city = [self adjustNilString:postalAddress.city];
  512. // NSString *street = [self adjustNilString:postalAddress.street];
  513. //
  514. // NSMutableDictionary *addressDic = [NSMutableDictionary dictionary];
  515. // addressDic[@"postalCode"] = postalCode;
  516. // addressDic[@"country"] = country;
  517. // addressDic[@"state"] = state;
  518. // addressDic[@"city"] = city;
  519. // addressDic[@"street"] = street;
  520. //
  521. // [addressArray addObject:addressDic];
  522. // NSLog(@"%@ %@ %@ %@ %@ %@", label, postalCode, country, state, city, street);
  523. // }
  524. // contactDictionary[@"address"] = addressArray;
  525. //
  526. // NSMutableArray *urlArray = [NSMutableArray array];
  527. // NSLog(@"=======================URL Address=======================");
  528. // NSArray<CNLabeledValue<NSString*>*> *urlAddresses = contact.urlAddresses;
  529. // for (CNLabeledValue<NSString*> *labeledValue in urlAddresses) {
  530. //
  531. // NSString *url = labeledValue.value;
  532. // NSString *label = [CNLabeledValue localizedStringForLabel:labeledValue.label];
  533. // NSLog(@"%@ %@", label, url);
  534. //
  535. // [urlArray addObject:url];
  536. // }
  537. // contactDictionary[@"url"] = urlArray;
  538. //
  539. // NSLog(@"**********************************************************");
  540. //
  541. // [contactArray addObject:contactDictionary];
  542. // }
  543. //
  544. // // return
  545. //
  546. // NSString *jsonString = [RAConvertor DataTOjsonString:contactArray];
  547. // [weakSelf returnToWebPage:params value:jsonString];
  548. //
  549. // NSLog(@"%@", jsonString);
  550. // }
  551. //
  552. // }];
  553. //}
  554. //
  555. //- (void)ra_requestAppInfo:(NSDictionary *)params {
  556. //
  557. // AppDelegate *delagate =(AppDelegate *) [[UIApplication sharedApplication]delegate];
  558. //
  559. // NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
  560. // NSString *ver = [infoDic objectForKey:@"CFBundleShortVersionString"];
  561. // NSString *build = [infoDic objectForKey:@"CFBundleVersion"];
  562. // NSString *appVer = [NSString localizedStringWithFormat:@"Ver: %@ build %@",ver,build];
  563. //
  564. // NSMutableDictionary * ret = [NSMutableDictionary new];
  565. // ret[@"appVer"]=appVer;
  566. // ret[@"apnToken"]=delagate.apnToken ;
  567. //
  568. // NSString* Sret=[RAConvertor dict2string:ret];
  569. // [self returnToWebPage:params value:Sret];
  570. //}
  571. //
  572. //- (void)ra_queryLaunchNotification:(NSDictionary *)params {
  573. //
  574. // AppDelegate *delagate =(AppDelegate *) [[UIApplication sharedApplication]delegate];
  575. //
  576. //// NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
  577. //// NSString *ver = [infoDic objectForKey:@"CFBundleShortVersionString"];
  578. //// NSString *build = [infoDic objectForKey:@"CFBundleVersion"];
  579. //// NSString *appVer = [NSString localizedStringWithFormat:@"Ver: %@ build %@",ver,build];
  580. ////
  581. //// NSMutableDictionary * ret = [NSMutableDictionary new];
  582. //// ret[@"appVer"]=appVer;
  583. //// ret[@"apnToken"]=delagate.apnToken ;
  584. ////
  585. //// NSString* Sret=[RAConvertor dict2string:ret];
  586. // [self returnToWebPage:params value:delagate.launchNotification];
  587. // delagate.launchNotification = nil;
  588. //
  589. //}
  590. //
  591. //- (void)ra_cleanCache:(NSDictionary *)params {
  592. //
  593. // /**
  594. //
  595. // // On-disk caches.
  596. // WKWebsiteDataTypeDiskCache API_AVAILABLE(macosx(10.11), ios(9.0));
  597. //
  598. // // In-memory caches.
  599. // WKWebsiteDataTypeMemoryCache API_AVAILABLE(macosx(10.11), ios(9.0));
  600. //
  601. // // HTML offline web application caches.
  602. // WKWebsiteDataTypeOfflineWebApplicationCache API_AVAILABLE(macosx(10.11), ios(9.0));
  603. //
  604. // // Cookies.
  605. // WKWebsiteDataTypeCookies API_AVAILABLE(macosx(10.11), ios(9.0));
  606. //
  607. // // HTML session storage.
  608. // WKWebsiteDataTypeSessionStorage API_AVAILABLE(macosx(10.11), ios(9.0));
  609. //
  610. // // HTML local storage.
  611. // WKWebsiteDataTypeLocalStorage API_AVAILABLE(macosx(10.11), ios(9.0));
  612. //
  613. // // WebSQL databases.
  614. // WKWebsiteDataTypeWebSQLDatabases API_AVAILABLE(macosx(10.11), ios(9.0));
  615. //
  616. // // IndexedDB databases.
  617. // WKWebsiteDataTypeIndexedDBDatabases API_AVAILABLE(macosx(10.11), ios(9.0));
  618. //
  619. // */
  620. //
  621. // NSArray *typeArr = @[
  622. // WKWebsiteDataTypeDiskCache,
  623. // WKWebsiteDataTypeMemoryCache,
  624. // WKWebsiteDataTypeOfflineWebApplicationCache,
  625. // WKWebsiteDataTypeLocalStorage,
  626. // WKWebsiteDataTypeWebSQLDatabases,
  627. // WKWebsiteDataTypeIndexedDBDatabases
  628. // ];
  629. // NSSet *typeSet = [NSSet setWithArray:typeArr];
  630. // NSDate *dateSince = [NSDate dateWithTimeIntervalSince1970:0];
  631. //
  632. // [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:typeSet modifiedSince:dateSince completionHandler:^{
  633. // [self returnToWebPage:params value:@""];
  634. // }];
  635. //}
  636. //
  637. //- (void)ra_handleLogin:(NSString *)user password:(NSString *)password {
  638. //
  639. // if (self.delegate && [self.delegate respondsToSelector:@selector(webview:didLogin:password:)]) {
  640. // [self.delegate webview:self didLogin:user password:password];
  641. // }
  642. //}
  643. //
  644. //- (void)ra_handleLogout {
  645. //
  646. // if (self.delegate && [self.delegate respondsToSelector:@selector(webviewDidLogout:)]) {
  647. // [self.delegate webviewDidLogout:self];
  648. // }
  649. //}
  650. #pragma mark - player
  651. //- (void)stringFromEvaluatingJavaScript:(NSString *)jsToExecute {
  652. // // return [self.wkwebView stringByEvaluatingJavaScriptFromString:jsToExecute];
  653. //
  654. //
  655. //[self.wkwebView evaluateJavaScript:jsToExecute completionHandler:^(id _Nullable result, NSError * _Nullable error) {
  656. // NSLog(@"%@----%@",result, error);
  657. // }];
  658. //
  659. //}
  660. - (void)stopVideo {
  661. [self.wkwebView reload];
  662. }
  663. #pragma mark - Utils
  664. - (NSString *)adjustNilString:(NSString *)str {
  665. if (str) {
  666. return str;
  667. }
  668. return @"";
  669. }
  670. //- (void)ra_injectJS:(NSString *)js {
  671. //
  672. //// NSStringEncoding encoding = NSUTF8StringEncoding;
  673. //// NSString *js = [NSString stringWithContentsOfFile:@"/Users/macmini1/eclipse-workspace/MyWeb/WebContent/JS/Interface.js" usedEncoding:&encoding error:nil];
  674. //
  675. // if (js) {
  676. // [self.wkwebView evaluateJavaScript:js completionHandler:^(id _Nullable result, NSError * _Nullable error) {
  677. // NSLog(@"create JS %@ Error: %@", result, error);
  678. // }];
  679. // }
  680. //}
  681. //
  682. //#pragma mark - Web Callback
  683. //
  684. //-(void) returnToWebPage:(NSDictionary*)params value:(NSString*)value
  685. //{
  686. // if(![params isNull:@"returnid"] )
  687. // {
  688. //
  689. // [self webPageReturn:params[@"returnid"] value:value];
  690. //
  691. // }else if(![params isNull:@"callback"])
  692. // {
  693. // [self webPageCallback:params caller:params[@"id"] value:value];
  694. // }
  695. //}
  696. //-(void) webPageCallback:(NSDictionary*)params caller:(NSString*) callerid value:(NSString*)value
  697. //{
  698. //// NSString* caller = [NSString stringWithFormat:@"'%@'",callerid];
  699. // NSString *caller = [callerid ra_stringByEscapingForJavascriptWithDelimiter:'\'' wrapWithDelimiters:YES];
  700. // if(value==nil)
  701. // value=@"";
  702. //// if([value isKindOfClass:NSString.class])
  703. // {
  704. //// value =[NSString stringWithFormat:@"'%@'",value];
  705. // value = [(NSString *)value ra_stringByEscapingForJavascriptWithDelimiter:'\'' wrapWithDelimiters:YES];
  706. // }
  707. //
  708. // NSString *jsStr = [NSString stringWithFormat:params[@"callback"],caller.UTF8String,value.UTF8String];
  709. // [self.wkwebView evaluateJavaScript:jsStr completionHandler:^(id _Nullable result, NSError * _Nullable error) {
  710. // NSLog(@"%@----%@",result, error);
  711. // }];
  712. //}
  713. //-(void) webPageReturn:(NSString*) returnid value:(NSObject*)value
  714. //{
  715. // if (returnid && value) {
  716. //
  717. // returnid = [returnid ra_stringByEscapingForJavascriptWithDelimiter:'\'' wrapWithDelimiters:YES];
  718. // value = [value.description ra_stringByEscapingForJavascriptWithDelimiter:'\'' wrapWithDelimiters:YES];
  719. // NSString *jsStr = [NSString stringWithFormat:@"document.getElementById(%@).value = %@;",returnid,value];
  720. //
  721. //// NSString *jsStr = [NSString stringWithFormat:@"document.getElementById(\"%@\").value = \"%@\";",returnid,value];
  722. // [self.wkwebView evaluateJavaScript:jsStr completionHandler:^(id _Nullable result, NSError * _Nullable error) {
  723. // NSLog(@"%@----%@",result, error);
  724. // }];
  725. //
  726. // }
  727. //}
  728. //
  729. //- (void)evaluateJs:(NSString *)js completionHandler:(void (^) (id result, NSError *error))handler {
  730. // [self.wkwebView evaluateJavaScript:js completionHandler:handler];
  731. //}
  732. //
  733. //#pragma mark - MFMailComposeViewControllerDelegate delegate
  734. //
  735. //- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
  736. // NSString *resultStr = nil;
  737. // switch (result)
  738. // {
  739. // case MFMailComposeResultCancelled:
  740. // resultStr = @"email canceled";
  741. // break;
  742. // case MFMailComposeResultSaved:
  743. // resultStr = @"email saved";
  744. // break;
  745. // case MFMailComposeResultSent:
  746. // resultStr = @"email send";
  747. // break;
  748. // case MFMailComposeResultFailed:
  749. // resultStr = [NSString stringWithFormat:@"email error: %@",[error localizedDescription]];
  750. // break;
  751. // default:
  752. // break;
  753. // }
  754. //
  755. // [self returnToWebPage:self.currentParams value:resultStr];
  756. // self.currentParams = nil;
  757. //
  758. // [controller dismissViewControllerAnimated:YES completion:nil];
  759. //}
  760. //
  761. //
  762. //#pragma mark - UIImagePickerControllerDelegate
  763. //
  764. //- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info {
  765. //
  766. // if (picker.sourceType == UIImagePickerControllerSourceTypePhotoLibrary) {
  767. //
  768. // UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
  769. // image = [UIImage img_compress:image kbsize:1024];
  770. //
  771. // [picker dismissViewControllerAnimated:YES completion:nil];
  772. //
  773. // NSData *data = UIImageJPEGRepresentation(image, 1.0);
  774. // NSString *imageSource = [NSString stringWithFormat:@"data:image/jpg;base64,%@",[data base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed]];
  775. //
  776. // [self returnToWebPage:self.currentParams value:imageSource];
  777. // self.currentParams = nil;
  778. //
  779. // }
  780. //}
  781. //
  782. //- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
  783. // [picker dismissViewControllerAnimated:YES completion:nil];
  784. //}
  785. //#pragma mark - WKUIDelegate
  786. //- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler
  787. //{
  788. //
  789. // UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提醒" message:message preferredStyle:UIAlertControllerStyleAlert];
  790. // [alert addAction:[UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  791. // if(completionHandler)
  792. // completionHandler();
  793. // }]];
  794. //
  795. // UIViewController* vc=[RAUtils getViewController:self];
  796. // [vc presentViewController:alert animated:YES completion:nil];
  797. //
  798. //// UIViewController* vc=[self getViewController];
  799. //// [vc presentViewController:alert animated:YES completion:nil];
  800. //}
  801. //-(UIViewController*) getViewController
  802. //{
  803. // for (UIView* next = [self superview]; next; next = next.superview) {
  804. // UIResponder* nextResponder = [next nextResponder];
  805. // if ([nextResponder isKindOfClass:[UIViewController class]]) {
  806. // return (UIViewController*)nextResponder;
  807. // }
  808. // }
  809. // return nil;
  810. //}
  811. //
  812. //#pragma mark - WKScriptMessageHandler
  813. //- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message
  814. //{
  815. // // message.body -- Allowed types are NSNumber, NSString, NSDate, NSArray,NSDictionary, and NSNull.
  816. // NSLog(@"%@",message.name);
  817. // NSLog(@"body:%@",message.body);
  818. //
  819. //
  820. // NSDictionary* params=[RAConvertor string2dict:message.body];
  821. //// RAConvertor
  822. // if ([message.name isEqualToString:@"CamScan"]) {
  823. //
  824. // [self handleCamScan:params];
  825. // } else if ([message.name isEqualToString:@"Location"]) {
  826. //
  827. // [self handleLocation:params];
  828. // } else if ([message.name isEqualToString:@"Share"]) {
  829. //
  830. // [self handleShare:params];
  831. // } else if ([message.name isEqualToString:@"JumpTo"]) {
  832. //
  833. //
  834. // [self handleJumpTo:params];
  835. // } else if ([message.name isEqualToString:@"ViewImage"]) {
  836. //
  837. //
  838. // [self handleViewImage:params];
  839. // } else if ([message.name isEqualToString:@"PostDialog"]) {
  840. //
  841. //
  842. // [self handlePostDialog:params];
  843. // }else if ([message.name isEqualToString:@"Photo"]) {
  844. //
  845. //
  846. // [self handlePhoto:params];
  847. // }else if ([message.name isEqualToString:@"Email"]) {
  848. //
  849. //
  850. //
  851. // [self handleEmail:params];
  852. // }else if ([message.name isEqualToString:@"Preview"]) {
  853. //
  854. //
  855. //
  856. // [self handlePreview:params];
  857. // }else if ([message.name isEqualToString:@"Navigation"]) {
  858. //
  859. //
  860. //
  861. // [self handleNavigation:params];
  862. // }else if ([message.name isEqualToString:@"Signature"]) {
  863. //
  864. //
  865. //
  866. // [self handleSignature:params];
  867. // }else if ([message.name isEqualToString:@"DatetimePicker"]) {
  868. //
  869. //
  870. //
  871. // [self handleDatetimePicker:params];
  872. // } else if ([message.name isEqualToString:@"SelectContact"]) {
  873. //
  874. // [self handleSelectContact:params];
  875. // } else if ([message.name isEqualToString:@"requestApplicationInfo"]) {
  876. //
  877. // [self handleRequestApplicationInfo: params];
  878. // }else if ([message.name isEqualToString:@"QueryLaunchNotification"]) {
  879. //
  880. // [self ra_queryLaunchNotification: params];
  881. // }
  882. //
  883. // else if ([message.name isEqualToString:@"UpdateBadge"]) {
  884. //
  885. // [self handleUpdateBadge: params];
  886. // } else if ([message.name isEqualToString:@"cleanCache"]) {
  887. //
  888. // [self handleCleanCache:params];
  889. // } else if ([message.name isEqualToString:@"login"]) {
  890. //
  891. // [self handleLogin:params];
  892. // } else if ([message.name isEqualToString:@"logout"]) {
  893. //
  894. // [self handleLogout];
  895. // }
  896. // else
  897. // {
  898. // NSLog(@"%@",params);
  899. // }
  900. //}
  901. //
  902. //- (void)handleDatetimePicker:(NSDictionary *)params
  903. //{
  904. //
  905. //
  906. // NSLog(@"handleDatetimePicker %@",params);
  907. //
  908. // [self ra_dateTimePicker:params];
  909. //}
  910. //
  911. //- (void)handleSignature:(NSDictionary *)params
  912. //{
  913. // [self ra_signature:params];
  914. //}
  915. //
  916. //- (void)handleCamScan:(NSDictionary *)params
  917. //{
  918. // [self ra_openQRCodeScanner:params];
  919. //}
  920. //
  921. //- (void)handleLocation:(NSDictionary *)params
  922. //{
  923. // [self returnToWebPage:params value:@"abc,def,'aaa"];
  924. //}
  925. //
  926. //- (void)handleShare:(NSDictionary *)params
  927. //{
  928. // [self ra_share:params];
  929. //}
  930. //
  931. //- (void)handleViewImage:(NSDictionary *)params
  932. //{
  933. // [self ra_viewPhotos:params];
  934. //}
  935. //
  936. //- (void)handlePostDialog:(NSDictionary *)params
  937. //{
  938. // UIAlertController *alert = [UIAlertController alertControllerWithTitle:params[@"title"] message:params[@"message"] preferredStyle:UIAlertControllerStyleAlert];
  939. //
  940. // NSArray* arr_btns=params[@"buttons"];
  941. // for(int i=0;i<arr_btns.count;i++)
  942. // {
  943. // NSDictionary* button = arr_btns[i];
  944. // [alert addAction:[UIAlertAction actionWithTitle:button[@"text"] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  945. //
  946. // [self returnToWebPage:params value:button[@"value"]];
  947. //// NSString *jsStr = [NSString stringWithFormat:params[@"callback"],button[@"value"]];
  948. //// [self.wkwebView evaluateJavaScript:jsStr completionHandler:^(id _Nullable result, NSError * _Nullable error) {
  949. //// NSLog(@"%@----%@",result, error);
  950. //// }];
  951. // }]];
  952. // }
  953. // UIViewController* vc=[RAUtils getViewController:self];
  954. // [vc presentViewController:alert animated:YES completion:nil];
  955. //
  956. //}
  957. //
  958. //- (void)handlePhoto:(NSDictionary *)params
  959. //{
  960. //
  961. // UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  962. // UIAlertAction *photoLibraryAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"wb_photo_libray_action", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  963. // [self ra_openPhotoLibrary:params];
  964. // }];
  965. // [alertVC addAction:photoLibraryAction];
  966. //
  967. // UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"wb_camera_action", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  968. // [self ra_openCamera:params];
  969. // }];
  970. // [alertVC addAction:cameraAction];
  971. //
  972. // UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"btn_cancel", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  973. //
  974. // }];
  975. // [alertVC addAction:cancelAction];
  976. //
  977. // [self.ra_viewController presentViewController:alertVC animated:YES completion:nil];
  978. //}
  979. //
  980. //- (void)handleEmail:(NSDictionary *)params
  981. //{
  982. // [self ra_mail:params];
  983. //}
  984. //
  985. //- (void)handlePreview:(NSDictionary *)params
  986. //{
  987. // [self ra_previewDocument:params];
  988. //}
  989. //
  990. //- (void)handleNavigation:(NSDictionary *)params
  991. //{
  992. // [self ra_navigate:params];
  993. //}
  994. //
  995. //- (void)handleJumpTo:(NSDictionary *)params
  996. //{
  997. //
  998. // if(self.JumpTo!=nil)
  999. // {
  1000. // self.JumpTo(params[@"url"], params[@"ios_module"]);
  1001. // return;
  1002. // }
  1003. //// UIViewController* vc=[self getViewController];
  1004. //
  1005. //}
  1006. //
  1007. //- (void)handleSelectContact:(NSDictionary *)params {
  1008. //
  1009. // [self ra_selectContact:params];
  1010. //}
  1011. //
  1012. //- (void)handleUpdateBadge:(NSDictionary *)params {
  1013. // [UIApplication sharedApplication].applicationIconBadgeNumber = [params[@"count"] intValue];
  1014. //// [self ra_requestAppInfo:params];
  1015. //}
  1016. //- (void)handleRequestApplicationInfo:(NSDictionary *)params {
  1017. // [self ra_requestAppInfo:params];
  1018. //}
  1019. //- (void)QueryLaunchNotification:(NSDictionary *)params {
  1020. // [self ra_requestAppInfo:params];
  1021. //}
  1022. //
  1023. //- (void)handleCleanCache:(NSDictionary *)params {
  1024. // [self ra_cleanCache:params];
  1025. //}
  1026. //
  1027. //- (void)handleLogin:(NSDictionary *)params {
  1028. //
  1029. // NSString *user = params[@"user"];
  1030. // NSString *password = params[@"password"];
  1031. // [self ra_handleLogin:user password:password];
  1032. //}
  1033. //
  1034. //- (void)handleLogout {
  1035. // [self ra_handleLogout];
  1036. //}
  1037. //
  1038. //#pragma mark - WKNavigationDelegate
  1039. //
  1040. //- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation {
  1041. // NSLog(@"%s",__func__);
  1042. //}
  1043. //
  1044. //- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler {
  1045. // NSLog(@"%s",__func__);
  1046. //
  1047. //
  1048. // //1)获取trust object
  1049. // SecTrustRef trust = challenge.protectionSpace.serverTrust;
  1050. // SecTrustResultType result;
  1051. //
  1052. // //2)SecTrustEvaluate对trust进行验证
  1053. // OSStatus status = SecTrustEvaluate(trust, &result);
  1054. //
  1055. // // CFArrayRef defaultPolicies = NULL;
  1056. // // SecTrustCopyPolicies(trust, &defaultPolicies);
  1057. // // NSLog(@"default policies: %@",(__bridge id)defaultPolicies);
  1058. //
  1059. // NSMutableArray *policies = [NSMutableArray array];
  1060. // [policies addObject:(__bridge_transfer id)SecPolicyCreateBasicX509()];
  1061. // SecTrustSetPolicies(trust, (__bridge CFArrayRef)policies);
  1062. //
  1063. // if (status == errSecSuccess && (result == kSecTrustResultProceed || result == kSecTrustResultUnspecified)) {
  1064. //
  1065. // //3)验证成功,生成NSURLCredential凭证cred,告知challenge的sender使用这个凭证来继续连接
  1066. // NSURLCredential *cred = [NSURLCredential credentialForTrust:trust];
  1067. // [challenge.sender useCredential:cred forAuthenticationChallenge:challenge];
  1068. // if (completionHandler) {
  1069. // completionHandler(NSURLSessionAuthChallengeUseCredential,cred); // 使用证书
  1070. // }
  1071. //
  1072. // } else {
  1073. //
  1074. // [challenge.sender cancelAuthenticationChallenge:challenge];
  1075. //
  1076. // //4)验证失败,取消这次验证流程
  1077. // if (completionHandler) {
  1078. // completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge,nil); // 忽略证书
  1079. // }
  1080. //
  1081. // }
  1082. //
  1083. //}
  1084. //
  1085. //
  1086. //- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error
  1087. //{
  1088. // NSLog(@"%@",error);
  1089. //}
  1090. //
  1091. ////禁止链接跳转
  1092. //- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
  1093. //
  1094. // NSLog(@"%s",__func__);
  1095. // NSLog(@"navigationAction = %@, \r!!!!!!!!!!!!!!!!!!!!!!\n request = %@",navigationAction,navigationAction.request.URL);
  1096. //
  1097. // NSString *url = navigationAction.request.URL.absoluteString;
  1098. // if (![url containsString:[NSBundle mainBundle].bundleURL.absoluteString]) {
  1099. // self.url = url;
  1100. // }
  1101. //
  1102. //
  1103. // decisionHandler(WKNavigationActionPolicyAllow);
  1104. //}
  1105. //
  1106. //- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
  1107. // NSLog(@"%s",__func__);
  1108. // @synchronized (self) {
  1109. // [self.wkwebView.scrollView hideEmpty];
  1110. // if (self.firstLoad && !self.hud) {
  1111. // self.hud = [RAProgressHUD showHUDOnView:self.ra_viewController.view];
  1112. // }
  1113. // if (self.delegate && [self.delegate respondsToSelector:@selector(webviewDidStarLoading:)]) {
  1114. // [self.delegate webviewDidStarLoading:self];
  1115. // }
  1116. // }
  1117. //}
  1118. //
  1119. //- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {
  1120. // NSLog(@"%s",__func__);
  1121. // NSLog(@"error: %@", error.localizedDescription);
  1122. // @synchronized (self) {
  1123. // if (self.hud) {
  1124. // [self.hud dismiss];
  1125. // self.hud = nil;
  1126. // }
  1127. //
  1128. // if (self.delegate && [self.delegate respondsToSelector:@selector(webviewDidFinishLoading:error:)]) {
  1129. // [self.delegate webviewDidFinishLoading:self error:error];
  1130. // }
  1131. //
  1132. // // NSURL *bundleURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
  1133. // // [webView loadHTMLString:@"<html></html>" baseURL:bundleURL];
  1134. //
  1135. // [self.wkwebView.scrollView showEmpty];
  1136. // }
  1137. //}
  1138. //
  1139. //- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
  1140. //{
  1141. // NSLog(@"%s",__func__);
  1142. // @synchronized (self) {
  1143. // if (self.hud) {
  1144. // [self.hud dismiss];
  1145. // self.hud = nil;
  1146. // self.firstLoad = NO;
  1147. // }
  1148. //
  1149. // if (self.delegate && [self.delegate respondsToSelector:@selector(webviewDidFinishLoading:error:)]) {
  1150. // [self.delegate webviewDidFinishLoading:self error:nil];
  1151. // }
  1152. //
  1153. // // self.wkwebView.scrollView.scrollEnabled = NO;
  1154. // [webView evaluateJavaScript:@"document.body.scrollHeight" completionHandler:^(id result, NSError *_Nullable error) {
  1155. //
  1156. // NSLog(@"web page height:%@",result);
  1157. // //result 就是加载完成后 webView的实际高度
  1158. // //获取后返回重新布局
  1159. //
  1160. // }];
  1161. // }
  1162. //
  1163. //// NSStringEncoding encoding = NSUTF8StringEncoding;
  1164. //// NSString *js = [NSString stringWithContentsOfFile:@"/Users/macmini1/eclipse-workspace/MyWeb/WebContent/JS/Interface.js" usedEncoding:&encoding error:nil];
  1165. //// [self ra_injectJS:js];
  1166. ////
  1167. // // css
  1168. // // /Users/macmini1/eclipse-workspace/MyWeb/WebContent/test.css
  1169. //// NSString *cssJs = [NSString stringWithFormat:];
  1170. //
  1171. //}
  1172. //
  1173. //- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation {
  1174. // NSLog(@"%s",__func__);
  1175. //}
  1176. //
  1177. //- (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView {
  1178. // NSLog(@"%s",__func__);
  1179. //}
  1180. //
  1181. //- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler
  1182. //{
  1183. // NSLog(@"%s",__func__);
  1184. // decisionHandler(WKNavigationResponsePolicyAllow);
  1185. //}
  1186. #pragma mark - WKNavigationDelegate
  1187. - (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation {
  1188. NSLog(@"%s",__func__);
  1189. }
  1190. - (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler {
  1191. NSLog(@"%s",__func__);
  1192. //1)获取trust object
  1193. SecTrustRef trust = challenge.protectionSpace.serverTrust;
  1194. SecTrustResultType result;
  1195. //2)SecTrustEvaluate对trust进行验证
  1196. OSStatus status = SecTrustEvaluate(trust, &result);
  1197. // CFArrayRef defaultPolicies = NULL;
  1198. // SecTrustCopyPolicies(trust, &defaultPolicies);
  1199. // NSLog(@"default policies: %@",(__bridge id)defaultPolicies);
  1200. NSMutableArray *policies = [NSMutableArray array];
  1201. [policies addObject:(__bridge_transfer id)SecPolicyCreateBasicX509()];
  1202. SecTrustSetPolicies(trust, (__bridge CFArrayRef)policies);
  1203. if (status == errSecSuccess && (result == kSecTrustResultProceed || result == kSecTrustResultUnspecified)) {
  1204. //3)验证成功,生成NSURLCredential凭证cred,告知challenge的sender使用这个凭证来继续连接
  1205. NSURLCredential *cred = [NSURLCredential credentialForTrust:trust];
  1206. [challenge.sender useCredential:cred forAuthenticationChallenge:challenge];
  1207. if (completionHandler) {
  1208. completionHandler(NSURLSessionAuthChallengeUseCredential,cred); // 使用证书
  1209. }
  1210. } else {
  1211. [challenge.sender cancelAuthenticationChallenge:challenge];
  1212. //4)验证失败,取消这次验证流程
  1213. if (completionHandler) {
  1214. completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge,nil); // 忽略证书
  1215. }
  1216. }
  1217. }
  1218. - (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error
  1219. {
  1220. NSLog(@"%@",error);
  1221. }
  1222. //禁止链接跳转
  1223. - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
  1224. NSLog(@"%s",__func__);
  1225. NSLog(@"navigationAction = %@, \r!!!!!!!!!!!!!!!!!!!!!!\n request = %@",navigationAction,navigationAction.request.URL);
  1226. // NSURLRequest * request = navigationAction.request;
  1227. return decisionHandler(WKNavigationActionPolicyAllow);
  1228. // if ([request.URL.host isEqual: self.originURL.host]) {
  1229. // return decisionHandler(WKNavigationActionPolicyAllow);
  1230. // } else if ([request.URL.scheme isEqual:@"ytplayer"]) {
  1231. // [self notifyDelegateOfYouTubeCallbackUrl:request.URL];
  1232. // return decisionHandler(WKNavigationActionPolicyCancel);
  1233. // } else if ([request.URL.scheme isEqual: @"http"] || [request.URL.scheme isEqual:@"https"]) {
  1234. // if( [self handleHttpNavigationToUrl:request.URL])
  1235. // return decisionHandler(WKNavigationActionPolicyAllow);
  1236. // else
  1237. // return decisionHandler(WKNavigationActionPolicyCancel);
  1238. // }
  1239. // return decisionHandler(WKNavigationActionPolicyAllow);
  1240. }
  1241. //
  1242. //- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
  1243. // NSLog(@"%s",__func__);
  1244. // @synchronized (self) {
  1245. // [self.wkwebView.scrollView hideEmpty];
  1246. // if (self.firstLoad && !self.hud) {
  1247. // self.hud = [RAProgressHUD showHUDOnView:self.ra_viewController.view];
  1248. // }
  1249. // if (self.delegate && [self.delegate respondsToSelector:@selector(webviewDidStarLoading:)]) {
  1250. // [self.delegate webviewDidStarLoading:self];
  1251. // }
  1252. // }
  1253. //}
  1254. //
  1255. //- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {
  1256. // NSLog(@"%s",__func__);
  1257. // NSLog(@"error: %@", error.localizedDescription);
  1258. // @synchronized (self) {
  1259. // if (self.hud) {
  1260. // [self.hud dismiss];
  1261. // self.hud = nil;
  1262. // }
  1263. //
  1264. // if (self.delegate && [self.delegate respondsToSelector:@selector(webviewDidFinishLoading:error:)]) {
  1265. // [self.delegate webviewDidFinishLoading:self error:error];
  1266. // }
  1267. //
  1268. // // NSURL *bundleURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
  1269. // // [webView loadHTMLString:@"<html></html>" baseURL:bundleURL];
  1270. //
  1271. // [self.wkwebView.scrollView showEmpty];
  1272. // }
  1273. //}
  1274. //
  1275. //- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
  1276. //{
  1277. // NSLog(@"%s",__func__);
  1278. // @synchronized (self) {
  1279. // if (self.hud) {
  1280. // [self.hud dismiss];
  1281. // self.hud = nil;
  1282. // self.firstLoad = NO;
  1283. // }
  1284. //
  1285. // if (self.delegate && [self.delegate respondsToSelector:@selector(webviewDidFinishLoading:error:)]) {
  1286. // [self.delegate webviewDidFinishLoading:self error:nil];
  1287. // }
  1288. //
  1289. // // self.wkwebView.scrollView.scrollEnabled = NO;
  1290. // [webView evaluateJavaScript:@"document.body.scrollHeight" completionHandler:^(id result, NSError *_Nullable error) {
  1291. //
  1292. // NSLog(@"web page height:%@",result);
  1293. // //result 就是加载完成后 webView的实际高度
  1294. // //获取后返回重新布局
  1295. //
  1296. // }];
  1297. // }
  1298. //
  1299. //
  1300. //}
  1301. //
  1302. //- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation {
  1303. // NSLog(@"%s",__func__);
  1304. //}
  1305. //
  1306. //- (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView {
  1307. // NSLog(@"%s",__func__);
  1308. //}
  1309. //
  1310. //- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler
  1311. //{
  1312. // NSLog(@"%s",__func__);
  1313. // decisionHandler(WKNavigationResponsePolicyAllow);
  1314. //}
  1315. @end