-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHYYShareView.m
executable file
·387 lines (302 loc) · 12.1 KB
/
HYYShareView.m
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
//
// HYYShareView.m
// 分享测试
//
// Created by qianfeng01 on 15/9/29.
// Copyright (c) 2015年 侯园园. All rights reserved.
//
#import "HYYShareView.h"
#import "Header.h"
#import "HyyUIButton.h"
#import "Header.h"
#import <CoreGraphics/CoreGraphics.h>
#import "WZLBadgeImport.h"
@implementation HYYShareView
-(instancetype)initWithFrame:(CGRect)frame andType:(TYPE)type{
self=[super initWithFrame:frame];
if (self) {
self.type=type;
[self loadData];
[self initItem];
self.alpha=0;
[UIView animateWithDuration:1 animations:^{
self.alpha = 1.0;
}];
}
return self;
}
//加载button视图
-(void)initItem{
//设置自身背景色
self.backgroundColor=[UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
//给自己添加手势消除界面
UITapGestureRecognizer * tapGR = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(closeShareView)];
[self addGestureRecognizer:tapGR];
self.userInteractionEnabled=YES;
//创建分享视图
CGFloat bgViewWIdth =itemWidth *numberOfItemInLine+itemHorMargin*(numberOfItemInLine +1);
CGFloat bgViewHeight =itemHeight*2+3*itemVerMargin;
CGFloat bgViewX = (SIZE_OF_SCREEN.width -bgViewWIdth)/2;
CGFloat bgViewY = (SIZE_OF_SCREEN.height -bgViewHeight)/2;
UIView * shareActionView =[[UIView alloc]initWithFrame:CGRectMake(bgViewX,bgViewY , bgViewWIdth,bgViewHeight)];
shareActionView.layer.cornerRadius=5;
shareActionView.layer.masksToBounds=YES;
[self addSubview:shareActionView];
shareActionView.backgroundColor = [UIColor whiteColor];
for (int i=0; i<iconList.count; i++) {
UIImage *img=[UIImage imageNamed:iconList[i]];
int row =i/numberOfItemInLine;
int col =i%numberOfItemInLine;
CGFloat x=(itemWidth +itemHorMargin)*col+itemHorMargin;
CGFloat y= (itemHeight +itemVerMargin)*row +itemVerMargin;
HyyUIButton * oneButton=[[HyyUIButton alloc]initWithFrame:CGRectMake(x,y ,itemWidth ,itemHeight )];
oneButton.titleLabel.font = [UIFont systemFontOfSize:14];
[oneButton setImage:img forState:UIControlStateNormal];
[oneButton setTitle:textList[i] forState:UIControlStateNormal];
[oneButton addTarget:self action:@selector(clickedButton:) forControlEvents:UIControlEventTouchUpInside];
if ([textList[i] isEqualToString:NSLocalizedString(@"微信好友", nil)]) {
oneButton.tag =SHARE_ITEM_WEIXIN_SESSION;
}else if([textList[i] isEqualToString:NSLocalizedString(@"朋友圈", nil)]){
oneButton.tag = SHARE_ITEM_WEIXIN_TIMELINE;
[oneButton showBadgeWithStyle:WBadgeStyleNew value:0 animationType:WBadgeAnimTypeBreathe];
oneButton.badgeCenterOffset=CGPointMake(-17, 3);
}else if([textList[i] isEqualToString:NSLocalizedString(@"QQ", nil)]){
oneButton.tag = SHARE_ITEM_QQ;
}else if([textList[i] isEqualToString:NSLocalizedString(@"QQ空间", nil)]){
oneButton.tag = SHARE_ITEM_QZONE;
}else if([textList[i] isEqualToString:NSLocalizedString(@"微博", nil)]){
oneButton.tag = SHARE_ITEM_WEIBO;
}
[shareActionView addSubview:oneButton];
}
}
-(void)loadData{
textList=[[NSMutableArray alloc]init];
iconList =[[NSMutableArray alloc]init];
_tencentOAuth =[[TencentOAuth alloc]initWithAppId:TENCENTKEY andDelegate:self];
//初始化redirectURL
_tencentOAuth.redirectURI=@"www.qq.com";
//设置应用需要用户授权的API列表
_permissons=[[NSArray alloc]initWithObjects:@"get_user_info", @"get_simple_userinfo", @"add_t", nil];
if (self.type==TYPE_SHARE) {
[iconList addObject:@"[email protected]"];
[textList addObject:NSLocalizedString(@"朋友圈", nil)];
[iconList addObject:@"[email protected]"];
[textList addObject:NSLocalizedString(@"QQ空间", nil)];
}
[iconList addObject:@"[email protected]"];
[textList addObject:NSLocalizedString(@"微信好友", nil)];
[iconList addObject:@"[email protected]"];
[textList addObject:NSLocalizedString(@"QQ", nil)];
[iconList addObject:@"[email protected]"];
[textList addObject:NSLocalizedString(@"微博", nil)];
}
-(void)clickedButton:(HyyUIButton *)sender{
if ( sender.tag == SHARE_ITEM_WEIXIN_SESSION ) {
if (self.type==TYPE_SHARE) {
[self shareToWeixinSession];
}else{
[self loginWithWeiXin];
}
}else if ( sender.tag == SHARE_ITEM_WEIXIN_TIMELINE ) {
[sender clearBadge];
[self shareToWeixinTimeline];
}else if ( sender.tag == SHARE_ITEM_QQ ) {
if (self.type==TYPE_SHARE) {
[self shareToQQ];
}else{
[self loginWithQQ];
}
}else if ( sender.tag == SHARE_ITEM_QZONE ) {
[self shareToQzone];
}else if ( sender.tag == SHARE_ITEM_WEIBO ) {
if (self.type==TYPE_SHARE) {
[self shareToWeibo];
}else{
[self loginWithWeiBo];
}
}
}
-(void)loginWithWeiXin{
SendAuthReq* req = [[SendAuthReq alloc] init] ;
req.scope = @"snsapi_message,snsapi_userinfo,snsapi_friend,snsapi_contact"; // @"post_timeline,sns"
req.state = @"xxx";
req.openID = @"0c806938e2413ce73eef92cc3";
[WXApi sendReq:req];
}
-(void)shareToWeixinSession{
BOOL canOpenWeiXin =[[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"weixin://"]];
if (!canOpenWeiXin ) {
[self showWithMessage:@"请确定你是否安装了微信"];
return;
}
WXMediaMessage *message = [WXMediaMessage message];
WXImageObject *object=[WXImageObject object];
NSData * data;
if (!self.shareImage) {
NSString * filePath = [[NSBundle mainBundle]pathForResource:self.shareImageName ofType:nil];
data = [NSData dataWithContentsOfFile:filePath];
}
else{
data =UIImageJPEGRepresentation(self.shareImage, 1);
}
object.imageData=data;
message.mediaObject=object;
SendMessageToWXReq * req =[[SendMessageToWXReq alloc]init];
req.bText=NO;
req.message=message;
req.scene=WXSceneSession;
[WXApi sendReq:req];
}
-(void)shareToWeixinTimeline{
BOOL canOpenWeiXin =[[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"weixin://"]];
if (!canOpenWeiXin ) {
[self showWithMessage:@"请确定你是否安装了微信"];
return;
}
WXMediaMessage *message = [WXMediaMessage message];
WXImageObject *object=[WXImageObject object];
NSData * data;
if (!self.shareImage) {
NSString * filePath = [[NSBundle mainBundle]pathForResource:self.shareImageName ofType:nil];
data = [NSData dataWithContentsOfFile:filePath];
}
else{
data =UIImageJPEGRepresentation(self.shareImage, 1);
}
object.imageData=data;
message.mediaObject=object;
SendMessageToWXReq * req =[[SendMessageToWXReq alloc]init];
req.bText=NO;
req.message=message;
req.scene=WXSceneTimeline;
[WXApi sendReq:req];
}
//分享到微信的回调消息
-(void)onReq:(BaseReq *)req{
NSLog(@"%@",req);
}
-(void)onResp:(BaseResp *)resp{
NSLog(@"%@",resp);
}
-(void)loginWithQQ{
[_tencentOAuth authorize:_permissons inSafari:NO];
}
-(void)shareToQQ{
BOOL canOpenQQ =[[UIApplication sharedApplication]canOpenURL:[NSURL URLWithString:@"QQ://"]];
NSLog(@"------%d",canOpenQQ);
if (canOpenQQ) {
NSData * data;
if (!self.shareImage) {
NSString * filePath = [[NSBundle mainBundle]pathForResource:self.shareImageName ofType:nil];
data = [NSData dataWithContentsOfFile:filePath];
}
else{
data =UIImageJPEGRepresentation(self.shareImage, 1);
}
QQApiImageObject * imageObject =[QQApiImageObject objectWithData:data previewImageData:data title:self.shareTitle description:self.shareText ];
SendMessageToQQReq *req =[SendMessageToQQReq reqWithContent:imageObject];
QQApiSendResultCode send=[QQApiInterface sendReq:req];
NSLog(@"%d",send);
}else{
QQApiNewsObject *newsObj =[QQApiNewsObject objectWithURL:[NSURL URLWithString:self.shareUrl] title:self.shareTitle description:self.shareText previewImageURL:[NSURL URLWithString:self.shareImageUrl]];
SendMessageToQQReq *req =[SendMessageToQQReq reqWithContent:newsObj];
QQApiSendResultCode sent=[QQApiInterface sendReq:req];
if (sent==0) {
// [self showWithMessage:@"success"];
NSLog(@"%d",sent);
}else {
NSLog(@"%d",sent);
}
}
}
-(void)shareToQzone{
QQApiNewsObject *newsObj =[QQApiNewsObject objectWithURL:[NSURL URLWithString:self.shareUrl] title:self.shareTitle description:self.shareText previewImageURL:[NSURL URLWithString:self.shareImageUrl]];
SendMessageToQQReq *req =[SendMessageToQQReq reqWithContent:newsObj];
QQApiSendResultCode sent=[QQApiInterface SendReqToQZone:req];
if (sent==0) {
NSLog(@"%d",sent);
}else {
NSLog(@"%d",sent);
}
}
#pragma mark -tencent分享代理-
-(void)checkPageFansResponse:(APIResponse *)response{
//分享到QZone回调
NSLog(@"%d,%d,%d%@%@",response.seq,response.retCode,response.detailRetCode,response.errorMsg,response.message);
}
-(void)tencentDidLogin{
if (_tencentOAuth.accessToken &&0!=[_tencentOAuth.accessToken length])
{ [self showWithMessage:@"登陆成功"];
}else{
[self showWithMessage:@"登陆失败"];
}
}
-(void)tencentDidNotNetWork{
[self showWithMessage:@"请检查网络连接"];
}
-(void)tencentDidNotLogin:(BOOL)cancelled{
if (cancelled) {
[self showWithMessage:@"用户取消了登录"];
}else{
[self showWithMessage:@"登录失败请重试"];
}
}
-(void)cgiRequest:(TCAPIRequest *)request didResponse:(APIResponse *)response{
}
-(NSArray *)getAuthorizedPermissions:(NSArray *)permissions withExtraParams:(NSDictionary *)extraParams{
return nil;
}
-(void)tencentDidLogout{
}
-(BOOL)tencentNeedPerformIncrAuth:(TencentOAuth *)tencentOAuth withPermissions:(NSArray *)permissions{
return YES;
}
-(void)addShareResponse:(APIResponse *)response{
}
-(void)getUserInfoResponse:(APIResponse *)response{
}
#pragma mark -分享到新浪微博-
-(void)loginWithWeiBo{
[WeiboSDK messageRegister:@"验证码登陆"];
}
-(void)shareToWeibo{
WBMessageObject *message=[WBMessageObject message];
message.text=self.shareText;
WBImageObject *image=[WBImageObject object];
NSData * data;
if (!self.shareImage) {
NSString * filePath = [[NSBundle mainBundle]pathForResource:self.shareImageName ofType:nil];
data = [NSData dataWithContentsOfFile:filePath];
}
else{
data =UIImageJPEGRepresentation(self.shareImage, 1);
}
image.imageData=data;
message.imageObject=image;
AppDelegate * myDelegate =(AppDelegate *)[UIApplication sharedApplication].delegate;
WBAuthorizeRequest * authRequest =[WBAuthorizeRequest request];
authRequest.redirectURI= XLkRedirectURL;
authRequest.scope=@"all";
WBSendMessageToWeiboRequest * request =[WBSendMessageToWeiboRequest requestWithMessage:message authInfo:nil access_token:myDelegate.wbtoken];
[WeiboSDK sendRequest:request];
}
//新浪微博的代理协议
-(void)didReceiveWeiboResponse:(WBBaseResponse *)response{
[self showWithMessage:response];
}
-(void)didReceiveWeiboRequest:(WBBaseRequest *)request{
[self showWithMessage:request];
}
-(void)closeShareView{
[UIView animateWithDuration:1 animations:^{
self.alpha=0;
} completion:^(BOOL finished) {
[self removeFromSuperview];
}];
}
-(void)showWithMessage:(id)message{
UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:nil message:[NSString stringWithFormat:@"%@",message] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alertView show];
}
@end