微博开发平台SDK使用示例(iOS版)

来源:互联网 发布:经典译林怎么样 知乎 编辑:程序博客网 时间:2024/06/05 11:50

为了提高应用的交互和扩散能力,引人微博或者QQ开放平台是个不错的选择。今天跟大家分享下微博开放平台SDK使用的示例。

首先,需要成为微博开放平台的开发者用户,然后可以新建应用,注意应用名称;


接下来,继续完善相应的应用信息,如Apple ID,测试阶段可以随意填写,Bundle ID必须和项目设置一致;记下AppKey需要在程序中用到


其次还需要在高级信息中设置回调页面(OAuth2.0授权)注意SDK文档里面的默认回调页面没有成功,可以设置成任意能够访问的页面即可,如http://www.baidu.com


回调页面设置不一致授权获取会如下提示:


完成以上设置就相当于在微博开放平台中注册了开发者应用的相关信息,接下来只需要下载SDK即可,页面如下

代码部分:

新建项目设置项目名称、Bundle Identifier和微博开放平台设置一致,并导人相应的SDK文件


应用程序代理.h文件中添加WeiboSDK.h应用和相应协议

#import <UIKit/UIKit.h>#import "WeiboSDK.h"#define kAppKey         @"1617942800"#define kRedirectURI    @"http://www.baidu.com"@interface gisxyAppDelegate : UIResponder <UIApplicationDelegate,WeiboSDKDelegate>@property (strong, nonatomic) UIWindow *window;@property (strong, nonatomic) NSString *wbtoken;@end
在应用程序的代理.m文件中设置开启调试和注册应用(Appkey)

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    // Override point for customization after application launch.    [WeiboSDK enableDebugMode:YES];        [WeiboSDK registerApp:kAppKey];        return YES;}
在合适的位置添加授权请求

-(IBAction)sinaAuth:(id)sender{    WBAuthorizeRequest *request = [WBAuthorizeRequest request];    request.redirectURI = @"http://www.baidu.com";    request.scope = @"all";    request.userInfo = @{@"SSO_From": @"gisxyViewController",                         @"Other_Info":@"gisxy"};    [WeiboSDK sendRequest:request];}
重写应用程序代理的OpenURL
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{    NSLog(@"ur %@",url);        return [WeiboSDK handleOpenURL:url delegate:self];}
实现WeiboSDKDelegate协议,设置请求相应处理

- (void)didReceiveWeiboResponse:(WBBaseResponse *)response{    if ([response isKindOfClass:WBSendMessageToWeiboResponse.class])    {        NSString *title = @"发送结果";        NSString *message = [NSString stringWithFormat:@"响应状态: %d\n响应UserInfo数据: %@\n原请求UserInfo数据: %@",(int)response.statusCode, response.userInfo, response.requestUserInfo];        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title                                                        message:message                                                       delegate:nil                                              cancelButtonTitle:@"确定"                                              otherButtonTitles:nil];        [alert show];    }    else if ([response isKindOfClass:WBAuthorizeResponse.class])    {        NSString *title = @"认证结果";        NSString *message = [NSString stringWithFormat:@"响应状态: %d\nresponse.userId: %@\nresponse.accessToken: %@\n响应UserInfo数据: %@\n原请求UserInfo数据: %@",(int)response.statusCode,[(WBAuthorizeResponse *)response userID], [(WBAuthorizeResponse *)response accessToken], response.userInfo, response.requestUserInfo];        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title                                                        message:message                                                       delegate:nil                                              cancelButtonTitle:@"确定"                                              otherButtonTitles:nil];                self.wbtoken = [(WBAuthorizeResponse *)response accessToken];        NSLog(@"token is %@",[(WBAuthorizeResponse *)response accessToken]);        [alert show];    }}- (void)didReceiveWeiboRequest:(WBBaseRequest *)request{    if ([request isKindOfClass:WBProvideMessageForWeiboRequest.class])    {        //  view  *controller = [[ProvideMessageForWeiboViewController alloc] init];        // [self.viewController presentModalViewController:controller animated:YES];                NSLog(@"logs is sina");    }}
完成以上,微博开放平台基本就可以正常使用啦,请求响应获取到accessToken 是关键。

对于文档中提到的设置工程回调URL Scheme貌似不设置也可以。




0 0
原创粉丝点击