Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Moodle App Deep Linking

From MoodleDocs
Revision as of 13:02, 14 July 2021 by David Mudrak (talk | contribs) (Text replacement - "<code>" to "<syntaxhighlight lang="php">")

Moodle App (Ionic 3 - legacy)


Moodle App 3.7


Overview

The Moodle app supports being launched using a Custom URL Scheme. In version 3.6.1 or older of the app, the format to do so was:

<syntaxhighlight lang="php">moodlemobile://link=https://mysite.es/mod/choice/view.php?id=8

However, in Moodle app 3.7.0 we introduced a better way to define these links. The new format will let you specify the URL to open, the username to use and also a token to authenticate the user.

Please notice that these links will only work if the app is installed in the device. E.g. if you click one of these links in Safari in an iOS device without the app installed, an error will be displayed.

Format

The new format to create the links is like this:

<syntaxhighlight lang="php">moodlemobile://https://username@domain.com?token=TOKEN&privatetoken=PRIVATETOKEN&redirect=http://domain.com/course/view.php?id=2

The only data required is the base URL of your site (in the example above, <syntaxhighlight lang="php">https://domain.com).

Site URL

As mentioned above, this is the only required param. It should be the base URL of the site (wwwroot). For example, you can use this URL to open your site in the app:

<syntaxhighlight lang="php">moodlemobile://https://domain.com

In the example above, if the site <syntaxhighlight lang="php">https://domain.com isn't stored in the app, the user will be redirected to the credentials screen to access the site.

Username

If you want the app to be opened with a certain username you can specify it in the URL:

<syntaxhighlight lang="php">moodlemobile://https://username@domain.com

In the example above, if the user <syntaxhighlight lang="php">username and the site <syntaxhighlight lang="php">https://domain.com aren't stored in the app, the user will be sent to the credentials screen to access the site (the username input will be prepopulated, but the user will be able to change it if he wants to). If the app has several users of this site stored, including "username", the right user will be loaded.

Token and Private token

If you specify a token in the URL, the user will be authenticated automatically in the app. This is really useful for external apps and systems, e.g. you can use this feature for SSO systems. The user token can be found in the database table "mdl_external_tokens".

The private token is used by the app to auto-login the user in the browser, and it will only be used if you also specify a token in the URL. If you specify a privatetoken but not a token, the privatetoken will be ignored. The private token can also be found in the database table "mdl_external_tokens".

It isn't recommended to include the token and privatetoken in links that will be rendered by a browser or apps that can be inspected. Please notice that anyone with the token will be able to authenticate as the user the token belongs to.

Example:

<syntaxhighlight lang="php">moodlemobile://https://domain.com?token=TOKEN&privatetoken=PRIVATETOKEN

The token has priority over the username parameter. E.g. if you specify username "u1" but the token belongs to user "u2", the user u2 will be authenticated in the app.

Redirect

The redirect parameter indicates which page you want to open in the app. Example:

<syntaxhighlight lang="php">moodlemobile://https://domain.com?redirect=http://domain.com/course/view.php?id=2

This link will open the course with ID=2 in the app. Please notice that the app doesn't support all Moodle URLs, only some of them are supported.

The redirect URL should belong to the same site as the base URL. E.g. if the base URL is <syntaxhighlight lang="php">http://domain.com but the redirect is <syntaxhighlight lang="php">http://anothersite.com/..., an error will be displayed.

The redirect parameter can be a relative URL based on the base URL. The example above can also be written like this:

<syntaxhighlight lang="php">moodlemobile://https://domain.com?redirect=/course/view.php?id=2

See also

Custom URL Scheme Cordova plugin used by the app