You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在.NET Maui的AppDelegate中注册AppLink?

.NET Maui iOS端AppLink注册实现步骤

1. 配置Info.plist

需要添加两个关键配置项,可通过编辑Info.plist源码或可视化编辑器操作:

  • URL Types:配置支持的URL协议与域名
  • Associated Domains:绑定你的域名,实现iOS通用链接识别

编辑Info.plist源码时,添加以下内容:

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>https</string>
        </array>
        <key>CFBundleURLName</key>
        <string>xxx.com</string>
    </dict>
</array>
<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <false/>
    <key>NSExceptionDomains</key>
    <dict>
        <key>xxx.com</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSAllowsArbitraryLoads</key>
            <true/>
        </dict>
    </dict>
</array>
<key>AssociatedDomains</key>
<array>
    <string>applinks:xxx.com</string>
</array>

2. 修改AppDelegate.cs处理链接回调

在iOS项目的AppDelegate.cs中,重写链接相关回调方法,将获取到的AppLink传递给Maui应用逻辑:

using Foundation;
using UIKit;
using Microsoft.Maui;

namespace YourAppNamespace.iOS;

[Register("AppDelegate")]
public class AppDelegate : MauiUIApplicationDelegate
{
    protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();

    // 处理iOS通用链接回调
    public override bool ContinueUserActivity(UIApplication application, NSUserActivity userActivity, UIApplicationRestorationHandler completionHandler)
    {
        if (userActivity.ActivityType == NSUserActivityType.BrowsingWeb)
        {
            var url = userActivity.WebPageUrl;
            if (url != null && url.AbsoluteString.StartsWith("https://xxx.com/Share"))
            {
                // 通过消息中心将链接传递给共享代码
                MessagingCenter.Send(this, "AppLinkReceived", url.AbsoluteString);
            }
        }
        return base.ContinueUserActivity(application, userActivity, completionHandler);
    }

    // 处理传统URL跳转回调
    public override bool OpenUrl(UIApplication app, NSUrl url, NSDictionary options)
    {
        if (url.AbsoluteString.StartsWith("https://xxx.com/Share"))
        {
            MessagingCenter.Send(this, "AppLinkReceived", url.AbsoluteString);
        }
        return base.OpenUrl(app, url, options);
    }
}

3. 应用内处理链接逻辑

在Maui共享代码中,订阅消息并处理接收到的AppLink,示例以MainPage为例:

public MainPage()
{
    InitializeComponent();
    // 订阅AppLink接收消息
    MessagingCenter.Subscribe<AppDelegate, string>(this, "AppLinkReceived", (sender, url) =>
    {
        // 自定义处理逻辑:解析参数、导航到对应页面等
        Console.WriteLine($"收到AppLink链接:{url}");
        Navigation.PushAsync(new SharePage(url));
    });
}

额外注意事项

  • 需在你的域名xxx.com的.well-known目录下放置**Apple App Site Association (AASA)**文件,确保iOS能识别通用链接,示例文件内容:
{
    "applinks": {
        "apps": [],
        "details": [
            {
                "appID": "你的Apple团队ID.应用BundleID",
                "paths": [ "/Share/*" ]
            }
        ]
    }
}

内容的提问来源于stack exchange,提问作者tengelberth

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 22:50:41