iOS - QQ分享的简单使用

作者: 重庆妹子在霾都 | 来源:发表于2016-05-28 17:37 被阅读9544次

写在前面

要想把QQ分享用起来前面最基本的配置些我就不赘述了,如果还不是特别清楚请移步至我的另一篇文章http://www.jianshu.com/p/bafce0975fec, 关于微信分享这篇文章里面有详细的步骤,我这里只说如何调用QQ的SDK来实现分享的功能。

1.handle open url:在AppDelegate.m文件里面添加如下代码:

- (BOOL)application:(UIApplication *)application openURL:(nonnull NSURL *)url options:(nonnull NSDictionary<NSString *,id> *)options {
    // 因为在我们的应用中可能不止实现一种分享途径,可能还有微信啊 微博啊这些,所以在这里最好判断一下。
   // QQAPPID:是你在QQ开放平台注册应用时候的AppID
    if ([url.scheme isEqualToString:WXAPPID]) {
        return [WXApi handleOpenURL:url delegate:self];
    }else if ([url.scheme isEqualToString:[NSString stringWithFormat:@"tencent%@",QQAPPID]]) {
      return [QQApiInterface handleOpenURL:url delegate:self];
    }else {
        return YES;
    }
}

2.实现QQApiInterfaceDelegate代理方法

#pragma mark -- QQApiInterfaceDelegate
// 处理来自QQ的请求,调用sendResp
- (void)onReq:(QQBaseReq *)req {
 
}

// 处理来自QQ的响应,调用sendReq
- (void)onResp:(QQBaseReq *)resp {
    switch (resp.type)
    {
        case ESENDMESSAGETOQQRESPTYPE:
        {
            SendMessageToQQResp* sendResp = (SendMessageToQQResp*)resp;
            if ([sendResp.result isEqualToString:@"0"]) {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"成功" message:@"QQ分享成功" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                [alert show];
            }
            else {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"失败" message:@"QQ分享失败" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                [alert show];
            }
            break;
        }
        default:
        {
            break;
        }
    }
}
// 处理QQ在线状态的回调
- (void)isOnlineResponse:(NSDictionary *)response {
    
}

3.实现QQ分享的具体调用

#pragma mark -- lazy load
- (UIButton *)QQInviteButton {
    if (_QQInviteButton == nil) {
        _QQInviteButton = [[UIButton alloc] initWithFrame:CGRectMake(50, 250, 80, 30)];
        _QQInviteButton.backgroundColor = [UIColor greenColor];
        [_QQInviteButton setTitle:@"QQ好友邀请" forState:UIControlStateNormal];
        _QQInviteButton.titleLabel.font = [UIFont systemFontOfSize:16.0];
        _QQInviteButton.layer.cornerRadius = 5;
        _QQInviteButton.layer.borderWidth = 1;
        _QQInviteButton.layer.borderColor = [UIColor grayColor].CGColor;
        [_QQInviteButton addTarget:self action:@selector(QQInviteButtonClick) forControlEvents:UIControlEventTouchUpInside];
        _QQInviteButton.clipsToBounds = YES;
    }
    return _QQInviteButton;
}

- (UIButton *)QQZoneInviteButton {
    if (_QQZoneInviteButton == nil) {
        _QQZoneInviteButton = [[UIButton alloc] initWithFrame:CGRectMake(150, 250, 120, 30)];
        _QQZoneInviteButton.backgroundColor = [UIColor greenColor];
        [_QQZoneInviteButton setTitle:@"QQ空间邀请" forState:UIControlStateNormal];
        _QQZoneInviteButton.titleLabel.font = [UIFont systemFontOfSize:16.0];
        _QQZoneInviteButton.layer.cornerRadius = 5;
        _QQZoneInviteButton.layer.borderWidth = 1;
        _QQZoneInviteButton.layer.borderColor = [UIColor grayColor].CGColor;
        [_QQZoneInviteButton addTarget:self action:@selector(QQZoneInviteButtonClick) forControlEvents:UIControlEventTouchUpInside];
        _QQZoneInviteButton.clipsToBounds = YES;
    }
    return _QQZoneInviteButton;
}

#pragma mark -- life circle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.QQInviteButton];
    [self.view addSubview:self.QQZoneInviteButton];
}

#pragma mark -- button click
- (void)QQInviteButtonClick {
    [self showMediaNewsWithScene:0];
}

- (void)QQZoneInviteButtonClick {
    
    [self showMediaNewsWithScene:1];
}

