Debugging App When Launched by Push Notification - ios

I am currently developing an app that receives Push Notifications. I have this all working 100% through a PHP page. There are several different types of push notifications my app can receive. The PHP handles this and sends different packets of information to my app which are all received just fine.
However, when the users "views" the notification and my app launches, I obviously want to take a different action than I would if the user just launched the app manually - and on top of that, different actions depending on the push notification type. I have got this working fine... structurally.
One of my push types is supposed to open a UIView that makes several connections to several different servers and negotiates data back and forth. This UIView works fine when, for example, triggered from the main menu - however when my push notification is triggering this UIView to appear, the socket connections are not acting as expected.
Now my question is not about the sockets, but more so - How do you debug such a problem?? From what I can tell (I am relatively new) when the app launched from a push notification there is no way to link that execution to the debugger / console / etc... I am having a very difficult time trying to debug the code using UIAlertViews, as there are many lines of communication back and forth between the various servers.
Any advice you have for me would be greatly appreciated.

In Xcode < 4.0 (for Xcode >= 4, see answer by delirus below), you can now configure Xcode to attach the debugger to the app after you launch it, instead of launching the app through the debugger. This lets you debug things that vary based on the launch state of your application, such as URL schemes, pasteboards, and push notifications.
In Xcode look in the Source bar, and below Targets there will be Executables.
Bring up the inspector for your app in executables.
Click on the Debugging tab tab in the inspector.
Uncheck "Start executable after starting debugger"
Check the "Wait for next launch/push notification"
Now when you click debug from Xcode instead of launching the app a window will display telling it is waiting for the app to launch. You then launch the app normally on the phone and Xcode attaches to it

For Xcode 4 you have to:
Edit your active scheme via
"Schemes" dropdown. (⌘+<)
Than choose your product - 'Run
MyApp.app' on the left.
Select 'Info' tab on the right.
And finally choose "Wait for
MyApp.app to launch" option.
More here in "Customize Executables in the Scheme Editor" section.
EDIT:
In case you miss logs in GDB, see Louis Gerbarg's comment to his answer.

For Xcode 10, 9, 8, 7 & 6:
Select the Scheme on the toolbar (just left of the device you're debugging with)
Choose Edit Scheme
Select Run in the left panel
For the Launch option, select Wait for executable to be launched
Close the Scheme Editor and Run your Xcode project
Now you can debug when launching from a notification, custom url or by touching the app icon.

I'm not working with push notifications, so I don't know the exact working of push notifications. However, I'd try mimicking the receipt of a push notification by letting the main method of your program pretend a notification was posted if possible, or calling the application:didFinishLaunchingWithOptions: UIApplicationDelegate method from within the applicationDidFinishLaunching: method.
HTH - Cheers, Daan

In Xcode 6:
connect iPhone to Macbook with USB
launch the app on the iPhone by tapping the icon
In Xcode menu, Debug > Attach to Process > Pick your App

For Xcode 11 and 10, you can do the following:
Select Scheme option.
Choose Edit Scheme.
Select Run option from the left panel
In the launch part, select wait for executable to be launched, then close the dialog.
Build and run the project. You can now launch the app either by tapping the app icon, or by tapping on the notification message.

Related

How to check if an app is running in iOS device from a mac terminal

I am trying to launch ios app from python script/terminal using ios-deploy. This is a test app so it might crash and need to relaunch multiple times.
I want to check if this app is currently running or not on the connected device, for my relaunch logic to work. But I am kind of stuck here.
I know idevicesyslog which can capture logs and then parsing logs I can check if my app is currently running or not. But isn't there any sophisticated way to just check if an app is currently running or not in device. Xcode/instruments can get the list of processes via GUI in connected device but I want a command line utility so that I can take certain action.
You can check app or debug app using safari.
Open safari.
And If develop option not available on top navigation bar then open preferences of safari and in advance tab tick show develop option in below that screen.

App freezes several seconds before start on device

I'm a free developer and I use my swift application on my iPhone 7. I'm using Swift 3 and iOS 10.
So the problem is, sometimes (like 2 of 10 times) when I start the app, iOS freezes like 5 seconds before my app actualy comes to the screen. In this time I only see the app icon is in the highlighted state. This only happens with my own app, not with other apps.
My app also does nothing special on startup and this only happens on the real device, the debugger in Xcode always starts immediately.
So is this a normal behavior? And does it work properly when I decide to put it on the App Store?
Thanks in advance.
I have had this happen to me before. No, this does not happen when it is put out on the App Store. One way to get around this is to use TestFlight. You can upload your archive to apple and select on the 'TestFlight' tab of your app. Click on internal testing and then click on your email and the version of the app you want to test. You should then get an email on your Apple ID telling you to test the app, and it will let you download it. Good question :)
To test the same version which would have been uploaded to the store quickly, change the build configuration to release. To do so, click on your app's name (top left), press edit schema, click the build configuration dropdown, also deselect debug executable. (make sure to turn it back when you're going to debug, or create a new schema with the above settings to let you switch quickly in the future)
You might also try using instruments, that lag on startup might happen if you're trying to load many things in memory on your initial view controller's viewDidLoad or app delegate's didFinishLaunchingWithOptions load, especially when trying to load big files such as images, videos or large plists. You might want to try using instruments (the time profiler instrument specifically) in order to check it out.

