GRVNetworkRequestAllowDelegate

The iOS SDK enforces strict security policies through the following restrictions:

  • JSAPI restrictions: JSAPIs can communicate with HTTPS domains only.
  • web-view restrictions: The web-view component can only load resources using HTTP or HTTPS schemes.

To suit development or business needs, you can customize network access to enable the following:

To customize access, implement the GRVNetworkRequestAllowDelegate protocol. This topic details the protocol's implementation and specifications.

Before you begin

Ensure that the integrated iOS IAPMiniProgram SDK is version 2.65.0 or later. For more information, see SDK release notes.

Procedure

To customize network access, take the following steps:

Step 1: Implement the GRVNetworkRequestAllowDelegate protocol

Create a class that implements the GRVNetworkRequestAllowDelegate protocol. Within the class, implement the following methods as needed:

  • shouldAllowHttpRequest: Allows the aforementioned JSAPIs to communicate with specific HTTP URLs.
  • shouldAllowScheme: Allows the web-view component to load resources using non-HTTP/HTTPS URL schemes.

For more information about the protocol and methods, refer to GRVNetworkRequestAllowDelegate. The following code provides a sample implementation:

copy
class DemoNetworkAllowedDelegateImpl: NSObject, GRVNetworkRequestAllowDelegate {
    
    // Allow specific HTTP URLs.
    // To allow a domain (and all its sub-URLs), extract the domain from the `url` parameter.
    func shouldAllowHttpRequest(_ url: String) -> Bool {
        if let url = URL(string: url),
           let host = url.host?.lowercased(),
           host == "www.example.com" {
            return true
        }
        return false
    }

    // Allow custom URL schemes (e.g., mailto).
    func shouldAllowScheme(_ url: String) -> Bool {   
        if let scheme = NSURL.init(string: url)?.scheme as? String,
           scheme == "mailto"
        {
            return true
        }
        return false
    }
}

Step 2: Configure networkAllowedDelegate

After initializing the SDK, create an instance of GRVExtensionDelegate and assign the implementation (for example, DemoNetworkAllowedDelegateImpl in the sample) to the networkAllowedDelegate property. Refer to the following code for a sample configuration:

copy
let extensionDelegate = GRVExtensionDelegate()
extensionDelegate.networkAllowedDelegate = DemoNetworkAllowedDelegateImpl()

Protocol

GRVNetworkRequestAllowDelegate protocol

The GRVNetworkRequestAllowDelegate protocol defines methods for the super app to customize network access for specific JSAPIs and the web-view component. When a request is restricted by the default security policies, the SDK calls the super app's delegate methods to check the custom allowlist, determining whether to allow or block the request. Refer to the following code for the protocol definition:

copy
@protocol GRVNetworkRequestAllowDelegate <NSObject>

@optional

- (BOOL)shouldAllowHttpRequest:(NSString *)url;

- (BOOL)shouldAllowScheme:(NSString *)url;

@end

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

Method

Description

shouldAllowHttpRequest:

Called by the SDK to check if requests to a specific HTTP URL are allowed for the following JSAPIs:

For more information, refer to shouldAllowHttpRequest:.

shouldAllowScheme:

Called by the SDK to check if a non-HTTP/HTTPS URL scheme is allowed in the web-view component. For more information, refer to shouldAllowScheme:.

shouldAllowHttpRequest: method

Parameters

Parameter

Data type

Required

Description

url

NSString

Yes

The absolute HTTP URL that the JSAPI requests.

Return value

Value

Description

YES

Return this value if the HTTP URL is in the allowlist, which allows the JSAPI call.

NO

Return this value if the HTTP URL is not in the allowlist, which blocks the JSAPI call.

shouldAllowScheme: method

Parameters

Parameter

Data type

Required

Description

url

NSString

Yes

The absolute URL whose scheme is neither HTTPS nor HTTP. The super app needs to parse the URL's scheme to match the custom allowlist.

Return value

Value

Description

YES

Return this value if the parsed URL scheme is in the allowlist, which allows web-view to load the URL with UIApplication.

NO

Return this value if the parsed URL scheme is not in the allowlist, which blocks web-view from loading the URL.