- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我想在我的 UIWebView 中支持 HTTP 基本身份验证。
目前,我正在取消
中的请求webView:shouldStartLoadWithRequest:navigationType:
然后在我自己的 NSURLConnectionDelegate 中处理它们以检查并在需要时提供凭据。然后,我使用 loadData:MIMEType:textEncodingName:baseURL:
在 Web View 中显示 HTML。这适用于传递给委托(delegate)的任何 URL。
我的问题是永远不会为嵌入元素调用委托(delegate),例如图像、JavaScript 或 CSS 文件。因此,如果我有一个 HTML 页面引用了受基本身份验证保护的图像,则无法正确加载该图像。此外,永远不会调用 webView:didFinishLoad:
,因为 Web View 无法完全加载页面。
我已经用 App Store 上提供的第三方浏览器 Terra 验证了这种情况,它完全可以应对这种情况。我认为可以通过提供我自己的 NSURLProtocol 来解决这个问题,但这似乎太复杂了。我错过了什么?
最佳答案
尝试对您需要验证的所有域使用 sharedCredentialStorage。
这是 UIWebView 的工作示例,它针对仅启用了 BasicAuthentication 的 Windows IIS 进行了测试
这是添加站点凭据的方法:
NSString* login = @"MYDOMAIN\\myname";
NSURLCredential *credential = [NSURLCredential credentialWithUser:login
password:@"mypassword"
persistence:NSURLCredentialPersistenceForSession];
NSURLProtectionSpace *protectionSpace = [[NSURLProtectionSpace alloc]
initWithHost:@"myhost"
port:80
protocol:@"http"
realm:@"myhost" // check your web site settigns or log messages of didReceiveAuthenticationChallenge
authenticationMethod:NSURLAuthenticationMethodDefault];
[[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential:credential
forProtectionSpace:protectionSpace];
[protectionSpace release];
编辑:Swift 4 中的相同代码
let login = "MYDOMAIN\\myname"
let credential = URLCredential(user:login, password:"mypassword", persistence:.forSession)
let protectionSpace = URLProtectionSpace(host:"myhost", port:80, protocol:"http", realm:"myhost", authenticationMethod:NSURLAuthenticationMethodDefault)
URLCredentialStorage.shared.setDefaultCredential(credential, for:protectionSpace)
您的 webView 现在应该可以工作了,如果它不工作,请使用下一个代码进行调试,尤其是检查 didReceiveAuthenticationChallenge 的日志消息。
#import "TheSplitAppDelegate.h"
#import "RootViewController.h"
@implementation TheSplitAppDelegate
@synthesize window = _window;
@synthesize splitViewController = _splitViewController;
@synthesize rootViewController = _rootViewController;
@synthesize detailViewController = _detailViewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
// Add the split view controller's view to the window and display.
self.window.rootViewController = self.splitViewController;
[self.window makeKeyAndVisible];
NSLog(@"CONNECTION: Add credentials");
NSString* login = @"MYDOMAIN\\myname";
NSURLCredential *credential = [NSURLCredential credentialWithUser:login
password:@"mypassword"
persistence:NSURLCredentialPersistenceForSession];
NSURLProtectionSpace *protectionSpace = [[NSURLProtectionSpace alloc]
initWithHost:@"myhost"
port:80
protocol:@"http"
realm:@"myhost" // check your web site settigns or log messages of didReceiveAuthenticationChallenge
authenticationMethod:NSURLAuthenticationMethodDefault];
[[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential:credential forProtectionSpace:protectionSpace];
[protectionSpace release];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://myhost/index.html"]
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:12
];
NSLog(@"CONNECTION: Run request");
[[NSURLConnection alloc] initWithRequest:request delegate:self];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
}
- (void)applicationWillTerminate:(UIApplication *)application
{
}
- (void)dealloc
{
[_window release];
[_splitViewController release];
[_rootViewController release];
[_detailViewController release];
[super dealloc];
}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;
{
NSLog(@"CONNECTION: got auth challange");
NSString* message = [NSString stringWithFormat:@"CONNECTION: cred cout = %i", [[[NSURLCredentialStorage sharedCredentialStorage] allCredentials] count]];
NSLog(message);
NSLog([connection description]);
NSLog([NSString stringWithFormat:@"CONNECTION: host = %@", [[challenge protectionSpace] host]]);
NSLog([NSString stringWithFormat:@"CONNECTION: port = %i", [[challenge protectionSpace] port]]);
NSLog([NSString stringWithFormat:@"CONNECTION: protocol = %@", [[challenge protectionSpace] protocol]]);
NSLog([NSString stringWithFormat:@"CONNECTION: realm = %@", [[challenge protectionSpace] realm]]);
NSLog([NSString stringWithFormat:@"CONNECTION: authenticationMethod = %@", [[challenge protectionSpace] authenticationMethod]]);
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
// release the connection, and the data object
[connection release];
// inform the user
NSLog(@"CONNECTION: failed! Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
{
NSLog(@"CONNECTION: received response via nsurlconnection");
}
- (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection;
{
NSLog(@"CONNECTION: USE!");
return YES;
}
@end
WebView 身份验证的最终解决方案是基于自定义协议(protocol)实现。所有协议(protocol)都注册为堆栈,因此如果您重新定义 HTTP 协议(protocol),它将拦截来自 webView 的所有请求,因此您必须检查与传入请求相关的属性并将其重新打包为新请求并通过您自己的连接再次发送。由于您在堆栈中,您的请求会立即再次出现,您必须忽略它。所以它将协议(protocol)栈向下传递到真正的 HTTP 协议(protocol)实现,因为您的请求未被验证,您将获得身份验证请求。在身份验证之后,您将从服务器获得真实响应,因此您重新打包响应并回复从 webView 收到的原始请求,仅此而已。
不要尝试创建新的请求或响应主体,您必须重新发送它们。最终代码大约为 30-40 行代码,非常简单,但需要大量调试和测试。
不幸的是我不能在这里提供代码,因为我已经被分配到不同的项目,我只是想说我的帖子是错误的,当用户更改密码时它卡住了。
关于ios - 如何正确地在 UIWebView 中进行身份验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8999776/
我在 Web 应用程序中尝试了一些字节码操作,到目前为止,效果很好。现在我需要在代码中的某些特定位置注入(inject)一些字节代码。我用 switch 语句和 method.inserAt(); 尝
我正在尝试对数组中的对象值求和,然后计算每个值相对于总数的百分比。 这里是数组: [ { "ratiototale": [ [ { "0":
我在接口(interface)中有以下方法.. Task> SearchAsync(TU searchOptions); 效果很好。 现在我正在尝试进行单元测试以测试何时出现问题 - 代码会抛出异常。
我似乎无法让 reveal 开始工作,我已经严格遵守所有内容,添加 modernizr,添加插件,添加 css(reveal 显然包含在 foundation 中),在开始时初始化 foundatio
我正在尝试移植 raspberrypi's userspace 的位从 C 到 golang 的代码,我遇到了一个涉及 ioctl() 的程序。 我在执行 C 代码时遇到了问题 #define MAJ
我一直在尝试用纯 Java 制作一个简单的游戏,但在绘图时遇到了问题。我正在尝试保持相对较高的帧速率,但是 JFrame.repaint() 不能被“强制”并且只是在下一个可用机会重绘帧的请求这一事实
给定一个字符串 "2*(i+j) = 20 我想返回 ((lambda x: x >= 20), ['i']) 我可以然后直接提供给 constraint。 最佳答案 您正在寻找 eval 的替代方法
我对改进我的 javascript 代码以使其成为正确的 OOP 很感兴趣....目前我倾向于做这样的事情: jQuery(document).ready(function () { Page
我有一个调用两个动画 Action 的事件监听器。不幸的是,它们的开始有少量错开(例如,函数中的第一个首先开始)。 有谁知道正确同步它们的方法吗? 这是我的代码: $("#nav ul li a").
我只需要检查目录是否存在!但是,如果目录是“E:\Test”,其中 E: 是 CD/DVD 驱动器,并且上面没有插入磁盘,我会看到以下 Delphi 和 Windows 问题。 第一种方法: func
同样的问题: https://stackoverflow.com/questions/11294207/exchange-web-services-argumentexception-using-my
如果您跳转到 this question 中的第一个答案你会看到他使用 Employee.prototype = new Person(); 将 Person.prototype 继承到 Employ
我需要知道如何正确地遍历元素的 ArrayList 并计算元素在列表中出现的次数,而无需事先知道该元素。我尝试了几种方法并且有一种目前有效,但我觉得它很丑陋且不合适。 为了更深入地解释,我有一个 ja
我有一个用 Python 编写的(非常基本但工作完美的)AWS lambda 函数,但是它具有嵌入式凭证以连接到:1)外部网络服务2) DynamoDB 表。 该函数的作用相当基本:它针对服务发布登录
我很好奇 Tornado 推荐的查询 Redis(或任何数据库)的方法是什么。 我见过一些像 https://gist.github.com/357306 这样的例子但他们似乎都在使用对 redis
这更像是一个“我做得对吗”的问题。 快速背景故事:我已经构建了一个 gradle 插件(在一个独立的 gradle/groovy 项目中)。我在另一个 java 项目中使用它。客户项目通过以下方式引用
在我的代码中,我有: function handleMessage() { const twilio = require('twilio')(process.env.TWILIO_ACCOUNT_
我正在努力在 Linux 中刷新嵌入式设备的先前 ROM 转储。我以前的转储包含 oob 数据。我是用nandwrite -n -N -o/dev/mtd0 backup.bin写的,然后再做一次RO
我正在尝试使用 go 为 react-router 提供服务,我已经做到了,但我遇到了麻烦,我认为我做的方式不正确,或者不完整。我正在使用 Mux。我遇到的麻烦是当我按下 时在我的应用程序中,它正确
我正在尝试 promise JSON.parse 方法,但不幸的是没有任何运气。这是我的尝试: Promise.promisify(JSON.parse, JSON)(data).then((resu
我是一名优秀的程序员,十分优秀!