Skip to content

Instantly share code, notes, and snippets.

@y-takagi
Last active April 16, 2021 00:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save y-takagi/28a87fa60fbf661afce10aba5031dd9a to your computer and use it in GitHub Desktop.
Save y-takagi/28a87fa60fbf661afce10aba5031dd9a to your computer and use it in GitHub Desktop.
Ionic Framework の紹介

Ionic Framework の紹介

Ionic Framework とは

Ionic(Drifty Co.)チームが開発している、Webテクノロジー(HTML、CSS、JavaScript)を使って、モバイルとデスクトップアプリケーションをつくるためののUIフレームワーク。

現在、AngularとReactを公式サポートしており、Vueのサポートも開発中。

Other Major Cross-Platform Frameworks

  • React Native
    • Created by Facebook
    • Reactで記述
  • NativeScript
    • Created by Progress
    • JavascriptまたはTypescriptなどのAltScriptで記述
  • Flutter
    • Created by Google
    • Dartで記述

How it Works

s_2E845B3BC67B84CD1FADE62336DF65606503A703F5D13D2E788594DCE0895246_1580127242330_reactnative-nativescript-2

※ iOSにはJavaScriptCore.frameworkがSDKで提供されている ※ FacebookがAndroid用にポートしたライブラリ facebook/android-jsc

How Ionic Works

s_2E845B3BC67B84CD1FADE62336DF65606503A703F5D13D2E788594DCE0895246_1580215223076_reactnative-nativescript-Page-2-2

プロジェクト作成

AngularCLIで新規プロジェクトを作成

npx -p @angular/cli ng new ionic-angular

Ionic追加

npx ng add @ionic/angular

iOSプロジェクトの追加

# Build app
npm run build

# Add capacitor
npx ng add @capacitor/angular

# Init
npx cap init

# Edit capacitor.config.json
{ ..., "webDir": "dist/appName" }

# Add iOS option
npx cap add ios

# Open Xcode
npx cap open ios

Androidプロジェクトの追加

# Add Android option
npx cap add android

# Open Android Studio
npx cap open android

プラグインの作成 (Android編)

GreetPlugin

  1. Convert Java to Kotlin

Right click on MainActivity file and select convert

  1. Create GreetPlugin
@NativePlugin
class GreetPlugin : Plugin() {
  @PluginMethod
  fun greet(call: PluginCall) {
    val value = call.getString("value")
    val result = JSObject().apply { put("value", "Hello, $value") }
    call.resolve(result)
  }
}
  1. Register Plugin
class MainActivity : BridgeActivity() {
  public override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    // Initializes the Bridge
    init(savedInstanceState, listOf(GreetPlugin::class.java))
  }
}
  1. Call greet function
import { Component, OnInit } from '@angular/core';
import { Plugins } from '@capacitor/core';
    
const { GreetPlugin } = Plugins;
    
@Component({
  selector: 'app-root',
  template: `
    <h1>{{message}}</h1>
    `
})
export class AppComponent implements OnInit {
  message = '';
    
  async ngOnInit() {
    const result = await GreetPlugin.greet({ value: "Yukiya"});
    this.message = result.value
  }
}

FullscreenPlugin

  1. Create FullscreenPlugin
@NativePlugin
class FullscreenPlugin : Plugin() {
  @PluginMethod
  fun fullscreen(call: PluginCall) {
    bridge.executeOnMainThread {
      activity.window.decorView.systemUiVisibility = (
        View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
          or View.SYSTEM_UI_FLAG_FULLSCREEN
          or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
          or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
      )
      call.resolve()
    }
  }
}
  1. Register Plugin
class MainActivity : BridgeActivity() {
  public override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    // Initializes the Bridge
    init(
      savedInstanceState, 
      listOf(GreetPlugin::class.java, FullscreenPlugin::class.java)
    )
  }
}
  1. Call fullscreen function
import { Component, OnInit } from '@angular/core';
import { Plugins } from '@capacitor/core';
    
const { GreetPlugin, FullscreenPlugin } = Plugins;
    
@Component({
  selector: 'app-root',
  template: `
    <h1>{{message}}</h1>
    <button (click)="fullscreen()">fullscreen</button>
    `
})
export class AppComponent implements OnInit {
  message = '';
    
  async ngOnInit() {
    const result = await GreetPlugin.greet({ value: "Yukiya"});
    this.message = result.value
  }
    
  fullscreen() {
    FullscreenPlugin.fullscreen();
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment