简体   繁体   English

使用参数的 Xamarin Forms 应用程序链接

[英]Xamarin Forms App Linking using parameters

I have a question.我有个问题。 I want to start my app from my php page and pass a variable over that link.我想从我的 php 页面启动我的应用程序并通过该链接传递一个变量。 Then on my app site, I need to read the parameter and perform the actions that I want with that parameter.然后在我的应用程序站点上,我需要读取参数并使用该参数执行我想要的操作。 Now I already found this link: Xamarin Android Start app using link with parameters .现在我已经找到了这个链接: Xamarin Android Start app using link with parameters This person does that with Xamarin.Android .此人使用Xamarin.Android做到这Xamarin.Android But how can I do this in Xamarin.Forms , or is it similar?但是我怎样才能在Xamarin.Forms做到这Xamarin.Forms ,或者是类似的?

I will show you how to do this by using the following url as an example: https://xamboy.com/hello/Rendy我将使用以下网址作为示例向您展示如何执行此操作: https : //xamboy.com/hello/Rendy

  • xamboy.com : Domain associated to app link xamboy.com : 与应用链接关联的域
  • hello : Is the app link path你好:是应用链接路径
  • Rendy : The parameter Rendy :参数

First, you need to configure app link support for each platform.首先,您需要为每个平台配置应用链接支持。 By doing the following steps:通过执行以下步骤:

Apple portal configuration苹果门户配置

  1. Go to App IDs section and click on your application转到 App IDs 部分并单击您的应用程序

在此处输入图片说明

  1. In the list of Application Services scroll to the bottom and click on Edit在应用程序服务列表中滚动到底部并单击编辑

在此处输入图片说明

  1. Enable Associated Domains and Save.启用关联域并保存。

在此处输入图片说明

Website configuration网站配置

In order to verify the domain association with the application, is necessary to upload a file in the website that defines this association.为了验证与应用程序的域关联,必须在定义此关联的网站中上传文件。

Need to create a file named apple-app-site-association.需要创建一个名为 apple-app-site-association 的文件。 This file should contain the following json structure:此文件应包含以下 json 结构:

{
    "applinks": {
        "apps": [],
        "details": [
            {
                "appID": "CNAL8C4H5U.com.crossgeeks.applinkssample",
                "paths": [ "/hello/*"]
            }
        ]
    }
}

Example: https://www.xamboy.com/.well-known/apple-app-site-association示例: https : //www.xamboy.com/.well-known/apple-app-site-association

