I want to use the WebView in Flutter to load a code redemption page from apple, so that my user can use discounts on subscriptions. For example pages like Google, Flutter.dev, … it works fine, but using the url from apple sadly won’t. I just get a blank white screen in my body – the appbar works fine. What could be wrong?
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:webview_flutter/webview_flutter.dart';
import '../../configuration/theme.dart';
class FumiProPromoPage extends StatefulWidget {
String code;
//Variable Code übergeben
FumiProPromoPage({
Key? key,
required this.code,
}) : super(key: key);
@override
State<FumiProPromoPage> createState() => _FumiProPromoPageState();
}
class _FumiProPromoPageState extends State<FumiProPromoPage> {
var loadingPercentage = 0;
late WebViewController controller;
final GlobalKey webViewKey = GlobalKey();
String promourl = "https://google.de";
String url = "";
double progress = 0;
final urlController = TextEditingController();
@override
void initState() {
super.initState();
promourl = 'https://apps.apple.com/redeem?ctx=offercodes&id=1567964963&code=${widget.code}';
print(promourl);
controller = WebViewController()
..setJavaScriptMode(JavaScriptMode.unrestricted)
..setBackgroundColor(const Color(0x00000000))
..setNavigationDelegate(
NavigationDelegate(
onProgress: (int progress) {
// Update loading bar.
},
onPageStarted: (String url) {},
onPageFinished: (String url) {},
onWebResourceError: (WebResourceError error) {},
onNavigationRequest: (NavigationRequest request) {
if (request.url.startsWith('https://www.youtube.com/')) {
return NavigationDecision.prevent;
}
return NavigationDecision.navigate;
},
),
)
..loadRequest(Uri.parse(promourl));
}
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
elevation: 0,
backgroundColor: fumigruenAccent,
title: const Text(
"Promocode einlösen",
style: TextStyle(
color: Colors.black,
),
),
leading: CloseButton(
color: Colors.black,
onPressed: () {
Navigator.of(context).pop();
},
),),
body:
Stack(
children: [
WebViewWidget(
controller: controller,
),
loadingPercentage < 100
? LinearProgressIndicator(
color: Colors.red,
value: loadingPercentage / 100.0,
)
: Container()
],
),
);
}
}
I also deactivated the Apple Transport Security (ATS) feature for http requests to figure out if that’s the problem. It sadly didn’t work.
To do this I added the following lines to my Info.plist
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key><true/>
</dict>
Any ideas? many thanks in advance!