微信和QQ分享网页链接

分享一哈微信和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];
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,455评论 25 708
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,250评论 4 61
  • 一直以来我都只有一个愿望,过一次生日,拥有一个属于自己的蛋糕,能和全家人一起庆祝。可是一直都只是个梦。 多可惜。 ...
    少女鹿阅读 258评论 0 0
  • 愿得一人铭心上,白首绛,勿相忘。黄发阙齿,笑看落夕阳。纵然世人皆由怨,泯然笑,相顾忙。 老滋回眸再凝望,琼椅晃,象...
    紫色领结阅读 766评论 0 3