2016-07-07 2 views
3

내 응용 프로그램에서 서버에서 오디오 파일을 다운로드하고 응용 프로그램이 포 그라운드에있을 때 파일을 다운로드하고 홈 버튼이나 잠금 버튼을 클릭하여 응용 프로그램을 백그라운드로 강제 실행 한 후 잠시 후 다운로드가 중지되고 오류는 1005 network connection lost과 같이 나타납니다. 뭐가 문제 야? 아무도이 문제를 설명 할 수 있습니까?백그라운드 모드에서 파일을 다운로드하는 방법 iOS? 네트워크 연결이 끊어졌습니다.

코드 :

 NSURL *url = [NSURL URLWithString:currentURL]; 
      NSURLRequest *theRequest = [NSURLRequest requestWithURL:url   cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60]; 
      receivedData = [[NSMutableData alloc] initWithLength:0]; 
      NSURLConnection * connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self  startImmediately:YES]; 
      myConnection = connection;    
      NSLog(@"%@ Download Started", currentURL); 


- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; 
    [receivedData setLength:0]; 
    expectedBytes = [response expectedContentLength]; 
} 

- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
    [receivedData appendData:data]; 
    float progressive = (float)[receivedData length]/(float)expectedBytes; 
    [downloadProgressView setProgress:progressive]; 
    NSInteger val = progressive*100; 
    downloadpercentageLabel.text = [NSString stringWithFormat:@"%ld%@",(long)val,@"%"];  
    //[UIApplication sharedApplication].idleTimerDisabled = YES;  
} 

- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 
} 
+1

백그라운드 모드에서 백그라운드 가져 오기를 확인 하시겠습니까? –

+1

이것은 중복되었을 수 있습니다. 이 질문을 확인한 적이 있습니까? http://stackoverflow.com/questions/8861390/ios-background-downloads-when-the-app-is-not-active? –

+0

@ MehmetEfeAkça 여러 답변을 시도했지만 해결하지 못했습니다. 그리고 대답을 찾을 수 없습니다.^{ [: 나는 배경이 프로젝트를 선택 –

답변

5

사용 배경 NSURLSession. 3 분을 초과하는 네트워크 중단 및 다운로드를 처리합니다. Downloading Content in the Background 섹션 () iOS 용 앱 프로그래밍 가이드. 배경 다운로드를 설명합니다. What’s New in Foundation Networking의 WWDC 2013 비디오를 참조하십시오 (비디오 뒷부분에서 다룹니다).

+0

고마워 롭, 하나의 URL을 다운로드 잘 작동합니다, 두 번째 URL을 그것의 작동하지 않는 –

+2

다운로드 실패하면 여러 다운로드도 잘 작동합니다. 동일한 세션 객체를 사용해야합니다 (또는 새로운 식별자로 새 인스턴스를 인스턴스화하는 것). – Rob

관련 문제