友盟第三方登录
一、在项目中要配置APPID和appKey
#import "AppDelegate.h" #import "UMSocial.h" #import "UMSocialSinaHandler.h" #import "UMSocialQQHandler.h" #import "UMSocialWechatHandler.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // 设置友盟的APPKEY [UMSocialData setAppKey:@"5616236ae0f55a3cb20031ef"]; // 打开新浪微博的SSO开关 [UMSocialSinaHandler openSSOWithRedirectURL:@"http://sns.whalecloud.com/sina2/callback"]; // 集成QQ第三方登录或者分享,注意:appKey是腾讯开发平台自己应用的APPKEY [UMSocialQQHandler setQQWithAppId:@"1104898900" appKey:@"PiiKO9JyFFXRjcxr" url:@"http://www.520it.com"]; // 设置微信AppId、appSecret,分享url [UMSocialWechatHandler setWXAppId:@"wx82fcee00c5e507de" appSecret:@"baea707463510cfdfd714adaaa92564a" url:@"http://www.520it.com"]; return YES; }
二、实现代码
#import "ViewController.h" #import "UMSocial.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; } #pragma mark - 第三方登录 - (IBAction)sinaLogin { UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina]; snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){ // 获取微博用户名、uid、token等 if (response.responseCode == UMSResponseCodeSuccess) { UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToSina]; NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL); }}); } - (IBAction)qqLogin { UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToQQ]; snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){ if (response.responseCode == UMSResponseCodeSuccess) { UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToQQ]; NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL); }}); } #pragma mark - 微信分享 - (IBAction)shareToSession { [self shareToWeixin:UMShareToWechatSession]; } - (IBAction)shareToTimeline { [self shareToWeixin:UMShareToWechatTimeline]; } - (void)shareToWeixin:(NSString *)shareToWeixin { UIImage *image = [UIImage imageNamed:@"dzq_icon.jpg"]; [[UMSocialDataService defaultDataService] postSNSWithTypes:@[shareToWeixin] content:@"梦想还是要有的,万一实现了呢" image:image location:nil urlResource:nil presentedController:self completion:^(UMSocialResponseEntity *response){ if (response.responseCode == UMSResponseCodeSuccess) { NSLog(@"分享成功!"); } }]; }
浙公网安备 33010602011771号