ios - 如何调试 iOS 扩展 (.appex)?

标签 ios xcode swift lldb ios8-share-extension

如何通过扩展使日志打印出现在 Xcode 的 lldb 调试器中?

最佳答案

简单的回答:

  • 不会打印日志消息,但是您可以在断点处停止,然后使用 lldb 打印所有内容。
  1. 运行您的应用
  2. 在应用运行时,转到调试 -> 按 PID 或名称附加到进程

enter image description here

  1. 写下您的扩展名(因为 bundle-id 不起作用!?),然后单击“附加”。

enter image description here

  1. 然后以您可以在设备上执行此操作的任何方式运行您的扩展程序。
  2. 等待 Xcode 的调试器在断点处停止扩展,但有些可能需要调用 waitForDebugger(这是一个自定义函数,请参见下面的逻辑)。

等待调试器示例

public static func isDebuggerAttached() -> Bool {
    // Buffer for "sysctl(...)" call's result.
    var info = kinfo_proc()
    // Counts buffer's size in bytes (like C/C++'s `sizeof`).
    var size = MemoryLayout.stride(ofValue: info)
    // Tells we want info about own process.
    var mib : [Int32] = [CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid()]
    // Call the API (and assert success).
    let junk = sysctl(&mib, UInt32(mib.count), &info, &size, nil, 0)
    assert(junk == 0, "sysctl failed")
    // Finally, checks if debugger's flag is present yet.
    return (info.kp_proc.p_flag & P_TRACED) != 0
}

@discardableResult
public static func waitForDebugger(_ timeout: Int = 30000) -> Bool {
    var now: UInt64 = DispatchTime.now().uptimeNanoseconds
    let begin = now
    repeat {
        if isDebuggerAttached() {
            // Wait a little bit longer,
            // because early breakpoints may still not work.
            Thread.sleep(forTimeInterval: 3.0)
            return true
        }
        Thread.sleep(forTimeInterval: 0.1)
        now = DispatchTime.now().uptimeNanoseconds
    } while Double(now - begin) / 1000000.0 < Double(timeout);
    return false;
}

关于ios - 如何调试 iOS 扩展 (.appex)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33524051/

相关文章:

IOS - UITableView 卡住

objective-c - 如何隐藏 AVFoundation 调试日志?

ios - 警报 View 按钮

ios - 按“后退”按钮执行 segue

ios - 如何在同步运行时使异步方法超时

ios - 如何在 iOS 应用程序中处理自动旋转,其中一个 View 不应自动旋转,而一个 View 应该自动旋转?

ios - 如何将我的标签栏图标连接到我的屏幕?

ios - 为什么我不能用 swift 更改 if 语句中的变量?

ios - 当我添加自动布局时, View 消失了

xcode - 在 Xcode 中调试 Quicklook 插件