iOS9苹果将原http协议改成了https协议的方法

 更新时间:2016年01月08日 13:33:43   投稿:mrr  
这篇文章主要介绍了iOS9苹果将原http协议改成了https协议的方法的相关资料,需要的朋友可以参考下

解决方法:

在info.plist 加入key

<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>

下面给大家介绍ios中http 和https 协议的访问

最近做个项目,开始采用的是HTTP协议实现客户端和服务器端的交互,后来需要改成HTTPS协议。在修改的过程中发现了一些问题,解决方案如下:

HTTP:

NSString *urlString =[NSString stringWithFormat:@"https://127.0.0.1/default.aspx?USER=%@",@"111"];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"GET"];
NSHTTPURLResponse* urlResponse = nil;
NSError *error = [[NSError alloc] init];
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
NSMutableString *result = [[NSMutableString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"The result string is :%@",result); 

HTTPS

事件触发

{ 
NSString *urlString =[NSString stringWithFormat:@"https://127.0.0.1/default.aspx?USER=%@",@"111"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:5];
//设置请求方式为get
[request setHTTPMethod:@"GET"];
//添加用户会话id
[request addValue:@"text/html" forHTTPHeaderField:@"Content-Type"];
//连接发送请求
finished = false;
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
//堵塞线程,等待结束
while(!finished) {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
} 
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse*)response 
{}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{
//[_waitingDialog dismissWithClickedButtonIndex:0 animated:NO];
[connection release];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{ 
}
- (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection{
return NO;
}
//下面两段是重点,要服务器端单项HTTPS 验证,iOS 客户端忽略证书验证。
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
} 
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { 
NSLog(@"didReceiveAuthenticationChallenge %@ %zd", [[challenge protectionSpace] authenticationMethod], (ssize_t) [challenge previousFailureCount]);
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]){
[[challenge sender] useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
[[challenge sender] continueWithoutCredentialForAuthenticationChallenge: challenge];
}
} 
NSLog(@"get the whole response");
//[receivedData setLength:0];
}
//处理数据 
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{ 
} 

相关文章

  • IOS如何在Host App 与 App Extension 之间发送通知

    IOS如何在Host App 与 App Extension 之间发送通知

    这篇文章主要介绍了IOS如何在Host App 与 App Extension 之间发送通知 的相关资料,需要的朋友可以参考下
    2016-03-03
  • iOS runtime forwardInvocation详解及整理

    iOS runtime forwardInvocation详解及整理

    这篇文章主要介绍了 iOS runtime forwardInvocation详解及整理的相关资料,需要的朋友可以参考下
    2017-02-02
  • ios实现自动获取label高度、宽度及最后一个位置详解

    ios实现自动获取label高度、宽度及最后一个位置详解

    这篇文章主要给大家介绍了关于ios如何实现自动获取label高度、宽度及最后一个位置的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2017-10-10
  • IOS 中NSTimer定时器的使用

    IOS 中NSTimer定时器的使用

    这篇文章主要介绍了IOS 中NSTimer定时器的使用的相关资料,希望通过本文能帮助到大家,能让大家彻底理解使用该方法,需要的朋友可以参考下
    2017-10-10
  • iOS 导航栏自带页面右滑pop使用示例

    iOS 导航栏自带页面右滑pop使用示例

    本篇文章主要介绍了iOS 导航栏自带页面右滑pop使用示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-12-12
  • iOS开发删除storyboard步骤详解

    iOS开发删除storyboard步骤详解

    这篇文章主要为大家介绍了iOS系列学习之删除storyboard步骤详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-11-11
  • iOS开发傻瓜式微信支付的方法教程

    iOS开发傻瓜式微信支付的方法教程

    最近因为工作的需要,要开发微信支付,发现网上的很多教程过于复杂,索性自己写一篇,所以下面这篇文章主要跟大家分享了关于iOS开发傻瓜式微信支付的方法教程,文中通过示例代码介绍的非常详细,需要的朋友们下面来一起看看吧。
    2017-07-07
  • ios利用RunLoop原理实现去监控卡顿实例详解

    ios利用RunLoop原理实现去监控卡顿实例详解

    这篇文章主要为大家介绍了ios利用RunLoop原理实现去监控卡顿实例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-09-09
  • iOS 检测网络状态的两种方法

    iOS 检测网络状态的两种方法

    一般有Reachability和AFNetworking监测两种方式,都是第三方的框架,下文逐一详细给大家讲解,感兴趣的朋友一起看看吧
    2016-10-10
  • 快速解决ios微信下audio无法自动播放的问题

    快速解决ios微信下audio无法自动播放的问题

    下面小编就为大家分享一篇快速解决ios微信下audio无法自动播放的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-03-03

最新评论