1
NSURLConnection
을 사용하여 동일한 데이터 (NSString
)를 서버에 보내고 이미지 또는 파일을 추가하여 콘텐트 유형의 값을 지정하고 싶습니다.NSURLConnection을 사용하여 파일 및 기타 매개 변수를 업로드하는 방법
인코딩
- (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];
}
이 데이터
- (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];
}
}
하지만 어떻게 파일과 간단한 데이터를 둘 다 혼합 할 수 있습니까? –
위의 @ Tamim.Z 코드 ** 데이터의 샘플 키 값 **은 단순 데이터 용이고 다른 하나는 파일입니다. – dianz