LoginSignup
11
11

More than 5 years have passed since last update.

[objc][memo] UIWebViewでDIGEST認証

Last updated at Posted at 2014-10-24

UIWebViewでダイジェスト認証したい場合、NSURLProtectionSpaceというのを使うらしい。
URLにIDPASSを含んでもいいんだけど、何らかの理由で含められない場合。

AppDelegateの頭とかで呼べば、以降のUIWebViewの認証が通る(?)ようです。
authenticationMethodNSURLAuthenticationMethodHTTPDigestを指定。

AppDelegate.m


#define DIGEST_USER_NAME = @"yourname";

#define DIGEST_PASSWORD = @"yourpassword";

#define DIGEST_HOST     = @"www.hoge.jp"

// realmは、「curl -Iv http://www.hoge.jp/fuga/piyo」等で調べられる
#define DIGEST_REALM    = @"Input ID and Password" 


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    //------------------------------------
    // ダイジェスト認証
    //------------------------------------

    NSURLCredential *creds = [NSURLCredential 
        credentialWithUser:DIGEST_USER_NAME password:DIGEST_PASSWORD
        persistence:NSURLCredentialPersistenceForSession];

    NSURLCredentialStorage* store = [NSURLCredentialStorage sharedCredentialStorage];

    NSURLProtectionSpace* protectionSpace = [[NSURLProtectionSpace alloc] initWithHost:DIGEST_HOST
            port:80
            protocol:@"http"
            realm:DIGEST_REALM
            authenticationMethod:NSURLAuthenticationMethodHTTPDigest];

    [store setCredential:creds forProtectionSpace:protectionSpace];
    return YES;
}

参考

やっと人類の悲願だった「UIWebView でBasic 認証のかかったサイトを閲覧する方法」がわかった - laiso - iPhoneアプリ開発グループ

さいごに

なにか間違いがありましたらご指摘いただけるとありがたいです

11
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
11