Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a touchable Url link to a url in a UIAlertView's message?

I want to add a clickable link of an url into UIAlertView's message. Such that when user sees the alert view, they can can touch on link inside message. Alternatively they can go next by clicking on the OK button.

Is possible to do it? How?

like image 676
Jayyrus Avatar asked Jan 18 '12 14:01

Jayyrus


2 Answers

The only way I see to implement what you are trying to is through a custom alert view.

There are several approaches that you can take. One is subclassing UIAlertView and here you can find a short tutorial: Subclass UIAlertView. In your subclass you could then build the alert any way you like to implement the touch-enabled text. Have a look at this tutorial for a way to do it.

like image 179
sergio Avatar answered Oct 19 '22 04:10

sergio


I ran into this problem today, I needed to have clickable phone numbers and addresses in my alert view and was stumped for quite awhile as custom alert views are out of the question.

After some research it seems that you can add a textview to an alertview which seemed to solve my problem. Here is my approach that allows for dynamically scaling alertviews (note: using C# with Xamarin):

// create text view with variable size message
UITextView alertTextView = new UITextView();
alertTextView.Text = someLongStringWithUrlData;

// enable links data inside textview and customize textview
alertTextView.DataDetectorTypes = UIDataDetectorType.All;
alertTextView.ScrollEnabled = false; // is necessary 
alertTextView.BackgroundColor = UIColor.FromRGB(243, 243, 243); // close to alertview default color
alertTextView.Editable = false;

// create UIAlertView 
UIAlertView Alert = new UIAlertView("Quick Info", "", null, "Cancel", "OK");
Alert.SetValueForKey(alertTextView, (Foundation.NSString)"accessoryView");

// IMPORTANT/OPTIONAL need to set frame of textview after adding to subview
// this will size the text view appropriately so that all data is shown (also resizes alertview
alertTextView.Frame = new CoreGraphics.CGRect(owner.View.Center, alertTextView.ContentSize);
Alert.Show();
like image 1
panthor314 Avatar answered Oct 19 '22 04:10

panthor314