iOS SDK App Level Opt Out: Difference between revisions

From Engineering Client Portal

(Created page with "{{Breadcrumb|}} {{Breadcrumb|Digital}} {{Breadcrumb|DCR & DTVR}} {{CurrentBreadcrumb}} Category:Digital === App Level Opt Out === This is only used if the Ad Framework is...")
 
Line 1: Line 1:
{{Breadcrumb|}} {{Breadcrumb|Digital}} {{Breadcrumb|DCR & DTVR}}  {{CurrentBreadcrumb}}
{{Breadcrumb|}} {{Breadcrumb|Digital}} {{Breadcrumb|DCR & DTVR}}  {{CurrentBreadcrumb}}
[[Category:Digital]]
[[Category:Digital]]
=== App Level Opt Out ===
= App Level Opt Out =
This is only used if the Ad Framework is not available.  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:
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
* Implement the UIWebView delegate method to open the Nielsen Privacy web page
* Capture user's selection
* Capture user's selection
* Pass the selection back to the SDK via the <code>userOptOut</code>.
* Pass the selection back to the SDK via the <code>userOptOut</code>.


==== Capture and forward user selection ====
=== Displaying Opt-Out in a WebView ===
{{ExampleCode|
==== Swift 5.0 ====
|Objective C = <syntaxhighlight lang="objective-c">
 
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
Notify the SDK of the user’s selection by implementing the '''UIWebViewDelegate protocol''' and passing the URL to
{
<br>
    NSString *command = [NSString stringWithFormat:@"%@",request.URL];
<syntaxhighlight lang="swift">
     if ([command isEqualToString:kNielsenWebClose]) {
nielsenApi?.userOptOut(url)
         // Close the WebView
</syntaxhighlight>
         [self performSelector:@selector(closeOptOutView) withObject:nil afterDelay:0];
<br>
         return NO;
as shown in the following example.
 
<syntaxhighlight lang="Swift">
    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
            }
         }
     }
     }
    // Retrieve next URL if it’s not opt-in/out selection
    return (![nAppApiObject userOptOut:command]);
}</syntaxhighlight>
*The app gets the user selection string via webviews shouldStartLoadWithRequest and invokes <code>userOptOut</code> with user selection. The delegate method handles the 'WebView' URL requests, interprets the commands, and calls the SDK accordingly.
**<code>[nAppApiObject userOptOut:command]</code> passes the user's selection on Nielsen Privacy page to the SDK to allow the SDK to perform the required functions.
<blockquote>'''Note:''' When 'WebView' is closed, pass the status returned from 'WebView' to the SDK within the app. The App SDK manages the user's choice (Opt-Out / Opt-In), the app does not need to manage this status.</blockquote>
|Swift = <syntaxhighlight lang="swift">
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {


        let command = request.url?.absoluteString
        if  command ==  "kNielsenWebClose"{


            self.perform(#selector(closeOptOutView), with: nil, afterDelay: 0)
</syntaxhighlight>
<br>
 
==== Objective C ====
 
 
 
Notify the SDK of the user’s selection by implementing the '''UIWebViewDelegate protocol''' and passing the URL to
 
<syntaxhighlight lang="objective-c">
- (BOOL)userOptOut:(NSString *)optOut;
</syntaxhighlight>


            return false
as shown in the following example.
        }  
<br>
        return NielsenAppApi.optOutURL
<br>
     }
<syntaxhighlight lang="objective-c">
- (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);}
      }
}
</syntaxhighlight>
</syntaxhighlight>
*The app gets the user selection string via webviews shouldStartLoadWithRequest and invokes <code>userOptOut</code> with user selection. The delegate method handles the 'WebView' URL requests, interprets the commands, and calls the SDK accordingly.
 
**<code>[nAppApiObject userOptOut:command]</code> passes the user's selection on Nielsen Privacy page to the SDK to allow the SDK to perform the required functions.
 
<blockquote>'''Note:''' When 'WebView' is closed, pass the status returned from 'WebView' to the SDK within the app. The App SDK manages the user's choice (Opt-Out / Opt-In), the app does not need to manage this status.</blockquote>
 
}}
<blockquote>'''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.</blockquote>

Revision as of 21:06, 1 September 2020

Engineering Portal / Digital / DCR & DTVR / 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.