[iOS elementary tutorial 2] DeepLink practice

[iOS elementary tutorial 2] DeepLink practice

1. Several ways to wake up iOS apps

      Waking up applications is a common technology in iOS development. There are many ways to wake up applications. In summary, they can be divided into the following categories:

  • Open the app directly
  • Wake up
  • scheme wake up
  • Universal Links wake up

      Opening an App directly is the most direct way to wake up an application. Taking the iPhone as an example, it can be opened from the home screen, search recommendations, application library and other scenes.

[iOS elementary tutorial 2] DeepLink practice    [iOS elementary tutorial 2] DeepLink practice    [iOS elementary tutorial 2] DeepLink practice

      When the user receives a notification message, clicking the notification message in the notification bar can also wake up the application. Notifications are divided into two types: local notification and remote notification. In terms of waking up the application, the logic of these two notifications is the same. The following code example sends a local notification:

- (void)addLocalNotification {
    [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound completionHandler:^(BOOL granted, NSError * _Nullable error) {

    }];
    UNMutableNotificationContent *notificaiton = [[UNMutableNotificationContent alloc] init];
    notificaiton.title = @"本地通知";
    notificaiton.body = @"通知内容";
    notificaiton.badge = @(1);

    UNTimeIntervalNotificationTrigger *trriger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO];

    UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"requsetID" content:notificaiton trigger:trriger];

    [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {

    }];
}

      It is also very simple to wake up the application through Scheme. In the previous tutorial, we introduced the integration of the sharing function, and some have configured and used Scheme. To wake up the application through Scheme, you first need to add URL Types, as shown in the figure below:

[iOS elementary tutorial 2] DeepLink practice

Above we defined a URL Scheme as deeplinkdemo, we can directly use the link starting with deeplinkdemo:// in the browser to wake up this application.

      Universal Link is a more powerful way to wake up applications. The relevant official website is as follows:

https://developer.apple.com/library/archive/documentation/General/Conceptual/AppSearch/UniversalLinks.html

Universal Link is a new application redirection function added after iOS 9. It can seamlessly connect websites and apps, without the need for a browser to do the transfer, which can realize seamless switching between applications.

      After understanding the wake-up methods of the above several applications, we return to the topic of this tutorial: DeepLink. DeepLink is also known as deep hyperlink, which refers to the application of the above technologies for seamless application launch, scene restoration, marketing wake-up and other business logic. Below, we will introduce how to use the Umeng tool platform as the basis to access the smart hyperlink function for the application.

Two, U-Link SDK integration

      It is very convenient to use CocoaPods to integrate U-Link SDK. You only need to add the following dependencies in the Podfile folder:

pod 'UMCommon'
pod 'UMDevice'

If the project is not managed by CocoaPods, you can also choose to manually introduce the SDK to integrate it. You can download the latest version of the SDK at the following URL:

https://developer.umeng.com/sdk/ios?acm=lb-zebra-609113-9110428.1003.4.8840408&scm=1003.4.lb-zebra-609113-9110428.OTHER\_16059174649951\_8840408

It should be noted that if the SDK is introduced manually, you need to add the -Objc parameter in the Targets->BuildSettings->Other Linker Flags of the project, and add the following system dependency libraries:

CoreTelephony.framework
libz.tbd      
libsqlite.tbd  
SystemConfiguration.framework

      After the SDK is integrated in the project, we need to do some simple configuration. First, we need to create an application in the Umeng background, and get the APP KEY parameter in the application settings page, as shown in the following figure:

[iOS elementary tutorial 2] DeepLink practice

This APP KEY parameter will be used during SDK initialization. In the Deeplink configuration page of the Umeng application background, we need to configure some Scheme information of the application, as shown below:

[iOS elementary tutorial 2] DeepLink practice

After that, we can use the smart hyperlink function by creating marketing activities. First, create marketing activities in the backend of Youmeng. The page is as shown below:

[iOS elementary tutorial 2] DeepLink practice

As you can see, when creating a marketing campaign, you can configure the campaign name, campaign description, in-app jump path and related parameters. When the user wakes up the app through the link of this marketing campaign, we can make the app based on these parameters Locate the instruction page to achieve seamless business connection. After the marketing campaign is created, the LinkID can be obtained on the details page of the campaign. We use this LinkID to wake up the application on the web page.

Three, Deeplink debugging and parameter reception

      After creating the marketing campaign and integrating the SDK, we can try to debug the smart hyperlink through a simple HTML DEMO. HTML sample code is as follows:

<!DOCTYPE html>
<html>
<head>
    <title>Demo</title>
    <meta charset="utf-8" />
    <script src="https://g.alicdn.com/jssdk/u-link/index.min.js"></script>
</head>
<body>
    <div>
        <h1>测试DeepLink跳转</h1>
        <button id="btn1">唤起 App</button>
    </div>
</body>

<script type="text/javascript">
    ULink.start({
        id: 'usr1ffnv829fu02h', /* 平台为每个应用分配的方案link ID,必填 */
        data: {
            custom:"customn1",
            custom2:"custom2"
        } /* 自定义参数,选填 */
    }).ready(function(ctx) { /* 初始化完成的回调函数 */
        document.getElementById('btn1').onclick = function(e){
        ctx.wakeup(); /* 用户点击某个按钮时启动app */
    };
});

</script>

<style type="text/css">
    div {
        text-align: center;
    }
    button {
        font-size: 60px;
    }
    h1 {
        font-size: 60px;
    }
</style>
</html>

As shown in the above code, u-link is the SDK of Umeng Smart Hyperlink on the JavaScript side. When calling the start method of ULink, the configured object id is the id of the marketing activity we created. In this way, waking up the application will automatically change the background The configured parameters are passed, and we can also define more dynamic parameters in the data to pass.

      For the Xcode project, the U-Link SDK needs to be initialized before the application starts, as follows:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [UMConfigure initWithAppkey:@"602505af668f9e17b8aef059" channel:nil];
    return YES;
}

Then implement the following callbacks of AppDelegate to handle hyperlinks:

#import "AppDelegate.h"
#import <UMCommon/UMConfigure.h>
#import <UMCommon/MobClickLink.h>

@interface AppDelegate ()<MobClickLinkDelegate>

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [UMConfigure initWithAppkey:@"602505af668f9e17b8aef059" channel:nil];
    return YES;
}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    return [MobClickLink handleLinkURL:url delegate:self];
}

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
    return [MobClickLink handleLinkURL:url delegate:self];
}

- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray<id<UIUserActivityRestoring>> * _Nullable))restorationHandler {
    return [MobClickLink handleUniversalLink:userActivity delegate:self];
}

// 解析后的回调函数,这里可以拿到所有的参数与跳转路径信息
- (void)getLinkPath:(NSString *)path params:(NSDictionary *)params {
    NSLog(@"getLinkPath:%@, %@", path, params);
}

@end

It should be noted that if it is an application created by a new version of Xcode, the above method needs to be implemented in the SceneDelegate class.

Guess you like

Origin blog.51cto.com/11643026/2665489