0

Hello everyone!) Need some help!)

How can I open any URL-link from UITextField in SFSafariViewController with interval?


This is what I meant when I said "any link"…

First: I means that if we have full link with https://.., like: https://www.google.com/ In this case, the link should be opened automatically with interval like 3 seconds.

Second: If we have link with without https://.., like: www.google.com In this case - https:// must be added automatically, then the link should be opened automatically with interval like 3 seconds.

And the third: If we have only stackoverflow.com without https://www.., Then - https://www must be added automatically and then the link should be opened automatically with interval like 3 seconds.


Code:

import UIKit
import Foundation
import SafariServices

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    
    if let urlString = linkTextField.text {
        let url: URL?
        if urlString.hasPrefix("http://") {
            url = URL(string: urlString)
        } else {
            url = URL(string: "http://" + urlString)
        }
        if let url = url {
            let sfViewController = SFSafariViewController(url: url)
            self.present(sfViewController, animated: true, completion: nil)
            print ("Your link was opened in SFSafariViewController")
        }
    }
    return true
}

Do you have any ideas how to implement this?) Thanks for every answer!)


Gucci
  • 47
  • 1
  • 7
  • 1
    Please only ask one question at a time. See also [ask]. – koen Sep 12 '22 at 13:25
  • `shouldChangeCharacters` will be called every time the user enters something. Consider executing this logic on a button press or on return. – Timmy Sep 12 '22 at 13:31
  • @koen Hi!) This is one question regarding the use of a UITextField and SFSafariViewController.) I just explained what should function do when we put our link in UITextField.) And that's all..) – Gucci Sep 12 '22 at 13:35
  • @Timmy Hi!) I understand, thats why I want to implement it with an interval. Also we can add text field clearing after exiting SFSafariViewController and "placeholder" like: "Type the next link..." – Gucci Sep 12 '22 at 13:45

1 Answers1

0

Even though I don't think it'll be a good user interface(for instance the user is thinking while typing & takes too long), use DispatchQueue.main.asynAfter:

DispatchQueue.main.asynAfter(deadline: .now() numberOfSeconds) {
    let sfViewController = SFSafariViewController(url: url)
    self.present(sfViewController, animated: true, completion: nil)
}
Timmy
  • 4,098
  • 2
  • 14
  • 34
  • Great, I will check it!) I'm still learning Swift.) This is just for personal development, not for a commercial project!) I just haven't found any solution yet, that's why I decide to discuss it.) Thanks a lot and have a nice day!) – Gucci Sep 12 '22 at 14:16
  • If my answer was helpful consider pressing the checkmark above to mark the answer as accepted. have a great day! – Timmy Sep 12 '22 at 18:43