[iOS微博项目 - 2.2] - 在app中获取授权
//
// HVWOAuthViewController.m
// HVWWeibo
//
// Created by hellovoidworld on 15/2/4.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import "HVWOAuthViewController.h" @interface HVWOAuthViewController () @end @implementation HVWOAuthViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view. // 创建UIWebView
UIWebView *webView = [[UIWebView alloc] init];
webView.frame = self.view.bounds; // 添加到主界面
[self.view addSubview:webView]; // 加载请求页面
NSURL *url = [NSURL URLWithString:@"https://api.weibo.com/oauth2/authorize?client_id=3942775926&redirect_uri=http://www.cnblogs.com/hellovoidworld/"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
} @end
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// 启动后显示状态栏
UIApplication *app = [UIApplication sharedApplication];
app.statusBarHidden = NO;
// 设置window
self.window = [[UIWindow alloc] init];
self.window.frame = [UIScreen mainScreen].bounds;
self.window.rootViewController = [[HVWOAuthViewController alloc] init];
[self.window makeKeyAndVisible];
return YES;
}

/** 截取web发送请求 */
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { NSString *urlStr = request.URL.absoluteString;
NSRange range = [urlStr rangeOfString:@"http://www.cnblogs.com/hellovoidworld/?code="];
if (range.length > ) { // 如果是匹配的url,即发送的是带access_code的url
// 截取access_code
NSUInteger accessCodeLocation = range.length + range.location;
NSString *accessCode = [urlStr substringFromIndex:accessCodeLocation]; HVWLog(@"%@", accessCode); return NO; // 阻止发送,不需要跳转到重定向页面
} return YES; // 其他情况照常发送
}

/** 根据access_code获取access_token */
- (void) accessTokenWithAccessCode:(NSString *) accessCode {
// 创建AFN的http操作请求管理者
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; // 参数设置
NSMutableDictionary *param = [NSMutableDictionary dictionary];
param[@"client_id"] = @"";
param[@"client_secret"] = @"cc577953b2aa3aa8ea220fd15775ea35";
param[@"grant_type"] = @"authorization_code";
param[@"code"] = accessCode;
param[@"redirect_uri"] = @"http://www.cnblogs.com/hellovoidworld/"; // 发送请求
[manager POST:@"https://api.weibo.com/oauth2/access_token" parameters:param success:^(AFHTTPRequestOperation *operation, NSDictionary *accountInfo) {
[MBProgressHUD hideHUD]; // 返回的是用户信息字典
HVWLog(@"%@", accountInfo); } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[MBProgressHUD hideHUD]; HVWLog(@"请求access_token失败 ----> %@", error);
}]; }

