| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- //
- // ViewController1.m
- // JS_OC_MessageHandler
- //
- // Created by Ray on 2018/11/22.
- // Copyright © 2018 Haley. All rights reserved.
- //
- #import "WKWebTestViewController.h"
- #import "RAWKWebView.h"
- #import "CRMUser.h"
- #import "RADataProvider.h"
- #import "NSString+RAJavascript.h"
- #import "CRMRemoteNotificationBroadcast.h"
- #import "config.h"
- #import "AppDelegate.h"
- @interface WKWebTestViewController () <RAWebViewDelegate>
- @property (weak, nonatomic) IBOutlet RAWKWebView *webview;
- @property (nonatomic,assign) BOOL loaded;
- @property (nonatomic,strong) NSString* notificationstr;
- @end
- @implementation WKWebTestViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
-
-
-
-
- self.navigationController.navigationBar.hidden =true;
-
- [UIApplication sharedApplication].keyWindow.backgroundColor = [UIColor whiteColor];
-
- if (@available(iOS 11, *)) {
- self.webview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
- } else {
- self.automaticallyAdjustsScrollViewInsets = NO;
- }
-
- if (!_url) {
- _url = URL_LOGIN;
- }
-
- self.webview.delegate = self;
- [self.webview LoadFromURL:_url];
- self.webview.JumpTo = ^(NSString* url, NSString* module){
- UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:module];
- [vc performSelector:@selector(setUrl:) withObject:url];
- [self.navigationController pushViewController:vc animated:YES];
- };
- AppDelegate *delagate =(AppDelegate *) [[UIApplication sharedApplication]delegate];
- NSMutableDictionary* aps =[delagate.launchNotification[@"aps"] mutableCopy];
- aps[@"launch_app"] =delagate.launchNotification[@"launch_app"];
- self.notificationstr = [RAConvertor dict2string:aps];
- [self regirsterBroadcast];
- }
- - (void)dealloc {
- [self unregisterBroadcast];
- }
- - (void)regirsterBroadcast {
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveBroadCast:) name:CRM_REMOTE_NOTIFICATION_RECEVIED object:nil];
- }
- - (void)unregisterBroadcast {
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- }
- #pragma mark - WebViewDelegate
- - (void)webview:(RAWKWebView *)webview didLogin:(NSString *)user password:(NSString *)password {
- [[CRMUser sharedUser] loginUser:user password:password];
- if (CRMUser.sharedUser.shouldUploadToken) {
- [RADataProvider uploadDeviceToken];
- }
- }
- - (void)webviewDidLogout:(RAWKWebView *)webview {
- [[CRMUser sharedUser] logout];
- }
- - (void)webviewDidStarLoading:(RAWKWebView *)webview {
- @synchronized (self) {
- self.loaded = NO;
- }
- }
- - (void)webviewDidFinishLoading:(RAWKWebView *)webview error:(NSError *)error {
- @synchronized (self) {
- self.loaded = !error;
- if (self.loaded) {
- [self readNotificationNow:self.notificationstr];
- }
- }
- }
- #pragma mark - Notification
- - (void)handleNotificationByJs:(NSString*)notificationstr {
-
-
- if(notificationstr.length==0)
- return;
- NSString *user = CRMUser.sharedUser.user;
- NSString *password = CRMUser.sharedUser.password;
-
- user = [user ra_stringByEscapingForJavascriptWithDelimiter:'\'' wrapWithDelimiters:YES];
- password = [password ra_stringByEscapingForJavascriptWithDelimiter:'\'' wrapWithDelimiters:YES];
-
- NSString *jsFunction = [NSString stringWithFormat:@"showPageForNotification(%@, %@, %@)", user, password, notificationstr];
- [self.webview evaluateJs:jsFunction completionHandler:^(id result, NSError *error) {
-
- NSLog(@"handleNotificationByJs: Result: %@ Error: %@", result, error.localizedDescription);
- }];
- }
- // 当前ViewController是WebVC
- - (void)readNotificationNow:(NSString*)notificationstr {
- if(notificationstr.length==0)
- return;
- // if (notificationId == -1) {
- // return;
- // }
- // self.notificationId = -1;
-
- if (!CRMUser.sharedUser.isLogin) {
-
- UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Warning" message:@"you should login first" preferredStyle:UIAlertControllerStyleAlert];
- UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
-
- }];
-
- [alertVC addAction:okAction];
-
- [self presentViewController:alertVC animated:YES completion:nil];
- return;
- }
-
- [self handleNotificationByJs:notificationstr];
- }
- - (void)receiveBroadCast:(NSNotification *)notification {
-
- if (self.navigationController.presentingViewController) {
- [self.navigationController.presentingViewController dismissViewControllerAnimated:YES completion:nil];
- }
-
- if (self.navigationController.topViewController != self) {
- [self.navigationController popToViewController:self animated:YES];
- }
-
- NSMutableDictionary *aps = [[notification.userInfo objectForKey:@"aps"] mutableCopy];
- aps[@"foreground"]= notification.userInfo[@"foreground"];
- NSString* notificationstr = [RAConvertor dict2string:aps];
- // NSInteger notificationId = [[aps objectForKey:@"id"] integerValue];
-
- @synchronized (self) {
- if (self.loaded) {
-
- [self readNotificationNow:notificationstr];
-
- } else {
-
- self.notificationstr = notificationstr;
- }
- }
- }
- @end
|