我正在尝试在 iOS 应用程序中创建一个身份验证系统,该系统允许用户登录并在他们还没有帐户的情况下进行注册。我昨天完全启动并运行了登录系统,但是当我为注册系统设置代码时,代码甚至无法 ping 服务器。然后我再次尝试测试登录系统,代码现在也不会ping服务器。
RegistrationTableViewController 的相关代码(它是一个自定义 TVC,在某些单元格中包含文本字段 - 例如,考虑创建新日历事件的视图):
- (IBAction)signUpButtonPressed {
// Get the values out of the text fields that the user has filled out.
NSString *email = self.emailTextField.text;
NSString *firstName = self.firstNameTextField.text;
NSString *lastName = self.lastNameTextField.text;
NSString *password = self.passwordTextField.text;
// Assuming that sign-up could potentially take a noticeable amount of time, run the
// process on a separate thread to avoid locking the UI.
dispatch_queue_t signUpQueue = dispatch_queue_create("sign-up authenticator", NULL);
dispatch_async(signUpQueue, ^{
// self.brain refers to a SignUpBrain property. See the code for the class below.
[self.brain signUpUsingEmail:email firstName:firstName lastName:lastName
andPassword:password];
dispatch_async(dispatch_get_main_queue(), ^{
[self performSegueWithIdentifier:@"ShowMainFromSignUp" sender:self];
});
});
dispatch_release(signUpQueue);
}
SignUpBrain 的相关代码:
- (void)signUpUsingEmail:(NSString *)email firstName:(NSString *)firstName
lastName:(NSString *)lastName andPassword:(NSString *)password {
self.email = email;
self.firstName = firstName;
self.lastName = lastName;
self.password = password;
// Handle sign-up web calls.
NSMutableURLRequest *signUpRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL
URLWithString:@"URL GOES HERE"]]; // obviously there's an actual URL in real code
NSString *postString = [NSString stringWithFormat:@"uname=%@&pw=%@&fname=%@&lname=%@",
email, password, firstName, lastName];
//NSLog(postString);
[signUpRequest setHTTPMethod:@"POST"];
[signUpRequest setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *signUpConnection =
[NSURLConnection connectionWithRequest:signUpRequest delegate:self];
[signUpConnection start];
// Store any user data.
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
self.signUpResponse = data;
NSError *error;
NSDictionary *jsonLoginResults = [NSJSONSerialization JSONObjectWithData:data
options:0 error:&error];
if (error) {
NSLog(error.description);
}
NSLog(jsonLoginResults.description);
// Return whether the user has successfully been registered.
// If success is 1, then registration has been completed successfully. 0 if not.
if ([jsonLoginResults objectForKey:@"status"]) {
NSLog(@"Success!");
}
}
我还将注意到我创建了一个在 UIWebView 中使用这些相同 Web 调用的测试,并且它可以成功运行。
如果我需要澄清任何内容或包含更多代码,请告诉我!提前致谢。