#pargma mark -- share method
// 发送纯文本
- (void)shareWithText {

 if (![TencentOAuth iphoneQQInstalled]) {
        NSLog(@"请移步App Store去下载腾讯QQ客户端");
    }else {
        // 这里要先授权,QQ的文档里面貌似没写
        self.tencentOAuth = [[TencentOAuth alloc] initWithAppId:QQAPPID
                                                    andDelegate:self];
        QQApiTextObject *newsObj = [QQApiTextObject objectWithText:@"QQ分享到好友列表的测试!"];   
       SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:newsObj];
      // PS:好多网友反馈问在这里提示API接口错误是什么原因,检查是否设置白名单,作者就是忘了设置白名单一直提示API接口错误。
        NSLog(@"haha - %d",[QQApiInterface sendReq:req]);
    }
}

// 发送图片文字链接
- (void)showMediaNewsWithScene:(int)scene {
   if (![TencentOAuth iphoneQQInstalled]) {
      NSLog(@"请移步App Store去下载腾讯QQ客户端");
    }else {
        self.tencentOAuth = [[TencentOAuth alloc] initWithAppId:QQAPPID
                                                    andDelegate:self];
        QQApiNewsObject *newsObj = [QQApiNewsObject
                                    objectWithURL:@"www.baidu.com"
                                    title:@"李易峰撞车了"
                                    description:@"李易峰的兰博基尼被撞了李易峰的兰博基尼被撞了李易峰的兰博基尼被撞了"
                                    previewImageURL:imageURL];
        SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:newsObj]; 
        if (scene == 0) {
             NSLog(@"QQ好友列表分享 - %d",[QQApiInterface sendReq:req]);
        }else if (scene == 1){
            NSLog(@"QQ空间分享 - %d",[QQApiInterface SendReqToQZone:req]);
        }
    }
}

总结

这里只是简单的分享使用,要详细的还是文档比较详细:http://wiki.open.qq.com/wiki/IOS_API%E8%B0%83%E7%94%A8%E8%AF%B4%E6%98%8E

最后:可能不同的SDK版本也是有一些写法上的差异,建议开发者们遇到问题还是先去看看他们的文档,很多问题都能解决。

相关文章

网友评论

  • 快叫朕万岁:swift好像不能强制转成id或者anyObject或者anyClass,用这个方法可以解决冲突,但是每次都会创建一个tool,能不能用单例实现。
  • ZMSBang:官方文档写的真蛋疼 还是这个好 不过我写的过程中遇到了一个问题就是怎么点都没反应 后来上网查了一下 添上一句代码搞定:qqNews.shareDestType = ShareDestTypeQQ; 妹纸写的很棒,赞一个👍 长得漂亮又会写代码的我第一次见(对了还有一个问题就是 我在openURL方法里是这样写的:[url.scheme isEqualToString:@"QQ41EECC5C"] 【我项目里有用友盟分享,当时是urls.cheme QQ是添加了两个】)
  • GavinKang:妹子,原生分享好像不能分享ZIP文件,你知道什么可以分享ZIP文件到QQ吗??
  • Stone_熊小叔:QQ官网看不懂....需要导入什么头文件吗.
  • ba9d15a97b9b:我测分享图片的回调,手机分享到qq和空间的都没问题;pad分享到qq可以,但是分享到空间没有回调,不知姐姐遇到没有?:fearful:
    ba9d15a97b9b:@重庆妹子在霾都 已解决。定义QQApiImageArrayForQZoneObject 类型的content就行了。不得不说,qq分享这个sdk还是有不少光怪陆离之处。:dizzy_face:
    重庆妹子在霾都:我没有在ipad上面试过 所以也不太清楚是什么情况 分享成功了吗没有回调。
  • 亚瑟王666:敢问美女,qq分享没有成功或失败回调么
    重庆妹子在霾都:分享成功返回当前应用才会走这个方法 或者从扣扣分享页面取消了分享也会走这个方法。
    亚瑟王666:@84bbf34f3758 额,没有收到呀呀,我打log断点完全走不到。只能在sendreq之后有返回值,0。网上也有不少人吐槽qq分享没回调。菇凉are u sure ?
    重庆妹子在霾都:文章- (void)onResp:(QQBaseReq *)resp里面就是回调。
  • 水户洋平_Psist:只是分享到QQ里面内容的话 不需要QQApiInterfaceDelegate代理方法吧?
  • 莫莫H:QQApiInterfaceDelegate代理方法 是写在哪里的?
    重庆妹子在霾都:@hanliey - (BOOL)application:(UIApplication *)application openURL:(nonnull NSURL *)url options:(nonnull NSDictionary<NSString *,id> *)options这方法里面你设置它代理是谁就写哪里面 我这里设置的AppDelegate 你也可以自己稍微封装一下。
  • 不误正业的开发者:这个是被leader逼着做原生分享你 ﹉
    无夜之星辰:@JeversonJee 哈哈 :smile:

本文标题:iOS - QQ分享的简单使用

本文链接:https://www.haomeiwen.com/subject/jcgfdttx.html