How to suppress alert message that appears when tapping on hyperlink in Xamarin.Forms PDF Viewer?
In Xamarin.Forms 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 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 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
Conclusion
I hope you enjoyed learning about how to suppress alert message that appears when tapping on hyperlink in Xamarin.Forms PDF Viewer.
You can refer to our Xamarin.iOS PDF Viewer feature tour page to know about its
other groundbreaking feature representations and documentation, and how to quickly get started
for configuration specifications. You can also explore our Xamarin.iOS PDF Viewer
example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!