微信分享与QQ分享功能现在都挺常见的,可以根据一些第三方社会化分功能快速实现,不过多多少少都不怎么纯净,最好都是自己看官方文档来实现就最好了~
一、微信分享
微信分享功能需要先在微信开放平台注册应用并获取APP ID,就可以获得微信提供的基本接口了
APP ID是对应唯一的应用包名和签名的,所以即使你的应用变了,只要包名和使用的签名key文件不变,就可以继续使用该ID
首先下载微信SDK,导入OpenSDK1.8.0所有文件
在Appdelegate注册微信AppID
//注册微信
[WXApi registerApp:MXWechatAPPID withDescription:@"项目名"];
设置URL_types URLScheme
配置白名单:
微信分享主要代码
一、文字类型分享示例
/** 发送文本到微信 */
- (void)shareSendTextContent:(NSString*)text{
SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
req.text = text;
req.bText = YES;
req.scene = _scene;
[WXApi sendReq:req];
}
二、图片类型分享示例
/** 发送图片到微信 */
- (void)shareSendImageContent:(UIImage*)image {
WXMediaMessage *message = [WXMediaMessage message];
[message setThumbImage:[UIImage imageNamed:@"res5thumb.png"]];
WXImageObject *ext = [WXImageObject object];
ext.imageData = UIImagePNGRepresentation(image);
message.mediaObject = ext;
SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
req.bText = NO;
req.message = message;
req.scene = _scene;
[WXApi sendReq:req];
}
三、音乐类型分享示例
四、视频类型分享示例
五、网页类型分享示例
/** 发送链接到微信 */
- (void)shareSendLinkContent:(NSString*)url title:(NSString*)title description:(NSString*)description thumbImage:(UIImage*)thumbImage{
WXMediaMessage *message = [WXMediaMessage message];
message.title = title;
message.description = description;
[message setThumbImage:thumbImage];
WXWebpageObject *ext = [WXWebpageObject object];
ext.webpageUrl = url;
message.mediaObject = ext;
SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
req.bText = NO;
req.message = message;
req.scene = _scene;
[WXApi sendReq:req];
}
微信分享及收藏目前支持文字、图片、音乐、视频、网页共五种类型,可以分享至微信好友会话、朋友圈或添加到微信收藏
分享或收藏的目标场景,通过修改scene场景值实现。
发送到聊天界面——WXSceneSession
发送到朋友圈——WXSceneTimeline
添加到微信收藏——WXSceneFavorite
二、QQ分享
QQ分享功能同样需要到connect.qq.com申请APP ID
下载官方SDK,导入TencentOpenAPI.framework文件
在Appdelegate注册QQAppID
//注册QQ
_OAuth = [[TencentOAuth alloc] initWithAppId:QQAppID andDelegate:nil];
设置URL_types URLScheme
配置白名单:
QQ分享主要代码
一、文字类型分享示例
//分享文本
- (void)shareWithText:(NSString *)text type:(PlatformSubType)type {
QQApiTextObject *newsObj = [QQApiTextObject objectWithText:text];
newsObj.shareDestType = ShareDestTypeQQ;
switch (type) {
//不要忘记设置这个flag
case 2:
[newsObj setCflag:kQQAPICtrlFlagQQShare];
break;
case 3:
[newsObj setCflag:kQQAPICtrlFlagQZoneShareOnStart];
break;
default:
break;
}
SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:newsObj];
[QQApiInterface sendReq:req];
}
二、图片类型分享示例
//分享图片
- (void)shareWithImage:(UIImage *)image forPlatFormSubType:(PlatformSubType)type {
NSData *data = UIImagePNGRepresentation(image);
QQApiImageObject* imageObj = [QQApiImageObject objectWithData:data previewImageData:data title:nil description:nil];
imageObj.shareDestType = ShareDestTypeQQ;
SendMessageToQQReq* req = [SendMessageToQQReq reqWithContent:imageObj];
switch (type) {
//不要忘记设置这个flag
case 2:
[imageObj setCflag:kQQAPICtrlFlagQQShare];
break;
case 3:
[imageObj setCflag:kQQAPICtrlFlagQZoneShareOnStart];
break;
default:
break;
}
[QQApiInterface sendReq:req];
}
三、网页类型分享示例
//分享链接
- (void)shareWithUrl:(NSString *)url title:(NSString *)title description:(NSString *)description previewImageURL:(NSString *)previewImageURL forPlatFormSubType:(PlatformSubType)type{
QQApiNewsObject *newsObj = [QQApiNewsObject
objectWithURL:[NSURL URLWithString:url]
title:title
description:description
previewImageURL:[NSURL URLWithString:previewImageURL]];
newsObj.shareDestType = ShareDestTypeQQ;
switch (type) {
//不要忘记设置这个flag
case 2:
[newsObj setCflag:kQQAPICtrlFlagQQShare];
break;
case 3:
[newsObj setCflag:kQQAPICtrlFlagQZoneShareOnStart];
break;
default:
break;
}
SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:newsObj];
[QQApiInterface sendReq:req];
}
代码比较简单,思路比较容易理解