web-dev-qa-db-ja.com

非推奨のiOS5 NSURLConnectionメソッド

ネットワーク経由でデータを取得するための非同期要求を行うiOSアプリを作成しようとしています。多くの人がこのためにNSURLConnectionの使用を推奨しており、デリゲートメソッドconnection:didReceiveData:について頻繁に言及しているようです。

残念ながら、このデリゲートメソッドが文書化されている場所を知ることはできません。 1つは、 NSURLConnectionDelegateのプロトコルリファレンス にありません。 NSURLConnection Class Reference にリストされていますが、iOS5の時点で廃止されているようです。ドキュメントでは、非推奨になった理由や、同様の機能を実現するために開発者が代わりに使用する必要があるものについては説明していません。

私は何が欠けていますか?私が読んだ多くのことは、iOS5のNSURLConnectionに大きな変更が加えられたことを暗示しているようです。これらの変更はどこに文書化されていますか?デリゲートメソッドのドキュメントは完全ですか?

ありがとう

65
Mike Chen

ヘッダーファイルをめぐって、メソッドが非公式プロトコル(廃止されたObj-Cパターン)からNSURLConnection.hにあるNSURLConnectionDataDelegateと呼ばれる正式なデリゲートプロトコルに移動されたことがわかりますが、公開文書を持っています。

ドキュメントの残りの部分では、以前と同じ方法を使用し続けているため、これはドキュメントの省略です。つまりメソッド(ほとんど)はどこにも行きません。それらはいくつかのプロトコルに変更されただけで、ドキュメントチームは緩んでいます。デリゲートオブジェクトを適切なプロトコルに準拠させ、ヘッダーファイルの署名を使用してメソッドを実装してみてください。

47
millimoose

NSURLConnection.hの4.3から5.0への変更ログを見ると、ドキュメントは確かに混乱しています。

削除しました

Removed -[NSObject connection:canAuthenticateAgainstProtectionSpace:]
Removed -[NSObject connection:didCancelAuthenticationChallenge:]
Removed -[NSObject connection:didFailWithError:]
Removed -[NSObject connection:didReceiveAuthenticationChallenge:]
Removed -[NSObject connection:didReceiveData:]
Removed -[NSObject connection:didReceiveResponse:]
Removed -[NSObject connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite:]
Removed -[NSObject connection:needNewBodyStream:]
Removed -[NSObject connection:willCacheResponse:]
Removed -[NSObject connection:willSendRequest:redirectResponse:]
Removed -[NSObject connectionDidFinishLoading:]
Removed -[NSObject connectionShouldUseCredentialStorage:]
Removed NSObject(NSURLConnectionDelegate)

追加しました

Added -[NSURLConnection currentRequest]
Added -[NSURLConnection originalRequest]
Added +[NSURLConnection sendAsynchronousRequest:queue:completionHandler:]
Added -[NSURLConnection setDelegateQueue:]
Added NSURLConnectionDataDelegate
Added -[NSURLConnectionDataDelegate connection:didReceiveData:]
Added -[NSURLConnectionDataDelegate connection:didReceiveResponse:]
Added -[NSURLConnectionDataDelegate connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite:]
Added -[NSURLConnectionDataDelegate connection:needNewBodyStream:]
Added -[NSURLConnectionDataDelegate connection:willCacheResponse:]
Added -[NSURLConnectionDataDelegate connection:willSendRequest:redirectResponse:]
Added -[NSURLConnectionDataDelegate connectionDidFinishLoading:]
Added NSURLConnectionDelegate
Added -[NSURLConnectionDelegate connection:canAuthenticateAgainstProtectionSpace:]
Added -[NSURLConnectionDelegate connection:didCancelAuthenticationChallenge:]
Added -[NSURLConnectionDelegate connection:didFailWithError:]
Added -[NSURLConnectionDelegate connection:didReceiveAuthenticationChallenge:]
Added -[NSURLConnectionDelegate connection:willSendRequestForAuthenticationChallenge:]
Added -[NSURLConnectionDelegate connectionShouldUseCredentialStorage:]
Added NSURLConnectionDownloadDelegate
Added -[NSURLConnectionDownloadDelegate connection:didWriteData:totalBytesWritten:expectedTotalBytes:]
Added -[NSURLConnectionDownloadDelegate connectionDidFinishDownloading:destinationURL:]
Added -[NSURLConnectionDownloadDelegate connectionDidResumeDownloading:totalBytesWritten:expectedTotalBytes:]
Added NSURLConnection(NSURLConnectionQueuedLoading)

それで、それらの機能は確かにまだそこにあるようです。プロトコルを@interface宣言に追加するだけで準備完了です。

新しいNSURLConnectionDownloadDelegateを試しましたが、デリゲートにこれらのメソッドが存在する場合、NSURLConnectionDataDelegateメソッドは呼び出されないことに注意してください。

また、destinationURLを開くときに問題が発生しました。iOSは、ファイルが存在しないことを通知し続けましたが、ドキュメントには、メソッド呼び出し中にファイルが存在することが保証されていることが示されています。

24
ekscrypto

ドキュメントは@ $ @#$の混乱です。それが本当の話です。

書かれているNSURLConnectionのドキュメンテーションは、あなたを高く乾燥させます。

ドキュメントの最初の部分では、NSURLConnectionプロトコルのさまざまなメソッド(connection:didReceiveData:)着信データを処理します。概要でこれらの方法のいずれかをクリックすると、非推奨の方法のリストが表示されます!)

私がまとめた本当の話は、以前NSURLConnectionProtocolにあったメソッドのほとんどがNSURLConnectionDataProtocolと呼ばれる新しいプロトコルに移動されたことです。残念ながら、その新しいプロトコルは文書化されていないか、インデックスに登録されていないため、見つけることができません。これは同じことです。)

他の興味深いニュースでは、明らかにNSURLConnectionDownloadDelegateと呼ばれる新しいプロトコルがあります。 iOSのNSURLConnectionは、MacOSのNSURLDownloadで利用可能な機能の一部を追加しているように聞こえます。 NSURLConnectionDownloadDelegateプロトコルIS文書化。

7
Duncan C

NSURLConnectionからこれらのメソッドを非推奨にすると、何らかの種類の説明者が必要になると思うかもしれませんが、まだ説明していません。これまでにできる最善の方法は、Appleの RL Loading System Programming Guide からです。

URLのコンテンツをダウンロードするために、アプリケーションは、少なくとも次のデリゲートメソッドを実装するデリゲートオブジェクトを提供する必要があります:connection:didReceiveResponse:connection:didReceiveData:connection:didFailWithError:およびconnectionDidFinishLoading:

これは非公式のプロトコルを意味します。

0
Clay Bridges