J'essaie de télécharger un fichier HTML à partir d'Internet et de le stocker dans mon application pour un visionnage ultérieur (hors ligne). J'utilise la méthode suivante pour télécharger le fichier, mais mon rapport NSLogs qu'aucune donnée n'a été téléchargée. Quel est le problème ici? (On suppose une connexion Internet active, prendre un chemin prédéfini dans le répertoire des documents locaux)Comment télécharger un fichier .html pour une utilisation locale?
urlAddress = @"http://subdomain.somesite.com/profile.html";
// Create and escape the URL for the fetch
NSString *URLString = [NSString stringWithFormat:@"%@%@", @"ttp://subdomain.somesite.com/profile.html"];
NSURL *URL = [NSURL URLWithString:
[URLString stringByAddingPercentEscapesUsingEncoding:
NSASCIIStringEncoding]];
// Do the fetch - blocks!
NSData *theData = [NSData dataWithContentsOfURL:URL];
if(theData == nil) {
NSLog(@"NO DATA DOWNLOADED");
}
// Do the write
NSString *filePath = [[self documentsDirectory] stringByAppendingPathComponent:@"Profile/profile.html"];
[theData writeToFile:filePath atomically:YES];
NSLog(@"WRITING profile.html to path %@!", filePath);
Ceci est incorrect si vous avez des caractères dans votre section de paramètres de l'URL. Vous devrez appeler stringByAddingPercentEscapesUsingEncoding pour cette partie de l'URL, mais pas le préfixe http: //. –
Aucune idée si cela a changé depuis votre publication, mais vous devez ajouter un objet NSURL à cette méthode. Comme ceci: 'NSData * data = [NSData dataWithContentsOfURL: [NSURL URLWithString: @" http://www.google.com/index.html "]]' – polyclick