首先按照相应的文档获得相应的key
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[WXApi registerApp:@"wxd930ea5d5a258f4f" withDescription:@"demo 2.0"];
return YES;
}
然后配置相应的url types
注册想你得到的appkey
之后在点击分享的target事件中添加需要的分享信息和链接
if ( [WXApi isWXAppInstalled]) {//isWXAppInstalled判断是否手机上面有微信
// 读取图片
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]];
UIImage *imageNew=[UIImage imageWithData:data];
if (!data || [imageUrl isEqualToString:@""]||!imageUrl) {
imageNew = [UIImage imageNamed:@"AM_Logo_Share"];
}
imageNew = [self scaleToSize:imageNew size:CGSizeMake(imageNew.size.width*0.9, imageNew.size.height*0.9)];
data= UIImageJPEGRepresentation(imageNew,1);//调整图片大小的,这个是自己的图片有些大
if ([data length]> 30*1024) {
data= UIImageJPEGRepresentation(imageNew,0.1);
}
WXMediaMessage *message = [WXMediaMessage message];
message.title = title; 分享的tittle
message.description =des; 分享的详情
[message setThumbData:data]; 分享的图片
WXWebpageObject *ext = [WXWebpageObject object];
ext.webpageUrl = link; link是分享的链接
message.mediaObject = ext;
SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
req.bText = NO;
req.message = message;
if (type == WeiXin) { //微信好友 判断是分享到朋友圈还是分享到好友
req.scene = WXSceneSession;
}else if (type == WXFriends){//朋友圈
req.scene = WXSceneTimeline;
}else{
}
[WXApi sendReq:req];
}else{
NSLog(@"还没安装微信"); //下面的是,如果没有微信,提示下在微信或者取消
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您还没有安装微信" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"下载安装微信", nil];
[alertView show];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch (buttonIndex) {
case 0:
break;
case 1:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[WXApi getWXAppInstallUrl]]];
break;
default:
break;
}
}
//这是自己调整图片大小的方法
- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{
UIGraphicsBeginImageContext(size);
[img drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}
之后还要跳转相应第三方app的处理
在APPdelegate里面加入两个方法
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
//注释的是如果有多个分享,比如qq ,微博等 而WeChatKey,SinaKey 这些是相应的请求得到的appkey
// if([url.scheme isEqualToString:WeChatKey])
// {
// return [WXApi handleOpenURL:url delegate:self];
// }else if([url.scheme isEqualToString:[NSString stringWithFormat:@"wb%@",SinaKey]])
// {
// return [WeiboSDK handleOpenURL:url delegate:self];
//
// }else
// {
// return [TencentOAuth HandleOpenURL:url];
// }
return [WXApi handleOpenURL:url delegate:self];
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
BOOL isSuc = [WXApi handleOpenURL:url delegate:self];
return isSuc;
}
如果要知道你分享的成功和失败,则需要调用
-(void) onReq:(BaseReq*)req
{
}
和
-(void) onResp:(BaseResp*)resp
{
}
这两个方法,在这两个方法内做相应的处理
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/wenhaiwang/article/details/47084301