DEV Community

HMS Community
HMS Community

Posted on

Tips for Creating a Link of App Linking Using the App Linking SDK

What Is App Linking
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 ads, or native app content that they can share with each other. You can create app links 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.
Enabling App Linking and Integrating the App Linking SDK

  1. Click My projects in AppGallery Connect, click your project card, and select your 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.
  3. Enter the App Store ID and team ID used in your signing certificate, and then click ☑.
  4. Go to Project settings > General information, and download the agconnect-services.plist file under App information.
  5. Open the CLI and navigate to the location of your Xcode project. Then, create a Podfile. Skip this step if a Podfile already exists.

cd project-directory
pod init

  1. Edit the Podfile to add the pod dependency AGConnectAppLinking.

target 'AGC-AppLinking-2' do
pod 'AGConnectAppLinking'
end

  1. Install the pod and open the .xcworkspace file to view the project. pod install

Designing the UI

You can create a page layout 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.

Requesting a URL Prefix

  1. Go to Grow > App Linking. Click the URL prefixes tab and click New URL prefix.
  2. In the Set domain name area, enter a URL prefix.
  3. Click Next. The system automatically checks whether the URL prefix is available. Creating a Link of App Linking in Your App
  4. 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()
return true
}

  1. Create an object named AGCAppLinkingComponents, and set urlPrefix to the URL prefix requested in AppGallery Connect. Set deepLink. Set iOS app parameters, where iosBundleId is the app package name, and iosDeepLink is the redirection link to your iOS app.

let components = AGCAppLinkingComponents()
components.uriPrefix = "https://codelab.drcn.agconnect.link"
components.deepLink = "https://developer.huawei.com/consumer/cn"
components.iosBundleId = Bundle.main.bundleIdentifier
components.iosDeepLink = "AppLinking://ios/test2=456"
components.socialTitle = "AppLinking"

  1. Call components.buildLongLink to generate and obtain a long link.

longLinkLabel.text = components.buildLongLink().absoluteString

  1. Call components.buildShortLink to generate and obtain a short link.

components.buildShortLink { self in
if let e = error {
let alert = UIAlertController.init(title: "Error", message: e.localizedDescription, preferredStyle: .alert)
alert.addAction(UIAlertAction.init(title: "OK", style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
return
}
shortAppLinking = shortLink?.url.absoluteString
shortLinkLabel.text = shortAppLinking
}

  1. In the method of sharing a link, the short link, generated by the App Linking SDK, is copied to the system clipboard.

@objc func shareLink() {
UIPasteboard.general.string = shortAppLinking
}
Receiving a Link of App Linking in Your App

  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. 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()
}

  1. 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
}

  1. 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 } Compiling and Testing Your App
  2. In Xcode, run your app on a mobile phone or simulator.

  3. Click Create App Linking to generate a long link and a short link using the App Linking SDK.

  4. Click Share short App Linking to copy the short link to the clipboard.

  5. Paste the short link to a browser and access the link. Click Open to launch your app as prompted.

Congratulations
Well done. You have successfully built an app that integrates App Linking of AppGallery Connect and learned how to:
Create a sharable link using the App Linking SDK.
Launch your app through the configured link and receive the deep link using the App Linking SDK.

Top comments (0)