(neu getesteten Code bearbeitet ursprüngliche Antwort enthalten) zu verwenden
Ich wollte nur meine Version dieser Anfrage einreichen. Ich habe ein Wörterbuch verwendet, um die Post-Parameter darzustellen.
Es ist ein Stück Code, ist aber einfach genug, um in eine Ansicht mit einem Webview zu fallen und für alle URL-Laden zu verwenden. Es wird nur POST, wenn Sie ein "postDictionary" senden. Andernfalls wird die URL, an die Sie sie gesendet haben, verwendet, um einfach Dinge zu holen.
- (void) loadWebView:(UIWebView *)theWebView withURLString:(NSString *)urlString andPostDictionaryOrNil:(NSDictionary *)postDictionary {
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:60.0];
// DATA TO POST
if(postDictionary) {
NSString *postString = [self getFormDataString:postDictionary];
NSData *postData = [postString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
}
[theWebView loadRequest:request];
}
- (NSString *)getFormDataString:(NSDictionary*)dictionary {
if(! dictionary) {
return nil;
}
NSArray* keys = [dictionary allKeys];
NSMutableString* resultString = [[NSMutableString alloc] init];
for (int i = 0; i < [keys count]; i++) {
NSString *key = [NSString stringWithFormat:@"%@", [keys objectAtIndex: i]];
NSString *value = [NSString stringWithFormat:@"%@", [dictionary valueForKey: [keys objectAtIndex: i]]];
NSString *encodedKey = [self escapeString:key];
NSString *encodedValue = [self escapeString:value];
NSString *kvPair = [NSString stringWithFormat:@"%@=%@", encodedKey, encodedValue];
if(i > 0) {
[resultString appendString:@"&"];
}
[resultString appendString:kvPair];
}
return resultString;
}
- (NSString *)escapeString:(NSString *)string {
if(string == nil || [string isEqualToString:@""]) {
return @"";
}
NSString *outString = [NSString stringWithString:string];
outString = [outString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// BUG IN stringByAddingPercentEscapesUsingEncoding
// WE NEED TO DO several OURSELVES
outString = [self replace:outString lookFor:@"&" replaceWith:@"%26"];
outString = [self replace:outString lookFor:@"?" replaceWith:@"%3F"];
outString = [self replace:outString lookFor:@"=" replaceWith:@"%3D"];
outString = [self replace:outString lookFor:@"+" replaceWith:@"%2B"];
outString = [self replace:outString lookFor:@";" replaceWith:@"%3B"];
return outString;
}
- (NSString *)replace:(NSString *)originalString lookFor:(NSString *)find replaceWith:(NSString *)replaceWith {
if (! originalString || ! find) {
return originalString;
}
if(! replaceWith) {
replaceWith = @"";
}
NSMutableString *mstring = [NSMutableString stringWithString:originalString];
NSRange wholeShebang = NSMakeRange(0, [originalString length]);
[mstring replaceOccurrencesOfString: find
withString: replaceWith
options: 0
range: wholeShebang];
return [NSString stringWithString: mstring];
}
Das ist besser. –
Dies ist, was ich versucht habe (siehe den Link in der OP) und ich habe versucht, die ConnectionDidFinishLoading-Methode, um etwas wie [UiWebViewThing loadRequest: responseData] tun, aber es ist die falsche Art von Daten. Die Antwortdaten sind NSMutable, aber es muss NSUrlRequest sein - ich muss etwas offensichtliches falsch machen. – Gazzer
Der obige Code verwendet eine NSURLRequest. Sie müssen ein NSMutableURLRequest verwenden. Verwechseln Sie nicht mit connectionDidFinishLoading, laden Sie es einfach mit -loadRequest in das WebView. –