ionic-appauth icon indicating copy to clipboard operation
ionic-appauth copied to clipboard

Some help to get ionic-appauth to work on Android after upgrading from Capacitor V2 to Capacitor V3

Open wrzr123 opened this issue 3 years ago • 6 comments

Hey,

yesterday I was upgrading my Ionic app from Capacitor 2 to Capacitor 3, and I was struggeling a long time to get the redirect to work from my identity provider back to the app on a real Android device. I just want to document this for others who maybe also run into the same problem, so maybe it can just be transfered into some readme or stay here for others to be found.

  • Follow the normal upgrade steps described here
  • update ionic-appauth to latest (I'm using 0.8.5)
  • in your new Android app (if you were following the steps, you've deleted your Android application and it was recreated from scratch by running npx cap add android), open the AndroidManifest.xml of the main app
  • under the existing intent-filter, add another one (so you will have 2 intent-filters in the end):
<intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <data android:scheme="@string/custom_url_scheme" />
</intent-filter>

This is necessary to make the redirect back to the app work. It's not necessary to replace anything here with your own app-Id, it works like that. It's strange, but with Capacitor 2 these lines were inserted automatically in the manifest. Now with Capacitor 3 you have to enter it manually.

wrzr123 avatar Dec 14 '21 08:12 wrzr123

Thanks for your contribution. I faced a similar issue, when using the demo for capacitor. On the mobile the device the InAppBrowser does not return to the app. I just get a blank screen. I tried to add your second intent-filter to the AndroidManifest.xml, but it still doesn't work. The file is located under angular-android\app\src\main\AndroidManifest.xml and looks like this:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.appauth.demo">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|smallestScreenSize|screenLayout|uiMode"
            android:name="com.appauth.demo.MainActivity"
            android:label="@string/title_activity_main"
            android:theme="@style/AppTheme.NoActionBarLaunch"
            android:launchMode="singleTask">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="@string/custom_url_scheme" />
            </intent-filter>
        </activity>

        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="${applicationId}.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths"></meta-data>
        </provider>
    </application>

    <!-- Permissions -->

    <uses-permission android:name="android.permission.INTERNET" />
</manifest>

Do I have to add anything else here?

Ben555555 avatar Jan 12 '22 13:01 Ben555555

I was able to solve the issue by adding those 2 intents:

 <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="appauth" />
            </intent-filter>
            
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:host=" " android:pathPrefix="/" android:scheme=" " />
            </intent-filter>

Ben555555 avatar Jan 12 '22 14:01 Ben555555

@Ben555555 @wrzr123 Would you be so kind and post the content of your string file? I cant get this package to work and am really desperate. Also, did you make any changes since the update?

wellwoller avatar May 24 '23 13:05 wellwoller

What do you mean by string file? Meanwhile we're at capacitor 4 and Android SDK 13, but at least in my AndroidManifest nothing has changed since my initial post.

I just noticed that the following bit: <data android:scheme="@string/custom_url_scheme" /> is misleading. You have to replace "@string/custom_url_scheme" with you app ID, e.g. com.yourAppName.app. That was automatically replaced while I copied from my AndroidManifest to my GitHub-Post here.

wrzr123 avatar May 24 '23 13:05 wrzr123

@wrzr123 Sadly that doesnt solve my problem. Could you tell me the content of your capacitor.config.ts? Mine is: appId: 'de.mydomain.myapp.app', appName: 'myappname', webDir: 'www', bundledWebRuntime: false, server: { iosScheme: "ionic", hostname: "de.mydomain.myapp.app" }

wellwoller avatar May 24 '23 15:05 wellwoller

I don't think it helps but here you go

{
  "appId": "com.myAppName.app",
  "appName": "myAppName",
  "bundledWebRuntime": false,
  "npmClient": "npm",
  "webDir": "www/de",
  "android": {
    "path": "android/app/src/main/assets/public"
  },
  "plugins": {
    "SplashScreen": {
      "launchShowDuration": 0
    }
  },
  "cordova": {}
}

wrzr123 avatar May 24 '23 21:05 wrzr123