如何使用AFHTTPSessionManager写入Firebase数据库?

时间:2017-02-07 21:36:03

标签: objective-c macos rest firebase afnetworking

我正在尝试编写一个macOS数据输入应用程序来操作Firebase数据库(为iOS应用程序供电)。

我可以使用代码从数据库中读取:

    NSString *urlString = @"https://[PROJECT_ID].firebaseio.com/Object.json";
    NSDictionary *parameters = @{
                                 @"orderBy" : @"\"updatedAt\"",
                                 @"startAt" : @"0",
                                 @"auth" : authString
                                 };

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

    [manager GET:urlString parameters:parameters progress:nil success:^(NSURLSessionTask *task, id responseObject) {
        NSLog(@"JSON: %@", responseObject);
    } failure:^(NSURLSessionTask *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];

但是我无法写作。为了解决问题,我已将Firebase数据库中的readwrite规则设置为true,并且只是尝试向数据库写入任何内容。例如:

    NSError *error;
    NSDictionary *objectDictionary = @{@"first" : @"Foo", @"last" : @"Bar"};
    NSData *objectData = [NSJSONSerialization dataWithJSONObject:objectDictionary options:kNilOptions error:&error];

    NSString *urlString2 = @"https://[PROJECT_ID].firebaseio.com/Object2.json"
    [manager PUT:urlString2 parameters:objectData success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"responseObject %@",responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"- error %ld - %@",(long)task.state,error);
    }];

我已尝试将PUT参数设为NSDictionary,NSString和NSData。每次我都会遇到同样的错误:Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: bad request (400)"

任何想法我做错了什么?

0 个答案:

没有答案