您的当前位置:首页正文

ios QQ分享

来源:要发发知识网
  • 导入SDK
    TencentOpenAPI.framework TencentOpenApi_iOS_Bundle.bundle


    导入SDK .png
  • 添加依赖库
    (记得16年第一次做 QQ分享的时候 参考的文档添加的依赖库,最后什么都配置好了,却一直分享不了。。流程检测了好几遍都没问题,也没有任何报错。。。。 无奈与官方Demo 对比 才发现文档上少写了几个库。。。。。不过 ,最近看 好像文档已经补全了。。。。。尽管文档还是很乱。。尽管官方Demo还是运行不了。。。。。)

添加SDK依赖的系统库文件。分别是”Security.framework”, “libiconv.dylib”,“SystemConfiguration.framework”,“CoreGraphics.Framework”、“libsqlite3.dylib”、“CoreTelephony.framework”、“libstdc++.dylib”、“libz.dylib”。


添加依赖库.png
  • 添加白名单以及URL scheme
    QQ与QQ空间白名单:mqzoneopensdk、mqzoneopensdkapi、mqzoneopensdkapi19、mqzoneopensdkapiV2、mqqOpensdkSSoLogin、mqqopensdkapiV2、mqqopensdkapiV3、wtloginmqq2、mqqapi、mqqwpa、mqzone、mqq。


    白名单.png
    F4E0224D-DF0B-4096-9870-18317BEF1683.png
  • 注册并实现方法

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    //注意: 初始化授权 开发者需要在这里填入自己申请到的 AppID
    _oauth = [[TencentOAuth alloc] initWithAppId:__TencentDemoAppid__ andDelegate:self];
    
    
    return YES;
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation


{     NSString *path = [url absoluteString];
    if ([self.qqDelegate respondsToSelector:@selector(shareSuccssWithQQCode:)]) {
        [self.qqDelegate shareSuccssWithQQCode:[[path substringWithRange:NSMakeRange([path rangeOfString:@"&error="].location+[path rangeOfString:@"&error="].length, [path rangeOfString:@"&version"].location-[path rangeOfString:@"&error="].location)] integerValue]];
    }
    return [TencentOAuth HandleOpenURL:url];

}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    return [TencentOAuth HandleOpenURL:url];
}

具体实现 (这里只记录了其中一种分享方式 其他的参考文档)
sendReq QQ

  NSURL *url = [NSURL URLWithString:_webModel.url];
            NSURL *preimageUrl = [NSURL URLWithString:_webModel.image];
            QQApiNewsObject* img = [QQApiNewsObject objectWithURL:url title:_webModel.title description:[NSString stringWithFormat:@"%@",_webModel.content] previewImageURL:preimageUrl];
            
            
            SendMessageToQQReq* req = [SendMessageToQQReq reqWithContent:img];
            
            appdelegate.qqDelegate = self;
            
            QQApiSendResultCode sent = [QQApiInterface sendReq:req];//分享给QQ好友
            
            [self handleSendResult:sent];

SendReqToQZone QQ空间

 NSURL *url = [NSURL URLWithString:_webModel.url];
            NSURL *preimageUrl = [NSURL URLWithString:_webModel.image];
            QQApiNewsObject* img = [QQApiNewsObject objectWithURL:url title:_webModel.title description:[NSString stringWithFormat:@"%@",_webModel.content] previewImageURL:preimageUrl];
            
            //请求帮助类,分享的所有基础对象,都要封装成这种请求对象。
            SendMessageToQQReq* req = [SendMessageToQQReq reqWithContent:img];
            
            //通过自定义的qqdelegate来通知本controller,是否成功分享
            appdelegate.qqDelegate = self;
            
            QQApiSendResultCode sent = [QQApiInterface SendReqToQZone:req];//分享到QQ空间
            
            [self handleSendResult:sent];

回调

- (void)handleSendResult:(QQApiSendResultCode)sendResult
{
    switch (sendResult)
    {
            
        case EQQAPIAPPNOTREGISTED:
        {
            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"App未注册" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
            [msgbox show];
            
            
            break;
        }
        case EQQAPIMESSAGECONTENTINVALID:
        case EQQAPIMESSAGECONTENTNULL:
        case EQQAPIMESSAGETYPEINVALID:
        {
            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"发送参数错误" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
            [msgbox show];
            
            
            break;
        }
        case EQQAPIQQNOTINSTALLED:
        {
            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"您的设备未安装手机QQ" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
            [msgbox show];
            
            
            break;
        }
        case EQQAPIQQNOTSUPPORTAPI:
        {
            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"您的设备未安装手机QQ" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
            [msgbox show];
            
            
            break;
        }
        case EQQAPISENDFAILD:
        {
            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"发送失败" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
            [msgbox show];
            
            
            break;
        }
        case EQQAPIVERSIONNEEDUPDATE:
        {
            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"当前QQ版本太低,需要更新" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
            [msgbox show];
            break;
        }
        default:
        {
            break;
        }
    }
}

#pragma mark QQ分享回调代理
/* 返回码 对照说明
 0   成功
 -1  参数错误
 -2  该群不在自己的群列表里面
 -3  上传图片失败
 -4  用户放弃当前操作
 -5  客户端内部处理错误
 */
-(void)shareSuccssWithQQCode:(NSInteger)code{
    NSLog(@"code %ld",(long)code);
    if (code == 0) {
        UIAlertView *aler = [[UIAlertView alloc]initWithTitle:@"提示" message:@"分享成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [aler show];
    }else{
        UIAlertView *aler = [[UIAlertView alloc]initWithTitle:@"提示" message:@"分享失败" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [aler show];
    }
}