分享一哈微信和QQ分享网页链接的代码,直接上代码,OC版本的
AppDelegate中注册
//导入头文件
#import "WXApi.h"
#import <TencentOpenAPI/QQApiInterface.h>
#import <TencentOpenAPI/TencentOAuth.h>
//注册
//微信
[WXApi registerApp:@"***************"];
//QQ
[[TencentOAuth alloc]initWithAppId:@"********" andDelegate:nil];
AppDelegate中分享回调
- (void)onResp:(BaseResp *)resp {
//微信
if ([resp isKindOfClass:[SendMessageToWXResp class]]) {
switch (resp.errCode) {
case WXSuccess:
NSLog(@"%@", [NSString stringWithFormat:@"wx share success,%@",resp.errStr]);
[[NSNotificationCenter defaultCenter]postNotificationName:@"shareResultNotification" object:@"success"];
break;
case WXErrCodeUserCancel:
NSLog(@"%@", [NSString stringWithFormat:@"wx share cancel,%@",resp.errStr]);
[[NSNotificationCenter defaultCenter]postNotificationName:@"shareResultNotification" object:@"cancel"];
break;
default:
NSLog(@"%@", [NSString stringWithFormat:@"wx share fail,%@",resp.errStr]);
break;
}
//QQ
} else if ([resp isKindOfClass:[SendMessageToQQResp class]]) {
SendMessageToQQResp *qqresp = (SendMessageToQQResp *)resp;
NSLog(@"%@ = %@",qqresp.result,qqresp.errorDescription);
if ([qqresp.result isEqualToString:@"0"]) {
NSLog(@"qq share success");
[[NSNotificationCenter defaultCenter]postNotificationName:@"shareResultNotification" object:@"success"];
}else if ([qqresp.result isEqualToString:@"-4"]) {
[[NSNotificationCenter defaultCenter]postNotificationName:@"shareResultNotification" object:@"cancel"];
NSLog(@"qq share cancel %@",qqresp.errorDescription);
}
}
}
具体分享代码
#pragma mark -- share to wechat
- (void)sendShareUrlToWXScenneSession:(NSInteger)session {
if ([WXApi isWXAppInstalled] && [WXApi isWXAppSupportApi]) {
WXMediaMessage *message = [WXMediaMessage message];
message.title = @"分享的标题";
message.description = @"分享的内容描述";
message.thumbData = UIImagePNGRepresentation([UIImage imageNamed:@"分享icon"]);
WXWebpageObject *web = [WXWebpageObject object];
web.webpageUrl = @"分享的url";
message.mediaObject = web;
SendMessageToWXReq *req = [[SendMessageToWXReq alloc]init];
req.bText = NO;
req.message = message;
req.scene = (int)session;
[WXApi sendReq:req];
}else {
[MBProgressHUD show:@"当前手机未安装微信或微信版本过低" view:self.view];
}
}
#pragma mark -- share to qq
- (void)sendShareUrlToQQWithType:(NSInteger)type {
if ([QQApiInterface isQQInstalled] && [QQApiInterface isQQSupportApi]) {
NSURL *url = [NSURL URLWithString:@"分享的url"];
NSData *imageData = UIImagePNGRepresentation([UIImage imageNamed:@"分享icon"]);
QQApiNewsObject *newsObj = [QQApiNewsObject objectWithURL:url title:@"分享的描述" previewImageData:imageData];
newsObj.shareDestType = ShareDestTypeQQ;
SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:newsObj];
if (type == 0) {
QQApiSendResultCode send = [QQApiInterface sendReq:req];
NSLog(@"%d",send);
}else if (type == 1) {
QQApiSendResultCode send = [QQApiInterface SendReqToQZone:req];
NSLog(@"%d",send);
}
}else {
[MBProgressHUD show:@"当前手机未安装QQ或QQ版本过低" view:self.view];
}
}
网友评论