//
//  CallbackUtil.m
//  MCPlus
//
//  Created by KangSH on 2018. 12. 20..
//  Copyright © 2018년 KangSH. All rights reserved.
//

#import "CallbackUtil.h"

@implementation CallbackUtil


- (NSString *) callBackFunc:(NSString*) callbackFn withDic:(NSMutableDictionary *) dic{

    if (callbackFn == nil) {
        return @"";
    }
    
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic // Here you can pass array or dictionary
                                                       options:NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string
                                                         error:&error];
    NSString * success;
    NSString *jsonString;
    if (jsonData) {
        jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        success = @"true";
        
    } else {
        NSLog(@"Got an error: %@", error);
        jsonString = @"json parsing error";
        success = @"false";
    }
    
    NSString *resultString;
    
    resultString = [NSString stringWithFormat:@"%@({\"success\":\"%@\",\"result\":%@})",callbackFn ,success, jsonString];
    
    NSLog(@"resultString is %@", resultString);
    return resultString;
}

@end

