X code,身份验证和NSUserDefaults的 [英] XCode, Authentication and NSUserDefaults

查看:162
本文介绍了X code,身份验证和NSUserDefaults的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,将与Web服务器(PHP与MySQL的后端)使用Web视图界面。我的应用已经与静态URL作为测试工作,但现在我需要调整的实际用户交互。

I am developing an App that will interface with a web server (PHP with MySQL back end) using web views. I have the application working with static URL's as tests, but now I need to adjust for actual user interaction.

我一直在寻找到HTTP验证解决方案,但相信这句话是不正确的,因为大多数搜索结果我得到的都是与休息的请求。我将使用一个URL(PHP / MySQL的),以验证身份验证和不相信有资格作为REST根据我的经验。

I have been looking for solutions to HTTP authentication but believe the phrase to be incorrect as most of the search results I get are all related to "REST" requests. I will be using a URL (PHP / MySQL) to validate authentication and do not believe that qualifies as REST based on my experience.

我没有SSL作为尚未在此服务器上,所以我不知道通过什么可可可供选择。

I do not have SSL as of yet on this server so I am not sure what options are available via Cocoa.

问题我有:


  1. 有没有一种方法来创建一个视图,加载如果该帐户ID是不是已经存储在本地?

  1. Is there a way to create a View that loads if the account ID isn't already stored locally?

如果是这样,我就可以code中的应用,以取代故事板如果需要认证,我创建?

If so, can i code the app to supersede the storyboard I have created if authentication is needed?

如果步骤1和2的工作,我怎么能与我的Web服务器进行身份验证接口? (preferred方法是提交用户的电子邮件和密码的MD5因为这是当前存储在数据库中)

If step 1 and 2 work, how can I interface with my web server to authenticate? ( preferred method is to submit user email and the MD5 of the password as that is what is currently stored in the database)

理想我很想只是提交网址,如login.php中login=me@blah.com&密码=(md5hash),并有提供的响应要么给我的Auth =真放;帐户ID = 5或AUTH =假......然后使用iPhone应用程序要么报告一个auth错误给用户或保存帐户ID NSUserDefaults的,以供将来使用(旁路AUTH更高版本)和已到位加载正常故事板。

Ideally i would love to just submit a url like "login.php?login=me@blah.com&password=(md5hash)" and have the provided response either give me "auth=true&accountID=5" or "auth=false" ... then use the iPhone app to either report an auth error to the user or save the account id to NSUserDefaults for future use (to bypass auth later) and load the normal storyboard already in place.

任何建议将AP preciated。

Any recommendations would be appreciated.

谢谢,

银虎

推荐答案

您与您的想法pretty接近。我用一个非常类似的方法来我的用户登录过程。

You are pretty close with your thinking. I use a very similar approach to my user login process.

我目前盐和散列电子邮件和密码1 40字符标记。如果登录成功返回,我保存令牌NSUserDefaults的。我使用此令牌的所有其他Web请求前进,直到用户注销,此时我删除用户的默认值。

I currently salt and hash the email and password into 1 40 char token. If the login returns successful, I save the token to NSUserDefaults. I use this token for all other web requests going forward until the user logs out, at which time I delete the user defaults.

下面是一些片段我使用相同的过程:

Here are a few snippets I use for the same process:

// 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"];
}

这篇关于X code,身份验证和NSUserDefaults的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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