我的应用程序在ios7上的请求中出现UTF8String崩溃,但在ios5上没有
使用解决方案更新: 问题是,服务器是dc,然后我无法连接到它。感谢@Hot Licks,我在日志中看到了错误,所以感谢大家我的应用程序在ios7上的请求中出现UTF8String崩溃,但在ios5上没有,ios,objective-c,Ios,Objective C,使用解决方案更新: 问题是,服务器是dc,然后我无法连接到它。感谢@Hot Licks,我在日志中看到了错误,所以感谢大家 更新:我尝试了热舔代码,然后我得到了以下日志: 2014-03-0416:00:16.445 finalAbogados[3166:70b]来自sendSynchronousRequest的错误:错误域=NSURlerErrorDomain代码=-1004“无法连接到服务器。”用户信息=0x8c82840{nserrorfailingurstringkey=localho
更新:我尝试了热舔代码,然后我得到了以下日志: 2014-03-04
16:00:16.445 finalAbogados[3166:70b]来自sendSynchronousRequest的错误:错误域=NSURlerErrorDomain代码=-1004“无法连接到服务器。”用户信息=0x8c82840{nserrorfailingurstringkey=localhost/scripts/logueoFinal.php,nserrorfailingurkey=localhost/scripts/logueoFinal.php,NSLocalizedDescription=无法连接到服务器,NSUnderlyingError=0x8ac9480“无法连接到服务器。”}–
我不知道怎么做,但是在iOS5上我可以实现连接,而现在在iOS7上我不能。代码是一样的。
可能是我必须使用NSURLSession而不是NSURLConnection
请帮忙
我用ios7将我的xcode更新为5,0,现在我的应用程序不工作了 代码使用POST方法发送请求,然后从服务器接收数据。如果数据正确,请转到其他视图控制器,否则显示警报 同样的代码也适用于ios5,但现在更新后应用程序崩溃:
-(IBAction)logClicked:(id)sender {
if ([user.text isEqualToString:@""] && [pass.text isEqualToString:@""]) {
UIAlertView *alert = [[[UIAlertView alloc] init] initWithTitle:@"Alerta." message:@"Escriba un usuario y contraseña." delegate:self cancelButtonTitle:@"Volver" otherButtonTitles:nil, nil];
[alert show];
}
else {
NSString *myRequestString = [NSString stringWithFormat:@"user=%@&pass=%@", user.text, pass.text];
NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString:@"http://localhost/scripts/logueoFinal.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[request setHTTPBody:myRequestData];
NSURLResponse *response;
NSError *error;
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *content = [NSString stringWithUTF8String:[returnData bytes]];
if ([content isEqualToString:@"1"]) {
UIAlertView *alert = [[[UIAlertView alloc] init] initWithTitle:@"Bienvenido." message:@"El usuario y contraseña es correcto." delegate:self cancelButtonTitle:@"Ingresar" otherButtonTitles:nil, nil];
[alert show];
asuntosViewController = [[AsuntosViewController alloc] initWithNibName:@"AsuntosViewController" bundle:nil];
asuntosViewController.ureceived = user.text;
asuntosViewController.preceived = pass.text;
[self.view addSubview:asuntosViewController.view];
}
else {
UIAlertView *alert = [[[UIAlertView alloc] init] initWithTitle:@"Error." message:@"El usuario y contraseña no es correcto." delegate:self cancelButtonTitle:@"Volver" otherButtonTitles:nil, nil];
[alert show];
}
}
}
这是应用程序崩溃时的日志:
2014-03-04 13:09:46.535 toiOS7[2707:70b] Could not load the "17905.png" image referenced from a nib in the bundle with identifier "com.microarea.toiOS7"
2014-03-04 13:09:59.995 toiOS7[2707:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSString stringWithUTF8String:]: NULL cString'
*** First throw call stack:
(
0 CoreFoundation 0x0173b5e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x014be8b6 objc_exception_throw + 44
2 CoreFoundation 0x0173b3bb +[NSException raise:format:] + 139
3 Foundation 0x010d7662 +[NSString stringWithUTF8String:] + 90
4 toiOS7 0x00002d2e -[ViewController logClicked:] + 1342
5 libobjc.A.dylib 0x014d0874 -[NSObject performSelector:withObject:withObject:] + 77
6 UIKit 0x0022e0c2 -[UIApplication sendAction:to:from:forEvent:] + 108
7 UIKit 0x0022e04e -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
8 UIKit 0x003260c1 -[UIControl sendAction:to:forEvent:] + 66
9 UIKit 0x00326484 -[UIControl _sendActionsForEvents:withEvent:] + 577
10 UIKit 0x00325733 -[UIControl touchesEnded:withEvent:] + 641
11 UIKit 0x0026b51d -[UIWindow _sendTouchesForEvent:] + 852
12 UIKit 0x0026c184 -[UIWindow sendEvent:] + 1232
13 UIKit 0x0023fe86 -[UIApplication sendEvent:] + 242
14 UIKit 0x0022a18f _UIApplicationHandleEventQueue + 11421
15 CoreFoundation 0x016c483f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
16 CoreFoundation 0x016c41cb __CFRunLoopDoSources0 + 235
17 CoreFoundation 0x016e129e __CFRunLoopRun + 910
18 CoreFoundation 0x016e0ac3 CFRunLoopRunSpecific + 467
19 CoreFoundation 0x016e08db CFRunLoopRunInMode + 123
20 GraphicsServices 0x036e09e2 GSEventRunModal + 192
21 GraphicsServices 0x036e0809 GSEventRun + 104
22 UIKit 0x0022cd3b UIApplicationMain + 1225
23 toiOS7 0x00003add main + 141
24 libdyld.dylib 0x01d79701 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
我什么都不懂,我在我的应用程序上花了这么多时间,现在不工作了,请帮助
感谢您的建议。您遗漏了一个重要步骤:
NSURLResponse *response;
NSError *error;
NSString* content = nil;
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (returnData) {
content = [NSString stringWithUTF8String:[returnData bytes]];
}
else {
NSLog(@"Error from sendSynchronousRequest: %@", error);
}
有很多页面与你的错误有关。用谷歌搜索它。你是否记录了
returnData
?我是ios上的新手,我不知道要在谷歌搜索什么,因为日志中有很多错误。你能给我发一个解决方案的链接吗?thanks@HimanshuJoshi现在我记录了returnData变量,它是空的。为什么???在iOS5上总是包含一个值。他可以吗请告诉我?我对请求做了什么错误吗?那么,当你记录它时,error
会说什么(你应该这么做,因为你从sendSynchronousRequest
)得到了一个nil值)?(error
parm不只是为了占用空间!)我试过你的代码,日志打印:2014-03-04 16:00:16.445 finalAbogados[3166:70b]来自sendSynchronousRequest的错误:错误域=NSURErrorDomain代码=-1004“无法连接到服务器。”用户信息=0x8c82840{NSERRORFAILINGURSTRINGKEY=,NSERRORFAILINGURKEY=,NSLocalizedDescription=无法连接到服务器,NSUnderlyingError=0x8ac9480“无法连接到服务器。”}