AsyncSound.m 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. //
  2. // AsyncSound.m
  3. // IBOSS
  4. //
  5. // Created by guan hong hou on 17/7/24.
  6. // Copyright © 2017年 沈阳东科云信软件有限公司. All rights reserved.
  7. //
  8. #import "AsyncSound.h"
  9. @implementation AsyncSound
  10. @synthesize response = _response;
  11. - (void)asyncLoad :(NSString *)urlString localPath:(NSString*) localFilePath{
  12. urlStr=urlString;
  13. localPath=localFilePath;
  14. fileManager = [NSFileManager defaultManager];
  15. if ([fileManager fileExistsAtPath:localPath]) {
  16. return;
  17. }
  18. NSURL *url = [NSURL URLWithString:urlStr];
  19. NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
  20. [NSURLConnection connectionWithRequest:urlRequest delegate:self];
  21. }
  22. #pragma mark - NSURLConnectionDataDelegate
  23. - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
  24. receiveData = [[NSMutableData alloc] init];
  25. allLength = [response expectedContentLength];
  26. self.response = response;
  27. }
  28. - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
  29. [receiveData appendData:data];
  30. }
  31. - (void)connectionDidFinishLoading:(NSURLConnection *)connection{
  32. [receiveData writeToFile:localPath atomically:YES];
  33. }
  34. #pragma mark - NSURLConnectionDelegate
  35. - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
  36. //网络连接失败,关闭菊花
  37. [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
  38. if (error) {
  39. NSLog(@"%@",[error localizedDescription]);
  40. }
  41. }
  42. @end