Intercepting mobile SDK network requests
Have greater control over networking requests made by the mobile SDKs.
CobrowseIO.instance().urlSession = \\ Your URLSession objectclass CustomURLSessionDelegate: NSObject, URLSessionTaskDelegate {
func urlSession(_ session: URLSession, didCreateTask task: URLSessionTask) {
let allowedHosts = ["api.cobrowse.io"]
guard let host = task.currentRequest?.url?.host, !allowedHosts.contains(host)
else { return }
print("Blocking data task for host: \(host)")
task.cancel()
}
}CobrowseIO.instance().okHttpClient(
OkHttpClient.Builder()
.addInterceptor(LoggingInterceptor())
.build()
)class LoggingInterceptor : Interceptor {
companion object {
private val allowedHosts = setOf(".cobrowse.io")
}
override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request()
val host = request.url().host()
if (allowedHosts.any { host.endsWith(it) }) {
return chain.proceed(request)
} else {
throw IOException("This request is not allowed: " + request.url())
}
}
}Last updated