Issue
I want to write code that receives a dynamic link and navigates to the desired page.
So I wrote the below, and both iOS and Android receive it.
Xamarin.Form code
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
MessagingCenter.Subscribe<App, TestModel>((App)Xamarin.Forms.Application.Current, "dynamicLink", getURl);
}
private void getURl(App sourece, TestModel model)
{
Console.WriteLine(model.page);
Navigation.PushAsync(model.page);
}
}
Xamarin.Android code
class OnSuccessListner : Java.Lang.Object, IOnSuccessListener
{
void IOnSuccessListener.OnSuccess(Java.Lang.Object result)
{
var link = result.JavaCast<PendingDynamicLinkData>();
Android.Net.Uri deeplink = null;
if (link != null)
{
Console.WriteLine("received");
deeplink = link.Link;
Console.WriteLine($"{deeplink}");
var Model = new TestModel();
if (deeplink.Path == "/Test")
{
switch(deeplink.GetQueryParameter("ModelName"))
{
case "Test1":
Model = new TestModel { page = new Test1() };
break;
case "Test2":
Model = new TestModel { page = new Test2() };
break;
default:
Console.WriteLine("Not Value");
break;
}
}
MessagingCenter.Send((App)Xamarin.Forms.Application.Current, "dynamicLink", Model);
}
}
}
Xamarin.iOS
public override bool ContinueUserActivity(UIApplication application, NSUserActivity userActivity, UIApplicationRestorationHandler completionHandler)
{
return DynamicLinks.SharedInstance.HandleUniversalLink(userActivity.WebPageUrl, (dynamicLink, error) => {
if (error != null)
{
System.Console.WriteLine(error.LocalizedDescription);
return;
}
var compoents = new NSUrlComponents(dynamicLink.Url, false);
var queryitems = compoents.QueryItems;
var Model = new TestModel();
if(compoents.Path == "/Test")
{
var queryitem = (from item in queryitems
where item.Name == "ModelName"
select item).First();
if(queryitem != null)
{
switch (queryitem.Value)
{
case "Test1":
Model = new TestModel { page = new Test1() };
break;
case "Test2":
Model = new TestModel { page = new Test2() };
break;
default:
Console.WriteLine("Not Value");
break;
}
}
}
Console.WriteLine(dynamicLink);
MessagingCenter.Send((App)Xamarin.Forms.Application.Current, "dynamicLink", Model);
// Handle Universal Link
});
However, on Android, there is a problem that MessagingCenter listens twice and an error of "System.InvalidOperationException: 'Page must not already have a parent.' "
So I modified the method. I change my Moethod like this.
private async void getURl(App sourece, TestModel model)
{
if(Device.RuntimePlatform == Device.iOS)
{
Console.WriteLine($"{model.page} - iOS");
await Navigation.PushAsync(model.page);
} else
{
Console.WriteLine($"{model.page} - Android");
await Navigation.PushAsync(model.page);
MessagingCenter.Unsubscribe<App, TestModel >((App)Xamarin.Forms.Application.Current, "dynamicLink");
}
}
MessagingCenter received it once, but it doesn't move to the page.
May I know what the problem is!?
Solution
I solved it. check that link.
void IOnSuccessListener.OnSuccess(Java.Lang.Object result)
{
var link = result.JavaCast<PendingDynamicLinkData>();
Android.Net.Uri deeplink = null;
if (link != null)
{
...
//MessagingCenter.Send((App)Xamarin.Forms.Application.Current, "dynamicLink", Model);
App.Current.MainPage.Navigation.PushAsync(Model.page);
}
}
Answered By - 장주명
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.