0
在我的IOS应用程序中,我有一个登录方法,它向java服务器发送用户名和密码。 问题是,如果服务器处于脱机状态,应用程序将冻结。当服务器重新启动时,应用程序将继续。 那么是否有异常处理,我应该为应用程序发送数据前检查服务器是否在线?如何检查服务器是否可以访问并运行?
- (void)initNetworkCommunication {
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"XXXXX", 678, &readStream,
&writeStream);
inputStream = (__bridge NSInputStream *)readStream;
outputStream = (__bridge NSOutputStream *)writeStream;
}
-(void) connection{
[self initNetworkCommunication];
[inputStream setDelegate:self];
[outputStream setDelegate:self];
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream open];
[outputStream open];
}
////// thats how I'm sending the username and password
NSString *username= [NSString stringWithFormat:@"%@\n",UsernameTextField.text];
NSString *password=[NSString stringWithFormat:@"%@\n",PasswordTextField.text];
NSLog(@"%@ %@",username,password);
NSData *usernamedata = [[NSData alloc] initWithData:[username dataUsingEncoding:NSASCIIStringEncoding]];
NSData *passworddata = [[NSData alloc] initWithData:[password dataUsingEncoding:NSASCIIStringEncoding]];
[outputStream write:[usernamedata bytes] maxLength:[usernamedata length]];
[outputStream write:[passworddata bytes] maxLength:[passworddata length]];