1. In order to establish communication between the web view(HTML) and the native code, we use the WKWebView and WKScriptMessage handler in native.

  2. This way you can pass the event information from the WebView to the native code. Once the data received in the interface method, it will be sent to the backend through the datagran_iOS_sdk.

  3. In order to pass the data from the HTML to native, you can use the WKUserContentController didReceiveScriptMessage which will handle it.

  4. TrackViewController

    #import "TrackViewController.h"
    
    @interface TrackViewController ()
    
    @end
    
    @implementation TrackViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
        WKUserContentController* userController = [[WKUserContentController alloc] init];
        
        [userController addScriptMessageHandler:self name:@"datagran"];
        configuration.userContentController = userController;
        
        self.webView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:configuration];
        [self.view addSubview:self.webView];
        
        self.webView.backgroundColor = [UIColor whiteColor];
        self.webView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
        [self.view addSubview:self.webView];
        
        
        NSURL *nsurl=[NSURL URLWithString:@"<https://jitpack-datagran.github.io/>"];
        NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
        
        
        [self.webView loadRequest: nsrequest];
        
        // Do any additional setup after loading the view from its nib.
    }
    
    - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
        NSDictionary *sentData = (NSDictionary*)message.body;
        NSString *command =  [sentData objectForKey:@"command"];
            
        if ([command isEqualToString:@"trackCustom"]) {
            NSLog(@"TackCustom ");
            NSString *name = [sentData objectForKey:@"name"] ;
            NSDictionary *parameters = [sentData objectForKey:@"parameters"];
                       
            [Tracker.shared trackCustomWithViewName:name action:parameters controller:self addGeo:false];
        } else if ([command isEqualToString:@"identify"]){
            NSLog(@"Identify ");
            NSString *userId = [sentData objectForKey:@"userId"];
            [Tracker.shared identifyWithId:userId controller:self addGeo:false];
        } else if ([command isEqualToString:@"reset"]){
            NSLog(@"Reset ");
            [Tracker.shared resetDGuserid];
            [self.delegate delegateMethod];
            //[self dismissViewControllerAnimated:YES completion:NULL];
            [self.view removeFromSuperview];
    
        } else {
            NSLog(@" Others ");
        }
        
    }
    @end