Skip to contentSkip to navigationSkip to topbar
On this page

Using WiFi for Silent Network Auth


All traffic on all operating systems will favor WiFi above cellular connections. However for Silent Network Auth, the API request must be made using the cellular connection. We realize that users are unlikely to turn off WiFi and so the following code is provided for both iOS and Android to include in your applications that will allow a small payload (~40 bytes) to be delivered over the cellular modem, even when WiFi is connected.

(error)

This article is for apps only

Only downloadable and installable applications are suitable for this fix. If you are attempting to use Silent Network Auth in a website this solution will not work. You should instruct users to turn off WiFi.


Android Sample Code

android-sample-code page anchor

The following will allow Android applications using API Release M and above to use Cellular for the Silent Network Auth API request.

1
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
2
public static void doAPIonCellularNetwork(Context context, final String url) {
3
4
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(CONNECTIVITY_SERVICE);
5
6
NetworkRequest request = new NetworkRequest.Builder()
7
.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
8
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET).build();
9
10
connectivityManager.requestNetwork(request, new ConnectivityManager.NetworkCallback() {
11
12
@Override
13
public void onAvailable(final Network network) {
14
OkHttpClient okHttpClient = new OkHttpClient.Builder().socketFactory(network.getSocketFactory()).build();
15
Request request = new Request.Builder()
16
.url(url)
17
.build();
18
try {
19
Response response = okHttpClient.newCall(request).execute();
20
Log.i("", "doAPIonCellularNetwork RESULT:\n" + response.body().string());
21
} catch (Exception ex) {
22
ex.printStackTrace();
23
}
24
}
25
});
26
}

For iOS, start by downloading the library from here(link takes you to an external page).

Add the NetworkingLogic folder, which contains six files, to your project folder as shown below.

File structure that begins with a directory named Boku Wi-Fi Solution with a directory under it named NetworkingLogic.

Include #import "HTTPRequester.h in the Swift bridging header file. Also, include the code below as a member function in the ViewController that fires the EVURL.

1
//
2
// EvurlRequestWithCellularData.swift
3
//
4
// Created by BENJAMIN BRYANT BUDIMAN on 05/09/18.
5
// Copyright © 2021 Boku, Inc. All rights reserved.
6
//
7
8
import Foundation
9
10
/**
11
Requests the EvURL with cellular data, and checks whether the Silent Network Auth process completes or not.
12
Important note: this function shall always return false if cellular data is not available.
13
14
- Parameter evurl: The EvURL to be requested
15
- Returns: true if the Silent Network Auth process is successful and vice versa
16
*/
17
func requestEvurlWithCellularData(evurl:String) -> Bool {
18
let response = requestHelper(url: evurl)
19
20
// If any internal and network errors occured in HTTPRequester.performGetRequest, the function will return "ERROR"
21
if response == "ERROR" {
22
return false;
23
}
24
25
// Boku returns "ErrorCode=0&ErrorDescription=Success" only when the Silent Network Auth process is complete
26
if response.range(of:"ErrorCode=0&ErrorDescription=Success") != nil {
27
return true;
28
}
29
30
// Any HTTP responses without the above substring indicate an incomplete Silent Network Auth process
31
return false;
32
}
33
34
/**
35
Recursive function that keeps requesting a new URL with cellular data when the HTTP request returns a HTTP redirect code (3xx)
36
37
- Parameter url: The URL to be requested
38
- Returns: string response from the HTTP request
39
*/
40
func requestHelper(url:String) -> String {
41
// If the HTTP GET request returns a HTTP redirect code (3xx), HTTPRequester.performGetRequest returns a
42
// formatted string that contains the redirect URL. The formatted string starts with "REDIRECT:"
43
// and it's followed with the redirect URL (e.g. REDIRECT:https://www.boku.com)
44
var response = HTTPRequester.performGetRequest(URL(string: url))
45
46
if response!.range(of:"REDIRECT:") != nil {
47
// 1. Get the redirect URL by getting rid of the "REDIRECT:" substring
48
let redirectRange = response!.index(response!.startIndex, offsetBy: 9)...
49
let redirectLink = String(response![redirectRange])
50
51
// 2. Make a request to the redirect URL
52
response = requestHelper(url: redirectLink)
53
}
54
55
return response!
56
}

Execute the EVURL as shown in the code sample below. Please note the EVURL will change and must be created each time.

1
override func viewDidLoad() {
2
super.viewDidLoad()
3
4
let EVURL = "http://boku-url/cimi/evurl?SKEY=BEHBuKkD3yxt6dD6NgEHKhjBLZTPwQgT2Yb06NMnCilwuVgEetIlH7lUL%2BgmAEY%2FlaFVkoxzfdndPlVmP9FBRycx%2BhKZeLAo1gmvBP9qdb0%3D"
5
6
_ = requestEvurlWithCellularData(evurl: EVURL)
7
}
(warning)

EVURLs are Unique

EVURLs are unique to each request, do not hard code one. EVURLs can be created manually using the Create an EVURL endpoint.

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.