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:
- If the user has installed your app, they will be redirected to
your app content directly. If the user has not installed your app,
they will be prompted to install your app first, and then view the
in-app content specified by the link.
- In a desktop browser, the user will see the web version of your
content.
What You Will Create
What You Will Learn
- Integrate App Linking.
- Create a link of App Linking in AppGallery Connect.
- Receive a link of App Linking in an iOS app.
Development Environment and Skill Requirements
- A Mac with Xcode 11 or later installed
- CocoaPods 1.10.0 or later installed
- A HUAWEI ID, whose identity has been verified
Device Requirements
To integrate the App Linking service, you need to complete the
following preparations:
- Create an app in AppGallery Connect.
- Create an iOS project.
- Integrate the App Linking SDK into your Xcode project.
For
details, please refer to Integration Preparations.
You need to
register as a developer first to complete the operations above.
Enabling App Linking
- 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.
- 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.
- Sign in to AppGallery Connect and click My
projects.
- Click your project card and select an app for SDK integration from
the app drop-down list on the top.
- Enter the App Store ID and
team ID used in your signing certificate, and then click"" .
- Go to Project settings > General
information. In the App information
area, download the agconnect-services.plist file.
- Copy the agconnect-services.plist file to your
project.
- 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
- Add the pod ‘AGConnectAppLinking'
dependency to the Podfile.
target 'AGC-AppLinking-1' do
pod 'AGConnectAppLinking'
end
- Install the pod and open the .xcworkspace file to
view the project.
pod install
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
}
- Go to Grow > App Linking.
Click the URL prefixes tab and click New
URL prefix.
- In the Set domain name area, enter the URL prefix
used in this codelab.
- Click Next. The system automatically checks
whether the URL prefix is available.
- 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.
- Add the following trusted URL formats.
- On the App Linking page, click the App
Linking tab, and click New link.
- In the Set short link area, use the default short
link and click Next.
- 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.
- 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.
- Ignore the optional parameters and click Release
in the upper right corner.
- 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.
- 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()
}
- 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
}
- 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
}
- In Xcode, run your app on a mobile phone or simulator.
- 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.
- When the link is accessed in a browser, confirm whether to launch
the app as prompted. Here, tap Open to launch the
app.
- 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:
- Create a link of App Linking in AppGallery Connect.
- Launch your app through the configured link and receive the deep
link using the App Linking SDK.
API Reference
Sample code