2
Introduction to mPaas-WKWebview network blocking FAQ

image.png

1. Background

Native WKWebView executes network requests in a process independent of the app process, and the requested data does not pass through the main process. Therefore, it is impossible to intercept the request by using NSURLProtocol directly on WKWebView. However, because the offline package mechanism of mPaas strongly relies on network interception, based on this, mPaas uses the hidden api of WKWebview to register and intercept network requests to meet the business scenario requirements of offline packages. The reference code is as follows:

[WKBrowsingContextController registerSchemeForCustomProtocol:@"https"]

However, for performance reasons, WK's network request will remove the request body when passing data to the main process, resulting in the loss of the request body parameter after interception. In the offline package scenario, since the page resource does not require body data, the offline package can be used normally without being affected. But other post requests in the H5 page will lose the data parameter. In order to solve the problem of post parameter loss, mPaas solved the problem by injecting code into js and hooking the XMLHTTPRequest object in the js context. By assembling the content of the method in the JS layer, then passing the content to the main process through the messageHandler mechanism of WKWebView, storing the corresponding HTTPBody, and then notifying the JS side to continue the request. After the network request reaches the main process, the post request corresponds to The HttpBody is added, so that the processing of a post request is completed. For the overall process, please refer to Cui's previous sharing flowchart as follows:

1620448978855-64d98dd0-84d6-4cde-acf4-cae9b62f0779.png
figure 1

2. Problems encountered

Through the above mechanism, it not only satisfies the resource interception demands of offline packages, but also solves the problem of post request body loss. However, there are still some problems in some scenarios, which require developers to adapt.

2.1. The mixed use of mPaas container and tripartite container causes the request body of the tripartite container to be lost

2.1.1. Problem scenario

A typical scenario is that multiple wkwebview containers are integrated in the App at the same time. Common problems are as follows: After opening the mPaas container, the three-party WK page is opened, and the post request body parameter in the three-party WK page is lost. The reason is that the mPaas container is registered with the global network interception, which leads to requests in the three-party container to also go to the mPaas network interception, but because the mPaas container is not started, it cannot go to the mPaas global interception and completion body link normally, resulting in The body parameter is missing.

2.1.2. Solution

Unregister when the three-party container is created, and register it back when it is destroyed.

j//反注册
Class cls = NSClassFromString(@"WKBrowsingContextController");
    SEL sel = NSSelectorFromString([NSString stringWithFormat:@"unregisterSchemeForCustomProtocol:"]);
    if ([(id)cls respondsToSelector:sel]) {
        [(id)cls performSelector:sel withObject:@"http"];
        [(id)cls performSelector:sel withObject:@"https"];
    }
  
  //注册
    Class cls = NSClassFromString(@"WKBrowsingContextController");
    SEL sel = NSSelectorFromString([NSString stringWithFormat:@"registerSchemeForCustomProtocol:"]);
    if ([(id)cls respondsToSelector:sel]) {
    #pragma clang diagnostic push
    #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
        [(id)cls performSelector:sel withObject:@"http"];
        [(id)cls performSelector:sel withObject:@"https"];
    #pragma
    }

2.2. The mPaas container directly accesses the virtual domain name after opening the offline package, resulting in a white screen

2.2.1. Problem scenario

Similar to the first case above, multiple wkwebview containers are integrated in the App at the same time. At the same time, the three-party containers will also operate the global network interception, resulting in the failure of mPaas' network interception. The common problem phenomenon is as follows: After opening the tripartite container, after opening the offline package of mPaas, it is found that the offline package will directly access the virtual domain name through the online network, without going offline, resulting in a blank screen.

2.2.1. Solution

Refer to the solution to the first problem. When starting the mPaas container, confirm that the global network interception can take effect normally.

2.3. The sendBeacn request body in the mPaas container is missing

2.3.1. Problem scenario

Some customers have integrated Shence's embedded point jssdk in the container, and found that the body parameter in the embedded point request is missing. By looking at the source code, it is found that Shence jssdk is a request sent through navigator.sendBeacon. At present, the js request hooked in mPaas only supports XMLHTTPRequest, and sendBeacon does not support it, so the body parameter is lost after the network interception.

1620455454211-830ae69c-bf20-41ee-8ede-7e7ce80303b7.png
figure 2

2.3.1. Solution

The Shence SDK supports the designated ajax method to report the buried points, and the problem is solved after the report method is modified to ajax. You can refer to the post text [1] for details.

1620455672344-6ded744e-6937-4b73-99ff-3494b6ccadb6.png
image 3

[1] JavaScript SDK instructions: https://www.sensorsdata.cn/2.0/manual/js\_sdk.html

We are the Alibaba Cloud Intelligent Global Technical Service-SRE team. We are committed to becoming a technology-based, service-oriented, and high-availability engineer team of business systems; providing professional and systematic SRE services to help customers make better use of the cloud 、Build a more stable and reliable business system based on the cloud to improve business stability. We hope to share more technologies that help enterprise customers go to the cloud, make good use of the cloud, and make their business operations on the cloud more stable and reliable. You can scan the QR code below to join the Alibaba Cloud SRE Technical Institute Dingding circle, and more The multi-cloud master communicates about those things about the cloud platform.

Copyright Notice: content of this article is contributed spontaneously by Alibaba Cloud real-name registered users, and the copyright belongs to the original author. The Alibaba Cloud Developer Community does not own the copyright, and does not bear the corresponding legal responsibility. For specific rules, please refer to the "Alibaba Cloud Developer Community User Service Agreement" and the "Alibaba Cloud Developer Community Intellectual Property Protection Guidelines". If you find suspected plagiarism in this community, fill in the infringement complaint form to report it. Once verified, the community will immediately delete the suspected infringing content.

阿里云开发者
3.2k 声望6.3k 粉丝

阿里巴巴官方技术号,关于阿里巴巴经济体的技术创新、实战经验、技术人的成长心得均呈现于此。