Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
An UIWebView subclass which uses WebKit private APIs to delegate authentications.
//
// BPPrivateWebView.h
// BPUI
//
// Created by akisute on 11/09/11.
//
/*
Copyright (c) 2011 Masashi Ono.
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#import <UIKit/UIKit.h>
@class BPPrivateWebView;
/*!
@abstract BPPrivateWebViewのdelegateです。
@discussion DEBUGフラグがpreprocessor definitionsで定義されている場合には内部で使用しているPrivate APIを消すため、一切メソッドが呼び出されなくなります。
*/
@protocol BPPrivateWebViewAuthenticationDelegate <NSObject>
@optional
- (BOOL)webView:(BPPrivateWebView *)webView canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace;
- (void)webView:(BPPrivateWebView *)webView didCancelAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;
- (void)webView:(BPPrivateWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;
@end
/*!
@class BPPrivateWebView
@abstract WebKitのプライベートなAPIを使って、開発サーバーでのbasic認証とかを突破できるようにしたUIWebViewです。
@discussion WebKitのプライベートAPIをガシガシ使っているので、間違っても本番では使わないでください!リジェクト対象になります。
一応念の為DEBUGフラグがpreprocessor definitionsで定義されている場合にはすべてのPrivate APIを使用している箇所が消えてなくなるようにしてありますが、
それでも本番ではクラス自体使わないようにするほうが良いかもしれません。
*/
@interface BPPrivateWebView : UIWebView
@property (nonatomic, assign) id<BPPrivateWebViewAuthenticationDelegate> authenticationDelegate;
@property (nonatomic, assign) BOOL enableVerboseLog;
@end
//
// BPPrivateWebView.m
// BPUI
//
// Created by akisute on 11/09/11.
//
/*
Copyright (c) 2011 Masashi Ono.
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#import "BPPrivateWebView.h"
#ifdef DEBUG
@interface UIWebView(BPPrivateWebView_PrivateAPI)
// とりあえず使えそうなUIWebViewのPrivate Methodを列挙してみました。
// UIWebViewはWebKitのクラスのdelegateになっているのでこの辺が使えます
- (BOOL)webView:(id)arg1 resource:(id)arg2 canAuthenticateAgainstProtectionSpace:(id)arg3 forDataSource:(id)arg4;
- (void)webView:(id)arg1 resource:(id)arg2 didCancelAuthenticationChallenge:(id)arg3 fromDataSource:(id)arg4;
- (void)webView:(id)arg1 resource:(id)arg2 didReceiveAuthenticationChallenge:(id)arg3 fromDataSource:(id)arg4;
- (void)webView:(id)arg1 resource:(id)arg2 didFailLoadingWithError:(id)arg3 fromDataSource:(id)arg4;
- (void)webView:(id)arg1 resource:(id)arg2 didFinishLoadingFromDataSource:(id)arg3;
- (id)webView:(id)arg1 resource:(id)arg2 willSendRequest:(id)arg3 redirectResponse:(id)arg4 fromDataSource:(id)arg5;
- (id)webView:(id)arg1 connectionPropertiesForResource:(id)arg2 dataSource:(id)arg3;
@end
#endif
#pragma mark -
@interface BPPrivateWebView ()
- (void)__logWebDataSource:(id)webDataSource;
@end
#pragma mark -
@implementation BPPrivateWebView
@synthesize authenticationDelegate = _authenticationDelegate;
@synthesize enableVerboseLog = _enableVerboseLog;
#pragma mark - Init/dealloc
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.authenticationDelegate = nil;
self.enableVerboseLog = NO;
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
// NSCoding not supported
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
#pragma mark - Private
- (void)__logWebDataSource:(id)webDataSource
{
if (self.enableVerboseLog) {
NSLog(@" * dataSource = %@", webDataSource);
NSLog(@" * dataSource.initialRequest = %@", [webDataSource valueForKey:@"initialRequest"]);
NSLog(@" * dataSource.request = %@", [webDataSource valueForKey:@"request"]);
NSLog(@" * dataSource.response = %@", [webDataSource valueForKey:@"request"]);
NSLog(@" * dataSource.isLoading = %d", [webDataSource isLoading]);
NSLog(@" * dataSource.pageTitle = %@", [webDataSource valueForKey:@"pageTitle"]);
NSLog(@" * dataSource.textEncodingName = %@", [webDataSource valueForKey:@"textEncodingName"]);
NSLog(@" * dataSource.webArchive = %@", [webDataSource valueForKey:@"webArchive"]);
NSLog(@" * dataSource.mainResource = %@", [webDataSource valueForKey:@"mainResource"]);
NSLog(@" * dataSource.data = %@", [webDataSource valueForKey:@"data"]);
}
}
#pragma mark - WebKit private callbacks
#ifdef DEBUG
- (BOOL)webView:(id)arg1 resource:(id)arg2 canAuthenticateAgainstProtectionSpace:(id)arg3 forDataSource:(id)arg4
{
if (self.enableVerboseLog) {
NSLog(@"%s", __func__);
NSLog(@" * resource = %@", arg2);
NSLog(@" * protectionSpace = %@", arg3);
[self __logWebDataSource:arg4];
}
if ([self.authenticationDelegate respondsToSelector:@selector(webView:canAuthenticateAgainstProtectionSpace:)]) {
return [self.authenticationDelegate webView:self canAuthenticateAgainstProtectionSpace:arg3];
} else {
return [super webView:arg1 resource:arg2 canAuthenticateAgainstProtectionSpace:arg3 forDataSource:arg4];
}
}
- (void)webView:(id)arg1 resource:(id)arg2 didCancelAuthenticationChallenge:(id)arg3 fromDataSource:(id)arg4
{
if (self.enableVerboseLog) {
NSLog(@"%s", __func__);
NSLog(@" * resource = %@", arg2);
NSLog(@" * authenticationChallenge = %@", arg3);
[self __logWebDataSource:arg4];
}
if ([self.authenticationDelegate respondsToSelector:@selector(webView:didCancelAuthenticationChallenge:)]) {
[self.authenticationDelegate webView:self didCancelAuthenticationChallenge:arg3];
} else {
[super webView:arg1 resource:arg2 didCancelAuthenticationChallenge:arg3 fromDataSource:arg4];
}
}
- (void)webView:(id)arg1 resource:(id)arg2 didReceiveAuthenticationChallenge:(id)arg3 fromDataSource:(id)arg4
{
if (self.enableVerboseLog) {
NSLog(@"%s", __func__);
NSLog(@" * resource = %@", arg2);
NSLog(@" * authenticationChallenge = %@", arg3);
[self __logWebDataSource:arg4];
}
if ([self.authenticationDelegate respondsToSelector:@selector(webView:didReceiveAuthenticationChallenge:)]) {
[self.authenticationDelegate webView:self didReceiveAuthenticationChallenge:arg3];
} else {
[super webView:arg1 resource:arg2 didReceiveAuthenticationChallenge:arg3 fromDataSource:arg4];
}
}
- (void)webView:(id)arg1 resource:(id)arg2 didFailLoadingWithError:(id)arg3 fromDataSource:(id)arg4
{
if (self.enableVerboseLog) {
NSLog(@"%s", __func__);
NSLog(@" * resource = %@", arg2);
NSLog(@" * error = %@", arg3);
[self __logWebDataSource:arg4];
}
[super webView:arg1 resource:arg2 didFailLoadingWithError:arg3 fromDataSource:arg4];
}
- (void)webView:(id)arg1 resource:(id)arg2 didFinishLoadingFromDataSource:(id)arg3
{
if (self.enableVerboseLog) {
NSLog(@"%s", __func__);
NSLog(@" * resource = %@", arg2);
[self __logWebDataSource:arg3];
}
[super webView:arg1 resource:arg2 didFinishLoadingFromDataSource:arg3];
}
- (id)webView:(id)arg1 resource:(id)arg2 willSendRequest:(id)arg3 redirectResponse:(id)arg4 fromDataSource:(id)arg5
{
if (self.enableVerboseLog) {
NSLog(@"%s", __func__);
NSLog(@" * resource = %@", arg2);
NSLog(@" * request = %@", arg3);
NSLog(@" * redirectResponse = %@", arg4);
[self __logWebDataSource:arg5];
}
return [super webView:arg1 resource:arg2 willSendRequest:arg3 redirectResponse:arg4 fromDataSource:arg5];
}
- (id)webView:(id)arg1 connectionPropertiesForResource:(id)arg2 dataSource:(id)arg3
{
if (self.enableVerboseLog) {
NSLog(@"%s", __func__);
NSLog(@" * resource = %@", arg2);
[self __logWebDataSource:arg3];
}
return [super webView:arg1 connectionPropertiesForResource:arg2 dataSource:arg3];
}
#endif
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment