iOS SDK App Level Opt Out

From Engineering Client Portal

Revision as of 21:07, 1 September 2020 by ColinBrown (talk | contribs) (App Level Opt Out)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Engineering Portal breadcrumbArrow.png Digital breadcrumbArrow.png DCR & DTVR breadcrumbArrow.png iOS SDK App Level Opt Out

App Level Opt Out

This can be used if the Ad Framework is not available, or you do not wish to use the ATTrackingManager. The Opt-Out occurs by opening a Nielsen-defined web page and passing the user choice from the 'WebView'. In order to do this, the application needs to:

  • Implement the UIWebView delegate method to open the Nielsen Privacy web page
  • Capture user's selection
  • Pass the selection back to the SDK via the userOptOut.

Displaying Opt-Out in a WebView

Swift 5.0

Notify the SDK of the user’s selection by implementing the UIWebViewDelegate protocol and passing the URL to

nielsenApi?.userOptOut(url)


as shown in the following example.

    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: 
         WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        if navigationAction.request.url?.absoluteString == "nielsen://close" {
            closeOptOutView()
            decisionHandler(.cancel)
        } else {
        if let url = navigationAction.request.url?.absoluteString, url.hasPrefix("nielsen") {
                       nielsenApi?.userOptOut(url)
                       decisionHandler(.cancel)
                   } else {
                       if navigationAction.navigationType == .linkActivated {
                           if let url = navigationAction.request.url?.absoluteString, url.hasSuffix("#") {
                               decisionHandler(.allow)
                           } else {
                               decisionHandler(.cancel)
                               webView.load(navigationAction.request)
                           }
                       } else {
                           decisionHandler(.allow)
                       }
                   }
    }
    }  
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = UIColor(patternImage: UIImage(named: "new_ios_bg.png")!)
        self.nielsenApi = NielsenInit.getSharedAppApi(delegate: self)
        if let appApi = self.nielsenApi {
            //Getting the optPut URL from SDK
            if let url = URL(string: appApi.optOutURL) {
                webView.load(URLRequest(url: url))
                webView.allowsBackForwardNavigationGestures = true
            }
        }
    }


Objective C

Notify the SDK of the user’s selection by implementing the UIWebViewDelegate protocol and passing the URL to

- (BOOL)userOptOut:(NSString *)optOut;

as shown in the following example.

- (void)viewDidLoad {
    [super viewDidLoad];   
    self.nielsenApi = [NielsenInit createNielsenAppApiWithDelegate:nil];    
    self.webView = [[WKWebView alloc] initWithFrame:self.view.frame];
    self.webView.frame = CGRectMake(self.view.frame.origin.x,self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
    [self.view addSubview:self.webView];    
    NSString *testString = @"https://priv-policy.imrworldwide.com/priv/mobile/de/de/optout_legacy.html?n=&rnd=1597242254945";  
    self.webView.navigationDelegate = self;
  // [self initWebView];
   [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:testString]]];
}

- (void)initWebView
{
    WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
    WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:configuration];
    webView.navigationDelegate = self;
    [self.view addSubview:webView];
    self.webView = webView;
    webView.translatesAutoresizingMaskIntoConstraints = NO;
}

- (void)closeOptOutView
{    [self dismissViewControllerAnimated:YES completion:nil];}

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
    if ([navigationAction.request.URL.absoluteString isEqualToString:kNielsenWebClose])
    {   [self performSelector:@selector(closeOptOutView) withObject:nil afterDelay:0];
        decisionHandler(WKNavigationActionPolicyCancel);}
    else
    {if ([navigationAction.request.URL.absoluteString hasPrefix:@"nielsen"])
        {   [self.nielsenApi userOptOut:navigationAction.request.URL.absoluteString];
            decisionHandler(WKNavigationActionPolicyCancel); }
        else
        {if (navigationAction.navigationType == WKNavigationTypeLinkActivated)  {
                if ([navigationAction.request.URL.absoluteString hasSuffix:@"#"])
                {decisionHandler(WKNavigationActionPolicyAllow);}
                else
                { decisionHandler(WKNavigationActionPolicyCancel);
                    [webView loadRequest:[NSURLRequest requestWithURL:navigationAction.request.URL]]; }
            }else
            {decisionHandler(WKNavigationActionPolicyAllow);}
       }
}


Note: Starting from SDK version 5.1.1.18 SDK will be sending the data pings to census even though SDK is opted out (In earlier releases all the traffic from SDK to census will be ceased). However, all the outgoing pings will have the parameter uoo set to true so that the backend can ignore this data.