OGeek|极客世界-中国程序员成长平台

标题: 没有 UIWebView 的 iOS 应用程序身份验证 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 08:52
标题: 没有 UIWebView 的 iOS 应用程序身份验证

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

这是我的后台登录代码:

@app.route('/login/', methods=['OST'])
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)



Best Answer-推荐答案


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

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

-(void)logMeInNSString *)username passwordNSString *)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"OST"];
[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
    }

}];

}

如需更多信息, 看看

关于没有 UIWebView 的 iOS 应用程序身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14986980/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4