Ich bin ein Web-Service, der eine WAV-Datei zurückgeben sollte. Ich erfasse das Ergebnis in einem NSData-Objekt und speichere es im lokalen Dokumentendirektor.NSData Dateityp Validierung
Wie überprüfe ich, dass die Daten in der Tat ein WAV ist, bevor Sie es speichern? Wenn auf dem Server ein Fehler aufgetreten ist, kann eine lange Zeichenfolge von HTML-Fehlern zurückgegeben werden.
Danke!
NSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL:RequestURL
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:30.0];
NSHTTPURLResponse* response = nil;
NSData *soundData = [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&error];
//save voicemail file locally
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *destPath = [documentsDirectory stringByAppendingPathComponent:@"returned.wav"];
[fileManager createFileAtPath:destPath contents:soundData attributes:nil];
+1 jeder Fehler Antwort von einem Server sollte eine Antwort im Bereich von 400 oder 500. – JeremyP
Da die Anforderung vom Standpunkt des Servers aus "erfolgreich" sein kann, ist die Überprüfung des MIME-Typs der ideale Weg. Vielen Dank! – mojo
Abschließender Code: NSHTTPURLResponse * response = nil; \t NSData * soundData = [NSURLConnection sendSynchronousRequest: req returningResponse: & Antwortfehler: & error]; // Voicemail-Datei lokal speichern NSDictionary * headers = [Antwort allHeaderFields]; NSString * contentType = (NSString *) [Kopfzeilen objectForKey: @ "Content-Type"]; if ([contentType ist gleich: @ "audio/x-wav"]) \t NSLog (@ "es ist eine WAV-Datei!"); sonst \t NSLog (@ "unerwarteter Antworttyp:% @", contentType); – mojo