Testing a closed ios app - ios

I'm currently trying to handle different application states (closed, background or in a different tab of the app) however when I try to test how the app works when it is closed and receives a push notification(double click home and force close the app then reopen it) I'm not sure where I'm going wrong in the code. Since I'm reopening the app from the phone itself and not xcode I can't test to see which method isn't being reached because no output is available in the console. Is there any way to test a situation like this or simulate a force close event in xcode so that when i re run the app on the phone it also launches in xcode?
I appreciate any responses.
Cheers!

If you force close app or stop (from xcode) then it close the connection with xcode. Then if you open it from phone then it will not make connection with xcode. You must run it from xcode. And there is no difference in opening app from phone or running from xcode. App's flow will be same in both case. So what you want to test that which methods get calls and in which sequence that you can check by rerunning the project.
Update:
Select the Scheme on the toolbar (just left beside from your device or simulator list)
Choose Edit Scheme
Select Run in the left panel
For the Launch option, select Wait for executable to be launched
Refer this link for more details

Since I'm reopening the app from the phone itself and not xcode I can't test to see which method isn't being reached because no output is available in the console.
In Xcode, hold down the Option key and choose Product->Run..., and then edit the Run scheme to use the "Wait for executable to be launched" option. You should then be able to choose Run in Xcode, and then open your app by some other means, such as responding to a notification, and Xcode will still connect and let you debug.

To debug the process after restarting the app again, attach the Xcode debug console to the running process.
In Xcode do:
Debug > Attach to process > [select your process]

Related

How can I debug an iOS app executed in mobile not launched by Xcode?

