インターネットからiOSのNSDataにデータをダウンロードしようとしています。
インターネットからデータをダウンロードすると、サーバーからダウンロードされた割合がわかりません。
UIWebViewを使用していません。
.mp3
NSDataを使用してインターネットからサウンド()をダウンロードします。
とにかくインターネットからダウンロードされたデータの量を知ることができますか?
前もって感謝します。
手順:
1).mp3のURLへのリクエストを使用してNSURLConnectionを作成します。
2)self
この接続のデリゲートとして設定します。
3)NSURLConnectionDataDelegateプロトコルを実装します。(クラスのインターフェース宣言の横に追加します。
4)次のメソッドを実装します。
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
statusCode = [httpResponse statusCode];
if ((statusCode/100) == 2)
{
contentLength = [httpResponse expectedContentLength];
if (contentLength == NSURLResponseUnknownLength)
NSLog(@"unknown content length %ld", contentLength);
}
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
bytesSum += [data length];
percent = (float)bytesSum / (float)contentLength;
// append the new data to the receivedData
[receivedData appendData:data]; //received data is a NSMutableData ivar.
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//the end. Write your data ( stored in receivedData ) to a local .mp3 file.
}
お役に立てれば。
乾杯!