2016-04-28 9 views
0

Wenn ich den Download mit async Aufgabe mit Hilfe von afnetworking starten, downloadet es ordnungsgemäß, auch wenn ich zurück gehe und com den Download erneut nur auf dem Weg ist, aber wenn ich auch auf den gleichen Haltepunkt Download-Bildschirm kommt auf dem Bildschirm gehen wieder, aber es funktioniert nicht Aktion jeden Titel-Taste oder eine solche UIBeim Herunterladen von PDF async, wenn ich zurück und VC erneut initiieren, dann ändert es nicht UI dh Button title

Hier ist mein Code

- (void) downloadByURL:(NSString *)URL 
{ 

    [self.progress setHidden:NO]; 
    [self.btnsubscribe setUserInteractionEnabled:NO]; 

    NSURL *url = [NSURL URLWithString:URL]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 

    AFHTTPSessionManager *session = [AFHTTPSessionManager manager]; 
    NSURLSessionDownloadTask *tasks= 
    [session downloadTaskWithRequest:request progress:nil destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) { 
     // 
     NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
     NSString *path = [documentsDirectory stringByAppendingPathComponent:[[[[NSString stringWithFormat:@"%@",self.ILModel.IssueID] stringByAppendingString:self.ILModel.Month] stringByAppendingString:self.ILModel.Year] stringByAppendingString:@".pdf"]]; 
     return [NSURL URLWithString: path]; 
    } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nonnull filePath, NSError * _Nonnull error) { 


     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^(void) { 
      [[NSData dataWithContentsOfURL:url] writeToFile:[filePath absoluteString] atomically:YES]; 
      [self.progress setHidden:YES]; 
      [KSToastView ks_showToast:@"Download Complete" duration:2.0f]; 

      [self.btnsubscribe setBackgroundColor:[UIColor colorWithHexString:@"5BE4CA"]]; 
      [self.btnsubscribe setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 
      [self.btnsubscribe setTitle:@"View" forState:UIControlStateNormal]; 
      [self.btnsubscribe setUserInteractionEnabled:YES]; 
     }); 
    }]; 

    [session setDownloadTaskDidWriteDataBlock:^(NSURLSession *session, NSURLSessionDownloadTask *downloadTask, int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) { 

     int prog= (int)(totalBytesWritten * 100/totalBytesExpectedToWrite); 

     [self.progress setHidden:NO]; 
     [self.btnsubscribe setTitle:@"Downloading" forState:UIControlStateNormal]; 
     [KSToastView ks_showToast:[NSString stringWithFormat:@"Downloaded %lld %%",totalBytesWritten * 100/totalBytesExpectedToWrite] duration:1.0f]; 
     [self performSelectorOnMainThread:@selector(setLoaderProgress:) withObject:[NSNumber numberWithFloat:(float)prog/100] waitUntilDone:NO]; 

     NSLog(@"Progress… %lld",totalBytesWritten * 100/totalBytesExpectedToWrite); 
    }]; 

    [tasks resume]; 
} 
zum herunterladen ändern

Vielen Dank im Voraus ...

Antwort

0

Sie führen die UI-Aufgabe im Hintergrundthread aus.

es auf folgende Weise tun:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^(void) { 

      [[NSData dataWithContentsOfURL:url] writeToFile:[filePath absoluteString] atomically:YES]; 
      dispatch_async(dispatch_get_main_queue(), ^{ 
        [self.progress setHidden:YES]; 
        [KSToastView ks_showToast:@"Download Complete" duration:2.0f]; 
        [self.btnsubscribe setBackgroundColor:[UIColor colorWithHexString:@"5BE4CA"]]; 
        [self.btnsubscribe setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 
        [self.btnsubscribe setTitle:@"View" forState:UIControlStateNormal]; 
        [self.btnsubscribe setUserInteractionEnabled:YES]; 
      }); 
}); 
+0

ja, aber rede nicht über die Aufgabe Hintergrund-Thread ... –

+0

i diesen Teil nehme. [session setDownloadTaskDidWriteDataBlock:^(NSURLSession * Sitzung NSURLSessionDownloadTask * downloadTask, int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) { int prog = (int) (totalBytesWritten * 100/totalBytesExpectedToWrite); [self.progress setHidden: NEIN]; [self.btsubscribe setTitle: @ "Herunterladen" forState: UIControlStateNormal]; [KSToastView ks_showToast: [NSString stringWithFormat: @ "Heruntergeladene% lld %%", totalBytesWritten * 100/totalBytesExpectedToWrite] duration: 1.0f]; }]; –

+0

nur einen Moment lassen Sie mich versuchen –