Shion のもくログ(旧: Shion の技術メモ)

使った技術のメモや、うまくいかなかった事とかを綴ります

PR

【iOS】Hyperion-iOS の導入メモ

概要

開発中のiOS アプリをインストールした端末で色々な設定を確認するには、willowtreeapps/Hyperion-iOS を導入すると手軽に出来ます。 あくまで開発用なので、くれぐれも本番アプリでは使わないようにお気をつけください。

使える機能

どのプラグインを導入するか次第ですが、下記のことがアプリ上で出来るようになります。

GitHub リポジトリのREADME より引用

First-Party Plugins

  • View Inspector
The View Inspector plugin allows you to inspect the properties of any view live within the app.
  • Measurements
The Measurements plugin allows you to measure the distance between any two views on the screen. No more guessing whether padding is correct-this plugin has you covered.
  • Slow Animations
Having trouble verifying an animation matches design? The Slow Animations plugin allows you to slow down all animations within the app to 75%, 50% or 25% the normal speed.

ライセンス

Copyright (c) 2017 WillowTree, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

GitHub リポジトリのLICENSE より引用

導入方法

CocoaPods, Carthage のどちらでもインストールできます。

CocoaPods

podfile に下記を記述しpod install するだけです。

use_frameworks!

pod "HyperioniOS/Core", :configurations => ['Debug']

#"Configurations => Debug" ensures it is only included in debug builds. Add any configurations you would like Hyperion to be included in.
pod 'HyperioniOS/AttributesInspector', :configurations => ['Debug'] # Optional plugin
pod 'HyperioniOS/Measurements', :configurations => ['Debug'] # Optional plugin
pod 'HyperioniOS/SlowAnimations', :configurations => ['Debug'] # Optional plugin

使い方

Hyperion の設定をしたアプリをインストールするだけです。 アプリ起動後に端末を振るか、右側にHyperion のドロワーを引っ張って表示することでメニューが出てきます。

プラグインの自作方法

まずPluginModule を継承したものを実装し、そのあとPlugin でラッピングする。

継承するPluginModule Hyperion メニュータップ後の挙動
HYPSnapshotPluginModule
HYPPluginModule

HYPPluginModule の例

PluginModule

import Foundation
import HyperionCore

class GoNativeRootHyperionPluginModule: HYPPluginModule {
    required init(with ext: HYPPluginExtensionProtocol) {
        super.init(with: ext)
    }

    override func pluginMenuItemTitle() -> String {
        return "Native 実装のルートへ遷移"
    }
}

extension GoNativeRootHyperionPluginModule: HYPPluginMenuItemDelegate {
    func pluginMenuItemSelected(_ pluginView: (UIView & HYPPluginMenuItemProtocol)!) {
        // タップ時の挙動を記述する
    }
}

Plugin

import Foundation
import HyperionCore

class GoNativeRootHyperionPlugin: NSObject, HYPPlugin {
    static func createPluginModule(_ pluginExtension: HYPPluginExtensionProtocol) -> HYPPluginModuleProtocol {
        return GoNativeRootHyperionPluginModule(with: pluginExtension)
    }

    static func pluginVersion() -> String {
        return "1.0.0"
    }
}

Tips

最前面にあるViewController を取得したい

ググってみるといくつか方法があるみたい。

  1. UIApplication から検索する(キーボード表示時の挙動は検証中) うまく行かないケースがあるらしいので要注意
    func findForegroundViewController() -> UIViewController? {
        var result = UIApplication.shared.keyWindow?.rootViewController
        while let candidate = result?.presentedViewController {
            result = candidate
        }
        return result
    }
    
  2. ViewTree を検索して取り出す
    extension UIResponder {
        func findFirstViewOrNil<T: UIResponder>() -> T? {
            var responder = self
            while let candidate = responder.next {
                if let result = candidate as? T {
                    return result
                }
                responder = candidate
            }
            return nil
        }
    }
    
    let vc: UIViewController = view.findFirstViewOrNil()
    

備考

参考文献

PR