JXCustomShareVC.m 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. //
  2. // JXCustomShareVC.m
  3. // share
  4. //
  5. // Created by 1 on 2019/3/20.
  6. // Copyright © 2019年 Reese. All rights reserved.
  7. //
  8. #import "JXCustomShareVC.h"
  9. #import "JXSelectFriendVC.h"
  10. #import "JXShareViewController.h"
  11. #import "JXHttpRequet.h"
  12. #import "JXShareViewController.h"
  13. #import "JXShareUser.h"
  14. #import <AVFoundation/AVFoundation.h>
  15. #import <AVKit/AVKit.h>
  16. #define HEIGHT 70
  17. #define MY_INSET 0 // 每行左右间隙
  18. #define TOP_ADD_HEIGHT 400 // 顶部添加的高度,防止下拉顶部空白
  19. @interface JXCustomShareVC () <JXShareVCDlegate, JXSelectFriendVCDlegate>
  20. @property (nonatomic, strong) UIScrollView *tableBody;
  21. @property (nonatomic, strong) UIView *baseView;
  22. @property (nonatomic, strong) UIImageView *imgV;
  23. @property (nonatomic, strong) UIButton *sendFBtn;
  24. @property (nonatomic, strong) UIButton *shareBtn;
  25. @property (nonatomic, strong) NSString *text;
  26. @property (nonatomic, strong) UIProgressView *rateProgressView;
  27. @property (nonatomic, assign) int proInt;
  28. @property (nonatomic, strong) UILabel *proTitle;
  29. @property (nonatomic, strong) UILabel *proOkLabel;
  30. @property (nonatomic, strong) UILabel *proDesLab;
  31. @property (nonatomic, strong) UILabel *proTotalLab;
  32. @property (nonatomic, strong) UIView *whiteV;
  33. @property (nonatomic, strong) UIView *proLine;
  34. @property (nonatomic, strong) JXShareUser *user;
  35. @property (nonatomic, assign) BOOL isSendToFriend;
  36. @property (nonatomic, strong) UIButton *pauseBtn;
  37. @property (nonatomic, strong) AVPlayerViewController *playerVC;
  38. @property (nonatomic, strong) UIView *Playerview;
  39. // 保存数据
  40. @property (nonatomic, strong) NSMutableArray *imgArray;
  41. @property (nonatomic, strong) NSMutableArray *videoArray;
  42. @property (nonatomic, strong) UIImageView *urlImageV;
  43. @property (nonatomic, strong) UILabel *urlTitle;
  44. @property (nonatomic, strong) UILabel *urlDetail;
  45. @property (nonatomic, strong) NSString *url;
  46. @end
  47. @implementation JXCustomShareVC
  48. - (void)viewDidLoad {
  49. [super viewDidLoad];
  50. // Do any additional setup after loading the view.
  51. if ([share_defaults objectForKey:kMY_ShareExtensionToken]) {
  52. self.imgArray = [[NSMutableArray alloc] init];
  53. self.videoArray = [[NSMutableArray alloc] init];
  54. // 获取图片数据
  55. NSExtensionItem *extensionItem = self.extensionContext.inputItems.firstObject;
  56. [self getDataWithItem:extensionItem];
  57. }else {
  58. [self setTintView];
  59. }
  60. }
  61. - (void)viewDidAppear:(BOOL)animated {
  62. [super viewDidAppear:animated];
  63. }
  64. - (void)setTintView {
  65. self.baseView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, JX_SCREEN_WIDTH, JX_SCREEN_HEIGHT)];
  66. self.baseView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
  67. [self.view addSubview:self.baseView];
  68. UIView *bigV = [[UIView alloc] initWithFrame:CGRectMake(30, (JX_SCREEN_HEIGHT-300)/2, JX_SCREEN_WIDTH-60, 300)];
  69. bigV.backgroundColor = [UIColor whiteColor];
  70. bigV.layer.masksToBounds = YES;
  71. bigV.layer.cornerRadius = 3.f;
  72. [self.baseView addSubview:bigV];
  73. UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, bigV.frame.size.width, 20)];
  74. title.text = APP_NAME;
  75. title.textAlignment = NSTextAlignmentCenter;
  76. [bigV addSubview:title];
  77. UIView *topLine = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(title.frame)+20, bigV.frame.size.width, LINE_WH)];
  78. topLine.backgroundColor = THE_LINE_COLOR;
  79. [bigV addSubview:topLine];
  80. UILabel *tintLab = [[UILabel alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(topLine.frame), bigV.frame.size.width- 40, 180)];
  81. tintLab.textAlignment = NSTextAlignmentCenter;
  82. tintLab.textColor = [UIColor grayColor];
  83. tintLab.numberOfLines = 0;
  84. tintLab.text = [NSString stringWithFormat:@"抱歉,请先打开%@,并登录,才可以使用分享功能",APP_NAME];
  85. [bigV addSubview:tintLab];
  86. UIView *botLine = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(tintLab.frame), bigV.frame.size.width, LINE_WH)];
  87. botLine.backgroundColor = THE_LINE_COLOR;
  88. [bigV addSubview:botLine];
  89. UILabel *okLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(botLine.frame), bigV.frame.size.width, bigV.frame.size.height-CGRectGetMaxY(botLine.frame))];
  90. okLabel.textAlignment = NSTextAlignmentCenter;
  91. okLabel.userInteractionEnabled = YES;
  92. okLabel.text = @"我知道了";
  93. okLabel.textColor = HEXCOLOR(0x31AD2A);
  94. [bigV addSubview:okLabel];
  95. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cancelBtnClickHandler:)];
  96. [okLabel addGestureRecognizer:tap];
  97. }
  98. #pragma mark - 发送给朋友
  99. - (void)sendToFriend {
  100. JXSelectFriendVC *selVC = [[JXSelectFriendVC alloc] init];
  101. selVC.delegate = self;
  102. [self presentViewController:selVC animated:NO completion:nil];
  103. }
  104. - (void)sendToFriendSuccess:(JXSelectFriendVC *)selectVC user:(JXShareUser *)user {
  105. self.user = user;
  106. self.proInt = 0;
  107. self.isSendToFriend = YES;
  108. [self setProgressView];
  109. [self updateViewsIsComplete:NO title:nil content:nil];
  110. if (self.url.length > 0) {
  111. int chatType = self.user.roomId.length > 0 ? 2 : 1;
  112. [[JXHttpRequet shareInstance] sendMsgToUserId:self.user.userId chatType:chatType type:1 content:self.url fileName:nil toView:self];
  113. return;
  114. }
  115. NSArray *arr;
  116. if (self.imgArray.count > 0) {
  117. arr = self.imgArray;
  118. }else if (self.videoArray.count > 0) {
  119. arr = self.videoArray;
  120. }
  121. for (int i = 0; i < arr.count; i++) {
  122. if (self.imgArray.count > 0) {
  123. NSString *url = [[JXHttpRequet shareInstance] getDataUrlWithImage:arr[i]];
  124. [[JXHttpRequet shareInstance] uploadFile:url validTime:nil messageId:nil toView:self];
  125. }else if (self.videoArray.count > 0) {
  126. [[JXHttpRequet shareInstance] uploadFile:arr[i] validTime:nil messageId:nil toView:self];
  127. }
  128. }
  129. }
  130. #pragma mark - 分享给生活圈
  131. - (void)shareLifeCircle {
  132. if (self.url.length > 0) {
  133. //分享图片到生活圈
  134. [self setProgressView];
  135. [self updateViewsIsComplete:NO title:nil content:nil];
  136. [[JXHttpRequet shareInstance] addMessage:self.url type:1 data:nil flag:3 toView:self];
  137. return;
  138. }
  139. JXShareViewController *shareVC = [[JXShareViewController alloc] init];
  140. shareVC.delegate = self;
  141. shareVC.image = self.imgV.image;
  142. [self presentViewController:shareVC animated:NO completion:nil];
  143. }
  144. - (void)sendToLifeCircleSucces:(JXShareViewController *)shareVC {
  145. self.text = shareVC.textView.text;
  146. self.proInt = 0;
  147. self.isSendToFriend = NO;
  148. [self setProgressView];
  149. [self updateViewsIsComplete:NO title:nil content:nil];
  150. NSString *path = [[JXHttpRequet shareInstance] getDataUrlWithImage:shareVC.image];
  151. [[JXHttpRequet shareInstance] uploadFile:path validTime:nil messageId:nil toView:self];
  152. }
  153. - (void)setProgressView {
  154. [self.baseView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
  155. self.baseView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
  156. _whiteV = [[UIView alloc] initWithFrame:CGRectMake(30, (JX_SCREEN_HEIGHT-189)/2, JX_SCREEN_WIDTH-60, 183)];
  157. _whiteV.backgroundColor = [UIColor whiteColor];
  158. _whiteV.layer.masksToBounds = YES;
  159. _whiteV.layer.cornerRadius = 3.f;
  160. [self.baseView addSubview:_whiteV];
  161. _proTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, _whiteV.frame.size.width, 20)];
  162. _proTitle.textAlignment = NSTextAlignmentCenter;
  163. _proDesLab.font = [UIFont boldSystemFontOfSize:17];
  164. [_whiteV addSubview:_proTitle];
  165. // 进度条初始化
  166. self.rateProgressView = [[UIProgressView alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(_proTitle.frame)+20, _whiteV.frame.size.width-40, 20)];
  167. // 进度条的底色
  168. self.rateProgressView.trackTintColor = [UIColor lightGrayColor];
  169. self.rateProgressView.progressTintColor = HEXCOLOR(0x31AD2A);
  170. self.rateProgressView.layer.masksToBounds = YES;
  171. self.rateProgressView.layer.cornerRadius = 1;
  172. [_whiteV addSubview:self.rateProgressView];
  173. _proDesLab = [[UILabel alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(_proTitle.frame)+27, _whiteV.frame.size.width-40, 25)];
  174. _proDesLab.textAlignment = NSTextAlignmentCenter;
  175. [_whiteV addSubview:_proDesLab];
  176. _proTotalLab = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.rateProgressView.frame)+20, _whiteV.frame.size.width, 20)];
  177. _proTotalLab.textAlignment = NSTextAlignmentCenter;
  178. _proTotalLab.textColor = [UIColor grayColor];
  179. _proTotalLab.text = [NSString stringWithFormat:@"共%ld张",self.imgArray.count];
  180. [_whiteV addSubview:_proTotalLab];
  181. _proLine = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_proTotalLab.frame)+20, _whiteV.frame.size.width, LINE_WH)];
  182. _proLine.backgroundColor = THE_LINE_COLOR;
  183. [_whiteV addSubview:_proLine];
  184. _proOkLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_proLine.frame), _whiteV.frame.size.width, _whiteV.frame.size.height-CGRectGetMaxY(_proLine.frame))];
  185. _proOkLabel.textAlignment = NSTextAlignmentCenter;
  186. _proOkLabel.userInteractionEnabled = YES;
  187. _proOkLabel.textColor = HEXCOLOR(0x31AD2A);
  188. _proOkLabel.text = @"好的";
  189. [_whiteV addSubview:_proOkLabel];
  190. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cancelBtnClickHandler:)];
  191. [_proOkLabel addGestureRecognizer:tap];
  192. }
  193. - (void)updateViewsIsComplete:(BOOL)isComplete title:(NSString *)title content:(NSString *)content {
  194. _proTitle.text = isComplete ? title : @"正在发送";
  195. _proOkLabel.hidden = !isComplete;
  196. self.rateProgressView.hidden = isComplete;
  197. _proTotalLab.hidden = isComplete;
  198. _proDesLab.hidden = !isComplete;
  199. _proLine.hidden = !isComplete;
  200. _proDesLab.text = content;
  201. CGRect frame = _whiteV.frame;
  202. frame.size.height = isComplete ? 183 : 183 - _proOkLabel.frame.size.height;
  203. frame.origin.y = (JX_SCREEN_HEIGHT - frame.size.height)/2;
  204. _whiteV.frame = frame;
  205. if (isComplete) {
  206. self.proInt = 0;
  207. }
  208. }
  209. - (void)getDataWithItem:(NSExtensionItem *)extensionItem {
  210. //可以从NSExtensionItem项中的attachments属性中获得附件数据,如音频、视频、图片等,NSItemProvide就是实例的表示
  211. NSItemProvider *itemProvider = [[extensionItem attachments] firstObject];
  212. if(!itemProvider){
  213. return;
  214. }
  215. // 获取图片数据
  216. if([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeImage]){
  217. [itemProvider loadItemForTypeIdentifier:(NSString *)kUTTypeImage options:nil completionHandler:^(UIImage *image, NSError *error) {
  218. if(image){
  219. dispatch_async(dispatch_get_main_queue(), ^{
  220. [self.imgArray addObject:image];
  221. // 图片加载完成后再创建UI, 避免出现无图片的空档时间
  222. [self setupViewsIsVideo:NO];
  223. //显示图片
  224. self.imgV.image = image;
  225. // 更新界面
  226. CGFloat b = image.size.height/image.size.width;
  227. // 图片最大高度
  228. CGFloat h = JX_SCREEN_HEIGHT - JX_SCREEN_TOP -230;
  229. CGRect frame = self.imgV.frame;
  230. frame.size = image.size;
  231. if (image.size.width > JX_SCREEN_WIDTH/3*2) {
  232. frame.size.width = JX_SCREEN_WIDTH/3*2;
  233. frame.size.height = JX_SCREEN_WIDTH/3*2 * b;
  234. if (frame.size.height > h) {
  235. frame.size.height = h;
  236. frame.size.width = h *(1/b);
  237. }
  238. }
  239. frame.origin.x = (JX_SCREEN_WIDTH-frame.size.width)/2;
  240. self.imgV.frame = frame;
  241. self.sendFBtn.frame = CGRectMake(MY_INSET,CGRectGetMaxY(self.imgV.frame)+30, JX_SCREEN_WIDTH-MY_INSET*2, HEIGHT);
  242. self.shareBtn.frame = CGRectMake(MY_INSET,CGRectGetMaxY(self.sendFBtn.frame), JX_SCREEN_WIDTH-MY_INSET*2, HEIGHT);
  243. });
  244. }
  245. }];
  246. }
  247. // 获取视频数据
  248. if([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeMovie]){
  249. [itemProvider loadItemForTypeIdentifier:(NSString *)kUTTypeMovie options:nil completionHandler:^(NSData *video, NSError *error) {
  250. if(video){
  251. dispatch_async(dispatch_get_main_queue(), ^{
  252. NSString *path = [[JXHttpRequet shareInstance] getDataUrlWithVideo:video];
  253. [self.videoArray addObject:path];
  254. // 获取第一帧图片
  255. UIImage *image = [[JXHttpRequet shareInstance] getFirstImageFromVideo:path];
  256. // 图片加载完成后再创建UI, 避免出现无图片的空档时间
  257. [self setupViewsIsVideo:YES];
  258. //显示图片
  259. self.imgV.image = image;
  260. // 更新界面
  261. CGFloat b = image.size.height/image.size.width;
  262. // 图片最大高度
  263. CGFloat h = JX_SCREEN_HEIGHT - JX_SCREEN_TOP -230;
  264. CGRect frame = self.imgV.frame;
  265. frame.size = image.size;
  266. if (image.size.width > JX_SCREEN_WIDTH/3*2) {
  267. frame.size.width = JX_SCREEN_WIDTH/3*2;
  268. frame.size.height = JX_SCREEN_WIDTH/3*2 * b;
  269. if (frame.size.height > h) {
  270. frame.size.height = h;
  271. frame.size.width = h *(1/b);
  272. }
  273. }
  274. frame.origin.x = (JX_SCREEN_WIDTH-frame.size.width)/2;
  275. self.imgV.frame = frame;
  276. self.imgV.hidden = YES;
  277. self.Playerview = [[UIView alloc] initWithFrame:self.imgV.frame];
  278. self.Playerview.backgroundColor = [UIColor blackColor];
  279. [self.tableBody addSubview:self.Playerview];
  280. self.playerVC = [[AVPlayerViewController alloc] init];
  281. self.playerVC.player = [AVPlayer playerWithURL:[NSURL fileURLWithPath:[self.videoArray firstObject]]];
  282. self.playerVC.showsPlaybackControls = YES;
  283. self.playerVC.view.frame = self.imgV.bounds;
  284. [self.Playerview addSubview:self.playerVC.view];
  285. self.pauseBtn = [[UIButton alloc] initWithFrame:CGRectMake(0,0, 26, 26)];
  286. [self.pauseBtn setBackgroundImage:[UIImage imageNamed:@"player_play"] forState:UIControlStateNormal];
  287. [self.pauseBtn setBackgroundImage:[UIImage imageNamed:@"player_pause"] forState:UIControlStateHighlighted];
  288. [self.pauseBtn addTarget:self action:@selector(openStart) forControlEvents:UIControlEventTouchUpInside];
  289. [self.imgV addSubview:self.pauseBtn];
  290. CGRect frameP = self.pauseBtn.frame;
  291. frameP.origin.x = (self.imgV.frame.size.width-frameP.size.width) * 0.5;
  292. frameP.origin.y = (self.imgV.frame.size.height-frameP.size.height) * 0.5;
  293. self.pauseBtn.frame = frameP;
  294. self.sendFBtn.frame = CGRectMake(MY_INSET,CGRectGetMaxY(self.imgV.frame)+30, JX_SCREEN_WIDTH-MY_INSET*2, HEIGHT);
  295. });
  296. }
  297. }];
  298. }
  299. // 获取URL数据
  300. if([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeURL]){
  301. [itemProvider loadItemForTypeIdentifier:(NSString *)kUTTypeURL options:nil completionHandler:^(NSURL *url, NSError *error) {
  302. if(url){
  303. dispatch_async(dispatch_get_main_queue(), ^{
  304. // 图片加载完成后再创建UI, 避免出现无图片的空档时间
  305. self.url = url.absoluteString;
  306. [self setupUrlViewWithUrl:url];
  307. //显示图片
  308. });
  309. }
  310. }];
  311. }
  312. }
  313. - (void)setHeadView {
  314. self.baseView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, JX_SCREEN_WIDTH, JX_SCREEN_HEIGHT)];
  315. self.baseView.backgroundColor = [UIColor whiteColor];
  316. [self.view addSubview:self.baseView];
  317. UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, JX_SCREEN_WIDTH, JX_SCREEN_TOP)];
  318. [self.baseView addSubview:headView];
  319. UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(20, JX_SCREEN_TOP - 35, 40, 20)];
  320. [btn setTitle:@"关闭" forState:UIControlStateNormal];
  321. [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  322. [btn addTarget:self action:@selector(cancelBtnClickHandler:) forControlEvents:UIControlEventTouchUpInside];
  323. [headView addSubview:btn];
  324. UILabel* p = [[UILabel alloc]initWithFrame:CGRectMake(60, JX_SCREEN_TOP - 35, JX_SCREEN_WIDTH-60*2, 20)];
  325. p.backgroundColor = [UIColor clearColor];
  326. p.textAlignment = NSTextAlignmentCenter;
  327. p.text = APP_NAME;
  328. p.font = [UIFont boldSystemFontOfSize:18.0];
  329. [headView addSubview:p];
  330. UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, JX_SCREEN_TOP-LINE_WH, JX_SCREEN_WIDTH, LINE_WH)];
  331. line.backgroundColor = [UIColor lightGrayColor];
  332. [headView addSubview:line];
  333. self.tableBody = [[UIScrollView alloc] initWithFrame:CGRectMake(0, JX_SCREEN_TOP, JX_SCREEN_WIDTH, JX_SCREEN_HEIGHT-JX_SCREEN_TOP)];
  334. self.tableBody.showsVerticalScrollIndicator = NO;
  335. self.tableBody.showsHorizontalScrollIndicator = NO;
  336. [self.baseView addSubview:self.tableBody];
  337. }
  338. - (void)setupUrlViewWithUrl:(NSURL *)url {
  339. [self setHeadView];
  340. // self.urlImageV = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 50, 50)];
  341. // self.urlImageV.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@://%@/favicon.ico", url.scheme, url.host]]]];
  342. // [self.tableBody addSubview:self.urlImageV];
  343. CGSize size = [url.absoluteString boundingRectWithSize:CGSizeMake(JX_SCREEN_WIDTH-140, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:SYSFONT(17)} context:nil].size;
  344. self.urlTitle = [[UILabel alloc] initWithFrame:CGRectMake(70, 20, JX_SCREEN_WIDTH-140, size.height)];
  345. self.urlTitle.text = url.absoluteString;
  346. self.urlTitle.textColor = [UIColor grayColor];
  347. self.urlTitle.numberOfLines = 0;
  348. [self.tableBody addSubview:self.urlTitle];
  349. _sendFBtn = [self createButton:@"发送给朋友" drawTop:YES drawBottom:YES icon:@"im_linksShare_send_friend" click:@selector(sendToFriend)];
  350. _sendFBtn.frame = CGRectMake(MY_INSET,CGRectGetMaxY(_urlTitle.frame)+30, JX_SCREEN_WIDTH-MY_INSET*2, HEIGHT);
  351. _shareBtn = [self createButton:@"分享给社交圈" drawTop:NO drawBottom:YES icon:@"im_linksShare_life" click:@selector(shareLifeCircle)];
  352. _shareBtn.frame = CGRectMake(MY_INSET,CGRectGetMaxY(_sendFBtn.frame), JX_SCREEN_WIDTH-MY_INSET*2, HEIGHT);
  353. }
  354. - (void)setupViewsIsVideo:(BOOL)isVideo {
  355. [self setHeadView];
  356. _imgV = [[UIImageView alloc] initWithFrame:CGRectMake(120, 30, JX_SCREEN_WIDTH-240, 300)];
  357. _imgV.image = [UIImage imageNamed:@"custom_share_no_image"];
  358. _imgV.userInteractionEnabled = YES;
  359. [self.tableBody addSubview:_imgV];
  360. _sendFBtn = [self createButton:@"发送给朋友" drawTop:YES drawBottom:YES icon:@"im_linksShare_send_friend" click:@selector(sendToFriend)];
  361. _sendFBtn.frame = CGRectMake(MY_INSET,CGRectGetMaxY(_imgV.frame)+30, JX_SCREEN_WIDTH-MY_INSET*2, HEIGHT);
  362. if (!isVideo) {
  363. _shareBtn = [self createButton:@"分享给社交圈" drawTop:NO drawBottom:YES icon:@"im_linksShare_life" click:@selector(shareLifeCircle)];
  364. _shareBtn.frame = CGRectMake(MY_INSET,CGRectGetMaxY(_sendFBtn.frame), JX_SCREEN_WIDTH-MY_INSET*2, HEIGHT);
  365. }
  366. }
  367. - (void)openStart {
  368. [self.playerVC.player play];
  369. }
  370. #pragma mark - 服务器返回数据
  371. -(void) didServerNetworkResultSucces:(JXNetwork*)aDownload dict:(NSDictionary*)dict array:(NSArray*)array1{
  372. if ([aDownload.action isEqualToString:act_MsgAdd]) {
  373. NSLog(@"---分享朋友圈成功---");
  374. if (self.proInt >= self.imgArray.count || self.proInt >= self.videoArray.count || self.url.length > 0) {
  375. [self updateViewsIsComplete:YES title:@"已发送"content:[NSString stringWithFormat:@"你可以在%@里查看",APP_NAME]];
  376. }
  377. }
  378. if ([aDownload.action isEqualToString:act_UploadFile]) {
  379. self.proInt += 1;
  380. if (self.isSendToFriend) {
  381. //发送消息给好友/群组
  382. int chatType = self.user.roomId.length > 0 ? 2 : 1;
  383. NSString *url;
  384. NSString *fileName;
  385. int type = 0;
  386. if (self.imgArray.count > 0) {
  387. url = [(NSDictionary *)[[dict objectForKey:@"images"] firstObject] objectForKey:@"oUrl"];
  388. fileName = [(NSDictionary *)[[dict objectForKey:@"images"] firstObject] objectForKey:@"oFileName"];
  389. type = 2;
  390. [self.rateProgressView setProgress:self.proInt/self.imgArray.count animated:YES];
  391. }else if (self.videoArray.count > 0) {
  392. url = [(NSDictionary *)[[dict objectForKey:@"videos"] firstObject] objectForKey:@"oUrl"];
  393. fileName = [(NSDictionary *)[[dict objectForKey:@"videos"] firstObject] objectForKey:@"oFileName"];
  394. type = 6;
  395. [self.rateProgressView setProgress:self.proInt/self.videoArray.count animated:YES];
  396. }
  397. [[JXHttpRequet shareInstance] sendMsgToUserId:self.user.userId chatType:chatType type:type content:url fileName:fileName toView:self];
  398. }else {
  399. //分享图片到生活圈
  400. [[JXHttpRequet shareInstance] addMessage:self.text type:2 data:dict flag:3 toView:self];
  401. }
  402. }
  403. if ([aDownload.action isEqualToString:act_SendMsg]) {
  404. NSLog(@"---发送消息成功---");
  405. if (self.proInt >= self.imgArray.count || self.proInt >= self.videoArray.count || self.url.length > 0) {
  406. [self updateViewsIsComplete:YES title:@"已发送"content:[NSString stringWithFormat:@"你可以在%@里查看",APP_NAME]];
  407. }
  408. }
  409. }
  410. -(int) didServerNetworkResultFailed:(JXNetwork*)aDownload dict:(NSDictionary*)dict{
  411. NSString *str = [NSString string];
  412. int resultCode = [[dict objectForKey:@"resultCode"] intValue];
  413. if(resultCode != 1) {
  414. if(resultCode == 1030101 || resultCode == 1030102){
  415. str = [NSString stringWithFormat:@"登录过期,请重新登录%@",APP_NAME];
  416. //登录过期
  417. return 1;
  418. }
  419. }
  420. if ([aDownload.action isEqualToString:act_SendMsg]) {
  421. str = @"消息发送失败";
  422. }
  423. if ([aDownload.action isEqualToString:act_MsgAdd]) {
  424. str = @"分享社交圈失败";
  425. }
  426. if ([aDownload.action isEqualToString:act_UploadFile]) {
  427. str = @"上传失败";
  428. }
  429. [self updateViewsIsComplete:YES title:str content:@"请检查网络"];
  430. return 1;
  431. }
  432. -(int) didServerNetworkError:(JXNetwork*)aDownload error:(NSError *)error{//error为空时,代表超时
  433. NSString *str = [NSString string];
  434. if ([aDownload.action isEqualToString:act_SendMsg]) {
  435. str = @"消息发送失败";
  436. }
  437. if ([aDownload.action isEqualToString:act_MsgAdd]) {
  438. str = @"分享社交圈失败";
  439. }
  440. if ([aDownload.action isEqualToString:act_UploadFile]) {
  441. str = @"上传失败";
  442. }
  443. [self updateViewsIsComplete:YES title:str content:@"请检查网络"];
  444. return 1;
  445. }
  446. -(void) didServerNetworkStart:(JXNetwork*)aDownload{
  447. }
  448. -(UIButton*)createButton:(NSString*)title drawTop:(BOOL)drawTop drawBottom:(BOOL)drawBottom icon:(NSString*)icon click:(SEL)click{
  449. UIButton* btn = [[UIButton alloc] init];
  450. btn.backgroundColor = [UIColor whiteColor];
  451. btn.userInteractionEnabled = YES;
  452. [btn addTarget:self action:click forControlEvents:UIControlEventTouchUpInside];
  453. [self.tableBody addSubview:btn];
  454. UILabel* p = [[UILabel alloc] initWithFrame:CGRectMake(20*2+30, 0, self_width-35-20-5, HEIGHT)];
  455. p.text = title;
  456. p.font = SYSFONT(16);
  457. p.backgroundColor = [UIColor clearColor];
  458. p.textColor = HEXCOLOR(0x323232);
  459. [btn addSubview:p];
  460. if(icon){
  461. UIImageView* iv = [[UIImageView alloc] initWithFrame:CGRectMake(20, (HEIGHT-30)/2, 30, 30)];
  462. iv.image = [UIImage imageNamed:icon];
  463. [btn addSubview:iv];
  464. }
  465. if(drawTop){
  466. UIView* line = [[UIView alloc] initWithFrame:CGRectMake(0,0,JX_SCREEN_WIDTH,LINE_WH)];
  467. line.backgroundColor = THE_LINE_COLOR;
  468. [btn addSubview:line];
  469. }
  470. if(drawBottom){
  471. UIView* line = [[UIView alloc] initWithFrame:CGRectMake(20,HEIGHT-LINE_WH,JX_SCREEN_WIDTH-40,LINE_WH)];
  472. line.backgroundColor = THE_LINE_COLOR;
  473. [btn addSubview:line];
  474. }
  475. if(click){
  476. UIImageView* iv;
  477. iv = [[UIImageView alloc] initWithFrame:CGRectMake(JX_SCREEN_WIDTH-15-7, (HEIGHT-13)/2, 7, 13)];
  478. iv.image = [UIImage imageNamed:@"new_icon_>"];
  479. [btn addSubview:iv];
  480. }
  481. return btn;
  482. }
  483. #pragma mark - 打开APP
  484. - (void)openApp {
  485. //自从文章发出来后不断被问到的一个问题。其实苹果官方除了Today Extension外,其他Extension是不提供跳转接口的。所以这里总结的是一种非正常的方式。直接上代码(会不会审核被拒就得看自己人品了~)
  486. //这种方式主要实现原理是通过响应链找到Host App的UIApplication对象,通过该对象调用openURL方法返回自己的应用。
  487. UIResponder *responder = self;
  488. while (responder)
  489. {
  490. if ([responder respondsToSelector:@selector(openURL:)])
  491. {
  492. [responder performSelector:@selector(openURL:) withObject:[NSURL URLWithString:@"testshare://"]];
  493. break;
  494. }
  495. responder = [responder nextResponder];
  496. }
  497. }
  498. - (void)cancelBtnClickHandler:(id)sender
  499. {
  500. //取消分享
  501. [self.extensionContext cancelRequestWithError:[NSError errorWithDomain:@"CustomShareError" code:NSUserCancelledError userInfo:nil]];
  502. }
  503. - (void)postBtnClickHandler:(id)sender
  504. {
  505. //执行分享内容处理
  506. [self.extensionContext completeRequestReturningItems:@[] completionHandler:nil];
  507. }
  508. /*
  509. #pragma mark - Navigation
  510. // In a storyboard-based application, you will often want to do a little preparation before navigation
  511. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  512. // Get the new view controller using [segue destinationViewController].
  513. // Pass the selected object to the new view controller.
  514. }
  515. */
  516. //当扩展处理完host app传来的图片数据后,它需要将处理好的的数据在传给host app,在扩展中的代码如下:
  517. - (void)done:(id)sender{
  518. NSExtensionItem *extensionItem = [[NSExtensionItem alloc]init];
  519. [extensionItem setAttachments:@[[[NSItemProvider alloc] initWithItem:[self.imgV image] typeIdentifier:(NSString *)kUTTypeImage]]];
  520. }
  521. @end