- (instancetype)init {
self = [super init];
if (!self) {
return nil;
}
self.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/plain", nil];
return self;
}
"access_token" = "2.00A5GpAGGIUpSEff44478fe90yykBw";
"expires_in" = 181897;
"remind_in" = 181897;
uid = 5508976272;
// 存储用户信息,包括access_token到沙盒中
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *filePath = [docPath stringByAppendingPathComponent:@"accountInfo.plist"];
[iOS微博项目 - 2.2] - 在app中获取授权的更多相关文章
- iOS利用HealthKit框架从健康app中获取步数信息
微信和QQ的每日步数最近十分火爆,我就想为自己写的项目中添加一个显示每日步数的功能,上网一搜好像并有相关的详细资料,自己动手丰衣足食. 统计步数信息并不需要我们自己去实现,iOS自带的健康app已经为 ...
- iOS从健康app中获取步数信息
统计步数信息并不需要我们自己去实现,iOS自带的健康app已经为我们统计好了步数数据 我们只要使用HealthKit框架从健康app中获取这个数据信息就可以了 1.如下图所示 在Xcode中打开Hea ...
- [iOS微博项目 - 2.6] - 获取微博数据
github: https://github.com/hellovoidworld/HVWWeibo A.新浪获取微博API 1.读取微博API 2.“statuses/home_time ...
- [iOS微博项目 - 2.3] - 用户取消对app的授权
github: https://github.com/hellovoidworld/HVWWeibo A.用户取消对app的授权 用户可以在微博网站上取消对某个应用(app)的授权 1.打开& ...
- [iOS微博项目 - 2.0] - OAuth授权3步
A.概念 OAUTH协议为用户资源的授权提供了一个安全的.开放而又简易的标准.与以往的授权方式不同之处是OAUTH的授权不会使第三方触及到用户的帐号信息(如用户名与密码),即第三方无需使用用 ...
- 将unity3d项目嵌入到Android App中使用
创建一个新的AndroidStudio app项目. 1.添加库文件:拷贝unity安装目录下的库文件:Unity\Editor\Data\PlaybackEngines\AndroidPlayer\ ...
- [iOS微博项目 - 2.1] - 获得新浪授权接口
A.如何获得新浪的授权接口 登陆新浪的开放平台 注册新浪账号 创建应用 获得应用id和请求地址 查阅相关API 关联需要进行测试的账号 1.登陆开放平台 http://open.weibo.com ...
- app中获取应用名称,版本等信息的方法
在app中,我们有时候需要显示一些信息,例如名称,版本等等...如果用写死的方式可能不太好,我们可以动态的读取.应用的信息主要是在info.plist这个文件中,实际就是一个xml文件,以源文件的方式 ...
- [iOS微博项目 - 2.4] - 重新安排app启动步骤
github: https://github.com/hellovoidworld/HVWWeibo A.app启动步骤 1.加入了授权步骤之后,最先要判断app内是否已经登陆了账号 2.在程序启 ...
随机推荐
- POJ3592 Instantaneous Transference tarjan +spfa
链接:http://poj.org/problem?id=3592 题意:题目大意:给定一个矩阵,西南角为出发点,每个单位都有一订价值的金矿(#默示岩石,不成达,*默示时佛门,可以达到指定单位),队# ...
- HDFS常用命令
HDFS 常用的文件操作命令 hdfs dfs -text /pub/20151019/1/4/gwmvod/mediags.moretv.com.cn/*.bz2 | wc -l hdfs dfs ...
- (转)iOS动画Core Animation
文章转载:http://blog.sina.com.cn/s/blog_7b9d64af0101b8nh.html 在iOS中动画实现技术主要是:Core Animation. Core Animat ...
- linux shared lib 使用与编译
一. 动态链接库的原理及使用 Linux提供4个库函数.一个头文件dlfcn.h以及两个共享库(静态库libdl.a和动态库libdl.so)支持动态链接. Ø ...
- css去掉a标签点击后的虚线框
outline是css3的一个属性,用的很少. 声明,这是个不能兼容的css属性,在ie6.ie7.遨游浏览器都不兼容. outline控制的到底是什么呢? 当聚焦a标签的时候,在a标签的区域周围会有 ...
- django - 修改 自增长id,起始值
常常你会遇到这样的情况,需要自增长的起始值是 0,再次从 0开始. 两个选择: 1. drop table_name; django重新建表. 2. ALTER TABLE table_name AU ...
- (七)7.2 应用机器学习方法的技巧,准确率,召回率与 F值
建立模型 当使用机器学习的方法来解决问题时,比如垃圾邮件分类等,一般的步骤是这样的: 1)从一个简单的算法入手这样可以很快的实现这个算法,并且可以在交叉验证集上进行测试: 2)画学习曲线以决定是否更多 ...
- (一)线性回归与特征归一化(feature scaling)
线性回归是一种回归分析技术,回归分析本质上就是一个函数估计的问题(函数估计包括参数估计和非参数估计),就是找出因变量和自变量之间的因果关系.回归分析的因变量是应该是连续变量,若因变量为离散变量,则问题 ...
- 【英语】Bingo口语笔记(77) - 临时改变计划的表达
- php 二维数组的排序
写这是之前一直二维数组排名困扰.自己写的好复杂. 正题: array_mutisort 官方帮助文档 <?php// 取得列的列表foreach ($data as $key => $ro ...