Chums Proxy example flutter application
In order to use Chums Proxy in your application, we need two flutter plugins: inapp_web_view_proxy and chums_proxy.
- flutter_inappwebview_proxy is required in order to use flutter_inappwebview together with the local proxy.
- chums_proxy - run a proxy service locally
First, we need to run our local proxy:
void main() {
WidgetsFlutterBinding.ensureInitialized();
ChumsProxyLifecycle().start();
runApp(const ProxyApp());
}
Then we will need the InappWebViewProxy instance to process requests:
late final InappWebViewProxy _browserProxyService;
void initState() {
super.initState();
_browserProxyService = InappWebViewProxy.instance
..initDefaultProxyHttpClient();
}
We also need to specify the settings for InAppWebView:
InAppWebView(
initialSettings: InAppWebViewSettings(
useShouldOverrideUrlLoading: true,
useShouldInterceptRequest: true,
resourceCustomSchemes: [if (Platform.isIOS) _browserProxyService.customProxyScheme],
),
onWebViewCreated: _onWebViewCreated,
onLoadStart: _onLoadStart,
shouldOverrideUrlLoading: _browserProxyService.onShouldOverrideUrlLoading,
onLoadResourceWithCustomScheme: _browserProxyService.onLoadResourceWithCustomScheme,
shouldInterceptRequest: _browserProxyService.onShouldInterceptRequest,
// ...
)
And some additional methods
_onWebViewCreated(InAppWebViewController controller) {
_webViewController = controller;
_loadUrl(_browserController.url.value);
}
_loadUrl(final String? text) async {
final request = _browserProxyService.onLoadUrl(text: text);
if(request != null) {
_webViewController?.loadUrl(urlRequest: request);
}
}
void _onLoadStart(_, Uri? uri) {
final url = _browserProxyService.onLoadStart(uri);
if(url != null) {
// on start load url
}
}