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

本文介绍如何通过成为微博开放平台的开发者用户并新建应用,完善应用信息,下载SDK,配置授权请求,以及实现WeiboSDKDelegate协议来实现微博开放平台的基本使用。重点在于获取accessToken,用于后续API调用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

为了提高应用的交互和扩散能力,引人微博或者QQ开放平台是个不错的选择。今天跟大家分享下微博开放平台SDK使用的示例。  
   首先,需要成为微博开放平台的开发者用户,然后可以新建应用, 注意应用名称;  
     
  
   接下来,继续完善相应的应用信息, 如Apple ID,测试阶段可以随意填写,Bundle ID必须和项目设置一致;记下AppKey需要在程序中用到  
     
  
    其次还需要在高级信息中设置回调页面(   OAuth2.0 授权)   , 注意SDK文档里面的默认回调页面没有成功,可以设置成任意能够访问的页面即可,如http://www.baidu.com  
  
     
   
   回调页面设置不一致授权获取会如下提示:  
     
  
    完成以上设置就相当于在微博开放平台中注册了开发者应用的相关信息,接下来只需要下载SDK即可,页面如下   
           
   代码部分:  
   新建项目设置项目名称、Bundle Identifier和微博开放平台设置一致,并导人相应的SDK文件  
       
    
    应用程序代理.h文件中添加WeiboSDK.h应用和相应协议   
  1. #import 
  2. #import "WeiboSDK.h"
  3. #define kAppKey @"1617942800"
  4. #define kRedirectURI @"http://www.baidu.com"
  5. @interface gisxyAppDelegate : UIResponder 
  6. @property (strong, nonatomic) UIWindow *window;
  7. @property (strong, nonatomic) NSString *wbtoken;
  8. @end
复制代码
在应用程序的代理.m文件中设置开启调试和注册应用(Appkey)
  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  2. { // Override point for customization after application launch. [WeiboSDK enableDebugMode:YES]; [WeiboSDK registerApp:kAppKey]; return YES;
  3. }
复制代码
在合适的位置添加授权请求
  1. -(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];
  2. }
复制代码
重写应用程序代理的OpenURL 
  1. - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
  2. { NSLog(@"ur %@",url); return [WeiboSDK handleOpenURL:url delegate:self];
  3. }
复制代码
实现 WeiboSDKDelegate协议, 设置请求相应处理
  1. - (void)didReceiveWeiboResponse:(WBBaseResponse *)response
  2. {        if ([response isKindOfClass:WBSendMessageToWeiboResponse.class])        {        NSString *title = @"发送结果";        NSString *message = [NSString stringWithFormat:@"响应状态: %dn响应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:@"响应状态: %dnresponse.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];        }
  3. }
  4. - (void)didReceiveWeiboRequest:(WBBaseRequest *)request
  5. {        if ([request isKindOfClass:WBProvideMessageForWeiboRequest.class])        {        // view *controller = [[ProvideMessageForWeiboViewController alloc] init];        // [self.viewController presentModalViewController:controller animated:YES];        NSLog(@"logs is sina");        }
  6. }
复制代码
完成以上,微博开放平台基本就可以正常使用啦,请求响应获取到  accessToken 是关键。     对于文档中提到的设置工程回调URL Scheme貌似不设置也可以。   
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值