A.发送授权请求
1.使用UIWebView加载请求页面
自定义一个继承UIViewController的HVWOAuthViewController
 //
// 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
 
把这个控制器作为window的根控制器运行测试:
AppDelegate:
 - (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;
}
 
 
 
2.使用UIWebView代理拦截发送url动作,发送授权请求之后发送的url就带有access_code
 /** 截取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; // 其他情况照常发送
}
 
Output:
5bef5308ba902cc52d9f2dc34bbfdd1c
 
注意这个access_code每次都不一样的
 
 
3.获取access_token
(1)获取access_token的请求API,发送POST请求
 
(2)使用AFN框架来发送post请求
 /** 根据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);
}]; }
 
(3)由于新浪返回的json数据Content-Type是 text/plain,但是AFNetworking框架默认的json序列化器不能识别,所以要修改一下json序列化器的acceptableContentType
 
 - (instancetype)init {
self = [super init];
if (!self) {
return nil;
} self.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/plain", nil]; return self;
}
 
Output:
{
    "access_token" = "2.00A5GpAGGIUpSEff44478fe90yykBw";
    "expires_in" = 181897;
    "remind_in" = 181897;
    uid = 5508976272;
}
 
(4)保存返回的用户信息到沙盒
        // 返回的是用户信息字典
        // 存储用户信息,包括access_token到沙盒中
        NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        NSString *filePath = [docPath stringByAppendingPathComponent:@"accountInfo.plist"];
        [accountInfo writeToFile:filePath atomically:YES];
 
 
 

[iOS微博项目 - 2.2] - 在app中获取授权的更多相关文章

  1. iOS利用HealthKit框架从健康app中获取步数信息

    微信和QQ的每日步数最近十分火爆,我就想为自己写的项目中添加一个显示每日步数的功能,上网一搜好像并有相关的详细资料,自己动手丰衣足食. 统计步数信息并不需要我们自己去实现,iOS自带的健康app已经为 ...

  2. iOS从健康app中获取步数信息

    统计步数信息并不需要我们自己去实现,iOS自带的健康app已经为我们统计好了步数数据 我们只要使用HealthKit框架从健康app中获取这个数据信息就可以了 1.如下图所示 在Xcode中打开Hea ...

  3. [iOS微博项目 - 2.6] - 获取微博数据

    github: https://github.com/hellovoidworld/HVWWeibo   A.新浪获取微博API 1.读取微博API     2.“statuses/home_time ...

  4. [iOS微博项目 - 2.3] - 用户取消对app的授权

    github: https://github.com/hellovoidworld/HVWWeibo   A.用户取消对app的授权 用户可以在微博网站上取消对某个应用(app)的授权   1.打开& ...

  5. [iOS微博项目 - 2.0] - OAuth授权3步

    A.概念      OAUTH协议为用户资源的授权提供了一个安全的.开放而又简易的标准.与以往的授权方式不同之处是OAUTH的授权不会使第三方触及到用户的帐号信息(如用户名与密码),即第三方无需使用用 ...

  6. 将unity3d项目嵌入到Android App中使用

    创建一个新的AndroidStudio app项目. 1.添加库文件:拷贝unity安装目录下的库文件:Unity\Editor\Data\PlaybackEngines\AndroidPlayer\ ...

  7. [iOS微博项目 - 2.1] - 获得新浪授权接口

    A.如何获得新浪的授权接口 登陆新浪的开放平台 注册新浪账号 创建应用 获得应用id和请求地址 查阅相关API 关联需要进行测试的账号   1.登陆开放平台 http://open.weibo.com ...

  8. app中获取应用名称,版本等信息的方法

    在app中,我们有时候需要显示一些信息,例如名称,版本等等...如果用写死的方式可能不太好,我们可以动态的读取.应用的信息主要是在info.plist这个文件中,实际就是一个xml文件,以源文件的方式 ...

  9. [iOS微博项目 - 2.4] - 重新安排app启动步骤

    github: https://github.com/hellovoidworld/HVWWeibo   A.app启动步骤 1.加入了授权步骤之后,最先要判断app内是否已经登陆了账号 2.在程序启 ...

随机推荐

  1. Qt之模型/视图(自定义按钮)

    简述 衍伸前面的章节,我们对QTableView实现了数据显示.自定义排序.显示复选框.进度条等功能的实现,本节主要针对自定义按钮进行讲解,这节过后,也希望大家对自定义有更深入的了解,在以后的功能开发 ...

  2. Python [Leetcode 342]Power of Four

    题目描述: Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Examp ...

  3. wxWidgets简单的多线程

    #include <wx/wx.h> #include <wx/thread.h> #include <wx/event.h> #include <wx/pr ...

  4. scala学习笔记(1):基本语法与容器

    1 var 可变,val 不可变 var (a,b) = (10,20) 分别对a,b赋值 a=10, b=20 var a,b = (10,20)则 是a=(10,20) b=(10,20) 2 L ...

  5. Android Studio 学习 - HelloWorld

    今天是学习Android Studio的第2天,加油! 1. 首先要记录下使用Android Studio的一个代码自动完成的功能.平常基本上用Delphi,乍一换工具,各种不习惯,或者说不熟悉.按照 ...

  6. 【转】如何调整CHM文件中的字体!非常有爱!

    原文网址:http://www.cnblogs.com/lijh_ray/archive/2011/01/25/1944668.html 如果html中字体大小是用像素px来定义,那么在IE中无法调整 ...

  7. OpenGL学习之路(一)

    1 引子 虽然是计算机科班出身,但从小对几何方面的东西就不太感冒,空间想象能力也较差,所以从本科到研究生,基本没接触过<计算机图形学>.为什么说基本没学过呢?因为好奇(尤其是惊叹于三维游戏 ...

  8. JBPM4之decision节点:1、好学生 | 坏学生

    JBPM入门系列文章: JBPM4入门——1.jbpm简要介绍 JBPM4入门——2.在eclipse中安装绘制jbpm流程图的插件 JBPM4入门——3.JBPM4开发环境的搭建 JBPM4入门—— ...

  9. arcgis for android 无法加载本地jpg影像解决办法

    因为jpg影像没有生成金子塔文件*.rrd 一个完整的JPG影像必须包括如下文件: K-50-96-(16).aux  辅助文件K-50-96-(16).jgw  坐标信息K-50-96-(16).j ...

  10. C#中 String 格式的日期时间 转为 DateTime

    C#中并没有表示时间的变量,只有DateTime,所以要表示时间,可以用TimeSpan表示. 方法一:Convert.ToDateTime(string) string格式有要求,必须是yyyy-M ...