iOS- how to get dynamic short links with custom parameters firebase - ios

I want to send parameter through dynamic link and also to receive the same.
I have passed the custom parameter through my short dynamic link. Here is my link: https://pc988.app.goo.gl/vQaV?test=1
And I am using the following code to receive the dynamic link:
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
if let dynamicLink = DynamicLinks.dynamicLinks()?.dynamicLink(fromCustomSchemeURL: url){
self.handleIncomingDynamicLink(dynamicLink: dynamicLink)
return true
}
else{
let handled = FBSDKApplicationDelegate.sharedInstance().application(app, open: url, options: options)
return handled
}
}
#available(iOS 8.0, *)
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: #escaping ([Any]?) -> Void) -> Bool {
if let incomingURL = userActivity.webpageURL{
let linkHandled = DynamicLinks.dynamicLinks()!.handleUniversalLink(incomingURL, completion:{ [weak self] (dynamiclink, error) in
guard let strongSelf = self else{ return }
if let dynamiclink = dynamiclink, let _ = dynamiclink.url {
strongSelf.handleIncomingDynamicLink(dynamicLink: dynamiclink)
}
})
return linkHandled
}
return false
}
func handleIncomingDynamicLink(dynamicLink: DynamicLink) {
if dynamicLink.matchConfidence == .weak{
}else {
guard let pathComponents = dynamicLink.url?.pathComponents else { return }
for nextPiece in pathComponents{
}
}
print("incoming link \(dynamicLink.url)")
}
And my exact problem was, I cannot get the 'test' parameter that I passed in dynamic short link which I mentioned above.
Help me to get rid off this problem.

To append custom parameter you need to append the parameter to the deep link, not to the dynamic link.
In your example the deep link is https://www.fitview.com/ (you can see this in debug page https://pc988.app.goo.gl/vQaV?d=1).
To accomplish your goal, set the deep link to https://www.fitview.com?test=1 , create dynamic link, and then shorten dynamic link.

Related

Handling dynamic link from Firebase in swift

I have made a dynamic link in Firebase, which op my iOS app, when I click it.
The problem is, that I can't print information about the link.
I use this function in the AppDelegate file to handle the dynamic link:
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: #escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
print("Handeling dynamic link")
if let incomingURL = userActivity.webpageURL {
print("Incoming URL is \(incomingURL)")
let linkHandled = DynamicLinks.dynamicLinks().handleUniversalLink(incomingURL) { (dynamicLink, error) in
guard error == nil else {
print("Found an error! \(error!.localizedDescription)")
return
}
if let dynamicLink = dynamicLink {
self.handleIncomingDynamicLink(dynamicLink)
}
}
if linkHandled {
return true
} else {
// Maybe do other things with our incoming url
return false
}
}
return false
}
When I click on a dynamic link and open the app, none of the print statements is printed to the console.
It seems like this function is never tapped into.
The handleDynamicLink function is:
func handleIncomingDynamicLink(_ dynamicLink: DynamicLink) {
guard let url = dynamicLink.url else {
print("That is weird. My dynamic link object has no url")
return
}
print("Your incoming link parameter is \(url.absoluteString)")
}
I want to print information about the url so I can debug and use the information in the url to redirect to pages in the app.
When I'm testing this, I run the app on an iPhone connected to my mac.
I don't run it on the iOS simulator.
The code in your UIApplication delegate method looks good. The only difference I see with the way you have yours compared to mine is I'm returning true in the last line & you're returning false.
By the way, put breakpoints in that UIApplication delegate method & let me know if you're ever even getting to the first line in that delegate method. Are you able to hit any breakpoints in your handleIncomingDynamicLink() method?
Also, try adding this in applicationDidBecomeActive:
func applicationDidBecomeActive(_ application: UIApplication) {
guard let url = self.launchURL else { return }
self.launchURL = nil
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0, execute: { // wait to init. notifs.
DynamicLinks.dynamicLinks().handleUniversalLink(url) { (dynamiclink, error) in
if let dynamiclink = dynamiclink {
self.handleIncomingDynamicLink(dynamiclink)
}
}
})
}
And add this:
func application(_ app: UIApplication,
open url: URL,
options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
print("RECEIVED A URL THROUGH A CUSTOM SCHEME: \(url.absoluteString)")
if let dynamicLink = DynamicLinks.dynamicLinks().dynamicLink(fromCustomSchemeURL: url) {
self.handleIncomingDynamicLink(dynamicLink)
return true
} else { // Maybe handle Google or Facebook sign-in here
return false
}
}

Firebase dynamic link not opening iOS app

