iOS - QQ分享的简单使用

写在前面

要想把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版本也是有一些写法上的差异,建议开发者们遇到问题还是先去看看他们的文档,很多问题都能解决。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容