Universal Links in iOS


#1

Hello,

Just getting started on RubyMotion. Trying to set up Universal Links in iOS to open URLs in my app. I’ve been reading through the Apple Developer documentation but I’m not sure how to implement the steps in RubyMotion instead of Xcode. Specifically these steps:

  • Add an entitlement that specifies the domains your app supports.
  • Update your app delegate to respond appropriately when it receives the NSUserActivity object.

Any help would be appreciated. Thanks


#2

You specify the entitlements in your Rakefile with app.entitlements. Create a new Xcode project. Use it to configure the appropriate entitlements and then open up the .entitlements to see what was added. E.g

<key>com.apple.developer.associated-domains</key>
<array>
    ...
</array>

Becomes:

app.entitlements['com.apple.developer.associated-domains'] = [...]

#3

Hey @hboon, just got around to trying to implement this now. I’ve added the app.entitlements to the Rakefile as mentioned. How do I go about checking for it in the .entitlements file though? I don’t see that file anywhere in my project directory.


#4

The .entitlements file is the one generated by the Xcode project. You create an Xcode project to explore the entitlements needed. You check against that file to see what entitlements you might need to add to your RubyMotion project.

After you update your Rakefile to add the entitlements (as you said you have done). Look for the Entitlements.plist file in your RubyMotion project’s build/'s subdirectories. This is the one that will be included in the build. So this should look like your Xcode project version of .entitlements file.


#5

Thanks @hboon. I’ve checked the Entitlements.plist file in the build directory and the entitlements are showing up correctly.


#6

I think I’ve done everything I need to do to set this up but I can’t seem to get it to work and not sure how to debug it. Here’s what I’ve done so far to try and set Universal Links up properly:

  1. Went to developer.apple.com and enabled Associated Domains for my app
  2. Added app.entitlements['com.apple.developer.associated-domains'] = ['subdomain.domain.com'] to the Rakefile
  3. Checked in the Entitlements.plist file in the build directory and confirmed the entitlement is there:
<key>com.apple.developer.associated-domains</key>
<array>
     <string>subdomain.domain.com</string>
</array>
  1. Created an AASA (apple-app-site-association) file which is as follows:
{
    "applinks": {
        "apps": [],
        "details": [
            {
                "appID": "prefix.appid",
                "paths": [ "*" ]
            }
        ]
    }
}
  1. Uploaded the AASA to the server for subdomain.domain.com
  2. Ensured it’s served over https and it uses application/json as the MIME type
  3. Added a method in app_delegate.rb to handle the link being clicked and opening the app:
def application(application, continue:userActivity, restorationHandler:handler)
  puts "I'm here"
end

Then to test if the setup is working:

  1. Deployed the app to a physical device using rake device
  2. Opened the email app on the device and clicked on a link to subdomain.domain.com/path

But the link always opens in Safari and the method in app_delegate.rb never seems to get called.

Any advice on why this isn’t working? Or how to debug it?


#7

Should be this instead:

<key>com.apple.developer.associated-domains</key>
<array>
    <string>applinks:subdomain.domain.com</string>
</array>

Missing “applinks:”.


#8

Thanks @hboon. I’ve updated the entitlements and now it looks like this:

<key>com.apple.developer.associated-domains</key>
<array>
	<string>applinks:subdomain.domain.com</string>
</array>

It’s still not working for me though. Tested in the simulator from a browser and on a device from the mail app and the browser but none of them are working for me.

Any advice on how to debug this? Is there a way to view the iOS logs to see if it’s even aware that the Universal Links exist for my app?


#9

I think you might need to increase the version of your app to trigger iOS to do the check.

1 of the essential but incomplete ways to verify things are working — check your server logs that your apple-app-site-association on the server is requested


#10

You might have the wrong method signature, try:
def application(application, continueUserActivity: userActivity, restorationHandler: handler)

The new Swift signatures have shorter parameter names, need to switch to the Objective C documentation to get the real names.

application:continueUserActivity:restorationHandler:


#11

Thanks @hboon. I checked the server logs and it looks like iOS was only trying to find the apple-app-site-association file in the /.well-known directory and not the root directory where it was stored. This is frustrating since the Apple documentation explicitly states that either location is fine:

After you create the apple-app-site-association file, upload it to the root of your HTTPS web server or to the .well-known subdirectory

Links are opening in the app instead of the browser now. However my method for handling these events isn’t being called so I’ll attempt the suggestion by @eamonn-webster


#12

I’ve got my method handling these events now thanks to the suggestion by @eamonn-webster.

Trying to figure out the next steps to get the link found in userActivity.webpageURL opened in a WKWebView object.

In app_delegate.rb I have a navigation controller as follows:

root_controller_class = AppControllerWKWebView
nav_controller = TopNavigationController.alloc.initWithRootViewController root_controller_class.new

Where AppControllerUIWebView is a WKWebView class defined as:

class AppControllerWKWebView < AppController

  def webViewClass
    WKWebView
  end

  ... other methods

Any guidance on how to load the webview? Having some difficulty translating Swift/Objective C documentation into something that works in RubyMotion. Thanks


#13

I’ve got my method handling these events now thanks to the suggestion by @eamonn-webster.


I’m now trying to allow for multiple subdomains by using a wildcard but it isn’t working as expected. In my Rakefile I have:

app.entitlements['com.apple.developer.associated-domains'] = ['applinks:*.domain.com']

However, it doesn’t work for any subdomains.

The following does work:

app.entitlements['com.apple.developer.associated-domains'] = ['applinks:subdomain1.domain.com']
app.entitlements['com.apple.developer.associated-domains'] += ['applinks:subdomain2.domain.com']

but this is obviously not ideal as I don’t want to have to update the app every time I need to support a new subdomain.

Any thoughts @hboon, @eamonn-webster?