本文主要是介绍IOS开发——使用shareSDK授权新浪微博、腾讯微博、微信并获取用户资料,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
IOS开发——使用shareSDK授权新浪微博、腾讯微博、微信并获取用户资料
授权:
/**AppDelegatte部分,参照shareSDK集成文档*/
#import "AppDelegate.h"
#import <ShareSDK/ShareSDK.h>
#import "WeiboSDK.h"
#import "WXApi.h"
#import "WXApiObject.h"@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{[ShareSDK registerApp:@"3ee****bab90"];//添加新浪微博应用 注册网址 http://open.weibo.com[ShareSDK connectWeChatWithAppId:@"wx8****4267"wechatCls:[WXApi class]];//添加新浪微博应用 注册网址 http://open.weibo.com[ShareSDK connectSinaWeiboWithAppKey:@"56****243"appSecret:@"38a4f8204******1f9f0daaf31e02e3"redirectUri:@"http://www.sharesdk.cn"];self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];self.window.backgroundColor = [UIColor whiteColor];[self.window makeKeyAndVisible];return YES;
}/**控制器部分*/
#import "UserInfo.h"
#import <ShareSDK/ShareSDK.h>@interface UserInfo ()<UIAlertViewDelegate ,ISSShareViewDelegate>
@end@implementation UserInfo.h
@synthesize bindingSina = _bindingSina;
- (void)viewDidLoad {[super viewDidLoad];
}#pragma mark UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{if(alertView.tag == 23322){if(buttonIndex == 0){[self cancelSinaWeiBo];}}
}
//新浪微博事件
- (IBAction)sinaWeiBoAction:(id)sender {NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];NSInteger markInt = [[userDefaults objectForKey:@"sinaBinding"] intValue];if(markInt == 1){UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:@"是否解绑新浪微博?" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];alertV.tag = 23322;[alertV show ];}else{//新浪微博授权[self bindingSinaWeiBo];}
}//新浪微博授权
-(void)bindingSinaWeiBo{id<ISSAuthOptions> authOptions = [ShareSDK authOptionsWithAutoAuth:YESallowCallback:YESauthViewStyle:SSAuthViewStyleFullScreenPopupviewDelegate:selfauthManagerViewDelegate:self];[ShareSDK authWithType:ShareTypeSinaWeibo //需要授权的平台类型options:authOptions //授权选项,包括视图定制,自动授权result:^(SSAuthState state, id<ICMErrorInfo> error) { //授权返回后的回调方法if (state == SSAuthStateSuccess){NSLog(@"成功");_weiboSwitch.on = YES;NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];[userDefaults setInteger:1 forKey:@"sinaBinding"];[userDefaults synchronize];[_sinaWeiboLab setText:@"解绑新浪微博"];}else if (state == SSAuthStateFail){NSLog(@"失败");_weiboSwitch.on = NO;NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];[userDefaults setInteger:0 forKey:@"sinaBinding"];[userDefaults synchronize];[_sinaWeiboLab setText:@"绑定新浪微博"];}}];
}
//取消授权
-(void)cancelSinaWeiBo{[ShareSDK cancelAuthWithType:ShareTypeSinaWeibo];_weiboSwitch.on = NO;NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];[userDefaults setInteger:0 forKey:@"sinaBinding"];[userDefaults synchronize];[_sinaWeiboLab setText:@"绑定新浪微博"];
}
@end
授权成功后,获取用户资料(头像,用户名...):
//获取用户名和头像
-(void)obtainNameAndPhoto{//获取授权用户资料[ShareSDK getUserInfoWithType:ShareTypeSinaWeibo //平台类型authOptions:nil //授权选项result:^(BOOL result, id<ISSPlatformUser> userInfo, id<ICMErrorInfo> error) { //返回回调if (result){NSLog(@"成功");NSDictionary *soureDict = [userInfo sourceData];NSString *photoPath = [soureDict objectForKey:@"avatar_hd"];NSString *name = [soureDict objectForKey:@"name"];//缓存新浪用户资料NSUserDefaults *userInfoDefaults = [NSUserDefaults standardUserDefaults];[userInfoDefaults setObject:photoPath forKey:@"avatar_hd"];[userInfoDefaults setObject:name forKey:@"sinaName"];[userInfoDefaults synchronize];}else{NSLog(@"失败");}}];
}
这篇关于IOS开发——使用shareSDK授权新浪微博、腾讯微博、微信并获取用户资料的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!