tencent cloud

HTTPDNS

HTTPDNS 정책
개인 정보 보호 정책
데이터 처리 및 보안 계약
문서 HTTPDNS

WKWebView Practice

포커스 모드
폰트 크기
마지막 업데이트 시간: 2025-09-05 17:52:26

Solution Description

This solution uses NSURLProtocol to intercept requests. Specific steps:
1. Register a scheme via WKWebView's Private API to ensure NSURLProtocol can intercept requests in WKWebView.
2. Create a request based on the network library type you use.
3. Load a request using WKWebView.loadRequest.

Prerequisites

Sample Code

Note:
This documentation provides a reference solution for HTTPDNS integration in WebView scenarios. The example code is not official code for online production environments. Before integration, we recommend that you fully evaluate the content of this documentation to ensure the robustness of the solution meets your production standards.
If you need to refer to an example, you can refer to the source code of MSDKDnsHttpMessageTools and modify or reuse it based on business needs.
The complete example code is in WebViewController.m of the Demo.
// 1. Register NSURLProtocol to intercept requests
[NSURLProtocol registerClass:[MSDKDnsHttpMessageTools class]];

// 2. Register scheme
Class cls = NSClassFromString(@"WKBrowsingContextController");
SEL sel = NSSelectorFromString(@"registerSchemeForCustomProtocol:");
if ([cls respondsToSelector:sel]) {
// Requests via http and https, likewise via other schemes but must meet the URL Loading System
[cls performSelector:sel withObject:@"http"];
[cls performSelector:sel withObject:@"https"];
}

// 3. Use WKWebView.loadRequest
[self.wkWebView loadRequest:request];



도움말 및 지원

문제 해결에 도움이 되었나요?

피드백