口袋优选

KBWebDetailController.m 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. //
  2. // KBWebDetailController.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/5/29.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "KBWebDetailController.h"
  9. #import <WebKit/WebKit.h>
  10. #import "WYWeakScriptMessageDelegate.h"
  11. #import "KBLoginViewController.h"
  12. #import "WechatShareTool.h"
  13. #import <AlibcTradeSDK/AlibcTradeSDK.h>
  14. #import <AlibabaAuthSDK/ALBBSession.h>
  15. #import <AlibabaAuthSDK/ALBBSDK.h>
  16. static NSString *app_h5_close = @"app_h5_close";
  17. static NSString *app_h5_get_token = @"app_h5_get_token";
  18. static NSString *app_h5_wx_share = @"app_h5_wx_share";
  19. static NSString *app_h5_get_wechat_info = @"app_h5_get_wechat_info";
  20. static NSString *refresh_h5 = @"refresh_h5";
  21. static NSString *app_h5_login = @"app_h5_login"; //h5唤起原生的登录
  22. static NSString *app_h5_wx_share_url = @"app_h5_wx_share_url"; //分享url
  23. static NSString *app_h5_wx_share_image = @"app_h5_wx_share_image"; //分享到微信image
  24. static NSString *app_h5_wx_share_video = @"app_h5_wx_share_video"; //分享到微信video
  25. static NSString *app_h5_wx_share_min = @"app_h5_wx_share_min"; //分享微信小程序(只支持微信好友)
  26. static NSString *app_h5_open_taobao_url = @"app_h5_open_taobao_url"; //打开淘宝url
  27. @interface KBWebDetailController ()<WKUIDelegate, WKNavigationDelegate,WKScriptMessageHandler>
  28. @property (nonatomic, strong) WKWebView *webView;
  29. /** 加载进度条 */
  30. @property (nonatomic, strong) UIProgressView *progressView;
  31. @property (nonatomic, strong) UIButton *backButton;
  32. @property (nonatomic, strong) UIButton *closeButton;
  33. @end
  34. @implementation KBWebDetailController
  35. - (void)viewWillAppear:(BOOL)animated {
  36. [super viewWillAppear:animated];
  37. [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
  38. }
  39. - (void)viewDidLoad {
  40. [super viewDidLoad];
  41. self.navigationController.navigationBar.hidden = YES;
  42. [self addNotification];
  43. [self configNavigationBar];
  44. [self addUserhandler];
  45. [self loadRequest];
  46. }
  47. - (void)addNotification {
  48. [[NSNotificationCenter defaultCenter] addObserver:self
  49. selector:@selector(refreshH5Action)
  50. name:UIApplicationDidBecomeActiveNotification object:nil];
  51. }
  52. - (void)configNavigationBar {
  53. self.view.backgroundColor = [UIColor whiteColor];
  54. [self.view addSubview:self.webView];
  55. [self.view addSubview:self.progressView];
  56. self.navigationBar.showNavigationBarBottomLine = YES;
  57. // [self.navigationBar setNavTitle:@"商品详情"];
  58. [self.navigationBar setCustomLeftButtons:@[self.backButton]];
  59. self.navigationBar.backgroundColor = [UIColor clearColor];
  60. if (@available(iOS 11.0, *)) {
  61. self.webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;//UIScrollView也适用
  62. }else {
  63. self.automaticallyAdjustsScrollViewInsets = NO;
  64. }
  65. }
  66. - (void)loadRequest {
  67. if (@available(iOS 9.0, *)) {
  68. NSArray * types=@[WKWebsiteDataTypeCookies,WKWebsiteDataTypeLocalStorage];
  69. NSSet *websiteDataTypes= [NSSet setWithArray:types];
  70. NSDate *dateFrom = [NSDate dateWithTimeIntervalSince1970:0];
  71. [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:websiteDataTypes modifiedSince:dateFrom completionHandler:^{
  72. }];
  73. }
  74. // NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.url]];
  75. NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.url]
  76. cachePolicy:NSURLRequestReloadIgnoringCacheData
  77. timeoutInterval:15.0];
  78. [self.webView loadRequest:theRequest];
  79. }
  80. - (void)addUserhandler {
  81. [[self.webView configuration].userContentController addScriptMessageHandler:[[WYWeakScriptMessageDelegate alloc] initWithDelegate:self] name:app_h5_close];
  82. [[self.webView configuration].userContentController addScriptMessageHandler:[[WYWeakScriptMessageDelegate alloc] initWithDelegate:self] name:app_h5_get_token];
  83. [[self.webView configuration].userContentController addScriptMessageHandler:[[WYWeakScriptMessageDelegate alloc] initWithDelegate:self] name:app_h5_wx_share];
  84. [[self.webView configuration].userContentController addScriptMessageHandler:[[WYWeakScriptMessageDelegate alloc] initWithDelegate:self] name:app_h5_get_wechat_info];
  85. [[self.webView configuration].userContentController addScriptMessageHandler:[[WYWeakScriptMessageDelegate alloc] initWithDelegate:self] name:app_h5_login];
  86. [[self.webView configuration].userContentController addScriptMessageHandler:[[WYWeakScriptMessageDelegate alloc] initWithDelegate:self] name:app_h5_wx_share_url];
  87. [[self.webView configuration].userContentController addScriptMessageHandler:[[WYWeakScriptMessageDelegate alloc] initWithDelegate:self] name:app_h5_wx_share_image];
  88. [[self.webView configuration].userContentController addScriptMessageHandler:[[WYWeakScriptMessageDelegate alloc] initWithDelegate:self] name:app_h5_wx_share_video];
  89. [[self.webView configuration].userContentController addScriptMessageHandler:[[WYWeakScriptMessageDelegate alloc] initWithDelegate:self] name:app_h5_wx_share_min];
  90. [[self.webView configuration].userContentController addScriptMessageHandler:[[WYWeakScriptMessageDelegate alloc] initWithDelegate:self] name:app_h5_open_taobao_url];
  91. }
  92. #pragma mark --- WKScriptMessageHandler
  93. - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
  94. if ([message.name isEqualToString:app_h5_close]) {
  95. //关闭页面
  96. [self.navigationController popViewControllerAnimated:YES];
  97. }
  98. else if ([message.name isEqualToString:app_h5_get_token]){
  99. //传token给js
  100. [self sendUserTokenToJS:message];
  101. }
  102. else if ([message.name isEqualToString:app_h5_wx_share]){
  103. //调起原生分享
  104. [self shareToNative:message];
  105. }
  106. else if ([message.name isEqualToString:app_h5_get_wechat_info]){
  107. //获取微信信息
  108. [self getWechatInfoWithMessage:message];
  109. }
  110. else if ([message.name isEqualToString:app_h5_wx_share_image]){
  111. //分享图片
  112. [self shareImage:message];
  113. }
  114. else if ([message.name isEqualToString:app_h5_wx_share_url]){
  115. //分享url
  116. [self shareURL:message];
  117. }
  118. else if ([message.name isEqualToString:app_h5_wx_share_video]){
  119. //分享video
  120. [self shareVideo:message];
  121. }
  122. else if ([message.name isEqualToString:app_h5_wx_share_min]){
  123. //分享小程序
  124. [self shareMinProgram:message];
  125. }else if ([message.name isEqualToString:app_h5_login]){
  126. //获取token
  127. [self gotoNativeLogin:message];
  128. }else if ([message.name isEqualToString:app_h5_open_taobao_url]){
  129. //打开淘宝
  130. [self openTaobao:message];
  131. }
  132. else {
  133. }
  134. }
  135. - (void)refreshH5Action {
  136. if (@available(iOS 9.0, *)) {
  137. NSArray * types=@[WKWebsiteDataTypeCookies,WKWebsiteDataTypeLocalStorage];
  138. NSSet *websiteDataTypes= [NSSet setWithArray:types];
  139. NSDate *dateFrom = [NSDate dateWithTimeIntervalSince1970:0];
  140. [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:websiteDataTypes modifiedSince:dateFrom completionHandler:^{
  141. }];
  142. }
  143. NSString *callBacKMethod = [NSString stringWithFormat:@"%@()",refresh_h5];
  144. [self.webView evaluateJavaScript:callBacKMethod completionHandler:^(id _Nullable value, NSError * _Nullable error) {
  145. }];
  146. }
  147. /**
  148. 传token给js
  149. */
  150. - (void)sendUserTokenToJS:(WKScriptMessage *)message {
  151. NSString *token = [AccountTool account].token;
  152. NSString *openid = [AccountTool account].openId;
  153. NSString *unionid = [AccountTool account].unionid;
  154. if (![AccountTool isLogin] || openid==nil || unionid ==nil) {
  155. KBLoginViewController *login = [[KBLoginViewController alloc] init];
  156. [self presentViewController:login animated:YES completion:nil];
  157. return;
  158. }
  159. NSDictionary *dict = [self dictionaryWithJsonString:message.body];
  160. NSString *jsMethod = dict[@"js_callback"];
  161. if (!token) {
  162. token = @"";
  163. }
  164. NSDictionary *tokenPara = @{@"token":token,
  165. @"openid":openid,
  166. @"unionid":unionid,
  167. };
  168. NSString *jsonDict = [self convertToJsonData:tokenPara];
  169. NSString *callBacKMethod = [NSString stringWithFormat:@"%@('%@')",jsMethod,jsonDict];
  170. [self.webView evaluateJavaScript:callBacKMethod completionHandler:^(id _Nullable value, NSError * _Nullable error) {
  171. }];
  172. }
  173. /**
  174. 唤起登录
  175. */
  176. - (void)gotoNativeLogin:(WKScriptMessage *)message {
  177. if (![AccountTool isLogin]) {
  178. KBLoginViewController *login = [[KBLoginViewController alloc] init];
  179. [self presentViewController:login animated:YES completion:nil];
  180. return;
  181. }
  182. NSDictionary *dict = [self dictionaryWithJsonString:message.body];
  183. NSString *jsMethod = dict[@"js_callback"];
  184. NSString *token = [AccountTool account].token;
  185. NSDictionary *param = @{@"token":token,
  186. };
  187. NSString *jsonDict = [self convertToJsonData:param];
  188. NSString *callBacKMethod = [NSString stringWithFormat:@"%@('%@')",jsMethod,jsonDict];
  189. [self.webView evaluateJavaScript:callBacKMethod completionHandler:^(id _Nullable value, NSError * _Nullable error) {
  190. }];
  191. }
  192. /**
  193. 获取微信信息
  194. */
  195. - (void)getWechatInfoWithMessage:(WKScriptMessage *)message {
  196. NSString *openid = [AccountTool account].openId;
  197. NSString *unionid = [AccountTool account].unionid;
  198. if (![AccountTool isLogin] || openid==nil || unionid ==nil) {
  199. KBLoginViewController *login = [[KBLoginViewController alloc] init];
  200. [self presentViewController:login animated:YES completion:nil];
  201. return;
  202. }
  203. NSDictionary *dict = [self dictionaryWithJsonString:message.body];
  204. NSString *jsMethod = dict[@"js_callback"];
  205. NSDictionary *param = @{@"openid":openid,
  206. @"unionid":unionid
  207. };
  208. NSString *jsonDict = [self convertToJsonData:param];
  209. NSString *callBacKMethod = [NSString stringWithFormat:@"%@('%@')",jsMethod,jsonDict];
  210. [self.webView evaluateJavaScript:callBacKMethod completionHandler:^(id _Nullable value, NSError * _Nullable error) {
  211. }];
  212. }
  213. /**
  214. 调起分享
  215. */
  216. - (void)shareToNative:(WKScriptMessage *)message {
  217. NSDictionary *dict = [self dictionaryWithJsonString:message.body];
  218. if ([dict[@"shareType"] isEqualToString:@"1"]) {
  219. [self shareImageWithplatformType:UMSocialPlatformType_WechatSession withDict:dict];
  220. }else {
  221. [self shareImageWithplatformType:UMSocialPlatformType_WechatTimeLine withDict:dict];
  222. }
  223. }
  224. - (void)shareImage:(WKScriptMessage *)message {
  225. NSDictionary *dict = [self dictionaryWithJsonString:message.body];
  226. if ([dict[@"shareType"] isEqualToString:@"1"]) {
  227. [WechatShareTool shareImageWithplatformType:UMSocialPlatformType_WechatSession withDict:dict];
  228. }else {
  229. [WechatShareTool shareImageWithplatformType:UMSocialPlatformType_WechatTimeLine withDict:dict];
  230. }
  231. }
  232. - (void)shareURL:(WKScriptMessage *)message {
  233. NSDictionary *dict = [self dictionaryWithJsonString:message.body];
  234. if ([dict[@"shareType"] isEqualToString:@"1"]) {
  235. [WechatShareTool shareURLWithplatformType:UMSocialPlatformType_WechatSession withDict:dict];
  236. }else {
  237. [WechatShareTool shareURLWithplatformType:UMSocialPlatformType_WechatTimeLine withDict:dict];
  238. }
  239. }
  240. - (void)shareVideo:(WKScriptMessage *)message {
  241. NSDictionary *dict = [self dictionaryWithJsonString:message.body];
  242. if ([dict[@"shareType"] isEqualToString:@"1"]) {
  243. [WechatShareTool shareVideoWithplatformType:UMSocialPlatformType_WechatSession withDict:dict];
  244. }else {
  245. [WechatShareTool shareVideoWithplatformType:UMSocialPlatformType_WechatTimeLine withDict:dict];
  246. }
  247. }
  248. - (void)shareMinProgram:(WKScriptMessage *)message {
  249. NSDictionary *dict = [self dictionaryWithJsonString:message.body];
  250. [WechatShareTool shareMinProgramWithplatformType:UMSocialPlatformType_WechatSession withDict:dict];
  251. }
  252. - (void)openTaobao:(WKScriptMessage *)message {
  253. NSDictionary *dict = [self dictionaryWithJsonString:message.body];
  254. NSString *url = dict[@"taobaoUrl"];
  255. [self jumpToTaobaoWithUrl:url];
  256. }
  257. - (void)jumpToTaobaoWithUrl:(NSString *)url {
  258. id<AlibcTradePage> page = [AlibcTradePageFactory page:url];
  259. //淘客信息
  260. AlibcTradeTaokeParams *taoKeParams=[[AlibcTradeTaokeParams alloc] init];
  261. taoKeParams.pid= ALTK_PID;
  262. //打开方式
  263. AlibcTradeShowParams* showParam = [[AlibcTradeShowParams alloc] init];
  264. showParam.openType = AlibcOpenTypeNative;
  265. [[AlibcTradeSDK sharedInstance].tradeService show:self.parentViewController page:page showParams:showParam taoKeParams:taoKeParams trackParam:nil tradeProcessSuccessCallback:^(AlibcTradeResult * _Nullable result) {
  266. } tradeProcessFailedCallback:^(NSError * _Nullable error) {
  267. }];
  268. }
  269. -(void)shareImageWithplatformType:(UMSocialPlatformType)platformType withDict:(NSDictionary *)dict{
  270. //创建分享消息对象
  271. UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
  272. //创建图片内容对象
  273. UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:dict[@"shareTitle"] descr:dict[@"shareDescirpt"] thumImage:dict[@"shareImageUrl"]];
  274. shareObject.webpageUrl = dict[@"shareUrl"];
  275. messageObject.shareObject = shareObject;
  276. [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:nil completion:^(id data, NSError *error) {
  277. }];
  278. }
  279. /**
  280. kvo监听进度条
  281. @param keyPath keyPath description
  282. @param object object description
  283. @param change change description
  284. @param context context description
  285. */
  286. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
  287. if ([keyPath isEqualToString:NSStringFromSelector(@selector(estimatedProgress))] && object == self.webView ) {
  288. [self.progressView setAlpha:1.0];
  289. [self.progressView setProgress:self.webView.estimatedProgress ];
  290. if (self.webView.estimatedProgress >= 1.0) {
  291. [UIView animateWithDuration:0.7 animations:^{
  292. [self.progressView setProgress:1.0 animated:YES];
  293. [self.progressView setAlpha:0.0];
  294. }];
  295. }
  296. }else{
  297. [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
  298. }
  299. }
  300. #pragma mark-
  301. #pragma mark- WKNavigationDelegate delegate
  302. /**
  303. 开始加载web的时候
  304. @param webView webView description
  305. @param navigation navigation description
  306. */
  307. - (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
  308. //开始加载的时候,让进度条显示
  309. self.progressView.hidden = NO;
  310. self.progressView.progress = 0;
  311. self.progressView.alpha = 1.0;
  312. [UIView animateWithDuration:0.8 animations:^{
  313. self.progressView.progress = 0.6;
  314. }];
  315. }
  316. /**
  317. 当网页加载完成的时候调用
  318. @param webView web描述
  319. @param navigation 导航的描述
  320. */
  321. - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
  322. [self.navigationBar setNavTitle:self.webView.title];
  323. [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:NO];
  324. [self updateNavigationBarButtons];
  325. }
  326. -(void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
  327. {
  328. NSURL *url = navigationAction.request.URL;
  329. UIApplication *app = [UIApplication sharedApplication];
  330. // 打开appstore
  331. if ([url.absoluteString containsString:@"https://itunes.apple.com/cn/app/"])
  332. {
  333. if ([app canOpenURL:url])
  334. {
  335. //设备系统为IOS 10.0或者以上的
  336. if (@available(iOS 10.0, *)) {
  337. [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
  338. } else {
  339. [[UIApplication sharedApplication] openURL:url];
  340. }
  341. decisionHandler(WKNavigationActionPolicyCancel);
  342. return;
  343. }
  344. }
  345. decisionHandler(WKNavigationActionPolicyAllow);
  346. }
  347. #pragma mark -----------
  348. - (void)updateNavigationBarButtons {
  349. if (self.webView.canGoBack) {
  350. [self.navigationBar setCustomLeftButtons:@[self.backButton,self.closeButton]];
  351. }else {
  352. [self.navigationBar setCustomLeftButtons:@[self.backButton]];
  353. }
  354. }
  355. - (void)backAction {
  356. if (self.webView.canGoBack) {
  357. [self.webView goBack];
  358. }else{
  359. [self.navigationController popViewControllerAnimated:YES];
  360. }
  361. }
  362. - (void)closeAction {
  363. [self.navigationController popViewControllerAnimated:YES];
  364. }
  365. - (WKWebView *)webView{
  366. if (!_webView) {
  367. _webView = [[WKWebView alloc]initWithFrame:CGRectMake(0, NavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight)];
  368. _webView.backgroundColor = [UIColor colorWithRed:240.0/255 green:240.0/255 blue:240.0/255 alpha:1.0];
  369. _webView.navigationDelegate = self;
  370. _webView.UIDelegate = self;
  371. //使用kvo监听进度
  372. [_webView addObserver:self forKeyPath:NSStringFromSelector(@selector(estimatedProgress)) options:0 context:nil];
  373. //手势触摸滑动
  374. _webView.allowsBackForwardNavigationGestures = YES;
  375. //自适应
  376. [_webView sizeToFit];
  377. //自适应
  378. [_webView sizeToFit];
  379. }
  380. return _webView;
  381. }
  382. #pragma mark-
  383. #pragma mark- SetupConstraints
  384. /** 加载配置以及视图添加*/
  385. - (void)loadSubViewsConfiguration{
  386. [self.view addSubview:self.webView];
  387. }
  388. - (UIButton *)backButton {
  389. if (!_backButton) {
  390. _backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
  391. [_backButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
  392. [_backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
  393. }
  394. return _backButton;
  395. }
  396. - (UIButton *)closeButton {
  397. if (!_closeButton) {
  398. _closeButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
  399. [_closeButton setImage:[UIImage imageNamed:@"close_web"] forState:UIControlStateNormal];
  400. [_closeButton addTarget:self action:@selector(closeAction) forControlEvents:UIControlEventTouchUpInside];
  401. }
  402. return _closeButton;
  403. }
  404. - (UIProgressView *)progressView{
  405. if (!_progressView) {
  406. _progressView = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleDefault];
  407. _progressView.frame = CGRectMake(0, NavBarHeight, self.view.frame.size.width, 2);
  408. //设置进度条的色彩
  409. [_progressView setTrackTintColor:[UIColor colorWithRed:240.0/255 green:240.0/255 blue:240.0/255 alpha:1.0]];
  410. _progressView.progressTintColor = [UIColor homeRedColor];
  411. }
  412. return _progressView;
  413. }
  414. //观察的移除
  415. - (void)dealloc{
  416. [self.webView removeObserver:self forKeyPath:NSStringFromSelector(@selector(estimatedProgress))];
  417. [[NSNotificationCenter defaultCenter] removeObserver:self];
  418. }
  419. /**
  420. json字符串转字典
  421. */
  422. - (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString
  423. {
  424. if (jsonString == nil) {
  425. return nil;
  426. }
  427. NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
  428. NSError *err;
  429. NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
  430. options:NSJSONReadingMutableContainers
  431. error:&err];
  432. if(err)
  433. {
  434. NSLog(@"json解析失败:%@",err);
  435. return nil;
  436. }
  437. return dic;
  438. }
  439. /**
  440. 字典转json字符串
  441. */
  442. - (NSString *)convertToJsonData:(NSDictionary *)dict
  443. {
  444. NSError *error;
  445. NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
  446. NSString *jsonString;
  447. if (!jsonData) {
  448. NSLog(@"%@",error);
  449. }else{
  450. jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
  451. }
  452. NSMutableString *mutStr = [NSMutableString stringWithString:jsonString];
  453. NSRange range = {0,jsonString.length};
  454. //去掉字符串中的空格
  455. [mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];
  456. NSRange range2 = {0,mutStr.length};
  457. //去掉字符串中的换行符
  458. [mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];
  459. return mutStr;
  460. }
  461. - (void)didReceiveMemoryWarning {
  462. [super didReceiveMemoryWarning];
  463. // Dispose of any resources that can be recreated.
  464. }
  465. /*
  466. #pragma mark - Navigation
  467. // In a storyboard-based application, you will often want to do a little preparation before navigation
  468. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  469. // Get the new view controller using [segue destinationViewController].
  470. // Pass the selected object to the new view controller.
  471. }
  472. */
  473. @end