WFCUBrowserViewController.m 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. //
  2. // BrowserViewController.m
  3. // WildFireChat
  4. //
  5. // Created by heavyrain.lee on 2018/5/15.
  6. // Copyright © 2018 WildFireChat. All rights reserved.
  7. //
  8. #import <WebKit/WebKit.h>
  9. #import "WFCUBrowserViewController.h"
  10. #import "WFCUForwardViewController.h"
  11. #import <WFChatClient/WFCChatClient.h>
  12. #import "dsbridge.h"
  13. #import "WFCUConfigManager.h"
  14. #import "WFCUContactListViewController.h"
  15. @interface WFCUBrowserViewController ()
  16. @property (nonatomic, strong)DWKWebView *webView;
  17. @property(nonatomic, strong)NSMutableDictionary<NSString *, NSNumber *> *configDict;
  18. @end
  19. @implementation WFCUBrowserViewController
  20. /*
  21. 野火开放平台,从JS调用原生代码有2种形式:
  22. 一种是异步形式,参数带有completion:(JSCallback)completionHandler,返回结果使用completionHandler回调回去。请参考getAuthCode:completion:方法;
  23. 另外一种是同步形式,直接返回数据。注意同步接口一定要返回参数,如果没有有效返回数据,请返回nil,不要用void函数。请参考 config: 方法
  24. */
  25. - (void)viewDidLoad {
  26. [super viewDidLoad];
  27. self.configDict = [[NSMutableDictionary alloc] init];
  28. self.webView = [[DWKWebView alloc] initWithFrame:self.view.bounds];
  29. [self.view addSubview:self.webView];
  30. [self.webView addJavascriptObject:self namespace:nil];
  31. #ifdef DEBUG
  32. [self.webView setDebugMode:YES];
  33. #endif
  34. if(self.url.length) {
  35. NSString *encodedString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)self.url, (CFStringRef)@"!$&'()*+,-./:;=?@_~%#[]", NULL, kCFStringEncodingUTF8));
  36. if (![NSURL URLWithString:encodedString].scheme) {
  37. encodedString = [@"http://" stringByAppendingString:encodedString];
  38. }
  39. [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:encodedString]]];
  40. if(!self.hidenOpenInBrowser) {
  41. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"..." style:UIBarButtonItemStyleDone target:self action:@selector(onRightBtn:)];
  42. }
  43. } else {
  44. [self.webView loadHTMLString:self.htmlString baseURL:nil];
  45. }
  46. }
  47. - (void)onRightBtn:(id)sender {
  48. UIAlertController* alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  49. __weak typeof(self)ws = self;
  50. // Create cancel action.
  51. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:WFCString(@"Cancel") style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  52. }];
  53. [alertController addAction:cancelAction];
  54. UIAlertAction *openInBrowserAction = [UIAlertAction actionWithTitle:WFCString(@"OpenInBrowser") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  55. if (@available(iOS 10, *)) {
  56. [[UIApplication sharedApplication] openURL:[[NSURL alloc] initWithString:ws.url] options:@{} completionHandler:nil];
  57. } else {
  58. [[UIApplication sharedApplication] openURL:[[NSURL alloc] initWithString:ws.url]];
  59. }
  60. [ws.navigationController popViewControllerAnimated:NO];
  61. }];
  62. [alertController addAction:openInBrowserAction];
  63. UIAlertAction *sendToFriendAction = [UIAlertAction actionWithTitle:WFCString(@"SendToFriend") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  64. WFCUForwardViewController *controller = [[WFCUForwardViewController alloc] init];
  65. WFCCLinkMessageContent *link = [[WFCCLinkMessageContent alloc] init];
  66. link.title = ws.webView.title;
  67. link.url = ws.webView.URL.absoluteString;
  68. link.thumbnailUrl = [NSString stringWithFormat:@"%@://%@/favicon.ico", ws.webView.URL.scheme, ws.webView.URL.host];
  69. WFCCMessage *msg = [[WFCCMessage alloc] init];
  70. msg.content = link;
  71. controller.message = msg;
  72. UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:controller];
  73. [ws.navigationController presentViewController:navi animated:YES completion:nil];
  74. }];
  75. [alertController addAction:sendToFriendAction];
  76. if(NSClassFromString(@"SDTimeLineTableViewController")) {
  77. UIAlertAction *sendToMomentsAction = [UIAlertAction actionWithTitle:WFCString(@"SendToMoments") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  78. }];
  79. [alertController addAction:sendToMomentsAction];
  80. }
  81. [self.navigationController presentViewController:alertController animated:YES completion:nil];
  82. }
  83. - (void)didReceiveMemoryWarning {
  84. [super didReceiveMemoryWarning];
  85. // Dispose of any resources that can be recreated.
  86. }
  87. - (void)getAuthCode:(NSDictionary *)message completion:(JSCallback)completionHandler {
  88. NSString *appId = message[@"appId"];
  89. int appType = [message[@"appType"] intValue];
  90. [[WFCCIMService sharedWFCIMService] getAuthCode:appId type:appType host:self.webView.URL.host success:^(NSString *authCode) {
  91. completionHandler(0, authCode,YES);
  92. } error:^(int error_code) {
  93. completionHandler(error_code, nil,YES);
  94. }];
  95. }
  96. - (id)openUrl:(NSString *)url {
  97. WFCUBrowserViewController *browser = [[WFCUBrowserViewController alloc] init];
  98. browser.url = url;
  99. browser.hidesBottomBarWhenPushed = YES;
  100. [self.navigationController pushViewController:browser animated:YES];
  101. return nil;
  102. }
  103. - (void)close:(NSDictionary *)message completion:(JSCallback)completionHandler {
  104. [self.navigationController popoverPresentationController];
  105. completionHandler(0, nil, YES);
  106. }
  107. - (id)config:(NSDictionary *)message {
  108. NSString *appId = message[@"appId"];
  109. int appType = [message[@"apptype"] intValue];
  110. int64_t timestamp = [message[@"timestamp"] longLongValue];
  111. NSString *nonceStr = message[@"nonceStr"];
  112. NSString *signature = message[@"signature"];
  113. __weak typeof(self)ws = self;
  114. [[WFCCIMService sharedWFCIMService] configApplication:appId type:appType timestamp:timestamp nonce:nonceStr signature:signature success:^{
  115. if(ws.webView.URL.host)
  116. [ws.configDict setObject:@(YES) forKey:ws.webView.URL.host];
  117. [ws.webView callHandler:@"ready" arguments:nil];
  118. } error:^(int error_code) {
  119. if(ws.webView.URL.host)
  120. [ws.configDict removeObjectForKey:ws.webView.URL.host];
  121. [ws.webView callHandler:@"error" arguments:@[@(error_code)]];
  122. }];
  123. return nil;
  124. }
  125. - (void)chooseContacts:(NSDictionary *)message completion:(JSCallback)completionHandler {
  126. if(!self.webView.URL.host || ![self.configDict[self.webView.URL.host] boolValue]) {
  127. NSLog(@"Error host %@ not config!", self.webView.URL.host);
  128. completionHandler(1, nil, YES);
  129. return;
  130. }
  131. int max = [message[@"max"] intValue];
  132. WFCUContactListViewController *contactVC = [[WFCUContactListViewController alloc] init];
  133. if(max > 0) {
  134. contactVC.multiSelect = YES;
  135. contactVC.maxSelectCount = max;
  136. }
  137. contactVC.selectContact = YES;
  138. contactVC.isPushed = YES;
  139. contactVC.selectResult = ^(NSArray<NSString *> *contacts) {
  140. if(contacts.count) {
  141. NSMutableArray *output = [[NSMutableArray alloc] init];
  142. [contacts enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  143. WFCCUserInfo *userInfo = [[WFCCIMService sharedWFCIMService] getUserInfo:obj refresh:NO];
  144. if(userInfo) {
  145. [output addObject:@{@"uid":userInfo.userId, @"displayName":userInfo.displayName}];
  146. } else {
  147. [output addObject:@{@"uid":obj}];
  148. }
  149. }];
  150. completionHandler(0, output, YES);
  151. } else {
  152. completionHandler(1, nil, YES);
  153. }
  154. };
  155. [self.navigationController pushViewController:contactVC animated:YES];
  156. }
  157. - (id)toast:(NSDictionary *)message {
  158. NSLog(@"toast: %@", message);
  159. return nil;
  160. }
  161. - (void)didMoveToParentViewController:(UIViewController *)parent {
  162. if(!parent) {
  163. [self.webView removeJavascriptObject:nil];
  164. }
  165. }
  166. @end