2

有没有办法在没有来自 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)
4

1 回答 1

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
    }

}];

}

欲了解更多信息,请查看

于 2013-02-20T19:09:15.707 回答