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
- 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.
- Go to Grow > App Linking. If it is the first time that you use the service, click Use now.
- Enter the App Store ID and team ID used in your signing certificate, and then click ☑.
- Go to Project settings > General information, and download the agconnect-services.plist file under App information.
- 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
- Edit the Podfile to add the pod dependency AGConnectAppLinking.
target 'AGC-AppLinking-2' do
pod 'AGConnectAppLinking'
end
- 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
- Go to Grow > App Linking. Click the URL prefixes tab and click New URL prefix.
- In the Set domain name area, enter a URL prefix.
- Click Next. The system automatically checks whether the URL prefix is available. Creating a Link of App Linking in Your App
- 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
}
- 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"
- Call components.buildLongLink to generate and obtain a long link.
longLinkLabel.text = components.buildLongLink().absoluteString
- 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
}
- 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
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.
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 } Compiling and Testing Your App
In Xcode, run your app on a mobile phone or simulator.
Click Create App Linking to generate a long link and a short link using the App Linking SDK.
Click Share short App Linking to copy the short link to the clipboard.
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)