新浪微博发送消息和授权机制原理(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作为阿里开源的一 ...
随机推荐
- 数据结构——RMQ
RMQ 今天临放学前终于是学会了RMQ,特此写一篇题解来缅怀 RMQ是一种数据结构,用途是查询区间内最大值或最小值 或者你所要求的任意条件,主要思想是二进制的思想,其中还用到了dp的思想, 是一种非常 ...
- oracle插入多表(insert all/first)
1.建测试表 CREATE TABLE EDW_INT ( AGMT_NO VARCHAR2(40 BYTE) NOT NULL, AG ...
- viewport移动端适配,读文笔记
文章地址: viewport移动端适配 笔记: 移动端适配目的: 希望在屏幕尺寸大小不同的手机上进行访问页面时,页面显示的效果能合理的展示,我们期望的是在手机屏幕较大时显示的内容比较大一些,手机屏幕小 ...
- luogu 2709小b的询问--莫队
https://www.luogu.org/problemnew/show/P2709 无修改的莫队几乎没有什么太高深的套路,比较模板吧,大多都是在那两个函数上动手脚. 这题询问每一种数字数量的平方和 ...
- 4. COLLATION_CHARACTER_SET_APPLICABILITY
4. COLLATION_CHARACTER_SET_APPLICABILITY 表COLLATION_CHARACTER_SET_APPLICABILITY表示哪种字符集适用于哪种排序规则. INF ...
- c++_奖券数目
奖券数目 有些人很迷信数字,比如带“4”的数字,认为和“死”谐音,就觉得不吉利.虽然这些说法纯属无稽之谈,但有时还要迎合大众的需求.某抽奖活动的奖券号码是5位数(10000-99999),要求其中不要 ...
- php中session和cookie的使用及区别
1.cookie的使用 什么是 Cookie? cookie 常用于识别用户.cookie 是服务器留在用户计算机中的小文件.每当相同的计算机通过浏览器请求页面时,它同时会发送 cookie.通过 P ...
- Django框架基础知识09-请求与响应
视图函数接受到的request到底是个什么对象呢? HttpRequest对象: 服务器接收到http协议的请求后,会根据报文创建HttpRequest对象视图函数的第一个参数是HttpRequest ...
- Rust所有权语义模型
编程语言的内存管理,大概可以分为自动和手动两种. 自动管理就是用 GC(垃圾回收)来自动管理内存,像 Java.Ruby.Golang.Elixir 等语言都依赖于 GC.而 C/C++ 却是依赖于手 ...
- Laya 类列表加载优化
Laya 类列表加载优化 @author ixenos 类列表:在一个页面展示的大量的零散单元的集合(聊天面板.背包) 一.按展示优化1.展示内容少,即使大量数据,但用户只看到少量信息的时候,考虑按需 ...