iOS-微信sdk使用汇总
URL分享
WXMediaMessage * message = [WXMediaMessage message];
message.title = title;
message.description = desc;
[message setThumbImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:img]]]];
WXWebpageObject * web = [WXWebpageObject object];
web.webpageUrl = [NSString stringWithH5Route:url];
message.mediaObject = web;
SendMessageToWXReq *req= [[SendMessageToWXReq alloc]init];
req.bText = NO;
req.message = message;
req.scene = WXSceneSession;
[WXApi sendReq:req];
分享小程序
NSString *filePath = [[NSBundle mainBundle] pathForResource:@”shareMiniprogram” ofType:@”png”];
NSData *image = [NSData dataWithContentsOfFile:filePath];
NSRange range = [[NSString stringWithH5Route:_shareUrl] rangeOfString:@”?client”];//匹配得到的下标
NSString* urlStr = [[NSString stringWithH5Route:_shareUrl] substringToIndex:range.location];//截取范围类的字符串
WXMiniProgramObject *wxMiniObject = [WXMiniProgramObject object];
wxMiniObject.userName = @”gh_8de940b01a61″;
wxMiniObject.webpageUrl = urlStr; //打不开小程序的自动转成网页
wxMiniObject.path = [[[@”pages/cvShare/cvShare?url=” stringByAppendingString:urlStr] stringByAppendingString:@”&title=”]stringByAppendingString:_clName];
wxMiniObject.hdImageData = image;
WXMediaMessage *message = [WXMediaMessage message];
message.title = _clName;
message.description = @””;
message.mediaObject = wxMiniObject;
message.thumbData = image; //分享出去的网页的图片
SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
req.message = message;
req.scene = WXSceneSession;
[WXApi sendReq:req];
直接打开小程序
WXLaunchMiniProgramReq *launchMiniProgramReq = [WXLaunchMiniProgramReq object];
launchMiniProgramReq.userName = name; //拉起的小程序的username(gh_7a4d78xxxxx)
launchMiniProgramReq.path = @“pages/index/index”; //拉起小程序页面的可带参路径,不填默认拉起小程序首页
launchMiniProgramReq.miniProgramType = WXMiniProgramTypeRelease;
[WXApi sendReq:launchMiniProgramReq];
Image分享
NSDictionary * dataDir = (NSDictionary*)data;
NSString * img = [NSString stringWithFormat:@”%@”, [dataDir objectForKey:@”img”]];
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:img]]];
NSData*imageData = UIImageJPEGRepresentation(image, 0.7);
WXImageObject *imageObject = [WXImageObject object];
imageObject.imageData = imageData;
WXMediaMessage *message = [WXMediaMessage message];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@”invite”
ofType:@”png”];
message.thumbData = [NSData dataWithContentsOfFile:filePath];
message.mediaObject = imageObject;
SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
req.bText = NO;
req.message = message;
req.scene = WXSceneTimeline;//分享到朋友圈 分享给会话是WXSceneSession
[WXApi sendReq:req];
ps: 几点注意
1.微信小程序分享图片不能超过32k 否则分享不出去
2.微信小程序分享图片必须是nsdata格式
NSString *filePath = [[NSBundle mainBundle] pathForResource:@”shareMiniprogram” ofType:@”png”];
NSData *image = [NSData dataWithContentsOfFile:filePath];
所以一定要这么取
ps:有时候图片拖进项目里会找不到图片这个时候需要选中图片之后选择菜单中的
Source Control —> add selected files 就加进去了
NSString * img = [NSString stringWithFormat:@”%@”, [dataDir objectForKey:@”img”]];
NSURL *url = [NSURL URLWithString:img];
NSData *imgData = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:imgData];