微信分享
1、注册第三方软件 + (BOOL) registerApp:(NSString *) appidwithDescription:(NSString *) appdesc
2、使appDelegate成为代理 + (BOOL) handleOpenURL: (NSURL *) urldelegate:(id< WXApiDelegate >) delegate
a、参数url 微信启动第三方应用时传递过来的url
b、参数delegate WXApiDelegate对象
3、发送消息给微信 + (BOOL) sendReq:(BaseReq *) req
a、函数调用后,会切换到微信的界面。第三方应用程序等待微信返回onResp。微信在异步处理完成后一定会调用onResp。支持以下类型 SendAuthReq、SendMessageToWXReq等。
b、BaseReq 基类 可以使用下面的三个子类
4、发送请求后等待微信响应,第一次交互。接收到微信传来的数据后,系统会自动调用
- (void)onResp:(BaseResq *)resp 可选方法
5、- (void)onResp:(BaseResq *)resp 显示微信返回的信息
示例代码:
AppDelegate.h两个重要方法:
//如果第三方程序向微信发送了sendReq的请求,那么onResp会被回调。sendReq请求调用后,会切到微信终端程序界面
- (void)onResp:(id)resp{
// QQ的
if ([resp isKindOfClass:[QQBaseResp class]]) {
}
else if ([resp isKindOfClass:[SendAuthResp class]]) {
SendAuthResp *aresp = (SendAuthResp *)resp;
/*
ErrCode ERR_OK = 0(用户同意)
ERR_AUTH_DENIED = -4(用户拒绝授权)
ERR_USER_CANCEL = -2(用户取消)
code 用户换取access_token的code,仅在ErrCode为0时有效
state 第三方程序发送时用来标识其请求的唯一性的标志,由第三方程序调用sendReq时传入,由微信终端回传,state字符串长度不能超过1K
lang 微信客户端当前语言
country 微信用户当前国家信息
*/
if (aresp.errCode== 0) {
weChatCode = aresp.code;
// NSDictionary *dic = @{@"code":code};
[self getAccessToken];
}
}else if([resp isKindOfClass:[SendMessageToWXResp class]])
{
SendMessageToWXResp *rsp = (SendMessageToWXResp*)resp;
NSString *strTitle = [NSString stringWithFormat:@"发送媒体消息结果"];
NSString *strMsg = [NSString stringWithFormat:@"errcode:%d", rsp.errCode];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle message:strMsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
// else if ([resp isKindOfClass:[AddCardToWXCardPackageResp class]])
// {
// AddCardToWXCardPackageResp* temp = (AddCardToWXCardPackageResp*)resp;
// NSMutableString* cardStr = [[NSMutableString alloc] init];
// for (WXCardItem* cardItem in temp.cardAry) {
// [cardStr appendString:[NSString stringWithFormat:@"cardid:%@ cardext:%@ cardstate:%u\n",cardItem.cardId,cardItem.extMsg,(unsigned int)cardItem.cardState]];
// }
// UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"add card resp" message:cardStr delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
// [alert show];
// }
}
//onReq是微信终端向第三方程序发起请求,要求第三方程序响应。第三方程序响应完后必须调用sendRsp返回。在调用sendRsp返回时,会切回到微信终端程序界面
//授权后回调
-(void)onReq:(id)req{
// QQ的
if ([req isKindOfClass:[QQBaseReq class]]) {
}
else if([req isKindOfClass:[GetMessageFromWXReq class]])
{
// GetMessageFromWXReq *temp = (GetMessageFromWXReq *)req;
// 微信请求App提供内容, 需要app提供内容后使用sendRsp返回
NSString *strTitle = [NSString stringWithFormat:@"微信请求App提供内容"];
NSString *strMsg = [NSString stringWithFormat:@"%@", AM_WXAPP_SECRET];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle message:strMsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
alert.tag = 1000;
[alert show];
}
else if([req isKindOfClass:[ShowMessageFromWXReq class]])
{
ShowMessageFromWXReq* temp = (ShowMessageFromWXReq*)req;
WXMediaMessage *msg = temp.message;
//显示微信传过来的内容
WXAppExtendObject *obj = msg.mediaObject;
NSString *strTitle = [NSString stringWithFormat:@"微信请求App显示内容"];
NSString *strMsg = [NSString stringWithFormat:@"openID: %@, 标题:%@ \n内容:%@ \n附带信息:%@ \n缩略图:%lu bytes\n附加消息:%@\n", AM_WXAPP_SECRET, msg.title, msg.description, obj.extInfo, (unsigned long)msg.thumbData.length, msg.messageExt];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle message:strMsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
else if([req isKindOfClass:[LaunchFromWXReq class]])
{
LaunchFromWXReq *temp = (LaunchFromWXReq *)req;
WXMediaMessage *msg = temp.message;
//从微信启动App
NSString *strTitle = [NSString stringWithFormat:@"从微信启动"];
NSString *strMsg = [NSString stringWithFormat:@"%@, messageExt:%@", AM_WXAPP_SECRET, msg.messageExt];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle message:strMsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
}
#import "WXApi.h"
// 构造微信分享内容
WXMediaMessage *message = [WXMediaMessage message];
message.title = @“your title";
message.description = @“your description";
[message setThumbImage:[UIImage imageNamed:@"article_1@2x.png"]];
WXWebpageObject *ext = [WXWebpageObject object];
ext.webpageUrl = @"http://t.cn/Rxakj";
message.mediaObject = ext;
message.mediaTagName = @"WECHAT_TAG_JUMP_SHOWRANK";
SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
req.bText = NO;
req.message = message;
// 分享微信好友
req.scene = WXSceneSession;
// 分享微信朋友圈
req.scene = WXSceneTimeline;
[WXApi sendReq:req];
QQ分享1.在工程中添加QQ互联IOS SDK2.生成分享的QQApiObject3.生成sendReq发送消息4.通过判断SendReq方法的返回值,查看是否成功调用定向分享5.查看分享出去的消息#import//构造QQ分享内容
NSString *utf8String = @"http://t.cn/Rxakj";
NSString *title = @“your title";
NSString *description = @“your description";
NSString *imgPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"article_1@2x.png"];
NSData *imgData = [NSData dataWithContentsOfFile:imgPath];
QQApiNewsObject *newsObj = [QQApiNewsObject objectWithURL:[NSURL URLWithString:utf8String] title:title description:description previewImageData:imgData];
SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:newsObj];
//将内容分享到qq
[QQApiInterface sendReq:req];
//将内容分享到qzone
[QQApiInterface SendReqToQZone:req];