用户登录AFNetworking [英] User Login With AFNetworking

查看:77
本文介绍了用户登录AFNetworking的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建我的第一个iOS应用程序。

I am building my first iOS app.

我已经完成了后端代码,但我正在努力使用它的Objective-C部分。

I have got the backend code done, but I am struggling with the Objective-C part of it.

我有一个注册/登录页面。

I have a signup / login page.

但我不知道如何使用Objective C将数据发送到我的服务器。

But I don't know how to send that data to my server using Objective C.

我有读到AFNetworking很好,但我想知道如何使用它进行用户登录。

I have read that AFNetworking is good, but I was wondering how I could use that for user login .

我已经下载了AFNetworking并将其添加到我的XCode项目并设置了标题。 / p>

I have downloaded and added AFNetworking to my XCode Project and set up headers.

AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://examplewebsite.com]];

[client setDefaultHeader:@"key" value:@"value"];
[client setAuthorizationHeaderWithUsername:@"username" password:@"password"];
[client setAuthorizationHeaderWithToken:@"token"];

NSURLRequest *request = [client requestWithMethod:@"someMethod" path:@"somePath" parameters:nil];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

但我仍然输了。

推荐答案

因为你正试图登录你的自己的API,你不需要setAuthorization的东西。这是基本的HTTP身份验证。相反,您想使用 getPath:parameters:success:failure 或postPath版本,具体取决于您的后端是否需要HTTP GET或HTTP POST。

Since you're trying to login to your own API, you don't want setAuthorization stuff. That's for basic HTTP auth. Instead you want to use getPath:parameters:success:failure or the postPath version, depending on if your backend is expecting HTTP GET or HTTP POST.

在参数参数中传递您的用户名/密码。您应该将 parameterEncoding 设置为正确的格式。您可能正在使用HTTP Forms URL编码或JSON。无论你的后端是什么预期。

Pass your userid / password in the parameters argument. You should set parameterEncoding to be the correct format. You're probably using HTTP Forms url encoding, or JSON. Whatever your backend expects.

这篇关于用户登录AFNetworking的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