Jak mogę przesłać plik i inny parametr za pomocą NSURLConnection
ja używamNSURLConnection
wysyłać te same dane (NSString
) na serwer i chciałbym dodać z nimi obraz lub plik, więc jaka jest wartość typu zawartości?
- (NSData *)encodingData:(NSMutableDictionary *)dictionary
{
NSMutableArray *arrayPosts = [[NSMutableArray alloc] init];
[dictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
NSString *encodedKey = [key stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *encodedValue = [obj stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[arrayPosts addObject:[NSString stringWithFormat:@"%@=%@",encodedKey,encodedValue]];
}];
NSString *encodedArrayPosts = [arrayPosts componentsJoinedByString:@"&"];
return [encodedArrayPosts dataUsingEncoding:NSUTF8StringEncoding];
}
Przesłać dane- (void)startAsyncRequest
{
// Enable the network activity indicator in the status bar
[self enableActivityIndicatorInStatusBar];
// Setting of the request
NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc] initWithURL:self.url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0];
[urlRequest setHTTPMethod:self.method];
[urlRequest setHTTPBody:[self encodingData:self.dictionaryPosts]];
// Send the request
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
if (connection) {
// Connection succeded
self.receiveData = [NSMutableData data];
} else {
// Connection Failed
self.error = @"Connection Failed";
// Inform the user that the connection failed
[self.delegate requestFailed:self];
}
}