0

I have an app with some Table View Controllers. In the last one of each cell I have a different url. I decided then to insert a SafariVC, but what is occurring is that it is like it was as a FirstResponder.

class Page3: UITableViewController, GADBannerViewDelegate, SFSafariViewControllerDelegate {

    ...

    @IBOutlet weak var websiteTextView: UITextView!

    ...

    override func viewDidLoad() {
        super.viewDidLoad()

        ...

        websiteTextView.text = goP3.website

        let safariVC = SFSafariViewController(url: URL(string: goP3.website)!)
        present(safariVC, animated: true, completion: nil)
        safariVC.delegate = self

        ...
    }
}

gif

After watch the gif above, I have two doubts:

1st. When I go into the Page3, it auto-opens the SafariViewController. How do I fix it?

2st. When I click on the url, it doesn't open the SafariViewController. What do I have to do to fix it?

ARNON
  • 1,097
  • 1
  • 15
  • 33

1 Answers1

1

For 1) 1st Question answer is below: You need to remove below code from ViewDidLoad Method:

websiteTextView.text = goP3.website
let safariVC = SFSafariViewController(url: URL(string: goP3.website)!)
present(safariVC, animated: true, completion: nil)
safariVC.delegate = self

For 2) 2nd Question answer is below: You need to create a UIButton and set title text for goP3.website. Then Create IBAction method for that UIButton. something like below

@IBOutlet weak var btnWebsite: UIButton!
override func viewDidLoad() {
    super.viewDidLoad()

    ...
    btnOK.setTitle(goP3.website, for: .normal)
}
@IBAction func btnWebsiteSelect(_ sender: UIButton) {

    let safariVC = SFSafariViewController(url: URL(string: goP3.website)!)
    present(safariVC, animated: true, completion: nil)
    safariVC.delegate = self
}
krunal
  • 452
  • 3
  • 11
  • I think I need 'websiteTextView.text = goP3.website' because it makes the text be show in the textView. And I don't think a button is gonna work. Because sometimes I have 3 or 4 URLs inside the same textView. Sorry, I forgot to explain this bafore. Do you have another suggestion? – ARNON May 31 '17 at 06:09
  • 1
    Yes, there are different ways to do. But the default behavior of UITextView is : Link will be open in Safari App only. I think you need to follow this link. If you find not helpful then let me know. I will help you with other solution. Just add a new comment here that it is helpful or not. https://stackoverflow.com/questions/17278169/ios-add-button-over-nsattributedstring – krunal May 31 '17 at 06:16
  • I read and check step by step. But unfortunately sometimes I have more than one URL inside the same textView. So putting a transparent button on top of the links would cause confusion between the links. Would not it be possible to change the default/pattern of opening links in textView? – ARNON May 31 '17 at 19:17