IOS开发中重定向302跳转的详细方法

来源:爱站网时间:2021-01-15编辑:网友分享
​​​​​​​浏览器将打单页的应用程序,在页面上启动Ajax请求后,后端返回302状态码并跳转到登录页面,现在爱站技术频道就为大家介绍IOS开发中重定向302跳转的详细方法,一起跟随进入下文看看吧!

浏览器将打单页的应用程序,在页面上启动Ajax请求后,后端返回302状态码并跳转到登录页面,现在爱站技术频道就为大家介绍IOS开发中重定向302跳转的详细方法,一起跟随进入下文看看吧!

一:前言

公司同事在做项目的时候遇到一个302地址跳转的问题,具体需求如下:

1、公司是做WIFI覆盖的,需要在下载APP后才能注册登录以后,自动连接到外网

2、苹果底层不允许我们码农进行WIFI的切换,所以公司考虑使用302重定向来实现,我通过2种方法来实现,NSURLConnetion和NSURLSession

二:NSURLConnetion方法

大概的思路:使用NSURLConnetion类的NSURLConnectionDataDelegate中的代理方法

 

 

- (nullable NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(nullable NSURLResponse *)response

 

都是码农,直接上代码吧

新建一个DEMO项目的话,记得把App Transport Security Settings Allow Arbitrary Loads设置为YES,否则无法请求网络

- (void)viewDidLoad {
  [super viewDidLoad];
 NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
  NSMutableURLRequest *quest = [NSMutableURLRequest requestWithURL:url];
  quest.HTTPMethod = @"GET";
  NSURLConnection *connect = [NSURLConnection connectionWithRequest:quest delegate:self];
  [connect start];
}

- (nullable NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(nullable NSURLResponse *)response{

  NSHTTPURLResponse *urlResponse = (NSHTTPURLResponse *)response;

  NSLog(@"%ld",urlResponse.statusCode);
  NSLog(@"%@",urlResponse.allHeaderFields);
  NSDictionary *dic = urlResponse.allHeaderFields;
  NSLog(@"%@",dic[@"Location"]);
  return request;
}

request 是你要发送的请求 urlResponse.statusCode 则是请求的状态码,302则是重定向 。urlResponse.allHeaderFields中保存着http的头信息:

{
  Connection = close;
  "Content-Type" = "text/html";
  Date = "Mon, 30 May 2016 04:00:49 GMT";
  Location = "http://118.244.233.137:6001/login/?gw_address=192.168.17.1&gw_port=2060&gw_id=ctzx_11&ip=192.168.17.24&mac=54:72:4f:30:c6:10&url=http%3A%2F%2Fwww.google.com%2F";
  Server = "Hughes Technologies Embedded Server";
}

通过urlResponse.allHeaderFields[@"Location"]即可拿到302跳转的url地址.如果需要拦截掉这个url地址跳转,则可在代理方法中自定义一个request

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
willPerformHTTPRedirection:(NSHTTPURLResponse *)response
    newRequest:(NSURLRequest *)request
 completionHandler:(void (^)(NSURLRequest * __nullable))completionHandler{

  //block为nil,则直接拦截掉302的url地址,防止自动跳转
  completionHandler(nil);

}

则直接拦截掉302的url地址,防止自动跳转 completionHandler(nil);}

三:NSURLSession方法

- (void)viewDidLoad {
  [super viewDidLoad];
  NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
  NSMutableURLRequest *quest = [NSMutableURLRequest requestWithURL:url];
  quest.HTTPMethod = @"GET";
  NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
  config.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
  NSURLSession *urlSession = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:[NSOperationQueue currentQueue]];

  NSURLSessionDataTask *task = [urlSession dataTaskWithRequest:quest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

    NSHTTPURLResponse *urlResponse = (NSHTTPURLResponse *)response;

    NSLog(@"%ld",urlResponse.statusCode);
    NSLog(@"%@",urlResponse.allHeaderFields);

    NSDictionary *dic = urlResponse.allHeaderFields;
    NSLog(@"%@",dic[@"Location"]);

  }];

  [task resume];

}


- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
willPerformHTTPRedirection:(NSHTTPURLResponse *)response
    newRequest:(NSURLRequest *)request
 completionHandler:(void (^)(NSURLRequest * __nullable))completionHandler{

  completionHandler(nil);

}

使用方法和第一种方法一样

Demo下载

以上就是关于IOS开发中重定向302跳转的详细方法,爱站技术频道在开发这个领域为大家带来了风向标,大家可以自由选择学习平台。

上一篇:IOS开发中生成图片验证码绘图实例代码

下一篇:IOS开发中常用的宏概述

您可能感兴趣的文章

相关阅读

热门软件源码

最新软件源码下载