1

私は簡単なGET要求を私のmacOSアプリケーションで実行しようとしています。しかし、私は次のエラーを得続ける:ネットワーク要求が失敗しています - NSURLSession - App Sandbox - Xcode 9

A server with the specified hostname could not be found.

私があるからJSONデータをダウンロードしようとしていますURL:

私は私のブラウザやオンラインAPIテスターのウェブサイトでそれをテストする場合

https://suggestqueries.google.com/complete/search?client=safari&q=mercedes

(例えばHurl.itのように)、要求は正常に動作します。 JSONファイルは自動的にダウンロードされます。

しかし、私のmacOSアプリケーションで要求を実行することはできません。ここに私のコードは次のとおりです。ここで

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://suggestqueries.google.com/complete/search?client=safari&q=mercedes"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; 
[request setHTTPMethod:@"GET"]; 

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; 
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil]; 

[[session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { 

    NSLog(@"Data: %@", data); 
    NSLog(@"Response: %@", response); 
    NSLog(@"Error: %@", error); 

}] resume]; 

は完全なエラーログです:私は間違っ

dnssd_clientstub ConnectToServer: connect()-> No of tries: 1 dnssd_clientstub ConnectToServer: connect()-> No of tries: 2 dnssd_clientstub ConnectToServer: connect()-> No of tries: 3 dnssd_clientstub ConnectToServer: connect() failed

path:/var/run/mDNSResponder Socket:6 Err:-1 Errno:1 Operation not permitted 2017-10-27 09:58:31.610493+0100 search suggestions [] nw_resolver_create_dns_service_locked DNSServiceCreateDelegateConnection failed: ServiceNotRunning(-65563) TIC TCP Conn Failed [1:0x600000164080]: 10:-72000 Err(-65563) Task <12212C3B-8606-49C2-BD72-AEBD575DB638>.<1> HTTP load failed (error code: -1003 [10:-72000]) Task <12212C3B-8606-49C2-BD72-AEBD575DB638>.<1> finished with error - code: -1003

Data: (null) Response: (null) Error: Error Domain=NSURLErrorDomain Code=-1003 "A server with the specified hostname could not be found." UserInfo={NSUnderlyingError=0x60400004fa20 {Error Domain=kCFErrorDomainCFNetwork Code=-1003 "(null)" UserInfo={_kCFStreamErrorCodeKey=-72000, _kCFStreamErrorDomainKey=10}}, NSErrorFailingURLStringKey= https://www.suggestqueries.google.com/complete/search?client=safari&q=mercedes , NSErrorFailingURLKey= https://www.suggestqueries.google.com/complete/search?client=safari&q=mercedes , _kCFStreamErrorDomainKey=10, _kCFStreamErrorCodeKey=-72000, NSLocalizedDescription=A server with the specified hostname could not be found.}

何をしているのですか?それは単純なGETリクエストですが、データがロードされない理由はわかりません。

お時間をありがとうございました。

答えて

3

下にしてみてください私は、これはもはや、ネットワーク要求を有効にするのに十分です、私はYESからAllow Arbitrary Loadsを設定していたにも関わらず、間違っていたものを考え出していません。

Allow Arbitrary Loads

すぎて、発信/着信ネットワーク接続を停止することができますApp Sandbox呼ばXcode 9の新しい設定があります!この設定をオフにしてから、すべてのネットワーク要求が機能し始めました。

App Sandbox Setting

+2

WebViewを動作させることができなかった理由をデバッグしようと2日を費やしました。それはサンドボックスの設定でした。このポストをありがとう。 –

0

コード

@property (nonatomic, strong) NSURLConnection *connection; 

NSMutableString *urlString = [NSMutableString stringWithString:BASE_URL]; 

[urlString appendFormat:@"%@",apiName]; //apiName —> Webservice Name 

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]]; 

[request setValue:@"gF!DeEfFrjqaAaD$gH#[email protected](z" forHTTPHeaderField:@"PC-API-KEY"]; //Optional Parameter pass if required key 
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 

[request setHTTPMethod:@"POST"]; //if your required Get than [request setHTTPMethod:@"GET"]; 

[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
[request setHTTPBody:[parameterDict toJSON]]; 
[request setTimeoutInterval:45.0]; 

self.connection =[[NSURLConnection alloc] initWithRequest:request delegate:self]; 

if (self.connection) { 
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; 
    self.downLoadedData = [NSMutableData data]; 
} 
+0

これは私の問題をどのように解決するかわかりません。これは基本的に私と同じコードですが、デリゲートメソッドを使用してブロックではなくデータを読み込む点が異なります。 – Supertecnoboff

+0

また、私のアプリケーションは '' macOS'''プラットフォーム用ですので、 '' [[UIApplication sharedApplication] setNetworkActivityIndi​​catorVisible:YES]; '' 'は適用されません。 – Supertecnoboff

関連する問題