iOS Simulatorをアプリに組み込む

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on. →

iOS Simulatorをアプリに組み込む

Kishikawa Katsumi

December 02, 2026

Annual Pro sale · Save $30

Have presentations of your own?

Share with private links, password protection, and more control over your work.

Transcript

33 slides
  1. iOS Simulatorをアプリに組み込む CoreSimulator / SimulatorKit ‒ デバイスやiOSランタイムの一覧取得 ‒ デバイスの作成・削除・初期化 ‒

    シミュレータの起動・終了 ‒ アプリのインストール・起動・終了 ‒ 画面データ(IOSurface)へのアクセス ‒ 管理サービスとの通信(SimServiceContextなど)
  2. iOS Simulatorをアプリに組み込む Simulator.app / Device Hub.app ‒ 公式のSimulator.app / Device

    Hub.appも実は同じ仕組み ‒ Simulator.app / Device Hub.appも外側のUIを作っているだけ ‒ CoreSimulatorとSimulatorKitと通信して画面のデータを受け取り、マウスの イベントをタッチイベントに変換して送信している ‒ そのほか、Homeボタンや画面収録、iPhone Duoの開く・閉じるなど ‒ 自作してもSimulator.app / Device Hub.appとシミュレータ性能は同じ
  3. iOS Simulatorをアプリに組み込む CoreSimulator / SimulatorKit var kit = directory +

    "/Library/PrivateFrameworks/SimulatorKit.framework/SimulatorKit" if !FileManager.default.fileExists(atPath: kit) { kit = URL(fileURLWithPath: directory).deletingLastPathComponent() .appending(path: "/SharedFrameworks/SimulatorKit.framework/SimulatorKit") .path } for path in ["/Library/Developer/PrivateFrameworks/CoreSimulator.framework/CoreSimulator", kit] { guard dlopen(path, RTLD_NOW | RTLD_GLOBAL) != nil else { let detail = dlerror().map { String(cString: $0) } ?? "Unknown loader error" throw simulatorError("Cannot load \(path): \(detail)") } }
  4. iOS Simulatorをアプリに組み込む CoreSimulator / SimulatorKit let context = try Runtime.context(directory:

    directory) self.context = context // SimServiceContext let deviceSet = try Runtime.deviceSet(context) self.deviceSet = deviceSet // SimDeviceSet
  5. iOS Simulatorをアプリに組み込む CoreSimulator / SimulatorKit static func context(directory: String) throws

    -> AnyObject { guard let service = NSClassFromString("SimServiceContext") else { throw simulatorError("SimServiceContext is unavailable.") } typealias Call = @convention(c) (AnyObject, Selector, NSString, ErrorPointer) -> Unmanaged<AnyObject>? let receiver = service as AnyObject let (call, selector) = try method( receiver, "sharedServiceContextForDeveloperDir:error:", as: Call.self ) var error: NSError? guard let result = call(receiver, selector, directory as NSString, &error)?.takeUnretainedValue() else { throw error ?? simulatorError("Could not obtain the simulator context.") } return result }
  6. iOS Simulatorをアプリに組み込む CoreSimulator / SimulatorKit static func deviceSet(_ context: AnyObject)

    throws -> AnyObject { typealias Call = @convention(c) (AnyObject, Selector, ErrorPointer) -> Unmanaged<AnyObject>? let (call, selector) = try method(context, "defaultDeviceSetWithError:", as: Call.self) var error: NSError? guard let result = call(context, selector, &error)?.takeUnretainedValue() else { throw error ?? simulatorError("Could not obtain the default device set.") } return result }
  7. iOS Simulatorをアプリに組み込む CoreSimulator / SimulatorKit Swifty Pseudo Code let context

    = try SimServiceContext.shared(forDeveloperDir: directory) let deviceSet = try context.defaultDeviceSet()
  8. iOS Simulatorをアプリに組み込む デバイスオブジェクトと入出力ポート let devices = try Runtime.objects(deviceSet, "availableDevices") guard

    let device = try devices.first(where: { (try Runtime.object($0, "UDID") as? UUID)?.uuidString.caseInsensitiveCompare(udid) == .orderedSame }) else { throw simulatorError("Device not found in the default device set. Check the UDID.") } self.device = device ... guard let io = try Runtime.object(device, "io") else { throw simulatorError("Device IO is unavailable.") } self.io = io
  9. iOS Simulatorをアプリに組み込む デバイスオブジェクトと入出力ポート Swifty Pseudo Code let devices = deviceSet.availableDevices

    // [SimDevice] guard let device = devices.first(where: { ... }) else { // SimDevice throw DeviceNotFound() } let io = device.io // SimDeviceIOClient io.updateIOPorts()
  10. iOS Simulatorをアプリに組み込む デバイスオブジェクトと入出力ポート Swifty Pseudo Code let displays = ports

    .filter { $0.portIdentifier == "com.apple.framebuffer.display" } .map { $0.descriptor } .filter { $0.responds(to: NSSelectorFromString("framebufferSurface")) }
  11. iOS Simulatorをアプリに組み込む デバイスオブジェクトと入出力ポート 識別子 関連する機能 com.apple.framebuffer.display 画面の画像データ com.apple.framebuffer.server 画面・フレームバッファの管理 com.apple.display.captureservice

    画面キャプチャ com.apple.accelerator.metal Metalによる描画 com.apple.accelerator.iosurface IOSurfaceを扱う描画基盤 com.apple.CoreSimulator.HID.LegacyHID タッチ・キーボード・ボタンなどの入力 com.apple.CoreSimulator.Audio.HostRoute ホストMacとの音声経路 com.apple.SimStreamProcessor.default ストリーム処理 com.apple.gputools.service.simulatorなど GPUの解析・デバッグ用サービス
  12. iOS Simulatorをアプリに組み込む 画面の表示 Swifty Pseudo Code let observationID = UUID()

    for display in displays { display.registerScreenCallbacks( withUUID: observationID, callbackQueue: .main, frameCallback: { readFrame() }, surfacesChangedCallback: { _, _ in readFrame() }, propertiesChangedCallback: { _ in readFrame() } ) }
  13. iOS Simulatorをアプリに組み込む 画面の表示 Swifty Pseudo Code func readFrame() { let

    surfaces = displays.compactMap { $0.framebufferSurface } guard let surface = surfaces.max(by: { $0.width * $0.height < $1.width * $1.height }) else { return } onFrame(surface) }
  14. iOS Simulatorをアプリに組み込む 画面の表示 try connection.connect(udid: CommandLine.arguments[1], developerDirectory: developerDirectory()) { guard

    let self else { return } CATransaction.begin() CATransaction.setDisableActions(true) self.screen.layer?.contents = nil self.screen.layer?.contents = surface CATransaction.commit() if !self.receivedFrame { self.receivedFrame = true let size = "\(IOSurfaceGetWidth(surface)) × \(IOSurfaceGetHeight(surface))" self.window.title = "Simulator Display — \(size)" } } [weak self] surface in
  15. iOS Simulatorをアプリに組み込む 座標の変換とイベントの送信 typealias MouseMessage = @convention(c) ( UnsafeMutablePointer<CGPoint>, UnsafeMutablePointer<CGPoint>?,

    UInt32, UInt, CGSize, UInt ) -> UnsafeMutableRawPointer? guard let symbol = dlsym(handle, "IndigoHIDMessageForMouseNSEvent") else { throw SimulatorError.symbolNotFound } let IndigoHIDMessageForMouseNSEvent = unsafeBitCast(symbol, to: MouseMessage.self) let message = IndigoHIDMessageForMouseNSEvent( &point, nil, 0x32, // ButtonEventTargetDigitizer eventType.rawValue, CGSize(width: 1, height: 1), 0 )
  16. iOS Simulatorをアプリに組み込む 応用例:Web iOS Simulator ‒ IOSurfaceをH.264にエンコード ‒ WebRTCで配信 ‒

    マウスイベントやボタンイベントを送信 ‒ 「ホーム画面に追加」でフルスクリーン表示 ‒ CloudFlare Tunnelでインターネットに公開 ‒ (Optional)CloudFlare ZeroTrustで認証

More Decks by Kishikawa Katsumi

Other Decks in Programming