关于授权,其实官方的SDK文档写得很详细,我就在这里简要阐述一下步骤,和一些需要注意的地方
PS:大部分内容出自新浪官方SDK文档
一、到新浪开放平台注册成为开发者http://open.weibo.com/
二、因为我们是开发IOS移动应用,所以选择创建应用
创建完后会得到一个App Key 和App Secrect,App Key在后面开发时用来注册应用,后者我目前还没用到 ,以后知道了来补充
三、创建完毕,到管理中心,编辑刚刚创建的应用,这里需要注意这几点
1、应用信息--基本信息:这里需要自己添加Apple ID和Bundle ID,Apple ID可以先随便填,对开发没影响,Bundle ID必须确保与你建的工程的Bundle Identifier一致,也就是说,以后建了工程,工程的Bundle Identifier 必须得按这个来填
2、应用信息--高级信息:这里要修改一下授权回调页,因为我们是应用,没有回调页,这里就填默认页为https://api.weibo.com/oauth2/default.html
四、接着创建我们的工程,这里工程需要修改两个地方:
1、这个要确保和之前的Bundle ID一致
2、选择Info页面,到最下面的URL Types,IdentifIer这样填即可,URL Schemes要填写成 wb+App Key,注意要填写正确,不然调不到授权页,会闪退。
3、从新浪开放平台上下IOS-SDK包下来,导入到我们的工程里,包含SDK的头文件,这里我是按照官方demo的做法,在Supporting Files\MySinaWeibo-Prefix.pch文件里面声明,然后把AppKey 和 RedirectURI定义出来
- #ifdef __OBJC__
- #import <UIKit/UIKit.h>
- #import <Foundation/Foundation.h>
- #import "WeiboSDK.h"
- #define kAppKey @"399211363"
- #define kRedirectURI @"https://api.weibo.com/oauth2/default.html"
- #endif
4、回到AppDelegate.h文件,添加委托
- @interface AppDelegate : UIResponder <UIApplicationDelegate,WeiboSDKDelegate>
5、到AppDelegate.m文件,注册应用
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- {
- [WeiboSDK enableDebugMode:YES]; //打开调试选项
- [WeiboSDK registerApp:kAppKey]; //注册应用
- self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
- // Override point for customization after application launch.
- self.window.backgroundColor = [UIColor whiteColor];
- [self.window makeKeyAndVisible]; LoginViewController *rootViewRootController = [[LoginViewController alloc] init];
- self.window.rootViewController = rootViewRootController;
- LoginViewController *rootViewRootController = [[LoginViewController alloc] init];
- self.window.rootViewController = rootViewRootController;
- return YES;
- }
- - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
- {
- return [WeiboSDK handleOpenURL:url delegate:self];
- }
- WBAuthorizeRequest *request = [WBAuthorizeRequest request];
- request.redirectURI = kRedirectURI;
- request.scope = @"all";
- [WeiboSDK sendRequest:request];
- #pragma mark - WeiboSDK delegate
- - (void)didReceiveWeiboRequest:(WBBaseRequest *)request
- {
- }
- - (void)didReceiveWeiboResponse:(WBBaseResponse *)response
- {
- if ([response isKindOfClass:WBAuthorizeResponse.class])
- {
- wbtoken = [(WBAuthorizeResponse *)response accessToken];
- userId = [(WBAuthorizeResponse * )response userID];
- NSLog(@"userid : %@",userId);
- [[NSUserDefaults standardUserDefaults] setObject:wbtoken forKey:@"access_token"];
- [[NSUserDefaults standardUserDefaults] setObject:userId forKey:@"userID"];
- [[NSUserDefaults standardUserDefaults] synchronize];
- }
- }
- if ([[NSUserDefaults standardUserDefaults] objectForKey:@"access_token"] == nil) {
- WBAuthorizeRequest *request = [WBAuthorizeRequest request];
- request.redirectURI = kRedirectURI;
- request.scope = @"all";
- [WeiboSDK sendRequest:request];
- }
好,大功告成,现在运行,应该能跳转到授权页面了,要确保模拟器能上网哦,亲~~
转载:http://blog.csdn.net/alezaizai/article/details/21592133
参考:http://blog.sina.com.cn/s/blog_945590aa0101acp4.html
参考:http://open.weibo.com/wiki/授权机制说明#.E5.8F.96.E6.B6.88.E6.8E.88.E6.9D.83.E5.9B.9E.E8.B0.83.E9.A1.B5