स्विफ्ट 3 के साथ
, UITextViewDelegate
एक textView(_:shouldInteractWith:in:interaction:)
तरीका प्रदान करता है। textView(_:shouldInteractWith:in:interaction:)
निम्नलिखित घोषणा की है:
प्रतिनिधि पूछता है कि निर्दिष्ट पाठ दृश्य पाठ की सीमा में दी गई दिए गए URL के साथ उपयोगकर्ता बातचीत की निर्दिष्ट प्रकार की अनुमति चाहिए।
optional func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool
निम्न कोड दिखाता है कि कैसे उन्हें सफारी अनुप्रयोग में खोलने का एक SFSafariViewController
में UITextView
वेब लिंक को खोलने के लिए के बजाय:
import UIKit
import SafariServices
class ViewController: UIViewController, UITextViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Set textView
let textView = UITextView()
textView.text = "http://www.yahoo.fr http://www.google.fr"
textView.isUserInteractionEnabled = true
textView.isEditable = false
textView.isSelectable = true
textView.dataDetectorTypes = UIDataDetectorTypes.link
// Add view controller as the textView's delegate
textView.delegate = self
// auto layout
view.addSubview(textView)
textView.translatesAutoresizingMaskIntoConstraints = false
textView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
textView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
textView.heightAnchor.constraint(equalToConstant: 300).isActive = true
textView.widthAnchor.constraint(equalToConstant: 300).isActive = true
}
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
// Open links with a SFSafariViewController instance and return false to prevent the system to open Safari app
let safariViewController = SFSafariViewController(url: URL)
present(safariViewController, animated: true, completion: nil)
return false
}
}
स्रोत
2017-01-24 14:06:09
Uhm, निजी जा रहा है, आपको लगता है कि यह होने वाला है कर ऐपस्टोर स्वीकृति के लिए एक समस्या? बीटीडब्ल्यू आज रात मैं आपका कोड जांचूंगा और मैं आपको बता दूंगा। धन्यवाद। – crash
यह ऐसी चीज नहीं है जिसे वे जांच सकते हैं, लेकिन भविष्य में ओएस परिवर्तनों के लिए आपको थोड़ा सा जोखिम हो सकता है (यह डेस्कटॉप समकक्ष में एक सार्वजनिक एपीआई है और यह संभावना नहीं है कि ऐप्पल वेबकिट का उपयोग करना बंद कर देगा, लेकिन यह संभव है) । सबसे बुरा यह हो सकता है कि व्यवहार मोबाइलसाफरी लॉन्च करने के डिफ़ॉल्ट पर वापस आ जाएगा। – rpetrich
बिल्कुल सही। यह सुचारू रूप से काम किया। धन्यवाद! – crash