通过URL方案将数据从一个应用程序传递到另一个应用程序?

时间:2019-08-14 13:48:24

标签: xamarin.ios

我试图通过网址计划将文本字段中提供的数据从一个应用程序传递到另一个应用程序

首先,我在接收器应用info.plist中设置了网址方案和标识符。 我已经在接收器应用程序的info.list中添加了LsApplicationQueriesScheme

我已将此代码放入第一个发件人应用程序中。 namefield是我在此应用中指定的Textfield。

partial void clicked(UIButton sender)
    {  string text = namefield.Text;
        var urlToSend = new Uri("soWhat://" + text);
        Console.WriteLine(urlToSend);
        UIApplication.SharedApplication.OpenUrl(urlToSend);
}

我已经把接收器应用程序AppDelegate.cs

public override bool OpenUrl(UIApplication app, NSUrl url, NSDictionary options)
    {
        UIAlertView alert = new UIAlertView();
        var uri = new Uri(url.Host);
        sun = uri.ToString();
        alert.Message = sun;
        Console.WriteLine(sun);
        alert.Show();
        alert.DangerousRelease();
        return base.OpenUrl(app, url, options);
    }

我必须通过urlscheme将数据从一个应用程序传递到另一个应用程序

1 个答案:

答案 0 :(得分:0)

Step1: first app sender-app.. 
info.plist Add this points 

        <Array>
    LSApplicationQuerieScheme 
        <string>receiverapp</string>
        </Array>

Step 2. add this code in this viewcontroller button action




    partial void btnact(UIButton sender)
        {
            string name = namefield.Text;
            var names = name.ToString();
            var app = UIApplication.SharedApplication;

            var na = "receiverapp://" + (names ?? "");
            var uri = new NSUrl(na);
            var dict = new NSDictionary();
            if (app.CanOpenUrl(uri))
                {
                app.OpenUrl(uri, options: dict, completion: null);
                Console.WriteLine(uri);
            }

Step 3: In receiverapp 
info.plist add url types 
url-scheme = receiverapp
url-identifier = com.receiverapp
Role = Editor

Step 4: in Appdelegate.cs in receiverapp
add this code



     public string message;
         NSNotificationCenter nc = NSNotificationCenter.DefaultCenter;

            public override bool OpenUrl(UIApplication app, NSUrl url, NSDictionary options)
            {
                message = url.Host;
                nc.PostNotificationName("AppInput", null);
                Console.WriteLine(message);
                return true;
                //return base.OpenUrl(app, url, options);
            }

Step5: ViewController.cs in receiverapp
add this code


         AppDelegate dele = (AppDelegate)UIApplication.SharedApplication.Delegate;
              public override void ViewDidLoad()
               {
                  base.ViewDidLoad();
                 NSNotificationCenter.DefaultCenter.AddObserver(this, new ObjCRuntime.Selector("GetData:"), new NSString("AppInput"), null);
            }
    [Export("GetData:")]
            private void GetData(NSNotification notification)
            {
                  label.Text = dele.message;

                Console.WriteLine(label.Text);
            }