This file should be placed inside .well-known folder or at the root path of the website ( https://yourdomain/.well-known/apple-app-site-association ).此文件应放置在 .well-known 文件夹内或网站的根路径 ( https://yourdomain/.well-known/apple-app-site-association )。

  • The appID is the combination of Prefix + (.) + ID appID 是 Prefix + (.) + ID 的组合在此处输入图片说明

  • Paths is the definition of all the paths our application will handle if present on the url. Paths 是我们的应用程序将处理的所有路径的定义(如果出现在 url 上)。 For example in our case is https://xamboy.com/hello because we just defined /hello as a path in the apple-app-site-association file.例如,在我们的例子中是https://xamboy.com/hello,因为我们刚刚将 /hello 定义为 apple-app-site-association 文件中的路径。

  • apple-app-site-association file shouldn't have a file extension apple-app-site-association 文件不应具有文件扩展名

Android Configuration安卓配置

Need to create a Digital Assets file named assetlinks.json.需要创建一个名为 assetlinks.json 的数字资产文件。 There's an online tool that assist you creating and testing this file here: https://developers.google.com/digital-asset-links/tools/generator .此处有一个在线工具可帮助您创建和测试此文件: https : //developers.google.com/digital-asset-links/tools/generator

It should contain the following json structure:它应该包含以下 json 结构:

[
   {
      "relation":[
         "delegate_permission/common.handle_all_urls"
      ],
      "target":{
         "namespace":"android_app",
         "package_name":"com.crossgeeks.applinkssample",
         "sha256_cert_fingerprints":[
            "3E:5D:E5:3B:BC:5A:61:BC:9E:96:34:C7:C2:D6:9F:BB:32:3C:8E:C5:FD:CE:D2:76:4C:81:98:2F:41:12:15:DD"
         ]
      }
   }
]

Example: https://www.xamboy.com/.well-known/assetlinks.json示例: https : //www.xamboy.com/.well-known/assetlinks.json

This file should be placed inside .well-known folder of the website ( https://yourdomain/.well-known/assetlinks.json ).此文件应放置在网站的.well -known 文件夹中( https://yourdomain/.well-known/assetlinks.json )。

If you want to test that everything is working fine you can do it by using this url:如果您想测试一切是否正常,您可以使用以下网址进行测试:

https://digitalassetlinks.googleapis.com/v1/statements:list?source.web.site= https://:&relation=delegate_permission/common.handle_all_urls https://digitalassetlinks.googleapis.com/v1/statements:list?source.web.site= https://:&relation=delegate_permission/common.handle_all_urls

If everything is ok it should look similar to this:如果一切正常,它应该类似于:

在此处输入图片说明

Configure App Linking in mobile projects在移动项目中配置应用链接

iOS project iOS项目

Go to the Entitlements.plist file, enable the property Associated Domain and add the domain of the website with the format applinks:yourdomain.com and applinks:*.yourdomain.com转到 Entitlements.plist 文件,启用属性 Associated Domain 并使用格式 applinks:yourdomain.com 和 applinks:*.yourdomain.com 添加网站的域

在此处输入图片说明

Android project安卓项目

Configure app links on Android MainActivity by adding an IntentFilter per each domain/path/protocol you want to support.通过为每个要支持的域/路径/协议添加一个 IntentFilter,在 Android MainActivity 上配置应用程序链接。

[Activity(Label = "AppLinksSample", Icon = "@mipmap/icon", Theme = "@style/MainTheme",
              MainLauncher = true,  
              ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]

    //Invite App Link
    [IntentFilter(new[] { Android.Content.Intent.ActionView },
                  DataScheme = "https",
                  DataHost = "xamboy.com",
                  DataPathPrefix = "/hello",
                  AutoVerify =true,
                  Categories = new[] { Android.Content.Intent.CategoryDefault, Android.Content.Intent.CategoryBrowsable })]
    [IntentFilter(new[] { Android.Content.Intent.ActionView },
                  DataScheme = "http",
                  DataHost = "xamboy.com",
                  AutoVerify = true,
                  DataPathPrefix = "/hello",
                  Categories = new[] { Android.Content.Intent.CategoryDefault, Android.Content.Intent.CategoryBrowsable })]

    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;

            base.OnCreate(savedInstanceState);
            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
            LoadApplication(new App());
        }

        protected override void OnNewIntent(Intent intent)
        {
            base.OnNewIntent(intent);
        }
    }

Forms project表单项目

In the App.cs file override the method OnAppLinkRequestReceived.在 App.cs 文件中覆盖方法 OnAppLinkRequestReceived。 This is where you handle what happens when your app is opened by an app link and process the parameters:这是您处理通过应用程序链接打开应用程序时发生的情况并处理参数的地方:

 protected override void OnAppLinkRequestReceived(Uri uri)
 {
      if (uri.Host.EndsWith("xamboy.com", StringComparison.OrdinalIgnoreCase))
      {

                if (uri.Segments != null && uri.Segments.Length == 3)
                {
                    var action = uri.Segments[1].Replace("/", "");
                    var msg = uri.Segments[2];

                    switch (action)
                    {
                        case "hello":
                            if(!string.IsNullOrEmpty(msg)){
                                Device.BeginInvokeOnMainThread(async() =>
                                {
                                    await Current.MainPage.DisplayAlert("hello", msg.Replace("&", " "), "ok");
                                });
                            }

                            break;

                        default:
                            Xamarin.Forms.Device.OpenUri(uri);
                            break;
                    }
                }
     }
}

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM