2014-06-06 6 views
1

J'ai obtenu ce code pour implémenter quelque chose qui m'aide à télécharger un fichier à partir d'une URL donnée.iOS NSURLSession Télécharger

-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location 
{ 
    NSLog(@"Temporary File :%@\n", location); 
    NSError *err = nil; 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSString *docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 

    NSURL *docsDirURL = [NSURL fileURLWithPath:[docsDir stringByAppendingPathComponent:@"out1.zip"]]; 
    if ([fileManager moveItemAtURL:location 
          toURL:docsDirURL 
          error: &err]) 
    { 
     NSLog(@"File is saved to =%@",docsDir); 
    } 
    else 
    { 
     NSLog(@"failed to move: %@",[err userInfo]); 
    } 

} 

-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite 
{ 
    //You can get progress here 
    NSLog(@"Received: %lld bytes (Downloaded: %lld bytes) Expected: %lld bytes.\n", 
      bytesWritten, totalBytesWritten, totalBytesExpectedToWrite); 
} 

Deuxième partie:

-(void) downloadFileWithProgress 
{ 
    NSURL * url = [NSURL URLWithString:@"https://s3.amazonaws.com/hayageek/downloads/SimpleBackgroundFetch.zip"]; 
    NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration]; 
    NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate:self delegateQueue: [NSOperationQueue mainQueue]]; 

    NSURLSessionDownloadTask * downloadTask =[ defaultSession downloadTaskWithURL:url]; 
    [downloadTask resume]; 

} 

Tout ce code est dans mon Download.m

Mon download.h est:

@interface Download : NSObject 
-(void) downloadFileWithProgress 
@end 

Je ne sais pas vraiment comment Commencez le téléchargement. Dans une autre classe, j'ai créé un bouton qui devrait commencer le téléchargement:

-(IBAction)buttonStartDownload:(id)sender { 
[Download downloadFileWithProgress]; 
} 

L'erreur est dans la dernière ligne:

No known class method for selector 'downloadFileWithProgress' 

Mais pourquoi?

+0

Nous avons besoin de plus d'informations, avez-vous importé votre classe d'aide au téléchargement? Pourquoi avez-vous besoin de trois classes différentes? – mstottrop

+0

Ok, oui j'ai importé le "Download.h" dans le "DownloadViewController.m" N'est-il pas utile d'externaliser le code de téléchargement dans une seule classe et de s'y référer dans une classe downloadViewController? – Velocity

+0

Je pense qu'il est plus facile pour l'instant de tout avoir dans votre classe DownloadViewController, puisque vous ne pourriez externaliser qu'une seule fonction. – mstottrop

Répondre

1

La méthode '- (void) downloadFileWithProgress' est une méthode d'instance que vous ne pouvez pas appeler cette méthode en utilisant le nom de classe 'Download'.

Pour appeler cette méthode, vous devez créer une instance de la classe 'Download' et appeler la méthode sur cette instance.

1
Method -(void)downloadFilwWithProgress in instance method...So to call that method 

-(IBAction)buttonStartDownload:(id)sender { 
Download *downldObj=[[Download alloc]init]; 
[downldObj downloadFileWithProgress]; 
} 


If you write method +(void)downloadFilwWithProgress then you can call like this.[Download downloadFileWithProgress] 
Questions connexes