![](/img/trans.png)
[英]How to cancel request sent by [NSURLConnection sendAsynchronousRequest:..]
[英]NSURLConnection sendAsynchronousRequest: not initiating request
我有一些看起來像應該嘗試發送異步請求的代碼,但是該請求從未生成:
這是函數:
-(void) setEmail: (NSString *) subject andBody: (NSString *) body
{
NSString *urlString = @"http://www.my_url.com?";
// Create encoded query string
NSString *query_string = @"subject=%@&body=%@";
NSString *query_with_args = [NSString stringWithFormat:query_string , subject , body];
// Now encode the query
// NSString *encodedQuery = (__bridge NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,
// (__bridge CFStringRef)query_with_args,
// NULL,
// (CFStringRef)@"!*'();:@&=+$,/?%#[]",
// kCFStringEncodingUTF8);
//
// Now concatinate url and query
NSString *final_url = [urlString stringByAppendingString:query_with_args];
NSURL *url = [NSURL URLWithString:final_url];
// Now send to the server
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
// TODO: ok I dont really understand what this is
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
NSLog(@"......before request");
[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
NSLog(@"On return");
NSLog(@"This is data: %@" , data);
NSLog(@"This is response: %@" , response);
NSLog(@"This is error: %@" , error);
NSLog(@"OK");
}];
}
知道這里可能出什么問題以及為什么從不發送請求嗎? 記錄器
NSLog(@“ ............在請求之前”); 確實被打印。
嘗試這樣。.您需要在其中實現NSURLConnectionDelegate
協議和方法。
NSString *final_url = [NSString stringWithFormat:@"http://www.my_url.com?subject=%@&body=%@",subject, body];
NSURL *url = [NSURL URLWithString:final_url];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if( theConnection )
{
webData = [NSMutableData data];
}
else
{
NSLog(@"theConnection is NULL");
}
}
希望這會對您有所幫助。.http ://www.cocoaintheshell.com/2011/04/nsurlconnection-synchronous-asynchronous/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.