Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug JavaScript in IOS Action App Extension?

Tags:

javascript

ios

With iOS, Action App Extension, one could run a JavaScript code against HTML. However, after quite some search on Google, I have not found any document explaining how to debug in this situation (insert a break point or simply add/view console out statement).

like image 808
BSharer App - Share Books Avatar asked Jul 27 '16 17:07

BSharer App - Share Books


1 Answers

It's not too hard, although recently I've had an issue with the page showing up when connected to the simulator. In that case I just ended up using my phone directly. (Apparently you need to start desktop Safari after the iOS Simulator has started to inspect "remote" Simulator sessions)

  • Ensure that on the device Settings->Safari->Advanced->Web Inspector is on
  • (Make sure you've Trusted the computer from the device)
  • Start MobileSafari on your device by running from Xcode and choosing Safari -- this isn't necessary but you can decode the iOS Objective-C/Swift code as well. You can just start MobileSafari manually and it will be visible in desktop Safari.

  • Start Safari on your desktop, make sure Show Develop menu in menu bar is on in Preferences.

  • In the Develop menu you will see your device name, say 'BSharer's iPhone'
  • select the page name underneath your device name, say 'en.m.wikipedia.org - Wikipedia'.

You are now debugging that device page on your desktop.

There are options to 'Automatically start debugger etc...', but I've found that when I tried these my code would not execute.

You can't place debugger statements in the app extension JavaScript as it isn't injected until your App Extension starts. Instead I would add a debugger; statement to my app extension JavaScript code which will pause the debugger in the app extension JavaScript code at the debugger; statement.

Start your app extension by selecting it in the Share menu, set any other breakpoints you are interested in, then hit the continue button on the debugger.

You will also catch uncaught exceptions of course.

The official documentation for this is here.

Make sure you remove the debugger; line from production code.

like image 139
J. Longman Avatar answered Nov 04 '22 21:11

J. Longman