gpt4 book ai didi

使用基本身份验证加载 UIWebView 资源

转载 作者:行者123 更新时间:2023-12-01 01:19:02 28 4
gpt4 key购买 nike

对于我正在创建的(网络)应用程序,我需要使用基本身份验证在我的 UIWebView 中加载页面。

现在设置我使用的授权 header :

NSString *result = [NSString stringWithFormat:@"%@:%@", username, password];
NSData *resultData = [result dataUsingEncoding:NSASCIIStringEncoding];

NSString *result64 = [NSString stringWithFormat:@"Basic %@", [resultData base64Encoding]];
[request setValue:result64 forHTTPHeaderField:@"Authorization"];

在我的 apache 访问日志中,我可以看到登录成功。但是随后 UIWebView 想要加载像 style.css 这样的资源和 jquery.min.js但是对这些资源的请求会失败,因为它们没有设置 Authorization header 。我怎样才能解决这个问题?

最佳答案

解决方案是继承 NSURLProtocol 以验证资源加载:

#import <Foundation/Foundation.h>

#import "AuthenticationUtils.h"

@interface CustomURLProtocol : NSURLProtocol <NSURLConnectionDelegate>
{
NSMutableURLRequest *_customRequest;
NSURLConnection *_connection;
}

@end

@implementation CustomURLProtocol

static NSString *AUTHORIZED_REQUEST_HEADER = @"X-AUTHORIZED";

+(BOOL) canInitWithRequest:(NSMutableURLRequest *)request
{
// check if the request is one you want to authorize
BOOL canInit = (![request.URL.scheme isEqualToString:@"file"] && [request valueForHTTPHeaderField:[AUTHORIZED_REQUEST_HEADER stringByAppendingString:[request.URL absoluteString]]] == nil);
return canInit;
}

-(id) initWithRequest:(NSURLRequest *)request cachedResponse:(NSCachedURLResponse *)cachedResponse client:(id<NSURLProtocolClient>)client
{
_customRequest = [request mutableCopy];
[_customRequest setValue:@"" forHTTPHeaderField:[AUTHORIZED_REQUEST_HEADER stringByAppendingString:[request.URL absoluteString]]];

self = [super initWithRequest:_customRequest cachedResponse:cachedResponse client:client];

return self;
}

+(NSURLRequest *) canonicalRequestForRequest:(NSURLRequest *)request
{
NSMutableURLRequest *customRequest = [request mutableCopy];
[customRequest setValue:@"" forHTTPHeaderField:[AUTHORIZED_REQUEST_HEADER stringByAppendingString:[request.URL absoluteString]]];

NSString *basicAuthentication = [AuthenticationUtils getBasicAuthentication];
[customRequest setValue:basicAuthentication forHTTPHeaderField:@"Authorization"];

return customRequest;
}

- (void) startLoading
{
NSString *basicAuthentication = [AuthenticationUtils getBasicAuthentication];
[_customRequest setValue:basicAuthentication forHTTPHeaderField:@"Authorization"];

_connection = [NSURLConnection connectionWithRequest:_customRequest delegate:self];
}

- (void) stopLoading
{
[_connection cancel];
}

#pragma mark - NSURLConnectionDelegate

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
// This protocol forgets to store cookies, so do it manually
if([redirectResponse isKindOfClass:[NSHTTPURLResponse class]])
{
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies:[NSHTTPCookie cookiesWithResponseHeaderFields:[(NSHTTPURLResponse*)redirectResponse allHeaderFields] forURL:[redirectResponse URL]] forURL:[redirectResponse URL] mainDocumentURL:[request mainDocumentURL]];
}

[[self client] URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[[self client] URLProtocol:self didLoadData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[[self client] URLProtocol:self didFailWithError:error];
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[[self client] URLProtocolDidFinishLoading:self];
}

-(NSURLRequest *) connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse
{
// This protocol forgets to store cookies, so do it manually
if([redirectResponse isKindOfClass:[NSHTTPURLResponse class]])
{
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies:[NSHTTPCookie cookiesWithResponseHeaderFields:[(NSHTTPURLResponse*)redirectResponse allHeaderFields] forURL:[redirectResponse URL]] forURL:[redirectResponse URL] mainDocumentURL:[request mainDocumentURL]];
}

// copy all headers to the new request
NSMutableURLRequest *redirect = [request mutableCopy];
for (NSString *header in [request allHTTPHeaderFields])
{
[redirect setValue:[[request allHTTPHeaderFields] objectForKey:header] forHTTPHeaderField:header];
}

return redirect;
}

@end

在你的 AppDelegate 中添加:
[NSURLProtocol registerClass:[CustomURLProtocol class]];

关于使用基本身份验证加载 UIWebView 资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10333959/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com