How to suppress alert message that appears when tapping on hyperlink in Xamarin.Android PDF Viewer?
The alert message that appears when a hyperlink is clicked can be suppressed by setting the `HyperlinkClickedEventArgs.Handled` property to `true` inside the handler method of the `HyperlinkClicked` event. This will prevent the clicked hyperlink from being opened. So, it must be opened at the application level using the `HyperlinkClickedEventArgs.Uri` property. Refer to the following code snippets.
Portable
Include an interface that defines the method to open the clicked hyperlink.
C#
namespace DisableAlertDemo { public interface IOpenURL { void OpenURL(string url); } }
Define the handler method for the HyperlinkClicked event. In the handler method, set the HyperlinkClickedEventArgs.Handled property to true and call the method to open hyperlinks using DependencyService.
C#
public partial class MainPage : ContentPage { public MainPage() { InitializeComponent(); Stream fileStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("DisableAlertDemo.Assets.HTTP_Succinctly.pdf"); pdfViewerControl.LoadDocument(fileStream); pdfViewerControl.HyperlinkClicked += PdfViewerControl_HyperlinkClicked; } private void PdfViewerControl_HyperlinkClicked(object sender, Syncfusion.SfPdfViewer.XForms.HyperlinkClickedEventArgs args) { // Block the alert message and URL navigation args.Handled = true; // Open the URL from the application DependencyService.Get<IOpenURL>().OpenURL(args.Uri); } }
Android
Add a new static class with a static property to the Android project.
C#
// Static class to store the context of the MainActivity internal static class ContextHelper { internal static Context context; }
To open the URL, store the context of the MainActivity in the static property inside the OnCreate method of the activity.
C#
ContextHelper.context = ApplicationContext;
Implement the IOpenURL interface in Android.
C#
// Register the Android implementation of IOpenURL with DependencyService [assembly:Xamarin.Forms.Dependency(typeof(OpenURLAndroid))] namespace DisableAlertDemo.Droid { class OpenURLAndroid : IOpenURL { public void OpenURL(string url) { Context context = ContextHelper.context; string annotationUri = url; Uri outURI; // Check whether the given URL is in valid format if (Uri.TryCreate(Uri.UnescapeDataString(annotationUri), UriKind.Absolute, out outURI) || Uri.TryCreate("http://" + annotationUri, UriKind.Absolute, out outURI)) { var uri = global::Android.Net.Uri.Parse(annotationUri); var intent = new Intent(Intent.ActionView, uri); intent.SetFlags(ActivityFlags.NewTask); global::Android.Content.PM.PackageManager packageManager = context.PackageManager; if (intent.ResolveActivity(packageManager) != null) { context.StartActivity(intent); } } } } }
iOS
Implement the IOpenURL interface in the iOS.
C#
// Register the iOS implementation of IOpenURL with DependencyService [assembly:Dependency(typeof(OpenURLiOS))] namespace DisableAlertDemo.iOS { class OpenURLiOS : IOpenURL { public void OpenURL(string url) { string urlAnnotation = url; Uri outURI; // Check whether the URL is in valid format if (Uri.TryCreate(Uri.UnescapeDataString(urlAnnotation), UriKind.Absolute, out outURI) || Uri.TryCreate("http://" + urlAnnotation, UriKind.Absolute, out outURI)) { UriBuilder builder = new UriBuilder(outURI); UIApplication.SharedApplication.OpenUrl(new NSUrl(builder.ToString())); } } } }
UWP
Implement the IOpenURL interface in the UWP.
C#
// Register the UWP implementation of IOpenURL with DependencyService [assembly:Dependency(typeof(OpenURLWindows))] namespace Syncfusion.SfPdfViewer.XForms.UWP { class OpenURLWindows : IOpenURL { public async void OpenURL(string url) { Uri outURI; // Check whether the URL is in valid format if (Uri.TryCreate(Uri.UnescapeDataString(url), UriKind.Absolute, out outURI) || Uri.TryCreate("http://" + url, UriKind.Absolute, out outURI)) { UriBuilder builder = new UriBuilder(url); await Windows.System.Launcher.LaunchUriAsync(builder.Uri); } } } }
Sample link