Created
September 12, 2011 18:19
-
-
Save laiso/1211980 to your computer and use it in GitHub Desktop.
UIWebView を使って BASIC認証のあるページにアクセスする
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// NOTE: 望んだように動かない…… | |
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions | |
{ | |
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; | |
UIWebView* webView = [[[UIWebView alloc] initWithFrame:self.window.frame] autorelease]; | |
webView.delegate = self; | |
NSString* url = @"http://example.com/authorization"; | |
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]]; | |
[self.window addSubview:webView]; | |
[self.window makeKeyAndVisible]; | |
return YES; | |
} | |
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType | |
{ | |
NSDictionary *headers = [request allHTTPHeaderFields]; | |
if(![[headers allKeys] containsObject:@"Authorization"]) { | |
NSMutableURLRequest *newRequest = [request mutableCopy]; | |
[newRequest addValue:@"Basic ${USER:PASSWORD_BASE64}" | |
forHTTPHeaderField:@"Authorization"]; | |
[webView loadRequest:newRequest]; | |
[newRequest release]; | |
return NO; | |
} | |
return YES; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// NOTE: 望んだように動かない…… | |
// → https://github.com/oscardelben/UIWebViewBasicAuth/blob/master/NSMutableURLRequest+BasicAuth.m | |
// だとうまくいった | |
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions | |
{ | |
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; | |
UIWebView* webView = [[[UIWebView alloc] initWithFrame:self.window.frame] autorelease]; | |
webView.delegate = self; | |
NSString* url = @"http://example.com/authorization"; | |
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]]; | |
[self.window addSubview:webView]; | |
[self.window makeKeyAndVisible]; | |
return YES; | |
} | |
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType | |
{ | |
NSDictionary *headers = [request allHTTPHeaderFields]; | |
if(![[headers allKeys] containsObject:@"Authorization"]) { | |
NSMutableURLRequest *newRequest = [request mutableCopy]; | |
[newRequest addValue:@"Basic ${USER:PASSWORD_BASE64}" | |
forHTTPHeaderField:@"Authorization"]; | |
[webView loadRequest:newRequest]; | |
[newRequest release]; | |
return NO; | |
} | |
return YES; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment