Overview

App Linking allows you to create redirection links that work across multiple platforms including Android, iOS, HarmonyOS, and web. With links of App Linking, you can redirect users to promotional information, or native app content that they can share with each other. You can create links of App Linking and send them to users, or allow users to share links dynamically generated in your app. Anyone who receives a link can tap it to access the specific app content.
When a user taps a link of App Linking:

What You Will Create

What You Will Learn

Development Environment and Skill Requirements

Device Requirements

To integrate the App Linking service, you need to complete the following preparations:

Enabling App Linking

  1. In AppGallery Connect, click My projects, and select the app for which you want to enable App Linking from the drop-down list on the top.
  2. Go to Grow > App Linking. If it is the first time that you use the service, click Use now.

If you are using Xcode, you need to integrate the App Linking SDK into your Xcode project with CocoaPods.

  1. Sign in to AppGallery Connect and click My projects.
  2. Click your project card and select an app for SDK integration from the app drop-down list on the top.
  3. Enter the App Store ID and team ID used in your signing certificate, and then click"" .
  4. Go to Project settings > General information. In the App information area, download the agconnect-services.plist file.
  5. Copy the agconnect-services.plist file to your project.
  6. Open the CLI and navigate to the location of the Xcode project. Then, create a Podfile. Skip this step if a Podfile already exists.
    cd project-directory pod init
  7. Add the pod ‘AGConnectAppLinking' dependency to the Podfile.
    target 'AGC-AppLinking-1' do pod 'AGConnectAppLinking' end
  8. Install the pod and open the .xcworkspace file to view the project.
    pod install
  9. The following figure shows the result after installation.

In this codelab, you can create a page in your iOS project and design the UI according to the following figure. On the page, a link of App Linking can be received and displayed.

Sample code:

let deeplinkLabel = UILabel(frame: CGRect(x: 60, y: 250, width: 200, height: 60)) override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = UIColor.white let welcomeLabel = UILabel(frame: CGRect(x: 60, y: 100, width: 200, height: 60)) welcomeLabel.textColor = UIColor.darkGray welcomeLabel.text = "Welcome to the Codelab" self.view.addSubview(welcomeLabel) let tipLabel = UILabel(frame: CGRect(x: 60, y: 200, width: 200, height: 60)) tipLabel.font = UIFont.systemFont(ofSize: 16, weight: .bold) tipLabel.text = "deeplink:" self.view.addSubview(tipLabel) deeplinkLabel.textColor = UIColor.darkGray deeplinkLabel.numberOfLines = 0 self.view.addSubview(deeplinkLabel) } func getDeepLink(deeplink: String?) { deeplinkLabel.text = deeplink }
  1. Go to Grow > App Linking. Click the URL prefixes tab and click New URL prefix.
  2. In the Set domain name area, enter the URL prefix used in this codelab.
  3. Click Next. The system automatically checks whether the URL prefix is available.
  1. Before creating a link of App Linking, you need to specify the trusted URL formats of the deep link used in this codelab. On the App Linking page, click the URL trustlist tab, and click New trusted URL format.
  2. Add the following trusted URL formats.
  3. On the App Linking page, click the App Linking tab, and click New link.
  4. In the Set short link area, use the default short link and click Next.
  5. In the Set deep link area, set required parameters for the deep link.
    • Link name: deep link name. In this codelab, enter AppLinking-easy.
    • Default deep link: HTML5 address, which can be accessed on a PC. Set it to https://developer.huawei.com/consumer/cn here.
    • iOS deep link: redirection link that can be received on an iOS device. Set it to AppLinking://iOS/testid=123 here. Ignore the optional parameters and click Next.
  6. In the Set link behavior area, set Set iOS link behavior to Open in app, and add your app package ID. Set Redirect a user not installing the app to to App details page on App Store.
  7. Ignore the optional parameters and click Release in the upper right corner.
  1. If you want the link of App Linking to be a custom URL, set a custom URL scheme under TARGETS > Info > URL Types in Xcode. In this codelab, set URL Schemes to AppLinking.
  2. Import AGConnectCore and AGConnectAppLinking to the AppDelegate class of the app, and call AGCInstance.startUp in the didFinishLaunchingWithOptions method for initialization.
    import AGConnectCore import AGConnectAppLinking func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { AGCInstance.startUp() }
  3. Call the AGCAppLinking.instance().handle method, and process the received link event in the callback.
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { let vc = ViewController() let nav = UINavigationController(rootViewController: vc) self.window?.rootViewController = nav AGCInstance.startUp() AGCAppLinking.instance().handle { (link, error) in let deepLink = link?.deepLink vc.getDeepLink(deeplink: deepLink) } self.window?.makeKeyAndVisible() return true }
  4. Implement the application: openURL: options: method, and return the value of AGCAppLinking.instance().openDeepLinkURL(url).
    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool { let isAppLinking = AGCAppLinking.instance().openDeepLinkURL(url) return isAppLinking }
  1. In Xcode, run your app on a mobile phone or simulator.
  2. In AppGallery Connect, copy the link or scan the QR code in the link record to obtain the URL of the link of App Linking.
  3. When the link is accessed in a browser, confirm whether to launch the app as prompted. Here, tap Open to launch the app.
  4. After that, your app obtains the deep link address, which is the iOS deep link address set in AppGallery Connect.

Well done. You have successfully built an app that integrates App Linking of AppGallery Connect and learned how to:

API Reference

Sample code

Code copied