WebViewController

webViewController 是轻应用的容器,除了能够支持普通webView的功能外,还能通过拦截器访问原生接口以达到扩展H5功能的目的。

拦截器 Interceptor

拦截器可以扩展H5的功能,让H5访问原生接口。

所有拦截器都要实现 InterceptorProtocol, 然后进行注册。

例如 ToastInterceptor: (底座已经集成)

  • 1.先实现 InterceptorProtocol
import Foundation
import Toaster

public class ToastInterceptor: InterceptorProtocol {

    public static func perform(url: URL, controller: UIViewController) -> Bool {
        if url.scheme == Interceptor.scheme, url.host == "toast" {
            Toast(text: url.getParams()?["text"].stringValue).show()
            return true
        }
        return false
    }
}
  • 2.再在 application(_ application: didFinishLaunchingWithOptions launchOptions:)去中注册

    Interceptor.register(ToastInterceptor.self)

  • 3.如此就能在H5调用Toast功能了

      const paramsMap = { text: '我是一个原生的 Toast' };
      const params = encodeURIComponent(JSON.stringify(paramsMap));
      const url = `cenarius://toast?params=${params}`;
      location.href = url;
    

    cenarius是协议的scheme, toast是host,params是参数的key,value就是后面encode过的JSON字符串。

results matching ""

    No results matching ""