我为自己的网站创建应用并遇到登录问题。我有登录和传递字段的登录页面和“登录”按钮。
我尝试写入帖子请求(您可以在下面看到)进行登录,但它不起作用。
请给我一些解决方案。
我已经在Obj-C编写了几个星期的代码,现在我没有很多东西。
NSString *login = loginField.text;
NSString *password = passField.text;
NSURL *theURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://mysite",login, password]]; //Here you place your URL Link
NSURLRequest *req = [NSURLRequest requestWithURL:theURL];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:req delegate:self];
if (connection) {
NSLog(@"connection successful");
}
else {
NSLog(@"Failed");
}
答案 0 :(得分:1)
NSURLRequest HTTPMethod默认为“GET”,您需要在代码中将其设置为“POST”。
我的示例显示了NSMutableURLRequest
,因为我已在应用程序中测试了此代码。如果您愿意,您应该可以使用NSURLRequest
执行相同操作。
NSMutableURLRequest *req = [[NSMutableURLRequest alloc] init];
[req setURL:[NSURL URLWithString:theUrl]];
[req setHTTPMethod:@"POST"];
希望这有帮助。