ShareView.m
12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
//
// ShareView.m
// IVTMeLiveTwo
//
// Created by XRG on 16/8/11.
// Copyright © 2016年 QLQ. All rights reserved.
//
#import "ShareView.h"
#import "WXApiObject.h"
#import "WXApi.h"
#import <TencentOpenAPI/TencentOAuth.h>
#import <TencentOpenAPI/QQApiInterface.h>
#import "WeiboSDK.h"
#import "AppDelegate.h"
#define kShareImageName @"icon-share.png"
static ShareView *instance = nil;
@interface ShareView ()
{
// UIView *bgView;
UIButton *wechatSessionButton;
UIButton *wechatTimelineButton;
UIButton *qqButton;
UIButton *qzoneButton;
UIButton *sinaButton;
ShareContentModel *shareEntity;
}
@end
@implementation ShareView
+ (ShareView *)sharedInstance{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[self alloc] init];
[instance setupView];
});
return instance;
}
- (void)setupView{
self.frame = [[UIScreen mainScreen] bounds];
self.backgroundColor = [UIColor clearColor];
/**
* 创建显示各个分享按钮的view
*/
CGFloat space = (kScreenWidth - 5*50)/6;
CGFloat heightSpace = 15;
_shareButtonView = [[UIView alloc]init];
_shareButtonView.backgroundColor = [UIColor whiteColor];
_shareButtonView.frame = CGRectMake(self.bounds.origin.x, kScreenHeight, kScreenWidth, 80);
_shareButtonView.alpha = 0;
[self addSubview:_shareButtonView];
/**
* 微信
*/
wechatSessionButton = [[UIButton alloc]init];
wechatSessionButton.frame = CGRectMake(space, heightSpace, 50, 50);
[wechatSessionButton setImage:[UIImage imageNamed:@"shareList1"] forState:UIControlStateNormal];
[wechatSessionButton addTarget:self action:@selector(doWechatSessionButton) forControlEvents:UIControlEventTouchUpInside];
[_shareButtonView addSubview:wechatSessionButton];
/**
* 微信朋友圈
*/
wechatTimelineButton = [[UIButton alloc]init];
wechatTimelineButton.frame = CGRectMake(wechatSessionButton.frame.origin.x + wechatSessionButton.frame.size.width + space, heightSpace, 50, 50);
[wechatTimelineButton setImage:[UIImage imageNamed:@"shareList2"] forState:UIControlStateNormal];
[wechatTimelineButton addTarget:self action:@selector(doWechatTimelineButton) forControlEvents:UIControlEventTouchUpInside];
[_shareButtonView addSubview:wechatTimelineButton];
/**
* QQ
*/
qqButton = [[UIButton alloc]init];
qqButton.frame = CGRectMake(wechatTimelineButton.frame.origin.x + wechatTimelineButton.frame.size.width + space, heightSpace, 50, 50);
[qqButton setImage:[UIImage imageNamed:@"shareList3"] forState:UIControlStateNormal];
[qqButton addTarget:self action:@selector(doQQButton) forControlEvents:UIControlEventTouchUpInside];
[_shareButtonView addSubview:qqButton];
/**
* QQ空间
*/
qzoneButton = [[UIButton alloc]init];
qzoneButton.frame = CGRectMake(qqButton.frame.origin.x + qqButton.frame.size.width + space, heightSpace, 50, 50);
[qzoneButton setImage:[UIImage imageNamed:@"shareList4"] forState:UIControlStateNormal];
[qzoneButton addTarget:self action:@selector(doQzoneButton) forControlEvents:UIControlEventTouchUpInside];
[_shareButtonView addSubview:qzoneButton];
/**
* 新浪微博
*/
sinaButton = [[UIButton alloc]init];
sinaButton.frame = CGRectMake(qzoneButton.frame.origin.x + qzoneButton.frame.size.width + space, heightSpace, 50, 50);
[sinaButton setImage:[UIImage imageNamed:@"shareList5"] forState:UIControlStateNormal];
[sinaButton addTarget:self action:@selector(doSinaButton) forControlEvents:UIControlEventTouchUpInside];
[_shareButtonView addSubview:sinaButton];
}
/**
* 微信好友
*/
- (void)doWechatSessionButton{
WXMediaMessage *message = [WXMediaMessage message];
message.title = shareEntity.shareTitle;
message.description = shareEntity.shareContent;
// [message setThumbImage:[UIImage imageNamed:@"icon-share.png"]];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:shareEntity.sharePictureURL]];
if ([data length] > 1024*32) {
UIImage *img = [UIImage imageWithData:data];
img = [img scaledImageWithWidth:120 andHeight:120];
data = UIImageJPEGRepresentation(img, 1);
}
message.thumbData = data;
WXWebpageObject *ext = [WXWebpageObject object];
ext.webpageUrl = shareEntity.shareURL;
message.mediaObject = ext;
message.mediaTagName = @"WECHAT_TAG_JUMP_APP";
message.messageExt = @"这是第三方带的测试字段";
message.messageAction = @"<action>dotalist</action>";
SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
req.bText = NO;
req.message = message;
req.scene = WXSceneSession;
[WXApi sendReq:req];
}
/**
* 微信朋友圈
*/
- (void)doWechatTimelineButton{
WXMediaMessage *message = [WXMediaMessage message];
message.title = shareEntity.shareTitle;
message.description = shareEntity.shareContent;
// [message setThumbImage:[UIImage imageNamed:@"icon-share.png"]];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:shareEntity.sharePictureURL]];
if ([data length] > 1024*32) {
UIImage *img = [UIImage imageWithData:data];
img = [img scaledImageWithWidth:120 andHeight:120];
data = UIImageJPEGRepresentation(img, 1);
}
message.thumbData = data;
WXWebpageObject *ext = [WXWebpageObject object];
ext.webpageUrl = shareEntity.shareURL;
message.mediaObject = ext;
message.mediaTagName = @"WECHAT_TAG_JUMP_APP";
message.messageExt = @"这是第三方带的测试字段";
message.messageAction = @"<action>dotalist</action>";
SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
req.bText = NO;
req.message = message;
req.scene = WXSceneTimeline;
[WXApi sendReq:req];
}
/**
* qq好友
*/
- (void)doQQButton{
// NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"icon-share.png"];
// NSData *data = [NSData dataWithContentsOfFile:path];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:shareEntity.sharePictureURL]];
if ([data length] > 1024*32) {
UIImage *img = [UIImage imageWithData:data];
img = [img scaledImageWithWidth:120 andHeight:120];
data = UIImageJPEGRepresentation(img, 1);
}
NSURL *url = [NSURL URLWithString:shareEntity.shareURL];
NSString *title = shareEntity.shareTitle;
NSString *desc = shareEntity.shareContent;
QQApiNewsObject *img = [QQApiNewsObject objectWithURL:url title:title description:desc previewImageData:data];
SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:img];
QQApiSendResultCode sent = [QQApiInterface sendReq:req];
[self handleSendResult:sent];
}
/**
* qq空间
*/
- (void)doQzoneButton{
// NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"icon-share.png"];
// NSData *data = [NSData dataWithContentsOfFile:path];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:shareEntity.sharePictureURL]];
if ([data length] > 1024*32) {
UIImage *img = [UIImage imageWithData:data];
img = [img scaledImageWithWidth:120 andHeight:120];
data = UIImageJPEGRepresentation(img, 1);
}
NSURL *url = [NSURL URLWithString:shareEntity.shareURL];
NSString *title = shareEntity.shareTitle;
NSString *desc = shareEntity.shareContent;
QQApiNewsObject *img = [QQApiNewsObject objectWithURL:url title:title description:desc previewImageData:data];
[img setCflag:kQQAPICtrlFlagQZoneShareOnStart];
SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:img];
QQApiSendResultCode sent = [QQApiInterface sendReq:req];
[self handleSendResult:sent];
}
- (void)handleSendResult:(QQApiSendResultCode)sendResult
{
switch (sendResult)
{
case EQQAPIAPPNOTREGISTED:
{
UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"App未注册" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
[msgbox show];
break;
}
case EQQAPIMESSAGECONTENTINVALID:
case EQQAPIMESSAGECONTENTNULL:
case EQQAPIMESSAGETYPEINVALID:
{
UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"发送参数错误" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
[msgbox show];
break;
}
case EQQAPIQQNOTINSTALLED:
{
UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"未安装手Q" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
[msgbox show];
break;
}
case EQQAPIQQNOTSUPPORTAPI:
{
UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"API接口不支持" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
[msgbox show];
break;
}
case EQQAPISENDFAILD:
{
UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"发送失败" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
[msgbox show];
break;
}
default:
{
break;
}
}
}
/**
* 新浪分享
*/
- (void)doSinaButton{
/**
* 封装request
*/
WBAuthorizeRequest *authRequest = [WBAuthorizeRequest request];
authRequest.redirectURI = @"http://sns.whalecloud.com";
authRequest.scope = @"all";
/**
* 封装message
*/
WBMessageObject *message = [WBMessageObject message];
message.text = shareEntity.shareContent;
WBWebpageObject *webpage = [WBWebpageObject object];
webpage.objectID = @"identifier1";
webpage.title = shareEntity.shareTitle;
webpage.description = shareEntity.shareContent;
// webpage.thumbnailData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"icon-share" ofType:@"png"]];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:shareEntity.sharePictureURL]];
if ([data length] > 1024*32) {
UIImage *img = [UIImage imageWithData:data];
img = [img scaledImageWithWidth:120 andHeight:120];
data = UIImageJPEGRepresentation(img, 1);
}
webpage.thumbnailData = data;
webpage.webpageUrl = shareEntity.shareURL;
message.mediaObject = webpage;
/**
* 发送请求
*/
WBSendMessageToWeiboRequest *request = [WBSendMessageToWeiboRequest requestWithMessage:message authInfo:authRequest access_token:[AppDelegate appDelegate].wbAccessToken];
// request.userInfo = @{@"ShareMessageFrom": @"ShareView",
// @"Other_Info_1": [NSNumber numberWithInt:123],
// @"Other_Info_2": @[@"obj1", @"obj2"],
// @"Other_Info_3": @{@"key1": @"obj1", @"key2": @"obj2"}};
[WeiboSDK sendRequest:request];
}
- (void)showWithEntity:(ShareContentModel *)entity{
shareEntity = entity;
/**
* 创建点击事件
*/
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hidden)];
[self addGestureRecognizer:tap];
[[AppDelegate appDelegate].window addSubview:self];//添加分享视图
CGRect rect = _shareButtonView.frame;
rect.origin.y -= rect.size.height;
[UIView animateWithDuration:.3 animations:^{
_shareButtonView.alpha = 1;
_shareButtonView.frame = rect;
}];
}
- (void)hidden
{
CGRect rect = _shareButtonView.frame;
rect.origin.y = kScreenHeight;
[UIView animateWithDuration:.3 animations:^{
_shareButtonView.alpha = 0;
_shareButtonView.frame = rect;
} completion:^(BOOL finished) {
// [bgView removeFromSuperview];
[self removeFromSuperview];
}];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@end