Debug WatchKit on real Apple Watch - Nothing happens

I am struggling with debugging my WatchKit Extension/App on a real Apple Watch. Debugging both the iPhone App and the WatchKit Extension using the simulator the simulator is not problem:
Select the WatchKit App Profile and run in Simulator ==> App is launched on in Watch Simulator and I can use breakpoints in the Extension code to debug.
To debug the iPhone app as well I launch the app in the simulator and attach the debugger manually ==> I can use breakpoints in the iPhone Code to debug.
It is no problem to observe both the work of the iPhone app and the watch app. At least not in the simulator.
I would like to do the same thing on a real Apple Watch. But when I select the WatchKit App Profile and my real iPhone (instead of the Simulator) and click on "Run" nothing happens. This means Xcode seems to build and start the app but nothing happens on the devices. The status field in Xcode shows:
Building MyApp WatchKit App: MyApp WatchKit Extension
Building MyApp WatchKit App: Finishing...
Running MyApp on My iPhone 6
This is all. No debug Window, breakpoints are ignored or do not work and the app is not launched neither on the iPhone or on the Apple Watch.
I found other questions about problems with debugging on real devices (e.g. here) but they all deal with installation and signing issues. In my case both the iPhone App and the WatchKit App are installed without any problem. When I click the app icon on the Watch I can start and use the app. Problem is that I cannot debug this process.
Why do I need to debug the process on real devices? Well there is one thing I cannot test using the simulator: What happens when the Watch App tries to contact the iPhone app using openParentApplication:reply: when the iPhone app is not already running? This works perfectly in the simulator but on real devices the Watch Apps seems to get no reply from the iPhone app and simply waits forever.
I already found hints to solve this but without being able to debug the Watch App and to see how the code is executed I cannot be sure what is going on...
thank you for your answers. I finally managed to get the debugging running (most of the time) on my real Apple Watch. However it is quite cumbersome and not very reliable. There is not enough space in the comments so I will use this answer to describe my solution. Maybe it is help full to other:
Make sure, the App on your Watch is not running (as BalestraPatrick described). Launch the app and hold the side button a few seconds to bring up the "Turn off your Watch" dialog. Hold the side button for another few seconds to close the app and to return to the Watch homescreen.
Make sure the iPhone is not locked.
Select the WatchKit App target and your real iPhone and run the project.
Running the Watch App resulted quite often in an error SPErrorInvalidBundleNoGizmoBinaryMessage. Re-starting Xcode and cleaning both the Watch App and the App target solved this.
If the build of the Watch App succeed there will be a short message in Xcode but nothing on your iPhone or Watch. If you have made changes to the Watch App it will take a few seconds to refresh the app on the Watch. This is indicated by the progress-circle overlay over the app icon. If you made no changes ore once the app has been transfered: Launch the watch app manually by tapping the icon. There will be no automatic launch.
In most cases Xcode will recognize the app launch and attach its debugger to it. This will allow to use breakpoints, inspect the code, etc.
In my case I whanted to inspect how the iPhone App handles the application:handleWatchKitExtensionRequest:reply: call when the app was not active before. This is important because this cannot be done using the simulator. If the app takes to long to handle the request the Watch will receive no valid response.
After following the steps described above Xcode is only attached the watch app and will not hold on breakpoints in the iPhone app code. To do this, one has to manually attach the Debugger to the iPhone app process that is started when the watch app sends its call.
To be able to attach the debugger I added a delay to the apps main function: [NSThread sleepForTimeInterval:5]:
Select function in Watch App that will start the call to the iPhone App
The iPhone App will be launched in background. The delays gives you 5 seconds to attach the debugger.
Choose Debug\Attach To Process\Likely Targes\Your iPhone Appin Xcode to attach the debugger.
After the 5 seconds delay the process will continue and you will be able to use breakpoints in your iPhone app code as well.
Do not forget do remove the delay code when you finished testing :-)
NOTE:
You will not be able to see NSLog output (or any console output at all) from the iPhone App since attaching the debugger does NOT re-route the console output.
Happy testing with this awesome new Apple product :-P
I am having the same kind of issue but to improve a little bit the debugging experience I usually restart my devices a few times. Try to restart your Apple Watch or your iPhone.
Before launching your app from Xcode, make sure that the app is completely closed on your Apple Watch (not stuck in the loading screen for example). To do that, you have to force close the app: enter your app, keep the side button pressed until the menu to turn off the watch appears, then press the same side button for a few more seconds until the watch will go back to the homescreen and force close your app.
Now you can try to build and run the app from Xcode and it should work more reliably.
watchOS 3+:
The force quit is done by pressing and holding the side button (the button just below the Digital Crown) until the shutdown screen appears, and then
let go of the side button, then press and hold the Digital Crown.
Here's a technique that seems to be be the most reliable for me, even though it still only works about 25% of the time (Xcode 7 beta 4, 7A165t):
Run the phone target in debug (on your actual iPhone of course)
While the iPhone app is still running, switch to the watch target and run that in debug.
Keep an eye on your watch homescreen as the app installs. Once the app has installed, tap it to open it. This will sometimes "kickstart" the watch app and allow the debugger to catch it. At this point you should be able to debug both your iPhone app and watchOS app together.
This question has different answers. All are good.
The reason is: in my experience there are at least two different problems that prevent debugging on a real watch. Both require a different strategy.
This answer tries to help you to decide when to use which strategy.
XCode installs, tells you it is running for a short time and then stops showing "running...".
start with the answer of BalestraPatrick:
restart your devices.
Do not forget to restart your iPhone. This did the trick the first time I had this problem.
XCode installs, tells you it is running for a longer period of time: "running <projectName> on Apple Watch of <yourname>.
start with the answer of Jay Hickey or Andrei Herford
starting the watch app manually probably does the trick.
I also had times when XCode was able to start my watch app without any help from me.
Bonus:
Starting with XCode 7.1 it seems XCode doesn't always compile all necessary Swift files (more often than before). This is not watch specific, it also applies to iOS apps. The symptoms can look similar to those of this question. -> clean your project and then restart XCode.

