このような方法があります。デバイスが Wi-Fi ネットワーク経由で接続されている場合は問題なく動作しますが、3G ネットワーク経由で接続されている場合はアプリが数秒間フリーズします。したがって、インタラクティブなアプリであるため、さまざまな投稿リクエストを行うときは、ユーザーがアプリを使い続けることができるように、実行を継続する必要があります。これに対する解決策はありますか?
[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
}