WebView API
Starting with NAM SDK 8.2.4
version, if you are monetizing from the GFP Web SDK via a WebView in your app that can display ads, you must register each WebView instance with the SDK.
This is a key function to improve ad targeting accuracy and improve the ad profitability of your app by communicating with websites that have GFP Web SDK loaded into the WebView.
If you use WebView, or in Hybrid-app case, be sure to apply the guide below.
This guide explains how to register a WebView
object, which is necessary to improve ad targeting effectiveness.
Enable third-party cookies
To improve your user's ad experience and be consistent with Chrome's cookie policy, enable third-party cookies on your WebView
instance.
- Kotlin
- Java
CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true)
CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true);
Web settings
Default WebView
settings are not optimized for ads. Use the WebSettings APIs
to configure your WebView
.
- Kotlin
- Java
import android.webkit.CookieManager
import android.webkit.WebView
class MainActivity : AppCompatActivity() {
private lateinit var webView: WebView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
webView = findViewById(R.id.webview)
// Let the web view accept third-party cookies.
CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true)
// Let the web view use JavaScript.
webView.settings.javaScriptEnabled = true
// Let the web view access local storage.
webView.settings.domStorageEnabled = true
// Let HTML videos play automatically.
webView.settings.mediaPlaybackRequiresUserGesture = false
}
}
import android.webkit.CookieManager;
import android.webkit.WebView;
public class MainActivity extends AppCompatActivity {
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webview);
// Let the web view accept third-party cookies.
CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true);
// Let the web view use JavaScript.
webView.getSettings().setJavaScriptEnabled(true);
// Let the web view access local storage.
webView.getSettings().setDomStorageEnabled(true);
// Let HTML videos play automatically.
webView.getSettings().setMediaPlaybackRequiresUserGesture(false);
}
}
Register the WebView
Call registerWebView()
on the UI thread to establish a connection with the JavaScript handler in GFP Web SDK
within each WebView instance. This should be done as early as possible, for example in the onCreate() method of your main activity. It should also be done before calling loadUrl()
.
- Kotlin
- Java
import android.webkit.CookieManager
import android.webkit.WebView
class MainActivity : AppCompatActivity() {
private lateinit var webView: WebView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
webView = findViewById(R.id.webView)
// Let the web view accept third-party cookies.
CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true)
// Let the web view use JavaScript.
webView.settings.javaScriptEnabled = true
// Let the web view access local storage.
webView.settings.domStorageEnabled = true
// Let HTML videos play automatically.
webView.settings.mediaPlaybackRequiresUserGesture = false
// Register the webView on GfpSdk.
// Should be run on UI Thread.
GfpSdk.registerWebView(webView)
// Load url on the webView.
webView.loadUrl(url)
}
}
import android.webkit.CookieManager;
import android.webkit.WebView;
public class MainActivity extends AppCompatActivity {
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webview);
// Let the web view accept third-party cookies.
CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true);
// Let the web view use JavaScript.
webView.getSettings().setJavaScriptEnabled(true);
// Let the web view access local storage.
webView.getSettings().setDomStorageEnabled(true);
// Let HTML videos play automatically.
webView.getSettings().setMediaPlaybackRequiresUserGesture(false);
// Register the webView on GfpSdk.
// Should be run on UI Thread.
GfpSdk.registerWebView(webView);
// Load url on the webView.
webView.loadUrl(url);
}
}