What it actually does is to intercept the UIWebView to launch a NSURLConnection to allow the server to be authenticated, therefore then continue the connection using UIWebView, and cancels out the NSURLConnection. Reason so is because after authorising the server once, the rest of the continuous connection would not be blocked. Hope i'm clear. :)

Finally I got it!

What you can do is this:

Initiate your request using UIWebView as normal. Then - in webView:shouldStartLoadWithRequest - we reply NO, and instead start an NSURLConnection with the same request.

Using NSURLConnection, you can communicate with a self-signed server, as we have the ability to control the authentication through the extra delegate methods which are not available to a UIWebView. So using connection:didReceiveAuthenticationChallenge we can authenticate against the self signed server.

Then, in connection:didReceiveData, we cancel the NSURLConnection request, and start the same request again using UIWebView - which will work now, because we've already got through the server authentication :)

Here are the relevant code snippets below.

Note: Instance variables you will see are of the following type: 
UIWebView *_web
NSURLConnection *_urlConnection
NSURLRequest *_request

(I use an instance var for _request as in my case it's a POST with lots of login details, but you could change to use the request passed in as arguments to the methods if you needed.)

#pragma mark - Webview delegate

// Note: This method is particularly important. As the server is using a self signed certificate,
// we cannot use just UIWebView - as it doesn't allow for using self-certs. Instead, we stop the
// request in this method below, create an NSURLConnection (which can allow self-certs via the delegate methods
// which UIWebView does not have), authenticate using NSURLConnection, then use another UIWebView to complete
// the loading and viewing of the page. See connection:didReceiveAuthenticationChallenge to see how this works.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
{
NSLog(@"Did start loading: %@ auth:%d", [[request URL] absoluteString], _authenticated); if (!_authenticated) {
_authenticated = NO; _urlConnection = [[NSURLConnection alloc] initWithRequest:_request delegate:self]; [_urlConnection start]; return NO;
} return YES;
} #pragma mark - NURLConnection delegate - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;
{
NSLog(@"WebController Got auth challange via NSURLConnection"); if ([challenge previousFailureCount] == 0)
{
_authenticated = YES; NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]; [challenge.sender useCredential:credential forAuthenticationChallenge:challenge]; } else
{
[[challenge sender] cancelAuthenticationChallenge:challenge];
}
} - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
{
NSLog(@"WebController received response via NSURLConnection"); // remake a webview call now that authentication has passed ok.
_authenticated = YES;
[_web loadRequest:_request]; // Cancel the URL connection otherwise we double up (webview + url connection, same url = no good!)
[_urlConnection cancel];
} // We use this method is to accept an untrusted site which unfortunately we need to do, as our PVM servers are self signed.
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}

UIWebView to view self signed websites (No private api, not NSURLConnection) - is it possible?的更多相关文章

  1. 导出Private API

    首先介绍下private API 它共分为两类: 1 在官方文档中没有呈现的API(在frameworks 下隐藏) 2 苹果明确申明不能使用的API ,在privateFrameworks 下 然后 ...

  2. Windows Self Signed Driver

    In particular, Microsoft® instituted a device driver certification process for its Windows® desktop ...

  3. iOS Programming UIWebView 2

    iOS Programming  UIWebView 1 Instances of UIWebView render web content. UIWebView可以显示web content. In ...

  4. Android 自定义View及其在布局文件中的使用示例

    前言: 尽管Android已经为我们提供了一套丰富的控件,如:Button,ImageView,TextView,EditText等众多控件,但是,有时候在项目开发过程中,还是需要开发者自定义一些需要 ...

  5. Android 自定义View及其在布局文件中的使用示例(三):结合Android 4.4.2_r1源码分析onMeasure过程

    转载请注明出处 http://www.cnblogs.com/crashmaker/p/3549365.html From crash_coder linguowu linguowu0622@gami ...

  6. 绘制 ToggleButton --重写view

    package com.example.compoundbuttonview.view; import com.example.compoundbuttonview.R; import android ...

  7. Android中View的绘制过程 onMeasure方法简述 附有自定义View例子

    Android中View的绘制过程 onMeasure方法简述 附有自定义View例子 Android中View的绘制过程 当Activity获得焦点时,它将被要求绘制自己的布局,Android fr ...

  8. UIWebView的应用和其中的JS与OC间传值

    现在有很多的应用已经采用了WebView和html语言结合的开发模式.html5一直很火因为一份代码可以在多个平台上运用啊,效果各不相同都很美观,也越来越有一些公司直接招后台程序员和html5程序员, ...

  9. 白话学习MVC(十)View的呈现二

    本节将接着<白话学习MVC(九)View的呈现一>来继续对ViewResult的详细执行过程进行分析! 9.ViewResult ViewResult将视图页的内容响应给客户端! 由于Vi ...

随机推荐

  1. 使用IDEA新建Maven项目没有完整的项目结构(src文件夹等等)

    maven还没加载好,右下角还有进度条在从中央仓库读,所以在创建maven项目的时候,需要加archetypeCatalog=internal 右边新增一项 如下图: 此时就可以快速的建立maven项 ...

  2. 第十六篇:django基础

    本篇内容 创建程序 程序目录 流程介绍 login实例 一.创建程序 命令行: django-admin startproject sitename. 常用命令: python manage.py r ...

  3. BZOJ 3932: [CQOI2015]任务查询系统 | 主席树练习题

    题目: 洛谷也能评测 题解: De了好长时间BUG发现是自己sort前面有一行for没删,气死. 题目询问第x秒时候前k小的P值之和. 朴素想法: 我们可以把P值离散化,然后对于每个时刻建一棵定义域是 ...

  4. 《c程序设计语言》-2.10 不用if-else 转换大小写

    #include <stdio.h> int lower(char a) { int b; b = (a >= 'A' && a <= 'Z') ? (a - ...

  5. bzoj3561 莫比乌斯反演

    DZY Loves Math VI Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 518  Solved: 344[Submit][Status][D ...

  6. 小Z爱划水(NOIP信(sang)心(bin)赛)From FallDream

    题目: 小Z在机房.他和其它机房同学都面临一个艰难的抉择,那就是 要不要划水? 每个人都有自己的一个意见,有的人想做题,有的人想划水. 当然,每个人只能选择一个事情做.如果一个人做的事情和他想做的不同 ...

  7. Unicode与UTF-8互转(C语言实现) 基本原理

    1. 基础 1.1 ASCII码 我们知道, 在计算机内部, 所有的信息最终都表示为一个二进制的字符串. 每一个二进制位(bit)有0和1两种状态, 因此八个二进制位就可以组合出 256种状态, 这被 ...

  8. shell 将字符串分割成数组

    代码:test.sh #!/bin/bash a="one,two,three,four" #要将$a分割开,可以这样: OLD_IFS="$IFS" IFS= ...

  9. /proc/sys/shm/drop_caches

    author:skate time:2012/02/22 手工释放linux内存--/proc/sys/vm/drop_cache 转载一篇文章 linux的内存查看: [root@localhost ...

  10. makefile函数集锦【转】

    转自:http://blog.csdn.net/turkeyzhou/article/details/8612841 Makefile  常用函数表一.字符串处理函数1.$(subst FROM,TO ...