Skip to main content

Synapse: iOS SDK

iOS WebView SDK for seamless communication between web and native codebase.

Table of Contents

Overview

This webview SDK is made to work in conjunction with its web counterpart npm module - https://www.npmjs.com/package/@tata1mg/synapse

Communication between web and native codebase is simplified with standarisation of communication rules. It provide various predefines bridges, with standard functionality which can be overridden. You can also define your own bridges.

Installation

CocoaPods is a dependency manager for Cocoa projects. For usage and installation instructions, visit their website. To integrate Synapse into your Xcode project using CocoaPods, specify it in your Podfile:

pod 'Synapse', :git => 'https://bitbucket.org/tata1mg/synapse-ios.git', :tag => '0.0.1'

Requirements

iOS 13

Bridges

  • Navigation
    • func webKitView(view: WebKitView, navigatorBridgeOnOpenWith data: [String: Any]?)
    • func webKitView(view: WebKitView, navigatorBridgeOnCloseWith data: [String: Any]?)
    • func webKitView(view: WebKitView, navigatorBridgeOnBackWith data: [String: Any]?)
    • func webKitView(view: WebKitView, navigatorBridgeOnReloadWith data: [String: Any]?)
  • Toolbar
    • func webKitView(view: WebKitView, hideToolbar data: [String: Any]?)
    • func webKitView(view: WebKitView, showToolbar data: [String: Any]?)
    • func webKitView(view: WebKitView, setToolbar data: [String: Any]?)
  • State
    • func webKitView(view: WebKitView, loaded data: [String: Any]?)
  • Interaction
    • func webKitView(view: WebKitView, copyToClipboard data: [String: Any]?)
    • func webKitView(view: WebKitView, allowsBackForwardNavigationGestures data: [String: Any]?)
  • Logger
    • func webKitView(view: WebKitView, onInfo data: [String: Any]?)
    • func webKitView(view: WebKitView, onError data: [String: Any]?)
  • PostMessage
    • func webKitView(view: WebKitView, postMessageBridgeCalledWith type: String, data: [String: Any]?)

Usage

  • WebKitView is a wrapper around WKWebView, could also be initialised using init funciton and passing custom bridges.
    init(frame: CGRect,
configuration: WKWebViewConfiguration,
bridges: [String: BridgeResolverProtocol])
  • Bridges are the simple strings which are registered to listen to particular events fired by web codebase. And its data is interpreted by respective resolver.
  • Some bridge delegates have default functionality which could also be overridden by setting a delegate for respective bridge.
    let webView = WebKitView(frame: .zero,
configuration: config,
bridges: customBridges)
webView?.webKitNaviagtionDelegate = self
webView?.setNavigatorBridgeDelegate(self)
  • Bridges defined above are by default registered to listen to respective web event fired using Synapse npm module from web codebase.
  • Custom bridge to register should have a respective resolver that should adhere to BridgeResolverProtocol.
  • Apart from Bridges there are WebKitViewNavigationProtocol functions which could be implemented in our codebase.
  • Sending event from native codebase to Web codebase is also standardised, you can use function
    func sendMessage(type: BridgeSendMessageProtocol, 
completion: ((Any?, Error?) -> Void)? = nil)
  • There are 2 predefined Message bridges defined in the framework for sending messages WebNavigator and WebState for now. You can also defined your own bridge for sending message by adhering to protocol BridgeSendMessageProtocol.