更新解决方案:
问题是,服务器是直流,然后我无法连接到它。感谢@Hot Licks,我在日志中看到了错误,感谢大家。
更新:我尝试了Hot Licks代码然后我得到了这个日志:
2014-03-04 16:00:16.445 finalAbogados[3166:70b] Error from sendSynchronousRequest: Error Domain=NSURLErrorDomain Code=-1004 "Could not connect to the server." UserInfo=0x8c82840 {NSErrorFailingURLStringKey=localhost/scripts/logueoFinal.php, NSErrorFailingURLKey=localhost/scripts/logueoFinal.php, NSLocalizedDescription=Could not connect to the server., NSUnderlyingError=0x8ac9480 "Could not connect to the server."} –
我不知道如何,但在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
我什么都不懂,我在我的应用程序上工作了很多时间,现在无法工作,请帮忙。
感谢您的建议。
答案 0 :(得分:0)
你遗漏了一个重要的步骤:
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);
}