newsDetailVController.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. //
  2. // newsDetailVController.m
  3. // ZhongJingLianMeng
  4. //
  5. // Created by youke on 2018/12/3.
  6. // Copyright © 2018 youke. All rights reserved.
  7. //
  8. #import "newsDetailVController.h"
  9. #import "UIAlertController+LY.h"
  10. #import <JavaScriptCore/JavaScriptCore.h>
  11. #import <ShareSDK/ShareSDK.h>
  12. #import <ShareSDKUI/ShareSDK+SSUI.h>
  13. #import "ZLCWebView.h"
  14. #import<ShareSDKUI/SSUIShareActionSheetStyle.h>//设置简单分享菜单样式
  15. #import "logInViewController.h"
  16. #import "activityDetailVController.h"
  17. @interface newsDetailVController ()<ZLCWebViewDelegate>
  18. @property (nonatomic, strong) ZLCWebView *webView;
  19. @property(nonatomic,strong) JSContext *jsContext;
  20. @property(nonatomic,strong)UIButton *collectionBtn;
  21. @property(nonatomic,strong)UIButton *shareBtn;
  22. @property (strong, nonatomic) HttpRequestManger *sessionManager;
  23. @property(nonatomic,strong)NSString *collectionStaus;
  24. @end
  25. @implementation newsDetailVController
  26. - (HttpRequestManger*) sessionManager{
  27. if (!_sessionManager) {
  28. _sessionManager = [[HttpRequestManger alloc]init];
  29. }
  30. return _sessionManager;
  31. }
  32. -(void)viewWillAppear:(BOOL)animated{
  33. [super viewWillAppear:YES];
  34. if ([_index isEqualToString:@"0"]) {
  35. [self loadIsCollection:self.typeStr];
  36. }
  37. }
  38. static void extracted(newsDetailVController *object, NSMutableDictionary *parma, newsDetailVController *const __weak weakself) {
  39. [object.sessionManager sendPostHttpRequestWithUrl:isCollection params:parma bodyBlock:nil progress:nil success:^(NSDictionary *response) {
  40. weakself.collectionStaus = response[@"isCollected"];
  41. if ([weakself.collectionStaus isEqualToString:@"1"]) {
  42. [weakself.collectionBtn setImage:[UIImage imageNamed:@"collection"] forState:UIControlStateNormal];
  43. }else{
  44. [weakself.collectionBtn setImage:[UIImage imageNamed:@"NoCollection"] forState:UIControlStateNormal];
  45. }
  46. } failure:^(NSError *error) {
  47. NSLog(@"%@",error);
  48. }];
  49. }
  50. -(void)loadIsCollection:(NSString *)typeString{
  51. if ([typeString isEqualToString:@"0"]) {
  52. kWeakSelf(self);
  53. NSMutableDictionary *parma = [NSMutableDictionary dictionary];
  54. parma[@"id"] = weakself.newsId;
  55. parma[@"type"] = @"0";
  56. self.sessionManager = [[HttpRequestManger alloc]init];
  57. extracted(self, parma, weakself);
  58. }else{
  59. kWeakSelf(self);
  60. NSMutableDictionary *parma = [NSMutableDictionary dictionary];
  61. parma[@"id"] = weakself.newsId;
  62. parma[@"type"] = @"2";
  63. self.sessionManager = [[HttpRequestManger alloc]init];
  64. [self.sessionManager sendPostHttpRequestWithUrl:isCollection params:parma bodyBlock:nil progress:nil success:^(NSDictionary *response) {
  65. weakself.collectionStaus = response[@"isCollected"];
  66. if ([weakself.collectionStaus isEqualToString:@"1"]) {
  67. [weakself.collectionBtn setImage:[UIImage imageNamed:@"collection"] forState:UIControlStateNormal];
  68. }else{
  69. [weakself.collectionBtn setImage:[UIImage imageNamed:@"NoCollection"] forState:UIControlStateNormal];
  70. }
  71. } failure:^(NSError *error) {
  72. NSLog(@"%@",error);
  73. }];
  74. }
  75. }
  76. - (void)viewDidLoad {
  77. [super viewDidLoad];
  78. [self.navigationView removeAllLeftButton];
  79. self.navigationView.titleLabel.text = self.titleStr;
  80. self.navigationView.titleLabel.textColor = [UIColor whiteColor];
  81. self.view.backgroundColor = [UIColor whiteColor];
  82. kWeakSelf(self);
  83. [self.navigationView setNavigationBackgroundColor:[UIColor colorWithHexValue:0x3C915C]];
  84. [self.navigationView addLeftButtonWithImage:[UIImage imageNamed:@"back_white"] clickCallBack:^(UIView *view) {
  85. [weakself.navigationController popViewControllerAnimated:YES];
  86. }];
  87. UIView *rightView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
  88. rightView.backgroundColor = [UIColor colorWithHexValue:0x3C915C];
  89. if ([_index isEqualToString:@"0"]) {
  90. [self.navigationView addRightView:rightView clickCallback:nil];
  91. }
  92. _collectionBtn =[UIButton buttonWithType:UIButtonTypeCustom];
  93. [_collectionBtn setImage:[UIImage imageNamed:@"NoCollection"] forState:UIControlStateNormal];
  94. [_collectionBtn addTarget:self action:@selector(collectionClick:) forControlEvents:UIControlEventTouchUpInside];
  95. [rightView addSubview:_collectionBtn];
  96. [_collectionBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  97. make.size.mas_equalTo(CGSizeMake(40, 40));
  98. make.left.equalTo(rightView.mas_left).offset(15);
  99. make.top.equalTo(rightView.mas_top);
  100. }];
  101. _shareBtn =[UIButton buttonWithType:UIButtonTypeCustom];
  102. [_shareBtn setImage:[UIImage imageNamed:@"share"] forState:UIControlStateNormal];
  103. [_shareBtn addTarget:self action:@selector(shareClick:) forControlEvents:UIControlEventTouchUpInside];
  104. [rightView addSubview:_shareBtn];
  105. [_shareBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  106. make.size.mas_equalTo(CGSizeMake(40, 40));
  107. make.left.equalTo(_collectionBtn.mas_right).offset(5);
  108. make.top.equalTo(rightView.mas_top);
  109. }];
  110. if ([_index isEqualToString:@"1"]) {
  111. self.webView = [[ZLCWebView alloc] initWithFrame:CGRectMake(0,isFullScreen?40:30, kGScreenWidth,isFullScreen?kGScreenHeight-40:kGScreenHeight-30)];
  112. }else{
  113. self.webView = [[ZLCWebView alloc] initWithFrame:CGRectMake(0,isFullScreen? NaviHeight -70:IS_IPHONE_4_OR_LESS? NaviHeight -55 :NaviHeight -60, kGScreenWidth,isFullScreen? kGScreenHeight- NaviHeight+70 :IS_IPHONE_4_OR_LESS? kGScreenHeight- NaviHeight+55: kGScreenHeight- NaviHeight+60)];
  114. }
  115. if (@available(iOS 11.0, *)) {
  116. self.webView.wkWebView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  117. }else {
  118. self.automaticallyAdjustsScrollViewInsets = NO;
  119. }
  120. self.webView.delegate = self;
  121. [self.view addSubview:self.webView];
  122. [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.urlStr]]];
  123. self.webView.wkWebView.scrollView.backgroundColor = [UIColor whiteColor];
  124. self.webView.wkWebView.scrollView.showsVerticalScrollIndicator = NO;
  125. // self.webView.wkWebView.scrollView.showsHorizontalScrollIndicator = NO;
  126. _webView.wkWebView.scrollView.bounces = NO;
  127. // Do any additional setup after loading the view.
  128. }
  129. -(void)collectionClick:(UIButton *)sender{
  130. if (!IsStrEmpty([DKGlobal sharedInstance].token)) {
  131. kWeakSelf(self);
  132. if (![weakself.collectionStaus isEqualToString:@"0"]) {
  133. NSMutableDictionary *parma = [NSMutableDictionary dictionary];
  134. parma[@"id"] = weakself.newsId;
  135. if ([_typeStr isEqualToString:@"2"]) {
  136. parma[@"collectType"] = @"2";
  137. }else{
  138. parma[@"collectType"] = @"0";
  139. }
  140. self.sessionManager = [[HttpRequestManger alloc]init];
  141. [self.sessionManager sendPostHttpRequestWithUrl:cancelCollection params:parma bodyBlock:nil progress:nil success:^(NSDictionary *response) {
  142. [MBProgressHUD showSuccess:@"取消收藏"];
  143. weakself.collectionStaus = @"0";
  144. [weakself.collectionBtn setImage:[UIImage imageNamed:@"NoCollection"] forState:UIControlStateNormal];
  145. } failure:^(NSError *error) {
  146. NSLog(@"%@",error);
  147. }];
  148. }else{
  149. kWeakSelf(self);
  150. NSMutableDictionary *parma = [NSMutableDictionary dictionary];
  151. parma[@"id"] = weakself.newsId;
  152. if ([_typeStr isEqualToString:@"2"]) {
  153. parma[@"type"] = @"2";
  154. }else{
  155. parma[@"type"] = @"0";
  156. }
  157. self.sessionManager = [[HttpRequestManger alloc]init];
  158. [self.sessionManager sendPostHttpRequestWithUrl:collectionNews params:parma bodyBlock:nil progress:nil success:^(NSDictionary *response) {
  159. [MBProgressHUD showSuccess:@"收藏成功"];
  160. weakself.collectionStaus = @"1";
  161. [weakself.collectionBtn setImage:[UIImage imageNamed:@"collection"] forState:UIControlStateNormal];
  162. } failure:^(NSError *error) {
  163. NSLog(@"%@",error);
  164. }];
  165. }
  166. }else{
  167. [MBProgressHUD showError:@"请先登录"];
  168. logInViewController *loginVC = [[logInViewController alloc]init];
  169. [self.navigationController pushViewController:loginVC animated:YES];
  170. }
  171. }
  172. - (void)zlcwebViewDidStartLoad:(ZLCWebView *)webview
  173. {
  174. NSString *requestString = webview.wkWebView.URL.absoluteString;
  175. if ([requestString containsString:@"activity://"]) {
  176. NSString *projectID = [requestString stringByReplacingOccurrencesOfString:@"activity://"withString:@""];
  177. NSLog(@"projectID=%@",projectID);
  178. activityDetailVController *activityDetail = [[activityDetailVController alloc]init];
  179. activityDetail.newsId = projectID;
  180. activityDetail.titleStr = @"活动详情";
  181. activityDetail.shareTitle =@"活动详情";
  182. activityDetail.index =@"0";
  183. activityDetail.urlStr = [NSString stringWithFormat:@"%@/applyDetail?id=%@&move=ios",baseH5,projectID];
  184. activityDetail.shareUrl = [NSString stringWithFormat:@"%@/applyDetail?id=%@",baseH5,projectID];
  185. [self.navigationController pushViewController:activityDetail animated:YES];
  186. }
  187. [SVProgressHUD show];
  188. }
  189. - (void)zlcwebView:(ZLCWebView *)webview shouldStartLoadWithURL:(NSURL *)URL
  190. {
  191. NSLog(@"截取到URL:%@",URL);
  192. }
  193. - (void)zlcwebView:(ZLCWebView *)webview didFinishLoadingURL:(NSURL *)URL
  194. {
  195. [SVProgressHUD dismiss];
  196. NSLog(@"页面加载完成%@",URL);
  197. }
  198. - (void)zlcwebView:(ZLCWebView *)webview didFailToLoadURL:(NSURL *)URL error:(NSError *)error
  199. {
  200. [SVProgressHUD dismiss];
  201. NSLog(@"加载出现错误");
  202. }
  203. //}
  204. -(void)viewWillDisappear:(BOOL)animated{
  205. [super viewWillDisappear:YES];
  206. _webView = nil;
  207. [self cleanCacheAndCookie];
  208. }
  209. /**清除缓存和cookie*/
  210. - (void)cleanCacheAndCookie{
  211. //清除cookies
  212. NSHTTPCookie *cookie;
  213. NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
  214. for (cookie in [storage cookies]){
  215. [storage deleteCookie:cookie];
  216. }
  217. //清除UIWebView的缓存
  218. [[NSURLCache sharedURLCache] removeAllCachedResponses];
  219. NSURLCache * cache = [NSURLCache sharedURLCache];
  220. [cache removeAllCachedResponses];
  221. [cache setDiskCapacity:0];
  222. [cache setMemoryCapacity:0];
  223. }
  224. -(void)shareClick:(UIButton *)sender{
  225. // AppIcon projectItem
  226. NSArray *arr =@[[UIImage imageNamed:@"AppIcon"]];
  227. if (IsStrEmpty(_shareTitle)) {
  228. _shareTitle = @"";
  229. }
  230. if (arr) {
  231. if (IsStrEmpty(self.ImgUrl)){
  232. self.ImgUrl = @"https://img2018.cnblogs.com/blog/1058389/201812/1058389-20181211200433415-1810829224.png";
  233. }
  234. NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
  235. [shareParams SSDKSetupShareParamsByText:@"中经联盟资讯"
  236. images:self.ImgUrl
  237. url:[NSURL URLWithString:[NSString stringWithFormat:@"%@",_shareUrl]]
  238. title:_shareTitle
  239. type:SSDKContentTypeWebPage];
  240. // [shareParams SSDKEnableUseClientShare];
  241. // [SSUIShareActionSheetStyle setShareActionSheetStyle:(ShareActionSheetStyle)ShareActionSheetStyleSimple];
  242. [ShareSDK showShareActionSheet:nil //要显示菜单的视图, iPad版中此参数作为弹出菜单的参照视图,只有传这个才可以弹出我们的分享菜单,可以传分享的按钮对象或者自己创建小的view 对象,iPhone可以传nil不会影响
  243. items:@[@(SSDKPlatformSubTypeWechatSession),@(SSDKPlatformSubTypeWechatTimeline)]
  244. shareParams:shareParams
  245. onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {
  246. switch (platformType) {
  247. case SSDKPlatformSubTypeWechatSession:
  248. break;
  249. case SSDKPlatformSubTypeWechatTimeline:
  250. break;
  251. default:
  252. break;
  253. }
  254. switch (state) {
  255. case SSDKResponseStateSuccess:
  256. // {
  257. // UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享成功"
  258. // message:nil
  259. // delegate:nil
  260. // cancelButtonTitle:@"确定"
  261. // otherButtonTitles:nil];
  262. // [alertView show];
  263. break;
  264. // }
  265. case SSDKResponseStateFail:
  266. // {
  267. // UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失败"
  268. // message:nil
  269. // delegate:nil
  270. // cancelButtonTitle:@"OK"
  271. // otherButtonTitles:nil, nil];
  272. // [alert show];
  273. //
  274. //
  275. // NSLog(@"%@",error);
  276. break;
  277. // }
  278. default:
  279. break;
  280. }
  281. }
  282. ];
  283. }
  284. }
  285. - (void)dealloc {
  286. if (_sessionManager) {
  287. [_sessionManager cancelAllTask];
  288. }
  289. [[NSNotificationCenter defaultCenter] removeObserver:self];
  290. DebugLog(@"%@没有造成循环引用", [self class]);
  291. }
  292. - (void)didReceiveMemoryWarning {
  293. [super didReceiveMemoryWarning];
  294. // Dispose of any resources that can be recreated.
  295. }
  296. /*
  297. #pragma mark - Navigation
  298. // In a storyboard-based application, you will often want to do a little preparation before navigation
  299. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  300. // Get the new view controller using [segue destinationViewController].
  301. // Pass the selected object to the new view controller.
  302. }
  303. */
  304. @end