I have set a universal link that's correctly opening my app when I tap on it, however I tried it when the app is not in background. The app gets opened through application: didFinishLaunchingWithOptions, but I don't see the URL anywhere in the options dictionary. So what should I do ?
Thanks for your help
Ok I asked that question too quickly, I finally fetched it in AppDelegate in:
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: #escaping ([Any]?) -> Void) -> Bool
Contained in userActivity.webpageURL.
Related
I've implemented spotlight index for iOS/macOS both shows the items in the Spotlight search.
To handle the tap, there is a dedicated method for iOS
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: #escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
...
}
but it seems like that method is not available for macOS.
I've read the documentation unfortunately there is no explanation about it.
My goal is to handle the click of spotlight search item for a macOS app in AppDelegate.
Note: The app is native Appkit based app.
I'm using Firebase's deep linking to try and provide a certain function in my app when a user clicks a link that was sent to them. The deep link is setup properly on the Firebase web portal.
Clicking the link sent to the user DOES open my app, but does not trigger the continue userActivity method in my App Delegate, as none of the code contained in there is every executed.
I've tried the solution suggested in this StackOverflow post but no success, that's switching [UIUserActivityRestoring] to [Any] in the method declaration.
In Xcode, I have setup the associated domain to match what I set on Firebase: applinks:myappname.page.link; and I have added a "URL type" with identifier "Bundle ID", and URL scheme of ca.mycompany.myappname, with role editor.
All this is running on-device, of course, as I don't expect this to work in the simulator.
This is the method in my app delegate which should be called, however is not.
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: #escaping ([Any]?) -> Void) -> Bool {
print ("The link worked")
}
I made sure didFinishLaunchingWithOptions returns true always as well.
I expect the "The link worked" to actually print in debugging console, but it does not. Breakpoints indicate the method isn't being called at all, as the breakpoints are never reached.
There is a good change that somebody facing this problem deals with iOS13 / SwiftUI, too. Just like me.
I have found this article which solved all my problems.
Basically you have to be aware that some methods from the new SceneDelegate replace what was called in AppDelegate before iOS 13. Additionally there seem to be edge cases where methods from the AppDelegate still might be needed to handle deeplinks processing.
For anyone who needs it:
you need to use the below code to handle deep links from the link to the app that is already installed(other func application are for different things)
func application(_ application: UIApplication, continue userActivity: NSUserActivity,
restorationHandler: #escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
let handled = DynamicLinks.dynamicLinks().handleUniversalLink(userActivity.webpageURL!) { (dynamiclink, error) in
// ...
}
return handled
}
https://firebase.google.com/docs/dynamic-links/ios/receive #6
For scene application try to add your logic as part of
func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
let handled = DynamicLinks.dynamicLinks().handleUniversalLink(userActivity.webpageURL!) { (dynamiclink, error) in
//...
}
}
I'm computing a NSUserActivity for when I view a specific screen in my app. (Doesn't matter what, imagine a Location)
I set a lot of information about this activity/location using the contentAttributeSet.
When I visit this screen over and over, I set the viewController's userActivity property to this derived activity, and also call becomeCurrent() on it.
Eventually Siri will suggest that shortcut in Spotlight. As expected and desired. I tap on that, it launches my app and calls:
func application( _ application: UIApplication,
continue userActivity: NSUserActivity,
restorationHandler: #escaping ([UIUserActivityRestoring]?) -> Void) -> Bool
however, the .contentAttributeSet property will be nil.
Why? Have I not understood something here? I guess any data I need to access later should be in the userInfo dictionary? That works (as long as I don't set webpageURL)
Is there an easy way to store my app's data in Swift 3?
I would like to store a single object in some way. I tried UserDefaults.standard.set(my_object, forKey: "my_object") but it doesn't seem to work.
I put this code in AppDelegate.swift in func applicationWillTerminate(_ application: UIApplication)
Also, I would like to read this object back in func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool, when the application starts.
UserDefaults is the simplest way.
I put this code in AppDelegate.swift in func applicationWillTerminate(_ application: UIApplication)
Well, that's pretty silly, because that method is basically never called. Save information out to UserDefaults somewhere such that your code will actually run. applicationDidEnterBackground(_:) and applicationWillResignActive(_:) are possible choices, but the way to be certain is simply to save the information out whenever the information changes.
Also, I would like to read this object back in func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool, when the application starts.
Well, 99.9% of the time your application starts, that method won't be called. But if that method is called, fine, go ahead and read the object back in.
The Swift 3 converter changed this (perfectly functioning) line:
func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) {
to this:
func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) {
but both produce the warning
Instance method 'application(:handleActionWithIdentifier:for:completionHandler:)' nearly matches optional requirement 'application(:handleActionWithIdentifier:for:completionHandler:)' of protocol 'UIApplicationDelegate'
and offer the solution of making the function private, or adding #nonobjc.
Whether the function is left with the warning, reverted to the Swift 2 syntax, or fixed in either suggested way, launching the app with a shortcut item does not trigger it.
This is not listed as a known issue here either. Does anybody have an idea?
The signature for that method is now:
optional func application(_ application: UIApplication,
performActionFor shortcutItem: UIApplicationShortcutItem,
completionHandler: #escaping (Bool) -> Void)
Note the completion handler is now #escaping, per SE-103 (Make non-escaping closures the default). This attribute changes the type signature of the closure parameter, which in turn changes the type signature of the method it's an argument to, so method with the old declaration won't be called.
In general, the compiler warnings/fixits aren't so great for catching all type signature changes, especially between betas. Your best bet is to return to the SDK header (or rather, the Swift interface generated from it) or the documentation on Apple's site / in Xcode for the class/protocol that defines a problem method so you can see what its new definition is.
Apple introduced the #escaping tag to the Swift 3 Beta 6.
All closures now are by default no escaping, so if you want a closure that escapes, you need to give that tag. For some reason swift translator did not add this tag, but according to documentation in the link below, you need this tag added before the closure.
https://developer.apple.com/reference/uikit/uiapplicationdelegate/1622935-application
Adding the closure to my code removed the warning:
func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler:#escaping (Bool) -> Void)
I did not test it, so it might just removed the warning for some other reason.