I am developing an application for iOS in Objective C with Xcode. This application schedules local push notifications every 6 hours and it is crashing when I open the push notifications.
I need to debug the error to solve it. I can have the mobile connected to Xcode, but as the app is executed from local push notification I can't see the error messages on my Xcode debug console, as I haven't launched my app from Xcode.
Is there a way to have the iOS device connected to my Mac and see what error is happening?
I know that I can go to the mobile settings to view logs, but these logs are too ambigous for me aren't giving any error.
In addition to opening the console log as described by #saurabhgoyal you can tell Xcode to wait for your app to launch and then attach the debugger to it when it does.
Select the scheme you're using to build your app, select edit scheme, and click on the run icon. Then Look for a pair of radio buttons titled "Launch" and select the one with the name "Wait for executable to be launched."
Then when you run your app in Xcode it builds it and installs it on the device but does not launch it.
When your notification fires and the app launches the debugger attaches to your app and you can debug as normal (except that NSLog statements don't print to the debug console any more - an annoyance.)
Yes there is a way to see device logs on Mac.
Connect your iOS device to the Mac system using USB
Launch Xcode–>Window–>Devices
Select your device from the left panel
Now you can see the logs on the screen including the background activities.
In order to save the running logs.
Reproduce the issue or start working on your device on a the app you wanted to capture the logs. After the issue is reproduced click on the Save Console icon bottom right corner Xcode screen
For more details please visit this link
Hope this Helps!
Check your crash log
1.Launch Xcode on your desktop machine.
2.Open the Xcode Devices window. (Window menu -> Devices and Simulators, or Cmd-Shift-2.)
3.Find your device in the left sidebar, then select “device logs”.
Choose a Chrome crash (or multiple crashes) and select “Export” at the bottom of the Organizer window.

Stop xcode before run?

I have a bug which occurs because an user installs the app but doesn't run it. He adds some files via itunes and only then runs the installed app.
So I need to repeat the same actions. Breakpoints should help but I don't know where to insert them. NOTE: main.m is not the starting point so I can't just place breakpoint in it - some code is executed before it (maybe because of submodules).
How to solve this issue?
Edit the scheme, in the Run Info find the Launch section at the bottom and select Wait for executable to be launched.
Now, when you run, Xcode will install the app and wait. You can then do whatever you want with the device / iTunes and when you manually start the app Xcode will kick in.
You can build your app from the "Product" menu without running. Then open Xcode's "Devices" window, select your target device, and drag your app from the "Products" section of the Project navigator to the Installed Apps part of the device window. That should get you an installed build that hasn't been launched.

How to debug app when launch by push notification in Xcode

I am using Xcode 5. I am working with push notifications in iOS. I am getting satisfying results for background mode and foreground mode that can be debugged easily on an iOS device.
But problem is when app is in closed state and launched by push notification tap but I don't know how to debug in this situation. I know the solution for Xcode 4 but not for Xcode 5.
So is there any solution for Xcode 5 and debugging the app when launching it by push notifications?
Provide steps to debug in this situation.
Edit your project scheme and set "Launch" to "Wait for *.app to be launched manually".
Then Run the project or hit "cmd+R". The app will not be launched automatically, but the debugger will attach to the process as soon as the app launches. So send your test push notification, and launch the app from the push.
Here you go!
it takes few seconds:
1- go to edit schema
2- select run section from left list
3- then in info tab goto part launch options
4- select option: wait for executable to be launched
5- then close the window
6- run the app : the app will build but not launched because it's waiting for you to launch the app manually
7- at this time send your notification to your device
8- tap on the notification and your app will launched and the debugger goes to breakpoints you attached in lines of code
You can also connect to the app in debug mode using the debug menu of Xcode : Debug > Attach to process by PID or name (start typing the name of your target and Xcode will autocomplete for you)
As soon as the app is launched or if it is already launched, the debugger will be plugged. Really helpful in two cases :
notification/location debug
you didn't launch the app through Xcode but you have a strange bug you want to understand

How to debug iOS 8 extensions with NSLog?

- (void)viewDidLoad
{
NSLog(#"%s", __func__);
// ...
}
in viewDidLoad of an iOS 8 extension. the NSLog outputs nothing in Xcode. NSLog works as usual in the container app though.
How can I get output from debug messages from an extension?
Debugging works for app extensions.
It works on simulator too.
If your app ext crashes in the simulator, you may find it is not easy to restart your app ext. Restarting your simulator is a quick solution.
Steps to debug an app extension:
Run the container app. In this step, Xcode uploads the container app and app extension to the device or simulator.
Stop the container app. This step is important when you debug in simulator. If you don't do it, Xcode will tell you the simulator is in use.
In Xcode, press menu Debug -> Attach to Process -> By Process Identifer (PID) or Name..., input the app ext's identifier, e.g. com.abc.ContainerApp.MyExtension, to start debugging. Don't forget to set break points. (Update on Aug 25, 2014: you can input MyExtension(your extension's name) directly.)
In the device or simulator, open your app extension.
Updates on Aug 23, 2014:
I found the debugging steps above do not work well on Xcode 6 beta 6 with iOS 8 SDK beta 5 on the simulator.
Solution:
Run your extension in the simulator.
Xcode menu Debug -> Attach to Process -> Choose "MyExtension(your extension's name)" in System section in the menu.
The breakpoints work. But I don't know why logs do not show in the output window.
I'm having this problem too. It works for me if you go in your Simulator under the menu Debug -> Open System Log...
From here you can see all the logs of the iPhone Simulator (included your extension's logs).
NSLog is working perfectly.
You just don't see what is being logged in the debug area of Xcode because the Xcode debugger isn't attached to your extension. Extensions are nearly completely independent from their containing app. They have separate bundle identifiers, for example, and they also are separate processes on the OS.
I have had varied success in getting Xcode to attach to extensions. Supposedly it seems it would attach automatically, and it appears in the debug navigator as "Waiting to attach", but never attaches.
Sometimes, I am able to run my extension target in Xcode:
And then have the option to choose what application to run my extension in. In this case, I would choose its recommendation of "today", which is notification center.
And then it would sometimes attach the debugger to my extension. Note this method only works on physical devices, it seems.
If it doesn't attach you can use the manual attachment method in #VinceYaun's answer,
I have also had varied success using other methods of attachment. Most have been unsuccessful, and it seems they are just bugs that will be fixed at a later date.
To view your log messages you go to Window -> Devices in the top bar and select your device. You can bring up the device log from the bottom of that window. If you are testing on a simulator, you can use #BalestraPatrick's answer.
Some of the bugs have been fixed in Beta 2, and my guess is that eventually the debugger will attach automagically when launching the extension.
Update: In the iOS 8 Beta 4 release notes:
Extensions
Fixed in beta 4
Extensions sometimes fail to launch when debugging from Xcode.
When Extension with UI is killed, it relaunches and is not dismissed.
Sometimes your Sharing or Action extension can hang.
Redeploying an extension may disable it in Notification Center.
I'm having this problem too. Xcode never attaches my debugger to the extension or displays NSLog messages. If you attach your Xcode debugger manually to your extension process, at least breakpoints work like a charm:
Debug->Attach to process->Your extension name (mine was "com.example.MyExtensionApp.MyExtension")
Create scheme for your extension
Run scheme
In a dialog choose container app
Enjoy
It works for me :)
Xcode 8 is able to debug extensions:
Choose the extension scheme in the combo next to the stop button and run it.
Select the parent application in the dialog that appears.
Result: breakpoints and log work as usual.
What finally allowed me to see the log in the debugging area, based on Michael's suggestion and Apple's documentation, is:
Build & Run the app extension in the simulator and, when prompted for a host app, choose the specific app that you're going to call the extension from. In my case I was launching my Action extension from Safari by pulling up a share sheet on a PDF.
What wasn't working before was following other people's suggestions of using Today as the host but then leaving that app and going over to Safari to call my extension. I no longer even need to run my containing app first before running the extension.
From Apple's Documentation:
In your extension scheme’s Run phase, you specify a host app as the executable. Upon accessing the extension through that specified host’s UI, the Xcode debugger attaches to the extension.
I actually got the logs to run quite simply in Xcode 6.3. Firstly, build and run the containing app. Once the containing app is running on the device, build and run the app extension by changing the scheme to the app extension.
Apple has documentation directly related to debugging, profiling and testing your app extension.
A trick which works for me (although it is quite an ugly one) is to place a dummy UILabel somewhere in the bottom corner of my extension. I usually call it logLabel. It is then possible to update the text of this label with any log statement that you want to get logged. Such approach is not very good if you need to log statements from instances of different classes. And, obviously, it clutters your UI.
However, if you have a fairly simple widget and you don't mind about slight cluttering of the UI this does the trick. I have tried all other solutions outlined in this discussion and, sadly, none of them worked for me.
The only way debugging works for me is by selecting Debug->Attach To Process By PID or Name
Then enter the PID not the extension name.
You can find the PID by running the extension on a device, go to Window->Devices. Find your device and viewing the console. When you see the name of your extension, its followed by 5 digit number. That is the PID
I also put a bunch of NSLog's in the extension in order to find the PID as well.
This is on xCode 7
Encounter the same problem about extension for NSLog, and break points. I have fought it with many days.
Device log can be found as following image. It is at XCode -> Window -> Devices and Simulators.
After enter the Open Console, there is a search field at the top-right of dialog. I can apply filter rule in there. For example, a process name contains Notification key word, or process name must equal to the name of extension target, ex: equal to MyNotificationServiceExtension process name.
Clearly something is broken in Xcode6-B5.
If I try to run a Photo extension on the Simulator I cannot see any Photos.app as an option for the extension attaching process.
The same, running on a real device, give me the correct behaviour.
In the first case any breakpoint is not honorated. In the latter case, breakpoints work like a charm.
You should know that the container app and extension are totally two difference process in iOS while the LLVM debugs only a thread a time, so when you are debugging, the console never log the extension and never stop at breakpoint.
You can solve most of the problems by #Vince Yuan's method.
However, my problem is that Xcode debugger hardly hook on my keyboard extension on both iOS Simulator and devices, like 1 time in 7-8 runs, It's totally a matter of probability. #Vince Yuan's method also work for just sometimes.
My little experience is that when you run you debug scheme, if the debug Session in the left panel showing that 'No Debug Session', there is no need to open your extension and test it, the debugger didn't hook on, just run again for lucky.
but when you see com.xxx.xxx.xxx is waiting to Attach, the extension can definitely get debugged.
This is a little trick for whom cannot debug iOS extension, especially keyboard extension.
As of Xcode 6 Beta 5, I've been able to use an actual device running iOS8 to debug my extension. Try running it on a device and select Safari to launch into
To overcome all the states caused by ever changing IDE, I'm using iOS Console by lemonjar.com – it displays a console window for any connected iOS device rendering syslog messages regardless the process ID. You can see both app and extension debug log messages at once here.
I could debug my extension with the way I describe below:
Xcode : Debug -> Attach to process by PID or Name. Your extension scheme name.
Then select your main app target and run.
I hope it also works for you guys.

How to attach debugger to iOS app after launch?

I have an issue I am troubleshooting which occurs very infrequently and doesn't seem to happen when I have things running under Xcode.
Is it possible to run an app normally (i.e. from Springboard) until my issue occurs, and then attach a debugger at that point?
I would prefer to do this without jailbreaking if possible.
Attach your device connected your Mac
Debug > Attach to Process by PID or Name
In the dialog sheet, enter the name of your App as it appears in the Debug navigator when started via Xcode (e.g. Target's name not bundle-id).
If the app is already running, the debugger will attach to the running process. If it isn't running, it will wait for the app to launch and then attach.
I'll leave this here since neither of the other 2 answers gave me quite enough detail without a little bit of a struggle.
Run your app in the simulator and take note of the name in the Debug navigator
Plug in your device and don't forget to select your device as the target
Debug > Attach to Process > By Process Identifier (PID) or Name
Enter the name from step 1 and attach. That should be all you have to do.
In Xcode 5.0.1 and 6 it's the menu bar items:
Debug > Attach to Process > By Process Identifier (PID) or Name...
In Xcode 7 it's just:
Debug > Attach to Process by PID or Name...
I was able to debug the app by adding a breakpoint on the AppCoordinator file init() method on the super.init() line.
I was able to turn off the wifi/internet and then by pass the developer verification.

Resources