Repository: flutter
Issue: [webview_flutter] Azure AD authentication doesn't redirect on iOS 
Labels: platform-ios,p: webview,package,P2,team-ios,triaged-ios

**Original issue** 
"I am using webview_flutter to do Azure AD authentication. It works fine on Android however when testing it on iOS the webview does not redirect and therefore cannot receive the token. If a bad password is entered it will tell you but when login is successful the loading dots show at the top of the device and just hangs there. Not sure if there is some special iOS setting or permissions I need to also implement to get this to work. Any help would be appreciated."

**Flutter Doctor**

```bash
[√] Flutter (Channel stable, 1.22.1, on Microsoft Windows [Version 10.0.19041.630], locale en-US)
    • Flutter version 1.22.1 at C:\Flutter
    • Framework revision f30b7f4db9 (8 weeks ago), 2020-10-08 10:06:30 -0700
    • Engine revision 75bef9f6c8
    • Dart version 2.10.1


[√] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
    • Android SDK at C:\Users\Eric\AppData\Local\Android\sdk
    • Platform android-29, build-tools 29.0.3
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
    • All Android licenses accepted.

[√] Android Studio (version 4.0)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin version 50.0.1
    • Dart plugin version 193.7547
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)

[√] VS Code (version 1.51.1)
    • VS Code at C:\Users\Eric\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.16.0
```

I have finished troubleshooting with MS support and ran packet captures which proved that MS is indeed seeing that the authentication was successful on iOS and sends the requested code back to the webview however the webview does not redirect the user to the redirect uri. I can setup an account for you to test with if you want to use the code I provided earlier.

Requesting code from token endpoint
![image](https://user-images.githubusercontent.com/72224310/100670528-ff50e000-332c-11eb-8e85-b6865b05e5ca.png)

Response from MS
![image](https://user-images.githubusercontent.com/72224310/100670540-05df5780-332d-11eb-9d1c-835429c53187.png)


I also found something very interesting, when pasting the authorization url with headers into Chrome it redirects to
![image](https://user-images.githubusercontent.com/72224310/100670568-1263b000-332d-11eb-9c80-125c59ee2278.png)


However, pasting the url into safari a popup shows in the foreground to download the file instead of loading the redirect uri. I believe this just isn't showing in the webview and it is waiting for a response hence the continuous loading dots at the top.
![image](https://user-images.githubusercontent.com/72224310/100670596-1b548180-332d-11eb-8094-8a6f14d5f649.png)


**Code (This will require an AD account in our tenant which I can make for you to test with)**

```dart
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';

//I cannot share this link for security purposes.
final _initialUrl = "This is my Azure AD authentication endpoint";


void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Webview Test',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Webview Test'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);


  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  static WebViewController _webViewController;

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      //Builder is wrapped around the webview to allow SnackBar to access the build context of the scaffold.
      // Without this the context would be from AuthLogin which does not contain a scaffold.
      body: Builder(builder: (context) =>
          WebView(
            onWebViewCreated: (WebViewController c) {
              _webViewController = c;
            },
            initialUrl: _initialUrl,
            onPageFinished: _getAuthCode,
            javascriptMode: JavascriptMode.unrestricted,
          ),
      ),
    );
  }

  Future<void> _getAuthCode(String value) async {

    // Wait for the web view to redirect after successful authentication then parse the code from the redirect url.
    // It then uses the code from the previous step to POST to the token endpoint.
    //Check that the web view has loaded the page before continuing
    if (_webViewController != null) {
      value = await _webViewController.currentUrl();
      Uri response = Uri.dataFromString(value);
      print(response.path);
      //Check that the user is past authentication and current URL is the redirect with the code.
      if(response.path == ',https://login.microsoftonline.com/common/oauth2/nativeclient' ) {
        //iOS does not get to this part. Only on android does the webview go to the redirect URI.
        //iOS shows the loading dots at the top of the screen and hangs there indefinitely.
        print("Webview has redirected");

      }
    }
  }

}

```