TaylorAllred TaylorAllred - 6 months ago 31
Swift Question

application(_:didFinishLaunchingWithOptions:) is not printing anything inside it

I am in the process of testing push notifications on a bare bones app. Currently, all it does is receive push notifications and do nothing else.

When I try to

print()
out info inside the
application(_:didFinishLaunchingWithOptions:)
function it refuses to print anything.

My code:

class AppDelegate: UIResponder, UIApplicationDelegate {
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
print("TEST TEST TEST")
registerForPushNotifications(application)
print("TEST TEST TEST")
if let notification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? [String: AnyObject] {
print(notification)
let aps = notification["aps"] as! [String: AnyObject]
print(aps)
}
return true
}

func registerForPushNotifications(application: UIApplication) {
let notificationSettings = UIUserNotificationSettings(forTypes: [.Badge, .Sound, .Alert], categories: nil)
application.registerUserNotificationSettings(notificationSettings)
}
}


None of the print lines in the function print anything to the console. I have confirmed they are being hit by using breakpoints. Why are is nothing being printed?

I was able to find a workaround with using alerts, and those work great and give me all the info I need, but it doesn't help answer why nothing is being printed in the console.

Workaround Code:

class AppDelegate: UIResponder, UIApplicationDelegate {
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
UIAlertView(title: "Remote Notif", message: "\(launchOptions)", delegate: nil, cancelButtonTitle: "OK").show()
registerForPushNotifications(application)
if let notification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? [String: AnyObject] {
UIAlertView(title: "Remote Notif", message: "\(notification)", delegate: nil, cancelButtonTitle: "OK").show()
let aps = notification["aps"] as! [String: AnyObject]
UIAlertView(title: "Remote Notif", message: "\(aps)", delegate: nil, cancelButtonTitle: "OK").show()
}
return true
}

func registerForPushNotifications(application: UIApplication) {
let notificationSettings = UIUserNotificationSettings(forTypes: [.Badge, .Sound, .Alert], categories: nil)
application.registerUserNotificationSettings(notificationSettings)
}
}


I am aware that UIAlertView is deprecated, but it still works fine for now.

Some extra info:

It is being run on a device.

The Run Scheme is set up so it waits for an executable to be launched (a user pressing on a push notification for instance).

The Main Storyboard file base name in my .plist is "Main"




After further testing, it seems like it only happens when my Launch Scheme is set to
Wait for executable to be launched
, if it is set to
Automatically
launch, it will print out the lines. The only thing I can think of is maybe the console isn't being booted up when woken up from a push notification. So it might be a bug with Xcode.

Answer

I've found that having Wait for application to be launched causes the Xcode debug console not to start with the application. I don't know if this intended or an Xcode bug. Try selecting Launch automatically in your scheme and your debug console should turn back on. You also may need to restart xCode after changing the scheme.