Have to mention this dynamic link was working before and suddenly for some reason stop working.
This is the dynamic link:
autobavaria1491911550production.page.link
and This is the link added to Associated Domains in capabilities: applinks:autobavaria1491911550production.page.link
and after testing with apple-app-site-association the result is:
{
"applinks": {
"apps": [],
"details": [
{
"appID": "K9B6CPM5NB.com.whipmobility.ios.operation",
"paths": [
"NOT /_/*",
"/*"
]
},
{
"appID": "S5Q34K6U59.com.autobavaria.ios.customer",
"paths": [
"NOT /_/*",
"/*"
]
}
]
}
}
com.autobavaria.ios.customer is the package name I want the dynamic link to open, which is available in app association but its not happening, and yes App Store ID and team ID is added in firebase console, any idea why is this happening?
Steps:
Delete your app
Restart your device.
Reinstall your app and test.
As per firebase, sometimes error comes while downloading "apple-app-site-association" file associated with your domain. That's why it happens.
Check out this video: https://youtu.be/KLBjAg6HvG0?t=483 for debugging help.
Setup Dynaminc link from start: https://www.youtube.com/watch?v=KLBjAg6HvG0
If you have completed the above-mentioned steps, please check if you have the right methods implemented in your AppDelegate file.
Method 1:
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
if DynamicLinks.dynamicLinks().shouldHandleDynamicLink(fromCustomSchemeURL: url) {
let dynamicLink = DynamicLinks.dynamicLinks().dynamicLink(fromCustomSchemeURL: url)
self.handleDynamicLink(dynamicLink)
return true
}
return true
}
Method 2:
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: #escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
if let incomingURL = userActivity.webpageURL {
print(incomingURL)
let linkHandled = DynamicLinks.dynamicLinks().handleUniversalLink(incomingURL) { (dynamicLink, error) in
guard error == nil else {
print("Found an error! \(error!.localizedDescription)")
return
}
if let dynamicLink = dynamicLink {
self.handleDynamicLink(dynamicLink)
}
}
if linkHandled {
return true
} else {
//May be do other things with our incoming URL?
return false
}
}
return false
}
Handle Dynamic Link common Method:
func handleDynamicLink(_ dynamicLink: DynamicLink?) {
guard let dynamicLink = dynamicLink else { return }
guard let deepLink = dynamicLink.url else { return }
}

Dynamic link object has no url

I created the DynamicLink for my firebase project when I am trying to receive the link I am getting "That's weird. My dynamic link object has no url".
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: #escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
if let incomingURL = userActivity.webpageURL{
print("Incoming URL is \(incomingURL)")
let linkHandled = DynamicLinks.dynamicLinks().handleUniversalLink(incomingURL)
{(dynamicLink, error) in
guard error == nil else{
print("Found an error! \(error!.localizedDescription)")
return
}
if let dynamicLink = dynamicLink{
self.handleIncomingDynamicLink(dynamicLink)
}
}
if linkHandled{
return true
}
else{
return false
}
}
return false
}
func handleIncomingDynamicLink(_ dynamicLink: DynamicLink){
guard let url = dynamicLink.url else{
print("That's weird. My dynamic link object has no url")
return
}
print("Your incoming link parameter is\(url.absoluteString)")
}
After checking all the blogs and posted this issue on firebase, I didn't find any solution for this but I came up with this concrete solution and it will work definitely
here: dynamicLinkURL is your main dynamic link and shortHandURL is your deeplink URL which is associated with your dynamic link. I hope the below snippet will help you.
func dynamicLinkhandler(_ dynamicLinkURL: URL, onCompletion: #escaping(_ success: Bool) -> Void) {
URLSession.shared.dataTask(with: dynamicLinkURL) { (data, response, error) in
guard error == nil else {
print("Found Error \(String(describing: error?.localizedDescription)))")
return
}
guard let shortHandURL = response?.url, shortHandURL != dynamicLinkURL else {
print("Thats Weird, my dynamic link has no URL")
onCompletion(false)
return
}
onCompletion(true)
}.resume()
}
Double check that the bundle id you set up in the dynamic link wizard creation within the firebase console it's the one you are running the app into.
I have three different bundle ids (dev, enterprise, production) and, for instance, if a set in the link the production bundle id but the app runs the dev bundle id, instead of returning back some error it returned an honest dynamicLink object but with a nil value in the url.

Deeplink just opens application but does not print componet needed for action swift

