iosurlroutesios-universal-linksnsuseractivity

Is it possible to change NSUserActivity's userInfo from another app?


I am trying to open app B, from app A using universal links as below:

@IBAction func openButtonPressed(_ sender: Any) {
    if let appURL = URL(string: "https://website.com/section/Paris") {
        UIApplication.shared.open(appURL) { success in
            if success {
                print("The URL was delivered successfully.")
            } else {
                print("The URL failed to open.")
            }
        }
    } else {
        print("Invalid URL specified.")
    }
}

In App B's AppDelegate -> application continueUserActivity restorationHandler method, it calls another function where the userActivity is processed. Here there is a check for NSUserActivity's userInfo. It is checking for a key PageName in NSUserActivity's userInfo property, and according to the value it assigns a type to the activity and routes the app accordingly.

NSString *page = activity.userInfo[@"PageName"];
if ([page isEqualToString:@"Places"]) {
    return UserActivityTypePlaces;
} else {
    return UserActivityTypeLink;
}

I was wondering if I can change or add a key value pair to userInfo in App A, so that App B can route me to another tab. (In this case I want it to open another tab and search the location string that is in the universal link)

I have checked other questions related with NSUserActivity's userInfo on StackOverflow and Apple Developer forums but they are not changing it on the calling app.(such as in App A)

Thank you


Solution

  • I made these in App A:

     let activity = NSUserActivity(activityType: NSUserActivityTypeBrowsingWeb)
     activity.title = "Places"
     activity.userInfo = ["Page" : "Places"]
     let userInfoSet:Set = ["Page"]
     activity.requiredUserInfoKeys = userInfoSet
     self.userActivity = activity
     self.userActivity?.becomeCurrent()
    

    and added the NSUserActivityDelegate method:

    override func updateUserActivityState(_ activity: NSUserActivity) {
            let dict:Dictionary = ["Page":"Places"]
            userActivity?.addUserInfoEntries(from: dict)
    }
    

    But the NSUserActivity object is not something that goes from one app to the other. So this change only works within app A.

    Hope this helps someone else too.