나는 이와 같은 방법이있다. 내 장치가 wifi 네트워크를 통해 연결되어 있으면 잘 작동하지만 3G 네트워크를 통해 연결되면 잠시 동안 내 응용 프로그램이 정지됩니다. 대화식 앱이기 때문에 여러 가지 게시물 요청을 할 때 계속해서 실행해야하므로 사용자는 앱을 계속 사용할 수 있습니다. 이것에 대한 해결책은 무엇입니까?NSURLRequest로 POST 요청을 할 때 왜 앱이 멈 춥니 까?
[theRequest setTimeoutInterval : 2.0]을 줄이려고 시도했습니다. 하지만 그건 내 문제를 해결하지 못했습니다.
// post request
- (void)postRequestWithURL:(NSString *)url
body:(NSString *)body
contentType:(NSString *)contentType
options:(NSDictionary *)dict
{
// set request
NSURL *requestURL = [NSURL URLWithString:url];
NSMutableURLRequest *theRequest = [[NSMutableURLRequest alloc] init];
if([dict count] > 0)
{
for (id key in dict) {
NSLog(@"[theRequest addValue:%@ forHTTPHeaderField:%@]", [dict valueForKey:key], key);
[theRequest addValue:[dict valueForKey:key] forHTTPHeaderField:key];
}
}
if (contentType != nil) {
[theRequest addValue:contentType forHTTPHeaderField:@"Content-type"];
}
[theRequest setURL:requestURL];
[theRequest setTimeoutInterval:2.0];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody:[body dataUsingEncoding:NSASCIIStringEncoding]];
[self.oauthAuthentication authorizeRequest:theRequest];
// make request
//responseData = [NSURLConnection sendSynchronousRequest:theRequest
// returningResponse:&response
// error:&error];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES];
self.web = conn;
[conn release];
NSLog(@"########## REQUEST URL: %@", url);
// request and response sending and returning objects
}
답변 해 주셔서 감사합니다! 그래서 내 NSURLConnection * conn = [NSURLConnection alloc] initWithRequest : theRequest 대리자를 편집해야합니다 : self startImmediately : YES]; ?? 나는 초고속 중매에서 그걸 할 수 없어? –
제 질문을 편집했습니다 -'connectionWithRequest : delegate :'를 호출하면 백그라운드에서 자동으로 시작됩니다. performSelectorInBackground : 또는 그와 비슷한 것을 생각할 필요가 없습니다. – deanWombourne