xcode 在真正的 Apple Watch 上调试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34695235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:24:25  来源:igfitidea点击:

Debugging on real Apple Watch

iosxcodedebuggingwatchkitapple-watch

提问by Matte.Car

I updated my app for Apple Watch, but I should have weighed it too much and now it's definitely slow. I can't understand the reasons of this slowdown so I would like to debug app but I don't know how to do that because in the simulator app works normally, slowdowns manifest only on the device.

我为 Apple Watch 更新了我的应用程序,但我应该考虑太多,现在它肯定很慢。我无法理解这种减速的原因,所以我想调试应用程序,但我不知道该怎么做,因为在模拟器应用程序中正常工作,减速仅在设备上表现出来。

I tried running app directly from xCode on Watch but once xCode launched the installation of the app on the Watch, it stops the run so I can't see what happens while running (for instance when run certain breakpoint or otherwise do us debug).

我尝试在 Watch 上直接从 xCode 运行应用程序,但是一旦 xCode 在 Watch 上启动应用程序的安装,它就会停止运行,因此我看不到运行时会发生什么(例如,当运行某个断点或以其他方式进行调试时)。

Does anyone have an idea of what's the right way to debug on a physical Apple Watch?

有没有人知道在物理 Apple Watch 上调试的正确方法是什么?

P.S.: For instance, this code:

PS:例如,这段代码:

func clearScreen() {
    firstPicker.setSelectedItemIndex(0)
    secondPicker.setSelectedItemIndex(0)

    defaultLabels()
}

func defaultLabels() {
    feesLabel.setText(NSLocalizedString ("FEES", comment: "Commissioni"))

    clearAllMenuItems()
    addMenuItemWithItemIcon(.Decline, title: NSLocalizedString("CAN_CEL", comment: ""), action: "clearScreen")
    if DefaultParameter.sharedInstance.wishMode == true {
        addMenuItemWithImage(UIImage(named: "will")! , title: NSLocalizedString("WILL_RECEIVE", comment: ""), action: "willWishButtonPressed")
        receivedLabel.setText(NSLocalizedString ("DESIRED_AMOUNT", comment: ""))
    } else {
        addMenuItemWithImage(UIImage(named: "wish")! , title: NSLocalizedString("WISH_RECEIVE", comment: ""), action: "willWishButtonPressed")
        receivedLabel.setText(NSLocalizedString ("RECEIVED_AMOUNT", comment: ""))
    }
}

takes around 7 seconds to run...

大约需要 7 秒才能运行...

回答by lehn0058

I do not recommend trying to debug your Apple Watch app by pressing the Run button in Xcode. It is very unpredictable as to whether or not it will work. Here is how I debug on the Apple Watch and it works for me every time:

我不建议尝试通过按 Xcode 中的“运行”按钮来调试 Apple Watch 应用程序。至于它是否会起作用,这是非常不可预测的。这是我在 Apple Watch 上调试的方法,它每次都对我有用:

  1. Connect your iPhone to your computer running Xcode
  2. Run your app on your iPhone from Xcode
  3. Click the stop button in Xcode
  4. On your Apple Watch, make sure that the new app update has been transferred over (Note that if you didn't make any changes to the watch app, a new one doesn't seem to get copied over at the moment).
  5. Launch your watch app from your watch (no Xcode yet!)
  6. In Xcode select the Watch Extension from the schemes, make sure your device is selected from device list, should show your phone + your Apple Watch.
  7. In Xcode, click "Debug -> Attach to process" and pick your watch extension's process. Note that you will see a process for both your watch app and the watch extension. Make sure to pick the watch extension
  8. On your watch, tap on some UI element, such as a button.
  9. Your watch app will have a loading spinner appear in the middle of the screen. This spinner continues until the watch can connect to Xcode. Sometimes this take a few minutes (around 3 for me!). When the spinner stops then you should be able to debug in Xcode normally.
  1. 将您的 iPhone 连接到运行 Xcode 的计算机
  2. 从 Xcode 在 iPhone 上运行您的应用程序
  3. 单击 Xcode 中的停止按钮
  4. 在您的 Apple Watch 上,确保新的应用程序更新已转移(请注意,如果您没有对手表应用程序进行任何更改,则目前似乎不会复制新的应用程序)。
  5. 从您的手表启动您的手表应用程序(还没有 Xcode!)
  6. 在 Xcode 中,从方案中选择 Watch Extension,确保您的设备是从设备列表中选择的,应该显示您的手机 + 您的 Apple Watch。
  7. 在 Xcode 中,单击“调试 -> 附加到进程”并选择您的手表扩展程序的进程。请注意,您将看到手表应用程序和手表扩展程序的流程。确保选择手表扩展
  8. 在您的手表上,点击一些 UI 元素,例如按钮。
  9. 您的手表应用程序将在屏幕中间出现一个加载微调器。这个微调器一直持续到手表可以连接到 Xcode。有时这需要几分钟(对我来说大约 3 分钟!)。当微调器停止时,您应该能够在 Xcode 中正常调试。

The only trick is to be patient when waiting for Xcode to connect in step 8. It is definitely slow to do, but gives you far more performance information that running on the simulator.

唯一的窍门是在第 8 步等待 Xcode 连接时要有耐心。这样做肯定很慢,但为您提供了比在模拟器上运行的性能信息多得多的信息。

回答by fredpi

Sounds like a familiar watchOS bug for me, so I presume you didn't do anything wrong.

对我来说,这听起来像是一个熟悉的 watchOS 错误,所以我认为你没有做错任何事。

You should try to delete the watch app from your watch using the Watch app on your phone, then run it again.

您应该尝试使用手机上的手表应用程序从手表中删除手表应用程序,然后再次运行它。

You will definitely need some patience, so try it multiple times if it doesn't work. I had the experience that it worked after a few attempts.

你肯定需要一些耐心,所以如果它不起作用,请多次尝试。我有过几次尝试后它奏效的经验。