#import "AppDelegate.h"
#import "WeiboSDK.h"
@interface AppDelegate ()<WeiboSDKDelegate>
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//设置SDK的调试模式,观察微博SDK日志
[WeiboSDK enableDebugMode:YES];
//注册APPKey;
[WeiboSDK registerApp:kAppKey];
return YES;
}
-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options
{
return [WeiboSDK handleOpenURL:url delegate:self];
}
-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
// 该方法已经被弃用 为了兼容低版本依然要写上
return [WeiboSDK handleOpenURL:url delegate:self];
}
-(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
return [WeiboSDK handleOpenURL:url delegate:self];
}
#pragma mark 协议方法
-(void)didReceiveWeiboRequest:(WBBaseRequest *)request
{
NSLog(@"接收到请求");
}
- (void)didReceiveWeiboResponse:(WBBaseResponse *)response
{
if ([response isKindOfClass:WBSendMessageToWeiboResponse.class])
{
NSString *title = NSLocalizedString(@"发送结果", nil);
NSString *message = [NSString stringWithFormat:@"%@: %d\n%@: %@\n%@: %@", NSLocalizedString(@"响应状态", nil), (int)response.statusCode, NSLocalizedString(@"响应UserInfo数据", nil), response.userInfo, NSLocalizedString(@"原请求UserInfo数据", nil),response.requestUserInfo];
WBSendMessageToWeiboResponse* sendMessageToWeiboResponse = (WBSendMessageToWeiboResponse*)response;
NSString* accessToken = [sendMessageToWeiboResponse.authResponse accessToken];
if (accessToken)
{
// self.wbtoken = accessToken;
}
NSString* userID = [sendMessageToWeiboResponse.authResponse userID];
if (userID) {
// self.wbCurrentUserID = userID;
}
}
else if ([response isKindOfClass:WBAuthorizeResponse.class])
{
NSLog(@"%ld",response.statusCode);
NSLog(@"用户信息:%@",response.userInfo);
NSLog(@"请求时候信息%@",response.requestUserInfo);
//保存登陆成功后的用户信息
[[NSUserDefaults standardUserDefaults]setObject:response.userInfo forKey:@"WeiboUserInfo"];
/*
WeiboSDKResponseStatusCodeSuccess = 0,//成功
WeiboSDKResponseStatusCodeUserCancel = -1,//用户取消发送
WeiboSDKResponseStatusCodeSentFail = -2,//发送失败
WeiboSDKResponseStatusCodeAuthDeny = -3,//授权失败
WeiboSDKResponseStatusCodeUserCancelInstall = -4,//用户取消安装微博客户端
*/
}
else if ([response isKindOfClass:WBPaymentResponse.class])
{
NSString *title = NSLocalizedString(@"支付结果", nil);
NSString *message = [NSString stringWithFormat:@"%@: %d\nresponse.payStatusCode: %@\nresponse.payStatusMessage: %@\n%@: %@\n%@: %@", NSLocalizedString(@"响应状态", nil), (int)response.statusCode,[(WBPaymentResponse *)response payStatusCode], [(WBPaymentResponse *)response payStatusMessage], NSLocalizedString(@"响应UserInfo数据", nil),response.userInfo, NSLocalizedString(@"原请求UserInfo数据", nil), response.requestUserInfo];
}
else if([response isKindOfClass:WBSDKAppRecommendResponse.class])
{
NSString *title = NSLocalizedString(@"邀请结果", nil);
NSString *message = [NSString stringWithFormat:@"accesstoken:\n%@\nresponse.StatusCode: %d\n响应UserInfo数据:%@\n原请求UserInfo数据:%@",[(WBSDKAppRecommendResponse *)response accessToken],(int)response.statusCode,response.userInfo,response.requestUserInfo];
}else if([response isKindOfClass:WBShareMessageToContactResponse.class])
{
NSString *title = NSLocalizedString(@"发送结果", nil);
NSString *message = [NSString stringWithFormat:@"%@: %d\n%@: %@\n%@: %@", NSLocalizedString(@"响应状态", nil), (int)response.statusCode, NSLocalizedString(@"响应UserInfo数据", nil), response.userInfo, NSLocalizedString(@"原请求UserInfo数据", nil),response.requestUserInfo];
WBShareMessageToContactResponse* shareMessageToContactResponse = (WBShareMessageToContactResponse*)response;
NSString* accessToken = [shareMessageToContactResponse.authResponse accessToken];
if (accessToken)
{
// self.wbtoken = accessToken;
}
NSString* userID = [shareMessageToContactResponse.authResponse userID];
if (userID) {
// self.wbCurrentUserID = userID;
}
}
}
#import "ViewController.h"
#import "WeiboSDK.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)weiboAuth:(id)sender {
WBAuthorizeRequest * request=[WBAuthorizeRequest request];
//授权网页回调URL和后台保持一致
request.redirectURI=kCallBackURL;
request.scope=@"all";
request.userInfo=@{@"what":@"yes"};
//发送请求
[WeiboSDK sendRequest:request];
}
- (IBAction)shareText:(id)sender {
WBAuthorizeRequest *authRequest = [WBAuthorizeRequest request];
authRequest.redirectURI = kCallBackURL;
authRequest.scope = @"all";
WBMessageObject *message = [WBMessageObject message];
message.text=@"需要分享的文本";
NSDictionary * dic=[[NSUserDefaults standardUserDefaults]objectForKey:@"WeiboUserInfo"];
WBSendMessageToWeiboRequest *request = [WBSendMessageToWeiboRequest requestWithMessage:message authInfo:authRequest access_token:dic[@"access_token"]];
request.userInfo = @{@"ShareMessageFrom": @"SendMessageToWeiboViewController",
@"Other_Info_1": [NSNumber numberWithInt:123],
@"Other_Info_2": @[@"obj1", @"obj2"],
@"Other_Info_3": @{@"key1": @"obj1", @"key2": @"obj2"}};
[WeiboSDK sendRequest:request];
}