我应该如何创建模型类来调用API(使用MVC模式)?

时间:2017-06-05 06:26:56

标签: ios objective-c model-view-controller

目前我在viewcontroller本身调用API,但根据MVC,这不是一个好的编程习惯 这是我的代码:

-(void)fetchData{

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:@"https://dl.dropboxusercontent.com/s/2iodh4vg0eortkl/facts.json"]];
    [request setHTTPMethod:@"GET"];
    [request addValue:@"text/plain" forHTTPHeaderField:@"Content-Type"];
    [request addValue:@"text/plain" forHTTPHeaderField:@"Accept"];

    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    [[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        NSString *requestReply = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
        NSData * responseData = [requestReply dataUsingEncoding:NSUTF8StringEncoding];
        if (responseData != nil) {

            NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
            NSLog(@"requestReply: %@", jsonDict);
            self.content = jsonDict[@"rows"];
            self.navigationTitle = jsonDict[@"title"];


            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView reloadData];

            });

            NSLog(@"result %@", self.content);

        }

    }] resume];


}

您能否建议我如何安排代码/文件以遵循MVC模式?
如果我创建单独的模型类,我应该如何管理现有代码以及如何将响应转移到viewcontroller

2 个答案:

答案 0 :(得分:1)

Swift 3 试试这个:

class WebRequester: NSObject {

    static let shared = WebRequester()

    func request(vc:UIViewController, url: String, parameter:String?, callback:@escaping (_ result:NSDictionary?, _ error:NSError?) -> Void) {

        let requestURL = url

        print("..............................")
        print("URL: ",requestURL)
        print("Parameter: ",parameter ?? "nil")
        print("..............................")

        var request = URLRequest(url: URL(string:requestURL)!)
        request.httpMethod = "POST"

        if parameter != nil {
            request.httpBody = parameter!.data(using: .utf8)
        }


        let session = URLSession.shared
        let task = session.dataTask(with: request, completionHandler: {data, response, error in
            UIApplication.shared.isNetworkActivityIndicatorVisible = false
            DispatchQueue.main.async {
                if error == nil {
                    do {
                        let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments)
                        print(json)
                        let dic = json as? NSDictionary
                        if dic != nil {
                            callback(json as? NSDictionary, nil)
                        }
                        else {
                            vc.alert(title: "Oops..", message: "Something went wrong please try again later")
                        }
                    }
                    catch {
                        print(error.localizedDescription)
                        callback(nil,error as NSError?)
                    }
                }
                else {
                    print(error?.localizedDescription ?? "")
                    callback(nil, error as NSError?)
                }
            }
        })
        task.resume()
        UIApplication.shared.isNetworkActivityIndicatorVisible = true
    }
}

<强>的ObjectiveC

<强> WebRequester.h

#import <Foundation/Foundation.h>

@interface WebRequester : NSObject

+(id)shared;

-(void)request1:(NSString *)url parameter:(NSString *)params callback:(void(^)(NSDictionary *result, NSError *error))callback;

@end

<强> WebRequester.m

#import "WebRequester.h"

@implementation WebRequester

+(id)shared {
    static WebRequesterObjC *sharedMyManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedMyManager = [[self alloc] init];
    });
    return sharedMyManager;
}

-(void)request1:(NSString *)url parameter:(NSString *)params callback:(void(^)(NSDictionary *result, NSError *error))callback {

    NSString *requestUrl = url;

    NSLog(@"====================");
    NSLog(@"%@",requestUrl);
    NSLog(@"%@",params);
    NSLog(@"====================");

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[[NSURL alloc] initWithString:requestUrl]];
    request.HTTPMethod = @"POST";

    if (params != nil) {
        request.HTTPBody = [params dataUsingEncoding:NSUTF8StringEncoding];
    }

    NSURLSession* session = [[NSURLSession alloc] init];
    NSURLSessionDataTask* task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (error == nil) {
            if (data != nil) {
                NSError * err;
                NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&err];
                if (err == nil) {
                    callback(dic,nil);
                }
                else {
                    callback(nil,err);
                }
            }
        }
        else {
            callback(nil,error);
        }
    }];
    [task resume];
}

@end

请致电

  [WebRequesterObjC.shared request1:<#url#> parameter:params callback:^(NSDictionary *result, NSError *error) {
        if (error == nil) {
            NSLog(@"%@",result)
        }
  }];

答案 1 :(得分:0)

class RestAPI: NSObject {
    static let sharedInstance = RestAPI()

    func restAPIMtd(urlString: String,  methodName: String, params: String = "", CompletionHandler:@escaping (_ success: Bool, _ response: NSDictionary)  -> Void) {
        // create post request
        let url = URL(string: urlString)!
        var request = URLRequest(url: url)
        request.httpMethod = methodName
        let postData = params.data(using: .utf8)
        request.httpBody = postData
        // insert json data to the request
        request.timeoutInterval = 60
        let task = URLSession.shared.dataTask(with: request as URLRequest, completionHandler: { data, response, error in
            guard error == nil else {
                self.showErrorMessage(error:(error)!)
                CompletionHandler(false, [:])
                return
            }
            guard let data = data else {
                self.showErrorMessage(error:(error)!)
                CompletionHandler(false, [:])
                return
            }
            do {
                if let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: AnyObject] {
                    CompletionHandler(true, json as NSDictionary)
                    return
                }
            } catch let error {
                CompletionHandler(false, [:])
                self.showErrorMessage(error: error)
            }
        })
        task.resume()
    }

    func showErrorMessage(error:Error) -> Void {
        DispatchQueue.main.async {
            print(error.localizedDescription)
        }
    }
}