Listen for the URL navigation event

When IAPMiniProgram SDK loads a URL, it uses WebView to load the page. If you want to open some URLs in your browser, you can intercept the URL navigation process of SDK by implementing the GriverUrlNavigationEvent interface. Each time when SDK loads a URL, it sends out the GriverUrlNavigationEvent interface and you can intercept it.

Procedures

To intercept for the mini program load url events, complete the following steps:

Step 1: Implement the GriverUrlNavigationEvent interface

Implement the GriverUrlNavigationEvent interface and write your business logic in the onStartUrlNavigationand allowLoadUrl method. Refer to the following sample on how to implement the interface.

copy
class DemoGriverUrlNavigationEventHandler : GriverUrlNavigationEvent {
    override fun onStartUrlNavigation(page: Page?, url: String?): Boolean {
        return false
    }

    override fun allowLoadUrl(page: Page?, url: String?): Boolean {
        return false
    }

    override fun onInitialized() {
    }

    override fun onFinalized() {
    }
}

Step 2: Register the DemoGriverUrlNavigationEventHandler class

Call the registerEventHandler interface to register the URL navigation event after initializing the SDK. For details, see the following GriverUrlNavigationEvent interface.

Code sample:

copy
Griver.registerEventHandler(
    GriverEventManifest(
        DemoGriverUrlNavigationEventHandler::class.java.name,
        Arrays.asList(GriverUrlNavigationEvent::class.java.name), Page::class.java
    )
)

Structures

GriverUrlNavigationEvent interface

The definition of the GriverUrlNavigationEvent interface is shown in the following code:

copy
interface GriverUrlNavigationEvent : GriverEvent {
    
    fun onStartUrlNavigation(page: Page?, url: String?): Boolean


    fun allowLoadUrl(page: Page?, url: String?): Boolean
}

Based on the definition, this interface provides the following methods:

Method Name

Description

allowLoadUrl

This method is called when SDK opens a new page through the URL.

  • If the return value is true, the SDK directly tells the WebView to load the URL, and the subsequent onStartUrlNavigation method is not triggered.
  • If the return value is false, the SDK continues to execute and trigger the onStartUrlNavigation method.

onStartUrlNavigation

This method will be called when SDK opens a new page through url.

  • If the return value is true, the SDK directly tells the WebView not to load the URL, and the logic behind the SDK is no longer executed.
  • If the return value is false, the SDK continues to execute the processing code for loading the URL, and whether the URL is loaded will be based on the SDK default logic.

allowLoadUrl method

Parameter

Data type

Required

Description

page

Page

Yes

Loads the current page object of the URL.

url

String

Yes

The URL address to be loaded.