1.在腾讯官网申请APP ID 和 APP KEY
2.添加URL type
3.将系统库文件TencentOpenApi_IOS_Bundle和TencentOpenAPI集成到项目中
4.添加SDK依赖的系统库文件
“Security.framework”、“libiconv.dylib”、“SystemConfiguration.framework”、“CoreGraphics.Framework”、“libsqlite3.dylib”、“CoreTelephony.framework”、“libstdc++.dylib”、“libz.dylib”
5.在AppDelegate.h中 导入 头文件
a.导入头文件
#import <TencentOpenAPI/TencentOAuth.h>
#import <TencentOpenAPI/QQApiInterface.h>
b.添加属性
@property (nonatomic, strong) TencentOAuth *oauth;
c.在app代理方法中注册QQ
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
self.oauth = [[TencentOAuth alloc] initWithAppId:@"1104757171" andDelegate:self];
self.oauth.redirectURI = nil;
}
6.在info.plist中的LSApplicationQueriesSchemes中添加如下字段:
7.在需要分享到QQ或QQ空间的地方写如下代码
#pragma mark - 分享到手机QQ
- (void)clickShareMobileQQ:(NSNotification *)note {
if (![TencentOAuthiphoneQQInstalled]) {
[WJShowHUD showMessageWithString:@"请移步App Store去下载手机QQ"];
} else {
NSString *urlLink = [[NSString alloc] init];
if([[AppDelegatesharedInstance].title isEqualToString:@"哈哈哈"]) {
[AppDelegate sharedInstance].redPap = @"哈哈哈";
NSString *shareTitle = @"哈哈哈";
self.shareTitle =shareTitle;
urlLink = self.shareLink;
} else {
[AppDelegate sharedInstance].redPap = @"";
self.shareTitle = [AppDelegate sharedInstance].title != nil ? [AppDelegate sharedInstance].title : @"";
urlLink = [AppDelegate sharedInstance].link != nil ? [AppDelegate sharedInstance].link : @"";
}
NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"share.png"];
NSData* data = [NSData dataWithContentsOfFile:path];
QQApiNewsObject *obj =[QQApiNewsObject objectWithURL:[NSURL URLWithString:urlLink] title:self.shareTitledescription:@""previewImageData:data];
SendMessageToQQReq*req = [SendMessageToQQReq reqWithContent:obj];
QQApiSendResultCodesent = [QQApiInterface sendReq:req];
[self handleSendResult:sent];
}
self.tempBtn.hidden = YES;
self.tempView.hidden = YES;
}
- (void)handleSendResult:(QQApiSendResultCode)sendResult {
switch (sendResult)
{
case EQQAPIAPPNOTREGISTED:
{
UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"App未注册" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
[msgbox show];
break;
}
case EQQAPIMESSAGECONTENTINVALID:
case EQQAPIMESSAGECONTENTNULL:
case EQQAPIMESSAGETYPEINVALID:
{
UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"发送参数错误" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
[msgbox show];
break;
}
case EQQAPIQQNOTINSTALLED:
{
UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"未安装手Q" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
[msgbox show];
break;
}
case EQQAPIQQNOTSUPPORTAPI:
{
UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"API接口不支持" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
[msgbox show];
break;
}
case EQQAPISENDFAILD:
{
UIAlertView *msgbox =[[UIAlertView alloc] initWithTitle:@"Error" message:@"发送失败" delegate:nilcancelButtonTitle:@"取消" otherButtonTitles:nil];
[msgbox show];
break;
}
caseEQQAPIVERSIONNEEDUPDATE:
{
UIAlertView *msgbox =[[UIAlertView alloc] initWithTitle:@"Error" message:@"当前QQ版本太低,需要更新" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
[msgbox show];
break;
}
default:
{
break;
}
}
}
8.在app代理方法中写如下分享后的回调方法
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
if ([url.schemeisEqualToString:@"wxe27d035b12e64362"]) {
//微信分享
return [WXApi handleOpenURL:urldelegate:self];
} elseif ([url.schemeisEqualToString:@"QQ41D941B3"]){
//QQ分享
return [QQApiInterface handleOpenURL:url delegate:self];
} else {
return YES;
}
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
if ([url.schemeisEqualToString:@"wxe27d035b12e64362"]) {
//微信分享
return [WXApi handleOpenURL:urldelegate:self];
} elseif ([url.schemeisEqualToString:@"QQ41D941B3"]){
//QQ分享
return [QQApiInterface handleOpenURL:url delegate:self];
} else {
return YES;
}
}