newsDetailVController.m 16 KB

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