首先第一步导入关于第三方的sdk:Required和ShareSDK
然后在other liker flags里面写上-ObjC
再点开工程找到 info 里面的url Types 里面的 url schemes添加微信的APPID:wx4868b35061f87885
再导入关于微信的库
libicucore.dylib
libz.dylib
libstdc++.dylib (这个库在XCode10之后找不到,已经被废弃,以libc++替代)
JavaScriptCore.framework
微信SDK依赖库
libsqlite3.dylib
在 MOBAppKey 和 MOBAppSecret 两个字段里面添加应用的appkey
在appdelegate.m里面写以下代码
#import “AppDelegate.h”
#import <ShareSDK/ShareSDK.h>
#import <ShareSDKConnector/ShareSDKConnector.h>
//腾讯开放平台(对应QQ和QQ空间)SDK头文件
#import <TencentOpenAPI/TencentOAuth.h>
#import <TencentOpenAPI/QQApiInterface.h>
//微信SDK头文件
#import “WXApi.h”
//新浪微博SDK头文件
#import “WeiboSDK.h”
//新浪微博SDK需要在项目Build Settings中的Other Linker Flags添加"-ObjC"
@interface AppDelegate ()
@end
@implementation AppDelegate
-
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//初始化的import参数注意要链接原生新浪微博SDK。
//初始化的import参数注意要链接原生微信SDK。/**初始化ShareSDK应用 @param activePlatforms 使用的分享平台集合 @param importHandler (onImport) 导入回调处理,当某个平台的功能需要依赖原平台提供的SDK支持时,需要在此方法中对原平台SDK进行导入操作 @param configurationHandler (onConfiguration) 配置回调处理,在此方法中根据设置的platformType来填充应用配置信息 */ [ShareSDK registerActivePlatforms:@[ @(SSDKPlatformTypeSinaWeibo), @(SSDKPlatformTypeMail), @(SSDKPlatformTypeSMS), @(SSDKPlatformTypeCopy), @(SSDKPlatformTypeWechat), @(SSDKPlatformTypeQQ), @(SSDKPlatformTypeRenren), @(SSDKPlatformTypeFacebook), @(SSDKPlatformTypeTwitter), @(SSDKPlatformTypeGooglePlus), ] onImport:^(SSDKPlatformType platformType) { switch (platformType) { case SSDKPlatformTypeWechat: [ShareSDKConnector connectWeChat:[WXApi class]]; break; case SSDKPlatformTypeQQ: [ShareSDKConnector connectQQ:[QQApiInterface class] tencentOAuthClass:[TencentOAuth class]]; break; case SSDKPlatformTypeSinaWeibo: [ShareSDKConnector connectWeibo:[WeiboSDK class]]; break; default: break; } } onConfiguration:^(SSDKPlatformType platformType, NSMutableDictionary *appInfo) { switch (platformType) { case SSDKPlatformTypeSinaWeibo: //设置新浪微博应用信息,其中authType设置为使用SSO+Web形式授权 [appInfo SSDKSetupSinaWeiboByAppKey:@"568898243" appSecret:@"38a4f8204cc784f81f9f0daaf31e02e3" redirectUri:@"http://www.sharesdk.cn" authType:SSDKAuthTypeBoth]; break; case SSDKPlatformTypeWechat: [appInfo SSDKSetupWeChatByAppId:@"wx4868b35061f87885" appSecret:@"64020361b8ec4c99936c0e3999a9f249"]; break; case SSDKPlatformTypeQQ: [appInfo SSDKSetupQQByAppId:@"100371282" appKey:@"aed9b0303e3ed1e27bae87c33761161d" authType:SSDKAuthTypeBoth]; break; case SSDKPlatformTypeRenren: [appInfo SSDKSetupRenRenByAppId:@"226427" appKey:@"fc5b8aed373c4c27a05b712acba0f8c3" secretKey:@"f29df781abdd4f49beca5a2194676ca4" authType:SSDKAuthTypeBoth]; break; case SSDKPlatformTypeFacebook: [appInfo SSDKSetupFacebookByApiKey:@"107704292745179" appSecret:@"38053202e1a5fe26c80c753071f0b573" displayName:@"shareSDK" authType:SSDKAuthTypeBoth]; break; case SSDKPlatformTypeTwitter: [appInfo SSDKSetupTwitterByConsumerKey:@"LRBM0H75rWrU9gNHvlEAA2aOy" consumerSecret:@"gbeWsZvA9ELJSdoBzJ5oLKX0TU09UOwrzdGfo9Tg7DjyGuMe8G" redirectUri:@"http://mob.com"]; break; case SSDKPlatformTypeGooglePlus: [appInfo SSDKSetupGooglePlusByClientID:@"232554794995.apps.googleusercontent.com" clientSecret:@"PEdFgtrMw97aCvf0joQj7EMk" redirectUri:@"http://localhost"]; break; default: break; } }]; return YES;
}
然后在viewcontroller.m里面写以下代码
#import “ViewController.h”
#import <ShareSDK/ShareSDK.h>
#import <ShareSDKUI/ShareSDK+SSUI.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
[btn addTarget:self action:@selector(addbtn) forControlEvents:UIControlEventTouchUpInside];
[btn setTitle:@“点击分享” forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor redColor]];
[self.view addSubview:btn];
}
-(void)addbtn{
[ShareSDK getUserInfo:SSDKPlatformTypeWechat
onStateChanged:^(SSDKResponseState state, SSDKUser *user, NSError *error)
{
if (state == SSDKResponseStateSuccess)
{
NSLog(@"uid=%@",user.uid);
NSLog(@"%@",user.credential);
NSLog(@"token=%@",user.credential.token);
NSLog(@"nickname=%@",user.nickname);
}
else
{
NSLog(@"%@",error);
}
}];
}