iOS: How to debug "freshly launching" an app from a URL

When launching an app from a URL, there is a distinction between whether the URL is freshly launching an app, or if it's resuming an app that has been put into a suspended state.
My question is, how do I debug the process of a "fresh launch" from a URL? Hitting "Run" in Xcode automatically opens the app. Then, I have to suspend the app to reach Safari and access my test site. But if I close my suspended app, Xcode is no longer attached to it and I'm unable to debug.
These steps can be followed on the device. Founded at this link.
Run the app from Xcode to install it on your device and then stop it
from Xcode.
Force quit the app from the app switcher UI on the device.
Navigate to the scheme for the project in Xcode. Under the Run
section's Info tab, there is a radio button for "Wait for executable
to be launched". Make sure this is checked instead of the
"Automatically" option.
Run the app from Xcode. It will not open
on the device, but the debugger will wait for it to open and then it
will attach to it.
On the simulator as suggested by Marc-Alexandre Bérubé.
Run your app on the simulator.
Force quit the app on the simulator. Press cmd+shift+(hit h twice). Swipe the app up.
Open your terminal and enter this xcrun simctl openurl booted http://yourdomain/path.
You need to configure your Xcode app scheme to wait for the app to be launched manually.
Here is how you do it: http://blogmobile.itude.com/2013/09/03/how-to-debug-deep-linking-in-ios/.
You then need to copy the launch URL and paste it to Mobile Safari. This will launch your app, and make Xcode get attached to the running app process.
While the answers that mention activating "Wait for executable to be launched" are good in theory, for various reasons these didn't work for me. On the simulator, though Xcode claimed it had "attached", no logs or breakpoints were ever shown. On-device I was running iOS 13, building from Xcode 10, and using Xcode 11 to load, so I didn't have any debugging symbols. That was more of a personal problem, I suspect on device debugging may have worked.
What worked best for me was just opening up Console.app on my mac, and using the OSLog APIs to get a solid stream of caveman debugging from the simulator.
Run the app from Xcode on the simulator.
Then immediately quit it on the simulator via the app launched (Cmd-Shift-H * 2)
Launch the app by tapping a deep link I sent to myself via the Messages app
Observe my logs in Console.app
It's not pretty, but it is highly reliable.
It's also worth mentioning, that you can protect this useful feature by testing cold start deep-linking via XCUITest. You can call app.terminate in your XCUITests, then open a deep link (typically via a static web page you've setup, I use public GitHub wikis for this) and write tests and assert behaviors just like normal.

How do I profile an iOS application startup from push notification?

I want to profile my iOS app when it starts up from the user swiping a push notification, from the not-running state.
How do I set up Instruments to attach to a particular app as soon as it starts up on the device (as opposed to starting the app via the "Profile" button in Xcode)?
You can change the process target in Instruments to "All Processes" on the device and then filter down to just your app.
I named my example app test in the screenshot above.

Resources