Php XCode、身份验证和NSUserDefault
我正在开发一个应用程序,它将使用web视图与web服务器(带有MySQL后端的PHP)进行接口。我让应用程序使用静态URL作为测试,但现在我需要根据实际用户交互进行调整 我一直在寻找HTTP身份验证的解决方案,但相信这个短语是不正确的,因为我得到的大多数搜索结果都与“REST”请求有关。我将使用一个URL(PHP/MySQL)来验证身份验证,根据我的经验,我认为这不符合REST 到目前为止,我在这台服务器上还没有SSL,因此我不确定通过Cocoa可以提供哪些选项 我的问题是:Php XCode、身份验证和NSUserDefault,php,mysql,xcode,authentication,Php,Mysql,Xcode,Authentication,我正在开发一个应用程序,它将使用web视图与web服务器(带有MySQL后端的PHP)进行接口。我让应用程序使用静态URL作为测试,但现在我需要根据实际用户交互进行调整 我一直在寻找HTTP身份验证的解决方案,但相信这个短语是不正确的,因为我得到的大多数搜索结果都与“REST”请求有关。我将使用一个URL(PHP/MySQL)来验证身份验证,根据我的经验,我认为这不符合REST 到目前为止,我在这台服务器上还没有SSL,因此我不确定通过Cocoa可以提供哪些选项 我的问题是: 如果帐户ID尚未存
银虎你的思维非常接近。我在用户登录过程中使用了非常类似的方法 目前我将邮件和密码添加到140个字符的令牌中。如果登录返回成功,我会将令牌保存到NSUserDefaults。我将此令牌用于所有其他web请求,直到用户注销为止,此时我将删除用户默认值 以下是我在同一过程中使用的几个片段:
// see if a login already exists
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
self.token = [defaults stringForKey:@"token"];
// if the token is nil/blank, launch login view
if(self.token == nil || [self.token isEqualToString:@""]) {
[self loadStartView];
return;
}
// build the request to update status
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyNever];
NSString *stringData = [NSString stringWithFormat:@"<your api string here"];
NSString *requestData = stringData;
NSData *myRequestData = [NSData dataWithBytes: [requestData UTF8String] length: [requestData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString:[NSString stringWithFormat:@"<your url request here>"]]];
[request setHTTPMethod: @"POST"];
[request setHTTPBody: myRequestData];
NSData *jsonData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];
NSString *json = [[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding] autorelease];
NSDictionary *payloadData = [json JSONValue];
[request release];
if([[payloadData objectForKey:@"success"] boolValue]) { // this is designed around my api, but you get the idea
//NSLog(@"updateStatus: %@", payloadData);
// updates the api version for every call
[defaults setObject:self.token forKey:@"token"];
}
//查看登录名是否已存在
NSUserDefaults*默认值=[NSUserDefaults standardUserDefaults];
self.token=[默认值stringForKey:@“token”];
//如果令牌为零/空白,则启动登录视图
if(self.token==nil | |[self.token isEqualToString:@”“){
[自动加载StartView];
返回;
}
//生成更新状态的请求
[[NSHTPCookieStorage sharedHTPCookieStorage]设置CookieAcceptPolicy:NSHTPCookieAptPolicyNever];
NSString*stringData=[NSString stringWithFormat:@“我没有足够的空间在笔记中发表评论。这是最后的代码。虽然不是我想要的,但它完成了任务:
为了让JSONValue发挥作用,我必须下载json框架并将其包含在我的项目中,如下所示:
一旦设置好,我就从JSONValue函数中得到了一个不完整的错误。我对PHP页面请求的原始URL请求提供了JSON响应,如下所示:
{
“成功”:“0”,
“帐户ID:”4“,
“错误”:”
}
出于某种原因,JSONValue函数不喜欢此输出,我不得不将其更改为更基本、更不受欢迎的版本,如下所示:
[“0”、“4”、“1”]
我完成这项工作后,代码的最终副本如下:
在应用程序启动时加载的主视图中,我添加了以下内容,以确定是否存在现有帐户ID,如果不存在,则启动“登录”视图:
- (void)viewDidLoad
{
NSString *accountID = [[NSUserDefaults standardUserDefaults] stringForKey:@"AccountID"];
if (![accountID length]) {
UIViewController *login = [self.storyboard instantiateViewControllerWithIdentifier:@"Login"];
[self presentModalViewController:login animated:YES];
}
[super viewDidLoad];
}
登录只不过是两个文本字段和一个用于身份验证的按钮。
这是我添加到按钮中的功能,目前可以使用:
-(IBAction)authenticate:(id)sender{
// This will be the validation code
// First we'll grab the field values
NSString *currentEmail = emailField.text;
NSString *currentPassword = passField.text;
// Now we will grab the user default settings (saved preferences)
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
// build the request to update status
NSString *myURL = [NSString stringWithFormat:@"http://www.myurl.com/auth.php?e=%@&p=%@",currentEmail,currentPassword];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; [request setURL:[NSURL URLWithString:myURL]];
[request setHTTPMethod:@"GET"];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *responseString = [[NSString alloc] initWithData:returnData encoding: NSUTF8StringEncoding];
NSArray *responseArray = [responseString JSONValue];
NSString *success = [responseArray objectAtIndex:0];
NSString *AccountID = [responseArray objectAtIndex:1];
NSString *error = [responseArray objectAtIndex:2];
if([success boolValue]) {
NSLog(@"User is Authenticated");
UIAlertView *auth1 = [[UIAlertView alloc] initWithTitle:@"T-of-U" message:@"Successfully Authenticated" delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil];
[auth1 show];
[defaults setObject:AccountID forKey:@"AccountID"];
[self dismissModalViewControllerAnimated:YES];
} else {
NSString *authMessage = error;
UIAlertView *auth2 = [[UIAlertView alloc] initWithTitle:@"Authentication Failed:" message:authMessage delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil];
[auth2 show];
}
}
我本希望使用键值对NSDictionary,但每次我发送带有键值对的json响应时,JSONValue都失败了,所以我最终只是将其作为一个简单的json响应,并引用数组位置中的对象来获取变量
如果有一种方法可以将键值对放入字典中,那就太好了,尽管我可能没有正确地生成json响应(PHP使用json_encode($data)方法按预期创建json,但Objective C函数不会解析它).太好了。我会尝试将这些代码片段集成到我的AppDeltage中,看看它是如何运行的。我会回复让您知道。我想我需要再询问一点。我知道了他的想法,但没有覆盖基本启动(选项卡式应用程序)的功能相反,用代码启动我的auth UIview,一旦我获得auth,我可以继续启动脚本中内置的选项卡式应用程序吗?值得注意的是,应用程序给我的第一个抱怨是没有“JSONValue”的方法……这是我需要包含的另一个导入吗?我将我的PHP auth脚本设置为以基本JSON作为响应{“success:“1”,AccountID:“account ID”,}-或-{“success:“0”,错误:“此处的详细消息”,}…一旦我获得JSONValue,这些应该可以正常工作”问题解决了。我必须下载并包含json库才能让functino正常工作,并将ym代码更改为上面请求中列出的新版本。这不是我想要的,但如果我的php url的json响应为[“1”,“4”,“3]”(成功,accountid,错误),它可以正常工作如果您使用的是您列出的json框架,假设您的数据以键值对的形式返回,您应该能够将其转换到字典中,正如我在上面的回答中所示。唯一不可能的方法是您的json字符串不是那样格式化的。是的。我必须继续尝试解释如何使用键值对。我的理解是,{[“success”:“0”,“AccountID”:“4”,“error”:“]}的格式可以工作,但我尝试了多种方法,JSONView从未工作过,直到我简化为一个值数组[“1”,“4”,““”]。我仍在研究它,因为如果我能解释表视图等的完整数据集,这将使我在未来受益。大概是一篇很大的文章。是否可能是我的错误?我推测这将是我的php数据元素,如:“?blah=blah&blah2=blah2”等?测试