新浪微博发送消息和授权机制原理(WeiboSDK)
1.首先是在微博发送消息,对于刚開始做weibo发送消息的刚開始学习的人会有一个误区,那就是会觉得须要授权后才干够发送消息。事实上发送消息仅仅须要几行代码就能够实现了,很easy,不须要先授权再发送消息,由于weibosdk已经帮我们封装好了。
(此情况须要用户安装client)
发送消息流程为:点击发送消息按键----SDK会自己主动帮我们推断用户是否安装了新浪微博client--假设未安装弹出安装提示----假设安装直接跳转到sina微博client进行发送----发送成功后自己主动跳回原应用程序。
1)在AppDelegate中注冊sdk, AppDelegate须要实现WeiboSDKDelegate
AppDelegate.h
@interface AppDelegate : UIResponder <UIApplicationDelegate, WeiboSDKDelegate>
{...
AppDelegate.m - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//注冊SDK
[WeiboSDK enableDebugMode:YES];
[WeiboSDK registerApp:kAppKey]; - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
return [WeiboSDK handleOpenURL:url delegate:self];
}
2)拼接消息对象WBMessageObject,并发送消息[WeiboSDK
sendRequest:request];
WBSendMessageToWeiboRequest *request = [WBSendMessageToWeiboRequest requestWithMessage:[self messageToShare]];
request.userInfo = @{@"ShareMessageFrom": @"SendMessageToWeiboViewController",
@"Other_Info_1": [NSNumber numberWithInt:123],
@"Other_Info_2": @[@"obj1", @"obj2"],
@"Other_Info_3": @{@"key1": @"obj1", @"key2": @"obj2"}};
// request.shouldOpenWeiboAppInstallPageIfNotInstalled = NO; [WeiboSDK sendRequest:request];//这句就能够发送消息了,不须要先授权
- (WBMessageObject *)messageToShare
{
WBMessageObject *message = [WBMessageObject message]; if (self.textSwitch.on)
{
message.text = @"測试通过WeiboSDK发送文字到微博!";
} if (self.imageSwitch.on)
{
WBImageObject *image = [WBImageObject object];
image.imageData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image_1" ofType:@"jpg"]];
message.imageObject = image;
} if (self.mediaSwitch.on)
{
WBWebpageObject *webpage = [WBWebpageObject object];
webpage.objectID = @"identifier1";
webpage.title = @"分享网页标题";
webpage.description = [NSString stringWithFormat:@"分享网页内容简单介绍-%.0f", [[NSDate date] timeIntervalSince1970]];
webpage.thumbnailData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image_2" ofType:@"jpg"]];
webpage.webpageUrl = @"http://sina.cn? a=1";
message.mediaObject = webpage;
} return message;
}
重要:假设程序发送完消息无法跳回原应用的话是由于在plist文件里没有配置URL Types,appKey在你的新浪开发人员帐号里有。
2.授权。通过授权我们能够在用户未安装client的情况下关注指定微博。
授权主要是为了得到:userID,accessToken。有了accessToken我们就能够訪问新浪weibo的API了http://open.weibo.com/wiki/%E5%BE%AE%E5%8D%9AAPI
response.userInfo对象就是我们要的东西
#import <UIKit/UIKit.h> @interface SinaWeiBoLoginViewController : UIViewController<UIWebViewDelegate>{
UIWebView *_webView;
void (^_completionHandler)(BOOL);
BOOL loadSuccess;
}
@property (nonatomic, copy)void (^completionHandler)(BOOL);
@property (nonatomic)BOOL loadSuccess; - (id)initWithLoginCompletion:(void (^) (BOOL isLoginSuccess))isLoginSuccess;
- (void)startRequest;
@end
//
// SinaWeiBoLoginViewController.m
// YinYin
//
// Created by Lw. on 14-7-3.
//
// #import "SinaWeiBoLoginViewController.h"
#import "SinaUtil.h"
#import "SVProgressHUD.h" #define title_height ((!IS_IOS_7)?40:60) @interface SinaWeiBoLoginViewController () @end @implementation SinaWeiBoLoginViewController @synthesize completionHandler,loadSuccess; - (id)initWithLoginCompletion:(void (^) (BOOL isLoginSuccess))isLoginSuccess
{
if (self = [super init]) {
self.completionHandler = isLoginSuccess;
}
return self;
} - (void)dealloc
{
[_webView release];
[super dealloc];
} - (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor whiteColor]];
//自己定义导航
UIView *customNavBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, title_height)];
customNavBar.backgroundColor = [UIColor blackColor];
[self.view addSubview:customNavBar];
[customNavBar release]; UIButton *returnBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[returnBtn setTitle:@"取消" forState:UIControlStateNormal];
returnBtn.titleLabel.font = [UIFont systemFontOfSize:15];
returnBtn.frame = CGRectMake(7, (!IS_IOS_7)?5:20, 50, 30);
[returnBtn addTarget:self action:@selector(Cancelbtn) forControlEvents:UIControlEventTouchUpInside];
[customNavBar addSubview:returnBtn]; _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,title_height, self.view.frame.size.width, self.view.frame.size.height - title_height)];
_webView.delegate = self;
[self.view addSubview:_webView];
[_webView release];
[self startRequest];
} -(void)Cancelbtn
{
_webView.delegate = nil;
if(self.completionHandler)
{
self.completionHandler(NO);
}
[self dismissViewControllerAnimated:YES completion:nil];
} #pragma mark - 请求网络 -(void)startRequest{
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[SinaUtil authorizeURL]];
[_webView loadRequest:request];
} #pragma mark - UIWebView代理方法
- (void)webViewDidStartLoad:(UIWebView *)webView{
[SVProgressHUD show];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
[SVProgressHUD dismiss];
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
[SVProgressHUD dismiss];
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
if ([request.URL.absoluteString rangeOfString:@"code="].location != NSNotFound) {
NSString *code = [[request.URL.query componentsSeparatedByString:@"="] objectAtIndex:1];
loadSuccess = YES;
if([SinaUtil getAccessToken:code]){
if([SinaUtil sinaAtt]){
[self dismissViewControllerAnimated:YES completion:^{
[SVProgressHUD showSuccessWithStatus:@"关注成功"];
}];
}else{
[SVProgressHUD showErrorWithStatus:@"关注失败"];
}
}else{
[SVProgressHUD showErrorWithStatus:@"授权失败"];
}
return NO;
}
return YES;
}
-(void)viewDidDisappear:(BOOL)animated{
[SVProgressHUD dismiss];
}
-(void)viewDidUnload{
[SVProgressHUD dismiss];
}
@end
#import <Foundation/Foundation.h>
#import "WeiboSDK.h"
#import "EnvConstant.h"
#import "ASIHTTPRequest.h" @class ShareMultiple; @interface SinaUtil : NSObject<WBHttpRequestDelegate> +(void)share:(NSString *)text imageData:(UIImage *)imageData imageUrl:(NSString *)imageUrl url:(NSString *)url urlTitle:(NSString *)urlTitle urlDesc:(NSString *)urlDesc urlThumbdata:(UIImage *)urlThumbdata; #pragma mark - public method
+(void)evaluatingJavaScriptFromString:(id)userInfo; +(void)timerFireMethod:(NSTimer*)theTimer; +(BOOL) startWith:(NSString*)prefix forString:(NSString*)text;
+(BOOL) stringEquals:(NSString*)str1 to:(NSString*)str2; #pragma mark - 关注功能
/**
* 授权地址
*
* @return UIWebView所要载入的url
*/
+ (NSURL *)authorizeURL;
+ (BOOL)getAccessToken:(NSString *)code;
+(BOOL)sinaAtt;
+(BOOL)isAuthorized;
+(void)saveLoginInfo:(NSDictionary *)aDic; @end
#import "SinaUtil.h"
#import "EnvConstant.h"
#import "AppDelegate.h"
#import "WXApiObject.h"
#import "ASIHTTPRequest.h"
#import "SVProgressHUD.h"
#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"
#import "SBJSON.h" @implementation SinaUtil //3.公布媒体,仅仅能文字图片。文字媒体,无法文字图片媒体
+(void)share:(NSString *)text imageData:(UIImage *)imageData imageUrl:(NSString *)imageUrl url:(NSString *)url urlTitle:(NSString *)urlTitle urlDesc:(NSString *)urlDesc urlThumbdata:(UIImage *)urlThumbdata{ WBMessageObject *message = [WBMessageObject message];
//1.文本
if(text != nil){
message.text = text;
}
//2.图片和超链接
if(imageData != nil){
WBImageObject *imageObj = [WBImageObject object];
imageObj.imageData = UIImagePNGRepresentation(imageData);
message.imageObject = imageObj;
}else if (imageUrl != nil){
if([@"http://www.yypt.com/home/resources/images/activity/weiboshare.jpg" isEqualToString:imageUrl]){
WBImageObject *imageObj = [WBImageObject object];
imageObj.imageData = UIImagePNGRepresentation([UIImage imageNamed:@"weiboshare.jpg"]);
message.imageObject = imageObj;
}else{
//message.text = [NSString stringWithFormat:@"%@%@",message.text,imageUrl];
}
}
//3.媒体
if(url != nil){
WBWebpageObject *webpage = [WBWebpageObject object];
webpage.objectID = @"identifier1";
webpage.title = urlTitle;
webpage.description = urlDesc;
webpage.thumbnailData = UIImagePNGRepresentation(urlThumbdata);
webpage.webpageUrl = url;
message.mediaObject = webpage;
}
WBSendMessageToWeiboRequest *request = [WBSendMessageToWeiboRequest requestWithMessage:message];
[WeiboSDK sendRequest:request];
} #pragma mark - WBHttpRequestDelegate
- (void)request:(WBHttpRequest *)request didFinishLoadingWithResult:(NSString *)result{
//收到网络回调
} - (void)request:(WBHttpRequest *)request didFailWithError:(NSError *)error;{
//请求异常
} #pragma mark - public method
//运行脚本
+(void)evaluatingJavaScriptFromString:(id)userInfo{
[NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(timerFireMethod:) userInfo:userInfo repeats:NO];
}
//timer回调,0成功,1失败,2取消
+(void)timerFireMethod:(NSTimer*)theTimer{
if([theTimer.userInfo isKindOfClass:[SendMessageToWXResp class]]){
SendMessageToWXResp* response = (SendMessageToWXResp *)theTimer.userInfo;
int d = 0;
switch (response.errCode) {
case WXSuccess:
d = 0;
break;
case WXErrCodeUserCancel:
d = 2;
break;
default:
d = 1;
break;
}
NSString* jsString = @"$(document).trigger('shareResultEvent',['%d'])";
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
[appDelegate.viewController.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:jsString,d]];//这里的errcode不同于weibo的statusCode }else if([theTimer.userInfo isKindOfClass:[WBBaseResponse class]]){
WBBaseResponse* response = (WBBaseResponse *)theTimer.userInfo;
int d = 0;
switch (response.statusCode) {
case WeiboSDKResponseStatusCodeSuccess:
d = 0;
break;
case WeiboSDKResponseStatusCodeUserCancel:
d = 2;
break;
default:
d = 1;
break;
} NSString* jsString = @"$(document).trigger('shareResultEvent',['%d'])";
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
[appDelegate.viewController.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:jsString,d]];
} }
#pragma mark - string
+(BOOL) startWith:(NSString*)prefix forString:(NSString*)text{
if ( text != nil && prefix != nil ){
if ( [prefix length] > [text length] ) {
return NO;
} NSString* prestr = [text substringToIndex:[prefix length]];
if ( [self stringEquals:prestr to:prefix] ) {
return YES;
} }
return NO;
}
+(BOOL) stringEquals:(NSString*)str1 to:(NSString*)str2
{
if ( str1 == nil || str2 == nil ) {
return NO;
}
return [str1 compare:str2 options:NSCaseInsensitiveSearch] == NSOrderedSame;
} #pragma mark - 关注功能
/**
* 授权地址
*
* @return UIWebView所要载入的url
*/
+ (NSURL *)authorizeURL
{
NSString *authStr = [NSString stringWithFormat:@"%@?client_id=%@&redirect_uri=%@&response_type=%@&display=%@",SINA_AUTHORIZE_URL,kWBSDKDemoAppKey,wbkRedirectURI,@"code",@"mobile"];
return [NSURL URLWithString:authStr];
} /**
* 获取AccessToken
*
* @param code 在webView的重定向地址中取得code
* @param isSuccess block 请求成功的回调
* BOOL isSuccess 请求是否成功 YES or NO
*/
+ (BOOL)getAccessToken:(NSString *)code{
NSURL *url = [NSURL URLWithString:[HOSTURL stringByAppendingString:@"/oauth2/access_token"]];
ASIFormDataRequest *request = [[ASIFormDataRequest alloc]initWithURL:url];
[request addPostValue:kWBSDKDemoAppKey forKey:@"client_id"];
[request addPostValue:kWBSDKDemoAppSecret forKey:@"client_secret"];
[request addPostValue:@"authorization_code" forKey:@"grant_type"];
[request addPostValue:code forKey:@"code"];
[request addPostValue:wbkRedirectURI forKey:@"redirect_uri"];
[request setRequestMethod:@"POST"];
[SVProgressHUD show];
[request startSynchronous]; NSError *err = [request error];
BOOL flag = NO;
if (!err) {
NSString *responseString = [request responseString];
NSLog(@"responseString = %@", responseString);
if ([responseString rangeOfString:@"access_token"].location != NSNotFound) {
SBJSON *json = [[SBJSON alloc] init];
NSDictionary *jsonDic = [json objectWithString:responseString];
[SinaUtil saveLoginInfo:jsonDic];
}
flag = YES;
}else{
flag = NO;
}
[SVProgressHUD dismiss];
return flag;
} //关注微博
+(BOOL)sinaAtt{
NSString *accessToken = [[NSUserDefaults standardUserDefaults] objectForKey:SINA_ACCESS_TOKEN_KEY];
NSString *path = @"/2/friendships/create.json";
NSString *requestUrl = [HOSTURL stringByAppendingString:path];
NSLog(@"requestUrl = %@",requestUrl);
ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:requestUrl]];
[request addPostValue:accessToken forKey:@"access_token"];
[request addPostValue:ATTENTION_SINA_WEIBO_NUM forKey:@"uid"];
[request addPostValue:ATTENTION_SINA_WEIBO_SCREEN_NAME forKey:@"screen_name"];
[request startSynchronous];
request.timeOutSeconds = 6;
NSError *error = [request error];
BOOL flag = NO;
if(error!=nil){
flag = NO;
}else{
flag = YES;
}
return flag;
} /**
* 推断有没有登录过,而且获得到的token有没有过期
*
* @return YES 有可用的token ,而且没有过期; NO 没有可用的token
*/
+(BOOL)isAuthorized{
NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults];
NSString *accessToken = [userDef objectForKey:SINA_ACCESS_TOKEN_KEY];
NSDate *expiresDate = [userDef objectForKey:SINA_ACCESS_EXPIRES_IN_KEY];
if (expiresDate){
return (NSOrderedDescending == [expiresDate compare:[NSDate date]] && accessToken);
}
return NO;
} /**
* 保存登录信息
*
* @param aDic 字典中有access_token,exoires_in,uid等信息
*/
+(void)saveLoginInfo:(NSDictionary *)aDic{
NSLog(@"保存token...");
[[NSUserDefaults standardUserDefaults] setObject:[aDic objectForKey:@"access_token"] forKey:SINA_ACCESS_TOKEN_KEY];
NSDate *expiresDate = [NSDate dateWithTimeIntervalSinceNow:[[aDic objectForKey:@"expires_in"] intValue]];
[[NSUserDefaults standardUserDefaults] setObject:expiresDate forKey:SINA_ACCESS_EXPIRES_IN_KEY];
[[NSUserDefaults standardUserDefaults] setObject:[aDic objectForKey:@"uid"] forKey:SINA_USER_ID_KEY];
[[NSUserDefaults standardUserDefaults] synchronize];
} @end
调用方式:
if([SinaUtil isAuthorized]){
if([SinaUtil sinaAtt]){
[SVProgressHUD showSuccessWithStatus:@"关注成功"];
}else{
[SVProgressHUD showErrorWithStatus:@"关注失败"];
}
}else{
//[self selectRow:@"follow-wb"];
SinaWeiBoLoginViewController *sinaVC = [[SinaWeiBoLoginViewController alloc]init];
[self presentViewController:sinaVC animated:YES completion:nil];
}
新浪微博发送消息和授权机制原理(WeiboSDK)的更多相关文章
- 深入理解Spring Security授权机制原理
原创/朱季谦 在Spring Security权限框架里,若要对后端http接口实现权限授权控制,有两种实现方式. 一.一种是基于注解方法级的鉴权,其中,注解方式又有@Secured和@PreAuth ...
- ios消息推送机制原理与实现
本文转载至 http://hi.baidu.com/yang_qi168/item/480304c542fd246489ad9e91 Push的原理: Push 的工作机制可以简单的概括为下图 图中, ...
- Kafka设计解析(八)- Exactly Once语义与事务机制原理
原创文章,首发自作者个人博客,转载请务必将下面这段话置于文章开头处. 本文转发自技术世界,原文链接 http://www.jasongj.com/kafka/transaction/ 写在前面的话 本 ...
- 深入理解 Android 消息机制原理
欢迎大家前往腾讯云社区,获取更多腾讯海量技术实践干货哦~ 作者:汪毅雄 导语: 本文讲述的是Android的消息机制原理,从Java到Native代码进行了梳理,并结合其中使用到的Epoll模型予以介 ...
- 深入研究RocketMQ生产者发送消息的底层原理
前言 hello,小伙伴们,王子又来和大家研究RocketMQ的原理了,之前的文章RocketMQ生产部署架构如何设计中,我们已经简单的聊过了生产者是如何发送消息给Broker的. 我们简单回顾一下这 ...
- Android Handler 消息机制原理解析
前言 做过 Android 开发的童鞋都知道,不能在非主线程修改 UI 控件,因为 Android 规定只能在主线程中访问 UI ,如果在子线程中访问 UI ,那么程序就会抛出异常 android.v ...
- Android Handler MessageQueue Looper 消息机制原理
提到Android里的消息机制,便会提到Message.Handler.Looper.MessageQueue这四个类,我先简单介绍以下这4个类 之间的爱恨情仇. Message 消息的封装类,里边存 ...
- 分布式开放消息系统RocketMQ的原理与实践(消息的顺序问题、重复问题、可靠消息/事务消息)
备注:1.如果您此前未接触过RocketMQ,请先阅读附录部分,以便了解RocketMQ的整体架构和相关术语2.文中的MQServer与Broker表示同一概念 分布式消息系统作为实现分布式系统可扩展 ...
- 分布式开放消息系统(RocketMQ)的原理与实践
分布式消息系统作为实现分布式系统可扩展.可伸缩性的关键组件,需要具有高吞吐量.高可用等特点.而谈到消息系统的设计,就回避不了两个问题: 消息的顺序问题 消息的重复问题 RocketMQ作为阿里开源的一 ...
随机推荐
- android 解密工具
androguard. 这个是python写的 安装: pip install androguard
- 最小生成树 || HDU 1301 Jungle Roads
裸的最小生成树 输入很蓝瘦 **并查集 int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); } 找到x在并查集里的根结点,如果 ...
- 用Multisim实现彩灯循环控制器
2019/06/06 !转载请注明出处 1.设计任务目的与要求 1.1 展示器件 10路彩灯分别用10个发光二极管L0.L1…..L9模拟,发光二极管L0.L1…..L9从左到右排列. 1.2 要求显 ...
- transformer模型解读
最近在关注谷歌发布关于BERT模型,它是以Transformer的双向编码器表示.顺便回顾了<Attention is all you need>这篇文章主要讲解Transformer编码 ...
- CSS---基础外部样式表
<link rel="stylesheet" type="text/css" href="style.css" media=" ...
- Ubuntu配置NFS服务器
NFS(Network File System)即网络文件系统,是FreeBSD支持的文件系统中的一种,它允许网络中的计算机之间通过TCP/IP网络共享资源.在NFS的应用中,本地NFS的客户端应用可 ...
- set()集合基本操作
运用频次:☆☆ set是一个无序且不重复元素集,基本操作如下: 1. 创建set集合,会自动转换成set类型 2. add():添加元素 def add(self, *args, **kwargs): ...
- c#导出word文档
为方便下次遇到不知道去哪找先把它存放在这里,以下是保存导出word主要类方法 public class BiultReportForm { /// <summary>word 应用对象 & ...
- coraldraw快捷键
显示导航窗口(Navigator window) [N] 运行 Visual Basic 应用程序的编辑器 [Alt]+[F11] 保存当前的图形 [Ctrl]+[S] 打开编辑文本对话框 ...
- C#通信学习(一)
基础知识 TCP/IP:Transmission Control Protocol/Internet Protocol,传输控制协议/因特网互联协议,又名网络通讯协议.简单来说:TCP控制传输数据,负 ...