今天在做iphone上传文件到服务器的过程中,突然出现了莫名其妙的Mach_msg_trap错误,百度,google得到的解决方案都不能解决我的问题。
代码如下:
-(BOOL)Connection
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSData *temp = [[NSData alloc]initWithContentsOfFile:@"/users/henry/2.jpeg"];
NSURL *myWebserverURL = [NSURL URLWithString:@"https://IPAddress/wap_test/soa/ReceiveData.aspx"];
[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[myWebserverURL host]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myWebserverURL];
// Add the Required WCF Header Values. This is what the WCF service expects in the header.
[request setTimeoutInterval: 10.0];
[request setCachePolicy: NSURLRequestUseProtocolCachePolicy];
// Set the action to Post
[request setHTTPMethod:@"POST"];
// Set the body
[request setHTTPBody:temp];
// Make this class the delegate so that the other connection events fire here.
// [NSURLConnection connectionWithRequest:request delegate:self];
NSError *WSerror;
NSURLResponse *WSresponse;
NSData *tempData = [NSURLConnection sendSynchronousRequest:request returningResponse:&WSresponse error:&WSerror];
NSString *str = [[NSString alloc]initWithData:tempData encoding:NSUTF8StringEncoding];
NSLog(@"return:%@",str);
[conn release];
[temp release];
[request release];
[str release];
return TRUE;
[pool release];
}
运行一切正常,服务器也能正常返回数据,但接下来就会crash。
最后终于得到原因,缺少如下语句:
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
// Check the connection object
if(!conn)
{
return FALSE;
}