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

iOSのKeyboard Extensionで日本語入力できるキーボードアプリを作ろう! - ...

Keita Miwa
September 03, 2023

iOSのKeyboard Extensionで日本語入力できるキーボードアプリを作ろう! - iOSDC2023

Keita Miwa

September 03, 2023
Tweet

Other Decks in Programming

Transcript

  1. azooKey • ೔ຊޠೖྗػೳ • ͔ͳ׽ࣈม׵ • ༧ଌม׵ • ֆจࣈɾه߸ྨͷೖྗ •

    Ϣʔβࣙॻ • ֶश • ޡΓగਖ਼ • ΩʔϘʔυΞϓϦػೳ • ணͤସ͑ • ϥΠϒม׵ • ΧελϜΩʔ • ΧελϜλϒ • ίϐʔཤྺอଘ • छʑͷઃఆ https://github.com/ensan-hcl/azooKey OSSʂ
  2. ࿩͢͜ͱ • ΩʔϘʔυΞϓϦͷ֓ཁ • ೔ຊޠೖྗ • ࢓૊Έ • ࡞ͬͯΈΑ͏ •

    ύοέʔδͷ঺հ • ࣙॻ • ΩʔϘʔυͷUIʢͷਏ͍ͱ͜Ζʣ • ·ͱΊ
  3. ΩʔϘʔυΞϓϦ = Keyboard Extension • ඪ४ΩʔϘʔυͱಉ༷ʹར༻Ͱ͖Δ • γεςϜͷ΄ͱΜͲͲ͜Ͱ΋ಈ͘ • ػೳ໘͸੍໿ڧΊ

    • ϓϥΠόγʔ • Ϧιʔε • ίϯςφΞϓϦʹಉࠝͯ͠഑෍͢Δ • ೔ຊޠೖྗରԠͷΞϓϦ͸গͳ͍
  4. ೔ຊޠೖྗ͍Ζ͍Ζ • ͍ΘΏΔʮ͔ͳ׽ࣈม׵ʯʢͻΒ͕ͳΛͨ͘͞Μଧͬͯม׵͢Δ΍ͭʣ • ଟ͘ͷ؀ڥͰσϑΥϧτͷ೔ຊޠೖྗํ๏ • ࠓճ͸͜ΕΛओʹѻ͍·͢ • SKKʢSimple Kana

    to Kanji conversion programʣ • iOSʹ΋FlickSKKɹɹɹɹɹɹɹɹɹɹɹɹɹɹɹɹɹɹ͕͋Γ·͢ • ׽ࣈ௚઀ೖྗ • ׽ࣈΛ௚઀ೖྗ͢Δํࣜɺෳࡶͳม׵ΤϯδϯཁΒͣ https://github.com/codefirst/FlickSKK
  5. ๲େͳީิ • Ұͭͷೖྗʹରͯ͠ɺ ͨ͘͞Μͷީิ͕ߟ͑ ΒΕΔ • ؒʹ߹͍ͦ͏Ͱ͢ • ਅʹѪ૝࿹૥ •

    ϚχΞҠૹDeath ೖྗɿ·ʹ͍͋ͦ͏Ͱ͢ ຐ Ҡૹ ؒʹ߹͍ ͦ͏ Ͱ͢ ډ ʹ ਅ ձ͍ ү Ѫ૝ ૯ग़ ࿹ ϚχΞ %FBUI ૥
  6. ๲େͳީิ • Ұͭͷೖྗʹରͯ͠ɺ ͨ͘͞Μͷީิ͕ߟ͑ ΒΕΔ • ؒʹ߹͍ͦ͏Ͱ͢ • ਅʹѪ૝࿹૥ •

    ϚχΞҠૹDeath ೖྗɿ·ʹ͍͋ͦ͏Ͱ͢ ຐ Ҡૹ ؒʹ߹͍ ͦ͏ Ͱ͢ ډ ʹ ਅ ձ͍ ү Ѫ૝ ૯ग़ ࿹ ϚχΞ %FBUI ૥
  7. ๲େͳީิ • Ұͭͷೖྗʹରͯ͠ɺ ͨ͘͞Μͷީิ͕ߟ͑ ΒΕΔ • ؒʹ߹͍ͦ͏Ͱ͢ • ਅʹѪ૝࿹૥ •

    ϚχΞҠૹDeath ೖྗɿ·ʹ͍͋ͦ͏Ͱ͢ ຐ Ҡૹ ؒʹ߹͍ ͦ͏ Ͱ͢ ډ ʹ ਅ ձ͍ ү Ѫ૝ ૯ग़ ࿹ ϚχΞ %FBUI ૥
  8. ࣮ࡍͷܭࢉ ຐ Ҡૹ ؒʹ߹͍ ͦ͏ Ͱ͢ ډ ʹ ਅ ձ͍

    ү Ѫ૝ ૯ग़ ࿹ ϚχΞ %FBUI ૥ • ީิ͸ͨ͘͞Μ͋Δ • είΞ͕࠷େʹͳΔ Α͏ͳީิΛ୳ࡧ • ݟ͚ͭΒΕΔͳΒํ๏ ͸ͳΜͰ΋͍͍ ೖྗɿ·ʹ͍͋ͦ͏Ͱ͢
  9. ࣮༻͢Δʹ͸ʁ • ෳ਺ͷީิʢN-BestղʣΛఏҊͰ͖ΔΑ͏ʹ͢Δ • จઅ෼ׂɺ෦෼ม׵ɺ༧ଌม׵ͳͲΛαϙʔτ͢Δ • ม׵ͷߴ଎Խ • ΩϟογϡΛར༻ͯ͠෦෼తͳมߋʹରͯ͠ߴ଎ͳॲཧΛߦ͏ •

    ଥ౰Ͱ͸ͳ͍ީิΛͳΔ΂͘ૣ͘ʹࣺͯΔ • ࣙॻҾ͖ͷߴ଎ԽɾলϝϞϦԽ • LOUDS Trieͱ͍͏σʔλߏ଄͕໾ཱͭʢଞʹ΋͍ΖΜͳѹॖख๏͕͋Δʣ
  10. ೖྗঢ়ଶ؅ཧ • Ϣʔβ͸ΩʔϘʔυΛܦ༝ͤͣʹςΩετΛૢ࡞Ͱ͖Δ • ςΩετͷબ୒ • ΧʔιϧҠಈ • ϖʔετ •

    ͜ΕΒΛద੾ʹݕ஌ͯ͠ॲཧ͠ͳ͍ͱɺϢʔβʹݟ͍͑ͯΔը໘ͱ ΩʔϘʔυͷ಺෦ঢ়ଶ͕ζϨͯ͠·͏ • ΩʔϘʔυΛܦ༝͠ͳ͍ૢ࡞ʹରԠͨ͠ೖྗঢ়ଶͷ؅ཧ͕ඞཁ
  11. ೖྗঢ়ଶ؅ཧɿಓ۩ • Keyboard Extension͕औΕΔͷ͸ ҎԼͷ3ͭͷΈ • Χʔιϧલͷจࣈྻ: var documentContextBeforeInput: String?

    • બ୒͍ͯ͠Δจࣈྻ: var selectedText: String? • Χʔιϧޙͷจࣈྻ: var documentContextAfterInput: String? • Keyboard Extension͕Ͱ͖Δͷ͸ ҎԼͷ3ͭͷΈʢ˞ʣ • ΧʔιϧҐஔ΁ͷจࣈͷૠೖ: func insertText(String) • Χʔιϧલͷจࣈͷ࡟আ: func deleteBackward() • ΧʔιϧͷҠಈ: func adjustTextPosition(byCharacterOffset: Int) • Keyboard Extension͕ड͚औΕΔ௨஌͸ҎԼͷ2ͭͷΈ • Ϣʔβ͕ʮԿ͔͢Δʯલ: func textWillChange(UITextInput?) • Ϣʔβ͕ʮԿ͔ͨ͠ʯޙ: func textDidChange(UITextInput?) ʢ˞ʣਖ਼֬ʹ͸΋͏গ͋͠Γ·͕͢ɺೖྗ؅ཧʹ࢖͑Δ΋ͷ͸͜Ε͚ͩͰ͢
  12. ೖྗঢ়ଶ؅ཧɿϢʔβʹΑΔΧʔιϧҠಈͷݕग़ྫ 1. Ϣʔβ͕ʮԿ͔͢Δʯ௨஌Λड͚औΔ 2. ͜ͷλΠϛϯάͰʮΧʔιϧͷલޙʯͷςΩετΛอଘ(before) 3. Ϣʔβ͕ʮԿ͔ͨ͠ʯ௨஌Λड͚औΔ 4. ͜ͷλΠϛϯάͰʮΧʔιϧͷલޙʯͷςΩετΛอଘ(after) 5.

    beforeͷࠨӈΛ࿈݁ͨ͠΋ͷͱɺafterͷࠨӈΛ࿈݁ͨ͠΋ͷ͕ಉ͔֬͡ೝ →ಉ͡ͳΒɺςΩετΛมԽͤ͞Δૢ࡞Ͱ͸ͳ͘ɺΧʔιϧҠಈͱ෼͔Δ →beforeͱafterͷࠨͷจࣈ਺Λൺֱ͠ɺ͕ࠩҠಈͨ͠จࣈ਺ͱ෼͔Δ ˞࣮ࡍʹ͸ʮϖʔετʯʮΧοτʯͳͲෳ਺ͷૢ࡞ͷՄೳੑΛಉ࣌ʹௐ΂Δ
  13. APIͷ೉͍͠ಈ࡞① • υΩϡϝϯτʹॻ͔Ε͍ͯͳ͍ڍಈ͕ଟ͍ • ΧʔιϧͷҠಈ: func adjustTextPosition(byCharacterOffset: Int) • υΩϡϝϯτʹΑΔͱҾ਺͸Ҡಈ͢Δจࣈ਺

    • ࣮ࡍ͸จࣈ਺Ͱ͸ͳ͘UTF-16ίʔυϙΠϯτ਺ • ᷖᮣʹ࣮૷͢ΔͱֆจࣈͷؒʹΧʔιϧ͕ೖͬͨΓ͢Δ • Ҡಈ͢ΔલʹΧʔιϧલޙΛ֬ೝ͠ɺίʔυϙΠϯτΛ਺͑Δඞཁ͕͋Δ
  14. APIͷ೉͍͠ಈ࡞② • ೖྗதͷจࣈྻʹ৭͚ͭΔ΍ͭ: func setMarkedText(String?, selectedRange: NSRange) • ৭͸͔֬ʹ͕ͭ͘ɺϢʔβૢ࡞͕Ұ੾τϥοΫग़དྷͳ͘ͳΔ •

    બ୒͍ͯ͠Δจࣈྻ: var selectedText: String? • υΩϡϝϯτʹΑΔͱબ୒͍ͯ͠Δจࣈྻ • ࣮ࡍ͸ʮબ୒͍ͯ͠Δจࣈྻɻͨͩ͠3ߦҎ্ͷจͷ৔߹͸࠷ॳͱ࠷ޙͷ 1ߦΛ͚ͬͭͨ͘΍ͭʯͱ͍͏ڍಈ • ࢓༷ʢςΩετϑΟʔϧυଆͷ࣮૷ʹΑΔʣ
  15. ϝϞϦ੍ݶ • ΩʔϘʔυΞϓϦʹڐ͞ΕͨϝϞϦ͸60MBલޙʢOSɾσόΠεґଘʣ • ্ݶ෇ۙͰ͸ಈ࡞͕ۃ୺ʹ஗͘ͳΔͷͰɺ࣮࣭తʹ͔ͦ͜Β10~20MBఔ౓ݮΔ • ϝϞϦΛফඅ͢ΔཁҼ • ࣙॻσʔλ •

    ணͤସ͑ͷը૾ • ֆจࣈϑΥϯτͷΩϟογϡ • Ұ౓දࣔ͞Εͨֆจࣈ͸ϝϞϦʹϑΥϯτͷΩϟογϡΛ࢒͢ʢ࡟আෆೳʣ • ֆจࣈશͯͰ10MBҎ্͍͔࣋ͬͯΕΔ→ֆจࣈΩʔϘʔυͰམͪΔʂ
  16. ͓͢͢Ίͷࢿྉ • Creating a custom keyboard | Apple Developer Documentation,

    https://developer.apple.com/ documentation/uikit/keyboards_and_input/creating_a_custom_keyboard/ • ޻౻୓, খদ߂޾, ՖԬढ़ߦ, ޲Ҫ३, & ాാ༔հ. (2011). ౷ܭత͔ͳ׽ࣈม׵γεςϜ Mozc. ݴ ޠॲཧֶձୈ 17 ճ೥࣍େձൃද࿦จू, 948-951., https://anlp.jp/proceedings/annual_meeting/2011/pdf_dir/C4-3.pdf • iOSΧελϜΩʔϘʔυͷ࡞Γํ - Qiita, https://qiita.com/Kyome/items/4257de4e33d3647085e6 • ಙӬ୓೭. (2012).೔ຊޠೖྗΛࢧ͑Δٕज़ʕมΘΓଓ͚Δίϯϐϡʔλͱݴ༿ͷੈք, ٕ ज़ධ࿦ࣾ, https://gihyo.jp/book/2012/978-4-7741-4993-6 • KeyboardKit/KeyboardKit - GitHub, https://github.com/KeyboardKit/KeyboardKit