0
NSString *login = @"Web Guest";
NSString *password = @"xxxxx";
NSError *myError = nil;

NSLog(@"CONNECTION: Adding credentials");

NSURLCredential *credential = [NSURLCredential credentialWithUser:login
                                                         password:password
                                                      persistence:NSURLCredentialPersistenceForSession];

NSURLProtectionSpace *protectionSpace = [[NSURLProtectionSpace alloc]
                                         initWithHost:@"54.225.xxx.xxx"
                                         port:80
                                         protocol:@"http"
                                         realm:@"54.225.xxx.xxx" 
                                         authenticationMethod:NSURLAuthenticationMethodDefault];

//Not sure if set up correctly

[[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential:credential forProtectionSpace:protectionSpace];   

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"xx.xxx.xxx.xxx/blah"]
                                                       cachePolicy:NSURLRequestReloadIgnoringCacheData
                                                   timeoutInterval:12];

NSLog(@"CONNECTION: Running request");

//Perform the request
NSURLResponse *response;

NSData *data = [NSURLConnection
                sendSynchronousRequest:request
                returningResponse:&response
                error:&myError];

この時点で、「キャッチされていない例外 'NSInvalidArgumentException' が原因でアプリを終了しています。理由: ' +[NSString stringWithCString:encoding:]: NULL cString'」というエラーが表示されます。

NSString *result = [NSString stringWithCString:[data bytes] encoding:NSUTF8StringEncoding];

NSLog(@"Webserver response: %@", result);

基本認証を使用した承認が必要な Web サーバーから JSON を返そうとしていますが、セットアップに問題があるようです。Objective-C プログラミングと iOS 開発は初めてですが、何か助けていただければ幸いです。ありがとう。

4

1 に答える 1

0

リクエスト URL 文字列でスキームを使用していません: @"xx.xxx.xxx.xxx/blah"。@" https://xx.xxx.xxx.xxx/blah " のようなものが必要です。それ以外の場合 [NSURL URLWithString:] は nil を返します。

于 2013-06-26T17:30:14.627 に答える