1

我开始使用 AFNetworking 开发我的应用程序。一切都很顺利,直到我想使用核心数据。我知道还有一个额外的类(AFIncrementalStore)。但是因为我是 IOS 开发的新手,并且没有太多关于这方面的信息。我决定改用 RestKit,因为这里有更多信息。现在,我学习了关于 AFNetworking 的教程。在这里,我创建了一个 API 类,其中包含此方法。

+(API *)sharedInstance
{
    static API *sharedInstance = nil;
    static dispatch_once_t oncePredicate;
    dispatch_once(&oncePredicate, ^ {
        sharedInstance = [[self alloc]initWithBaseURL:[NSURL URLWithString:kAPIHost]];
    });
    return sharedInstance;
}

#pragma mark - init
//intialize the API class with the destination host name

-(API *)init
{
    //call super init
    self = [super init];

    if (self != nil){
        //initialize the object
        user = nil;

        [self registerHTTPOperationClass:[AFJSONRequestOperation class]];

        // Accept HTTP Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
        [self setDefaultHeader:@"Accept" value:@"application/json"];
    }
    return self;
}

-(void)loginCommand:(NSMutableDictionary *)params onCompletion:(JSONResponseBlock)completionBlock{
    NSLog(@"%@%@",kAPIHost,kAPILogin);
    NSMutableURLRequest *apiRequest = [self multipartFormRequestWithMethod:@"POST" path:kAPILogin parameters:params constructingBodyWithBlock:^(id <AFMultipartFormData>formData){
        //TODO: attach file if needed

    }];
    AFJSONRequestOperation *operation = [[AFJSONRequestOperation alloc] initWithRequest:apiRequest];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject){
        //success!
        NSLog(@"SUCCESSSS!");
        completionBlock(responseObject);
    }failure:^(AFHTTPRequestOperation *operation, NSError *error){
        //Failure
        NSLog(@"FAILUREE!");
        completionBlock([NSDictionary dictionaryWithObject:[error localizedDescription] forKey:@"error"]);
    }];
    [operation start];

}

这处理了我的 web 服务和应用程序之间的通信。在 viewControler 本身中,我这样称呼这个方法。

   /*  [[API sharedInstance] loginCommand:[NSMutableDictionary dictionaryWithObjectsAndKeys:_txtLogin.text,@"email",_txtPass.text,@"pwd", nil] onCompletion:^(NSDictionary *json){
     //completion
         if(![json objectForKey:@"error"]){
             NSLog(@"status %@",[json valueForKeyPath:@"data.status"]);
             if([[json valueForKeyPath:@"data.status"]intValue] == 200){
                  // Everything is oké, and login is succesfull
             }else{
                 //show validation
             }
         }else {
             NSLog(@"Cannot connect to the server");
         }
     }];*/

这就是我在 AFnetworking 中的做法。但是当我在 RestKit 中这样做时有什么区别。我搜索了教程。但是在从 RestKit 1.0 更新到 2.0 之后,很多这些教程都已经过时了。所以我希望任何人都可以帮助我解决这个问题!

亲切的问候!

4

1 回答 1

0

我使用本教程来使用 RestKit。它向您展示了如何使用它,您可以了解其他详细信息。http://www.youtube.com/watch?v=dFi9t8NW0oY

于 2014-01-13T20:06:13.140 回答