Upgrade to Pro — share decks privately, control downloads, hide ads and more …

JavaScriptとSwift&JavaをつなげるCapacitorと、これからのWeb F...

Masahiko Sakakibara
December 01, 2019
3.6k

JavaScriptとSwift&JavaをつなげるCapacitorと、これからのWeb Frontend/Linking JavaScript with Swift&Java,
And the future of Web Frontend.

Masahiko Sakakibara

December 01, 2019
Tweet

More Decks by Masahiko Sakakibara

Transcript

  1. 6TJOH$BQBDJUPS $ npm install @capacitor/core @capacitor/cli $ npx cap init

    { "appId": “jp.rdlabo.capacitor", "appName": "ng-capacitor", "bundledWebRuntime": false, "npmClient": "npm", "webDir": “dist/ng-capacitor", "cordova": {} } capacitor.config.json
  2. 8IBUJT$"1#SJEHF7JFX$POUSPMMFS ˠ1PEpMF platform :ios, '11.0' use_frameworks! # workaround to avoid

    Xcode 10 caching of Pods that requires # Product -> Clean Build Folder after new Cordova plugins installed # Requires CocoaPods 1.6 or newer install! 'cocoapods', :disable_input_output_p aths => true def capacitor_pods # Automatic Capacitor Pod dependencies, do not delete pod 'Capacitor', :path => '../../ node_modules/@capacitor/ios' pod 'CapacitorCordova', :path => '../../node_modules/@capacitor/ios' # Do not delete end Podfile
  3. $"1#SJEHF7JFX$POUSPMMFSWJFXTQVCMJD func loadWebView() { let fullStartPath = URL(fileURLWithPath: "public").appendingPathComponent(startDir).appen dingPathComponent("index")

    if Bundle.main.path(forResource: fullStartPath.relativePath, ofType: "html") == nil { fatalLoadError() } hostname = bridge!.config.getString("server.url") ?? "\ (bridge!.getLocalUrl())" allowNavigationConfig = bridge!.config.getValue("server.allowNavigation") as? Array<String> CAPLog.print("⚡ Loading app at \ (hostname!)...") let request = URLRequest(url: URL(string: hostname!)!) _ = webView?.load(request) } CAPBridgeViewController.swift
  4. 8IBUJTAQVCMJDA $ npm install @capacitor/core @capacitor/cli $ npx cap init

    { "appId": “jp.rdlabo.capacitor", "appName": "ng-capacitor", "bundledWebRuntime": false, "npmClient": "npm", "webDir": “dist/ng-capacitor", "cordova": {} } capacitor.config.json 5PQVCMJDEJSFDUPSZ
  5. #SJEHF'VODUJPO if (win.webkit && win.webkit.messageHandlers && win.webkit.messageHandlers.bridge) { // ios

    platform postToNative = function iosBridge(data) { data.type = 'message'; win.webkit.messageHandlers.bridge.postMessage(data); }; capacitor.isNative = true; capacitor.isIOS = true; capacitor.platform = 'ios'; } capacitor/core/native-bridge.js
  6. .BJO"DUJWJUZFYUFOET#SJEHF"DUJWJUZ public class MainActivity extends BridgeActivity { @Override public void

    onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Initializes the Bridge this.init(savedInstanceState, new ArrayList<Class<? extends Plugin>>() {{ // Additional plugins you've installed go here // Ex: add(TotallyAwesomePlugin.class); }}); } } MainActivity.java
  7. #SJEHF"DUJWJUZMPBET#SJEHF$MBTT protected void load(Bundle savedInstanceState) { Log.d(LogUtils.getCoreTag(), "Starting BridgeActivity"); webView

    = findViewById(R.id.webview); … pluginManager = mockWebView.getPluginManager(); cordovaInterface.onCordovaInit(pluginManager); bridge = new Bridge(this, webView, initialPlugins, cordovaInterface, pluginManager, preferences); Splash.showOnLaunch(this); if (savedInstanceState != null) { bridge.restoreInstanceState(savedInstanceState); } this.keepRunning = preferences.getBoolean("KeepRunning", true); this.onNewIntent(getIntent()); } MainActivity.java
  8. #SJEHF$MBTTMPBETXFCWJFX Bridge.java public class Bridge { … public static final

    String DEFAULT_WEB_ASSET_DIR = "public"; public static final String CAPACITOR_HTTP_SCHEME = "http"; public static final String CAPACITOR_HTTPS_SCHEME = "https"; public static final String CAPACITOR_FILE_START = "/_capacitor_file_"; public static final String CAPACITOR_CONTENT_START = "/_capacitor_content_"; public Bridge(Activity context, WebView webView, List<Class<? extends Plugin>> initialPlugins, CordovaInterfaceImpl cordovaInterface, PluginManager pluginManager, CordovaPreferences preferences) { this.context = context; this.webView = webView; this.initialPlugins = initialPlugins; this.cordovaInterface = cordovaInterface; this.preferences = preferences; … this.loadWebView(); }
  9. #SJEHF$MBTTMPBETXFCWJFX Bridge.java private void loadWebView() { appUrlConfig = Config.getString("server.url"); String[]

    appAllowNavigationConfig = Config.getArray("server.allowN avigation"); … localUrl = scheme + "://" + authority; localServer = new WebViewLocalServer(context, this, getJSInjector(), authorities, html5mode); localServer.hostAssets(DEFAU LT_WEB_ASSET_DIR);
  10. #SJEHF$MBTTJTl+BWB4DSJQU*OUFSGBDFz Bridge.java public Bridge(Activity context, WebView webView, List<Class<? extends Plugin>>

    initialPlugins, CordovaInterfaceImpl cordovaInterface, PluginManager pluginManager, CordovaPreferences preferences) { this.context = context; this.webView = webView; this.initialPlugins = initialPlugins; this.cordovaInterface = cordovaInterface; this.preferences = preferences; // Start our plugin execution threads and handlers handlerThread.start(); taskHandler = new Handler(handlerThread.getLooper()); Config.load(getActivity()); // Initialize web view and message handler for it this.initWebView(); this.msgHandler = new MessageHandler(this, webView, pluginManager);
  11. #SJEHF$MBTTJTl+BWB4DSJQU*OUFSGBDFz MessageHandler.java public class MessageHandler { private Bridge bridge; private

    WebView webView; private PluginManager cordovaPluginManager; public MessageHandler(Bridge bridge, WebView webView, PluginManager cordovaPluginManager) { this.bridge = bridge; this.webView = webView; this.cordovaPluginManager = cordovaPluginManager; webView.addJavascriptInterface(this, "androidBridge"); }
  12. #SJEHF'VODUJPO if (win.androidBridge) { // android platform postToNative = function

    androidBridge(data) { win.androidBridge.postMessage(JSON.stringify(data)); }; capacitor.isNative = true; capacitor.isAndroid = true; capacitor.platform = 'android'; } capacitor/core/native-bridge.js
  13. 4Pʜ QVCMJD JOEFYIUNM 8FC #SPXTFS J04 "OESPJE 63-3FRVFTU +BWB4DSJQU XFC7JFX

    MPBE SFRVFTU TIPVME4UBSU-PBE8JUI3FRVFTU XFC7JFXMPBE6SM BQQ6SM +BWB4DSJQU*OUFSGBDF
  14. .PCJMF%FWJDF"1* %5PVDI "##::3FBM5JNF 3FDPHOJUJPO "&4 "DUJPO4IFFU "E.PC'SFF "E.PC1MVT "E.PC1SP "EKVTU

    "ENPC "MJQBZ "OBMZUJDT'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