iOS开发笔记--URL重定向 URL redirect

关于iOS的url跳转的问题,看了下官方的文档,都是英文的似乎越看越糊涂。在网上搜到了以下这篇博客,感觉写的非常清楚就转过来了感谢原作者!这个是原文的链接:点击打开链接


今天分享一下iOS应用中URL地址如何重定向


就用一个很简单的例子

http://www.google.com谷歌的首页


都知道现在浏览器中打开google.com的话事实上会变成http://www.google.com.hk


网址被重定向了


如何在app中完成重定向呢


使用NSURLConnetion类的NSURLConnectionDataDelegate委托

NSURLConnectionDataDelegate委托中的这个方法

[html]  view plain copy
  1. - (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response  

可以得到重定向以后的URL

看代码

[html]  view plain copy
  1. #import "ViewController.h"  
  2.   
  3. @interface ViewController ()  
  4.   
  5. @end  
  6.   
  7. @implementation ViewController  
  8.   
  9. - (void)viewDidLoad {  
  10.     [super viewDidLoad];  
  11.       
  12.     NSURL *url = [NSURL URLWithString:@"http://www.google.com"];  
  13.     NSURLRequest *request = [NSURLRequest requestWithURL:url];  
  14.     NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];  
  15.       
  16.     if (!connection) {  
  17.         NSLog(@"FAIL");  
  18.     }  
  19. }  
  20.   
  21. - (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response {  
  22.     NSLog(@"================================================");  
  23.     NSLog(@"will send request\n%@", [request URL]);  
  24.     NSLog(@"redirect response\n%@", [response URL]);  
  25.       
  26.     return request;  
  27. }  
  28.   
  29. @end  

以上省略了部分无关代码

解释一下代码

在viewDidLoad方法中进行了一次连接 正是谷歌的首页


再看看这个

[html]  view plain copy
  1. - (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response  

这个方法在请求将要被发送出去之前会调用

返回值是一个NSURLRequest就是那个真正将要被发送的请求

第二个参数request就是被重定向处理过后的请求 在这里就可以拿到需要的URL

第三个参数response是一个将要触发重定向的请求


这里把request跟response中的URL打出来看一下

并直接返回request


运行看看结果


[html]  view plain copy
  1. ====will send request====  
  2. http://www.google.com/  
  3. ====redirect response====  
  4. (null)  
  5.   
  6.   
  7. ====will send request====  
  8. http://www.google.com.hk/url?sa=p&hl=zh-CN&pref=hkredirect&pval=yes&q=http://www.google.com.hk/&ust=1347589441099727&usg=AFQjCNEsBYcrlh_jqpBWRwsc0NpBj2_lFg  
  9. ====redirect response====  
  10. http://www.google.com/  
  11.   
  12.   
  13. ====will send request====  
  14. http://www.google.com.hk/  
  15. ====redirect response====  
  16. http://www.google.com.hk/url?sa=p&hl=zh-CN&pref=hkredirect&pval=yes&q=http://www.google.com.hk/&ust=1347589441099727&usg=AFQjCNEsBYcrlh_jqpBWRwsc0NpBj2_lFg  

可以看出进行了两次重定向

第一次

[html]  view plain copy
  1. ====will send request====  
  2. http://www.google.com/  
  3. ====redirect response====  
  4. (null)  

由于是第一次调用 没有进行重定向处理 所以redirect response是null

而想要被发送的请求就是www.google.com


第二次

[html]  view plain copy
  1. ====will send request====  
  2. http://www.google.com.hk/url?sa=p&hl=zh-CN&pref=hkredirect&pval=yes&q=http://www.google.com.hk/&ust=1347589441099727&usg=AFQjCNEsBYcrlh_jqpBWRwsc0NpBj2_lFg  
  3. ====redirect response====  
  4. http://www.google.com/  
 这个时候redirect response就不是null了 就是第一次中的那个request 也就是说这一次的重定向是由www.google.com这个URL引发的

而重定向的结果就是http://www.google.com.hk/url?sa=p&hl=zh-CN&pref=hkredirect&pval=yes&q=http://www.google.com.hk/&ust=1347589441099727&usg=AFQjCNEsBYcrlh_jqpBWRwsc0NpBj2_lFg


第三次

[html]  view plain copy
  1. ====will send request====  
  2. http://www.google.com.hk/  
  3. ====redirect response====  
  4. http://www.google.com.hk/url?sa=p&hl=zh-CN&pref=hkredirect&pval=yes&q=http://www.google.com.hk/&ust=1347589441099727&usg=AFQjCNEsBYcrlh_jqpBWRwsc0NpBj2_lFg  
当然这个http://www.google.com.hk/url?sa=p&hl=zh-CN&pref=hkredirect&pval=yes&q=http://www.google.com.hk/&ust=1347589441099727&usg=AFQjCNEsBYcrlh_jqpBWRwsc0NpBj2_lFg

这个网址不是那个熟悉的网址

因为还要一次重定向

这次就跟上面一样了 那个很长的URL这次出现在了redirect response里 触发了这次重定向

结果可以看到

就是那个熟悉的www.google.com.hk


当获得想要的URL以后可以调用[connection cancel];方法来取消连接

并返回nil就好了


今天的分享就到这里


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS开发中上传图片可以采用以下步骤: 1.选择要上传的图片,可以使用系统提供的UIImagePickerController控制器,或者使用第三方库,例如TZImagePickerController。 2.将选中的图片转换为NSData格式。 3.使用NSURLSession或AFNetworking等网络库,将图片数据上传到服务器。 以下是一个简单的上传图片的示例代码: ``` // 选择图片 UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; imagePicker.delegate = self; [self presentViewController:imagePicker animated:YES completion:nil]; // 将选中的图片转换为NSData格式 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info { UIImage *selectedImage = info[UIImagePickerControllerOriginalImage]; NSData *imageData = UIImageJPEGRepresentation(selectedImage, 0.5); // 上传图片到服务器 NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration]; NSURL *url = [NSURL URLWithString:@"http://example.com/upload.php"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; request.HTTPMethod = @"POST"; NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request fromData:imageData completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { // 处理服务器返回的响应 }]; [uploadTask resume]; [picker dismissViewControllerAnimated:YES completion:nil]; } ``` 其中,upload.php是服务器端接收图片的脚本文件。在服务器端,可以使用PHP等语言来处理上传的图片数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值