Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Back link to previous app from containing app progmatically in case of deeplinks

I created a keyboard extension with a scan button to open a barcode scanner in my containing app. When the scan is completed, it should navigate back to the initial app and the barcode data should be set as text to the textfield that initiated the keyboard and we clicked on scan button.

enter image description here

There is this app Scandit Wedge that does it the same way. But I couldn't find a way to achieve the same. Please refer GIF below.

https://s3.amazonaws.com/id123-dev-ios/scandit.gif

Any help would be much appreciated.

like image 799
Sameer Bhide Avatar asked Nov 08 '22 14:11

Sameer Bhide


1 Answers

There is no public API to switch to the previous app, here is the answer: https://stackoverflow.com/a/13447282/1433612

But you could do that if you know the app's bundle id and url scheme. You can find unofficial lists on internet. Assuming that you are able to recognize the source app you can do something like this in your AppDelegate:

public func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
    guard let applicationBundleId = options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String else {
        return true
    }

    // Save your source application
    sourceApplicationBundleId = applicationBundleId
    return true
}

var sourceApplicationBundleId: String?

// Attempt to open application from which your app was opened
func openApplication() {
    guard let applicationBundleId = sourceApplicationBundleId, let url = url(for: applicationBundleId) else {
        return
    }
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
}

func url(for bundleId: String) -> URL? {
    guard let scheme = knownUrlSchemes[bundleId] else {
        return nil
    }

    return URL(string: scheme)!
}

// A list of known url schemes
var knownUrlSchemes: Dictionary<String, String> = {
    return ["com.google.Maps": "comgooglemaps://",
            "com.facebook.Facebook": "fb://"]
}()
like image 69
Au Ris Avatar answered Nov 15 '22 10:11

Au Ris