I added Universal Link to my project and done the whole process of uploading an associate file to my server. Tested and it is available. now I am trying to print a particular value but whenever I click the application, it only opens application. I tried printing some values on the console but they never get printed below is my code
extension AppDelegate {
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: #escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
guard userActivity.activityType == NSUserActivityTypeBrowsingWeb,
let url = userActivity.webpageURL,
let components = URLComponents(url: url, resolvingAgainstBaseURL: true) else {
return false
}
log("DEEP :LINK 1 \(components)", .fuck)
log("DEEP :LINK 2 \(components.url)", .fuck)
log("DEEP :LINK 3 \(Int(components.url?.lastPathComponent ?? "0"))", .fuck)
// presentDetailViewController(Int(components.url?.lastPathComponent ?? "0") ?? 0)
NotificationEvent.id.accept(Int(components.url?.lastPathComponent ?? "0") ?? 0)
NotificationEvent.isFromNotification.accept(true)
if let webpageUrl = URL(string: "https://example.com") {
log("DEEP :LINK 1 weby \(webpageUrl)", .fuck)
application.open(webpageUrl)
return false
}
return true
}
}
Do You try use
print("something")
instead of
log("something")
?

iOS 12 - AppAuth redirect URL not trigger AppDelegate

I am using AppAuth on my code.
I manage to authenticate successful , but when the SFSafariViewController gets dismiss from my Controller , the redirect url does not trigger the AppDelegate func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool
The redirect URL is my Bundle Identifier name : BundleIdentifier://authenticate
I have setup in info.plist url Schemes and url identifier which they have the same name.
When I run my code setting a break point on this func I can see my redirect url correct for standarizedURL and standarizedRedirectURL
- (BOOL)shouldHandleURL:(NSURL *)URL {
NSURL *standardizedURL = [URL standardizedURL];
NSURL *standardizedRedirectURL = [_request.redirectURL standardizedURL];
return OIDIsEqualIncludingNil(standardizedURL.scheme, standardizedRedirectURL.scheme) &&
OIDIsEqualIncludingNil(standardizedURL.user, standardizedRedirectURL.user) &&
OIDIsEqualIncludingNil(standardizedURL.password, standardizedRedirectURL.password) &&
OIDIsEqualIncludingNil(standardizedURL.host, standardizedRedirectURL.host) &&
OIDIsEqualIncludingNil(standardizedURL.port, standardizedRedirectURL.port) &&
OIDIsEqualIncludingNil(standardizedURL.path, standardizedRedirectURL.path);
But when AppAuth finishes the authentication and I have an access token , func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool doesn't get triggered.
Any idea why?
Here is my code
class func signInAuth(discoveryURLstr: String,presenter : UIViewController,completionHandler: #escaping ( (OIDAuthState?,Error?) -> () )){
guard let discoveruURL = URL(string: discoveryURLstr) else{
completionHandler(nil,AuthErrors.InvalidDiscoveryURL)
return
}
appAuthDiscoverConfiguration(discoveryURL: discoveruURL) { (configurationFile, error) in
guard let configurationFile = configurationFile else {
completionHandler(nil,AuthErrors.InvalidConfigurationFile)
return
}
let authRequest = appAuthRequest(configurationFile: configurationFile)
self.appAuthenticationSession = OIDAuthState.authState(byPresenting: authRequest, presenting: presenter, callback: { (state, error) in
if let error = error {
//self.authState = nil
completionHandler(nil,error)
return
}
if let state = state {
self.authState = state
completionHandler(state,nil)
}else{
completionHandler(nil,AuthErrors.InvalideState)
}
})
}
}
class func appAuthDiscoverConfiguration(discoveryURL : URL, completionHandler: #escaping ((OIDServiceConfiguration?,Error?) -> ())) {
OIDAuthorizationService.discoverConfiguration(forDiscoveryURL: discoveryURL) { (configuration, error) in
if let error = error {
completionHandler(nil,error)
return
}else{
guard let configurationFile = configuration else {
completionHandler(nil,AuthErrors.InvalidConfigurationFile)
return
}
completionHandler(configurationFile,nil)
}
}
}
class func appAuthRequest(configurationFile : OIDServiceConfiguration) -> OIDAuthorizationRequest{
return OIDAuthorizationRequest(configuration: configurationFile, clientId: AppAuthConstants.clientId, clientSecret: nil, scope: AppAuthConstants.scope, redirectURL: AppAuthConstants.redirectURL, responseType: AppAuthConstants.responseType, state: nil, nonce: nil, codeVerifier: nil, codeChallenge: nil, codeChallengeMethod: nil, additionalParameters: AppAuthConstants.additionalParameters)
}
On iOS 12, App-Auth uses ASWebAuthenticationSession, and on iOS 11, it uses the now-deprecated SFAuthenticationSession instead of requiring the app to support handling the redirect manually. To support earlier versions of iOS, you still need your code in the func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool method.
For reference, you can see what AppAuth is doing under the covers here. Also, this is a great answer that explains how to generically get an OAuth token on iOS without using AppAuth.

Resources