【问题标题】:iOS App Authentication Without UIWebView没有 UIWebView 的 iOS 应用程序身份验证
【发布时间】:2013-02-20 18:11:50
【问题描述】:

有没有办法在没有来自 Flask(Python) 后端的 webview 的情况下对用户进行身份验证?

这是我的后台登录代码:

@app.route('/login/', methods=['POST'])
def login():
    params = json.loads(request.data)
    username = params['username']
    password = params['password']
    u = User.query.filter_by(username=username).first()
    if u:
        if utils.check_password_hash(password, u.pw_hash):
            login_user(u)
            return ('', 200)
        else:
            return ('', 400)
    else:
        return ('', 400)

【问题讨论】:

  • 我希望这个问题比较笼统,因此对未来的用户有所帮助。如果您希望我对问题添加任何内容,请告诉我。谢谢。
  • 您可以使用NSURLConnection 对您的API 端点执行GETPOST 请求。
  • 那个登录用户,我现在有那个,但它没有验证用户。

标签: ios objective-c ios6 flask flask-login


【解决方案1】:

是的,你当然可以做到这一点。这就是我通常的做法,根据您的需要进行更改(请记住,NSJSONSerialization 仅在 iOS 5+ 中可用):

@property (nonatomic) NSOperationQueue *basicQueue; //add this to your header or as an instance variable

-(void)logMeIn:(NSString *)username password:(NSString *)password {

basicQueue = [[NSOperationQueue alloc] init];

NSDictionary *userCredentialsDict = [NSDictionary dictionaryWithObjectsAndKeys:username, @"username", password, @"password", nil];
NSData *encodedDict = [NSJSONSerialization dataWithJSONObject:userCredentialsDict options:NSJSONWritingPrettyPrinted error:NULL];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://yoursite.com/login"]];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:encodedDict];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

[NSURLConnection sendAsynchronousRequest:request queue:basicQueue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*) response;
    NSString *stringFromData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    if (httpResponse.statusCode == 200) {
        //do stuff
        NSLog(@"Received data, parsed into string: %@", stringFromData);
    } else {
        //do error stuff or whatever
    }

}];

}

如需了解更多信息, 看看

【讨论】:

  • 非常感谢。很有帮助。 :) 将阅读 Apple Developer 链接。
  • 有趣的解决方案。在接受httpResponse.statusCode == 200 之后,如何存储会话以便连续访问 Web 服务不需要任何进一步的登录请求?还是在每次请求时将用户名/密码传递给服务器的常用方法?
猜你喜欢
  • 2012-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-23
  • 2015-02-24
  • 2013-12-21
相关资源
最近更新 更多