iosxamarinxamarin.ioshtml-parsingtttattributedlabel

Clicking on link freezes the app


I'm using Xamarin.TTTAttributedLabel to parse HTML in xamarin.ios app. Most of the times, whenever I click on the link it freezes the app. Sometimes it works and sometimes it doesn't. I'm using Label inside UIView --> ScrollView. My scrollview doesn't have any tap gesture. This doesn't happen on simulator. Only on physical device.

View.AddSubview (scrollView)
TTTAttributedLabel lbl = new TTTAttributedLabel();
lbl.SetText(attributedString, 15f);
lbl.Lines = 0;
lbl.EnabledTextCheckingTypes = NSTextCheckingType.Link | NSTextCheckingType.PhoneNumber;
lbl.UserInteractionEnabled = true;
lbl.Delegate = new TTTHTMLLabelDelegate(this);
lbl.Frame = new CGRect (15, 0, Common.screenWidth - 30, 20);
lbl.SizeToFit();
view2.AddSubview(lbl);
scrollView.AddSubview(view2);



//Delegate code
public override void DidSelectLinkWithURL(TTTAttributedLabel label, NSUrl url)
        {
            if (url != null)
            {
                var PageUrl = url.ToString();
                if (!string.IsNullOrWhiteSpace(PageUrl))
                {
                    if (PageUrl.StartsWith("mailto"))
                    {
                        //code to open email app
                    }
                    else if (PageUrl.StartsWith("tel"))
                    {
                       //code to open phone app

                    }
                    else
                    {
                        //for <a> links
                        UIApplication.SharedApplication.OpenUrl(url)
                    }
                }
            }

        }

UPDATED with sample code. I have observed one more thing, DidSelectLinkWithURL not get called when app freezes.


Solution

  • Try to store your variable

    public class YourClass
    {
    TTTAttributedLabel lbl;
    
     ViewDidLoad()
     {
           View.AddSubview (scrollView)
           lbl = new TTTAttributedLabel();
           lbl.SetText(attributedString, 15f);
           lbl.Lines = 0;
           lbl.EnabledTextCheckingTypes = NSTextCheckingType.Link | 
           NSTextCheckingType.PhoneNumber;
           lbl.UserInteractionEnabled = true;
           lbl.Delegate = new TTTHTMLLabelDelegate(this);
           lbl.Frame = new CGRect (15, 0, Common.screenWidth - 30, 20);
           lbl.SizeToFit();
           view2.AddSubview(lbl);
           scrollView.AddSubview(view2);
     }
    }