WKWebTestViewController.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //
  2. // ViewController1.m
  3. // JS_OC_MessageHandler
  4. //
  5. // Created by Ray on 2018/11/22.
  6. // Copyright © 2018 Haley. All rights reserved.
  7. //
  8. #import "WKWebTestViewController.h"
  9. #import "RAWKWebView.h"
  10. #import "CRMUser.h"
  11. #import "RADataProvider.h"
  12. #import "NSString+RAJavascript.h"
  13. #import "CRMRemoteNotificationBroadcast.h"
  14. #import "config.h"
  15. #import "AppDelegate.h"
  16. @interface WKWebTestViewController () <RAWebViewDelegate>
  17. @property (weak, nonatomic) IBOutlet RAWKWebView *webview;
  18. @property (nonatomic,assign) BOOL loaded;
  19. @property (nonatomic,strong) NSString* notificationstr;
  20. @end
  21. @implementation WKWebTestViewController
  22. - (void)viewDidLoad {
  23. [super viewDidLoad];
  24. self.navigationController.navigationBar.hidden =true;
  25. [UIApplication sharedApplication].keyWindow.backgroundColor = [UIColor whiteColor];
  26. if (@available(iOS 11, *)) {
  27. self.webview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  28. } else {
  29. self.automaticallyAdjustsScrollViewInsets = NO;
  30. }
  31. if (!_url) {
  32. _url = URL_LOGIN;
  33. }
  34. self.webview.delegate = self;
  35. [self.webview LoadFromURL:_url];
  36. self.webview.JumpTo = ^(NSString* url, NSString* module){
  37. UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:module];
  38. [vc performSelector:@selector(setUrl:) withObject:url];
  39. [self.navigationController pushViewController:vc animated:YES];
  40. };
  41. AppDelegate *delagate =(AppDelegate *) [[UIApplication sharedApplication]delegate];
  42. NSMutableDictionary* aps =[delagate.launchNotification[@"aps"] mutableCopy];
  43. aps[@"launch_app"] =delagate.launchNotification[@"launch_app"];
  44. self.notificationstr = [RAConvertor dict2string:aps];
  45. [self regirsterBroadcast];
  46. }
  47. - (void)dealloc {
  48. [self unregisterBroadcast];
  49. }
  50. - (void)regirsterBroadcast {
  51. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveBroadCast:) name:CRM_REMOTE_NOTIFICATION_RECEVIED object:nil];
  52. }
  53. - (void)unregisterBroadcast {
  54. [[NSNotificationCenter defaultCenter] removeObserver:self];
  55. }
  56. #pragma mark - WebViewDelegate
  57. - (void)webview:(RAWKWebView *)webview didLogin:(NSString *)user password:(NSString *)password {
  58. [[CRMUser sharedUser] loginUser:user password:password];
  59. if (CRMUser.sharedUser.shouldUploadToken) {
  60. [RADataProvider uploadDeviceToken];
  61. }
  62. }
  63. - (void)webviewDidLogout:(RAWKWebView *)webview {
  64. [[CRMUser sharedUser] logout];
  65. }
  66. - (void)webviewDidStarLoading:(RAWKWebView *)webview {
  67. @synchronized (self) {
  68. self.loaded = NO;
  69. }
  70. }
  71. - (void)webviewDidFinishLoading:(RAWKWebView *)webview error:(NSError *)error {
  72. @synchronized (self) {
  73. self.loaded = !error;
  74. if (self.loaded) {
  75. [self readNotificationNow:self.notificationstr];
  76. }
  77. }
  78. }
  79. #pragma mark - Notification
  80. - (void)handleNotificationByJs:(NSString*)notificationstr {
  81. if(notificationstr.length==0)
  82. return;
  83. NSString *user = CRMUser.sharedUser.user;
  84. NSString *password = CRMUser.sharedUser.password;
  85. user = [user ra_stringByEscapingForJavascriptWithDelimiter:'\'' wrapWithDelimiters:YES];
  86. password = [password ra_stringByEscapingForJavascriptWithDelimiter:'\'' wrapWithDelimiters:YES];
  87. NSString *jsFunction = [NSString stringWithFormat:@"showPageForNotification(%@, %@, %@)", user, password, notificationstr];
  88. [self.webview evaluateJs:jsFunction completionHandler:^(id result, NSError *error) {
  89. NSLog(@"handleNotificationByJs: Result: %@ Error: %@", result, error.localizedDescription);
  90. }];
  91. }
  92. // 当前ViewController是WebVC
  93. - (void)readNotificationNow:(NSString*)notificationstr {
  94. if(notificationstr.length==0)
  95. return;
  96. // if (notificationId == -1) {
  97. // return;
  98. // }
  99. // self.notificationId = -1;
  100. if (!CRMUser.sharedUser.isLogin) {
  101. UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Warning" message:@"you should login first" preferredStyle:UIAlertControllerStyleAlert];
  102. UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  103. }];
  104. [alertVC addAction:okAction];
  105. [self presentViewController:alertVC animated:YES completion:nil];
  106. return;
  107. }
  108. [self handleNotificationByJs:notificationstr];
  109. }
  110. - (void)receiveBroadCast:(NSNotification *)notification {
  111. if (self.navigationController.presentingViewController) {
  112. [self.navigationController.presentingViewController dismissViewControllerAnimated:YES completion:nil];
  113. }
  114. if (self.navigationController.topViewController != self) {
  115. [self.navigationController popToViewController:self animated:YES];
  116. }
  117. NSMutableDictionary *aps = [[notification.userInfo objectForKey:@"aps"] mutableCopy];
  118. aps[@"foreground"]= notification.userInfo[@"foreground"];
  119. NSString* notificationstr = [RAConvertor dict2string:aps];
  120. // NSInteger notificationId = [[aps objectForKey:@"id"] integerValue];
  121. @synchronized (self) {
  122. if (self.loaded) {
  123. [self readNotificationNow:notificationstr];
  124. } else {
  125. self.notificationstr = notificationstr;
  126. }
  127. }
  128. }
  129. @end