2009-08-20 14 views
15

मैं विकास कर रहा हूं और आईफोन 3.0 एप्लिकेशन। और मैं सफारी के बजाय UIWebView में UITextView में वेब लिंक खोलने की कोशिश कर रहा हूं। लेकिन अभी भी कोई भाग्य नहीं है।सफारी के बजाय UIWebView में UITextView वेब लिंक कैसे खोलें?

UITextView संपादन योग्य नहीं है, और यह पूरी तरह से वेब लिंक का पता लगाता है और उन्हें सफारी में खोलता है।

इससे कैसे बचें? उस यूआरएल को कैसे पकड़ें ताकि मैं अपने UIWebView के साथ उपयोग कर सकूं?

उत्तर

7

सबसे आसान तरीका है तो तरह UITextView पर webView:decidePolicyForNavigationAction:request:frame:decisionListener: विधि ओवरराइड करने के लिए है:

@interface UITextView (Override) 
@end 

@class WebView, WebFrame; 
@protocol WebPolicyDecisionListener; 

@implementation UITextView (Override) 

- (void)webView:(WebView *)webView decidePolicyForNavigationAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id <WebPolicyDecisionListener>)listener 
{ 
    NSLog(@"request: %@", request); 
} 
@end 

यह आपके एप्लिकेशन में सभी UITextView रों को प्रभावित करेगा। यदि आपको केवल एक दृश्य पर इसकी आवश्यकता है, तो उप-वर्ग बनाएं और उस पर विधि को ओवरराइड करें।

नोट: यह तकनीकी रूप से एक निजी एपीआई है और इसे किसी भी समय हटाया जा सकता है। सार्वजनिक एपीआई के माध्यम से ऐसा करने का कोई तरीका नहीं है।

संपादित करें: आईओएस 7.0 के रूप में UITextViewDelegate पर इसका समर्थन करने के लिए एक नई विधि पेश की गई है। विवरण के लिए निहाद का जवाब देखें।

+1

Uhm, निजी जा रहा है, आपको लगता है कि यह होने वाला है कर ऐपस्टोर स्वीकृति के लिए एक समस्या? बीटीडब्ल्यू आज रात मैं आपका कोड जांचूंगा और मैं आपको बता दूंगा। धन्यवाद। – crash

+0

यह ऐसी चीज नहीं है जिसे वे जांच सकते हैं, लेकिन भविष्य में ओएस परिवर्तनों के लिए आपको थोड़ा सा जोखिम हो सकता है (यह डेस्कटॉप समकक्ष में एक सार्वजनिक एपीआई है और यह संभावना नहीं है कि ऐप्पल वेबकिट का उपयोग करना बंद कर देगा, लेकिन यह संभव है) । सबसे बुरा यह हो सकता है कि व्यवहार मोबाइलसाफरी लॉन्च करने के डिफ़ॉल्ट पर वापस आ जाएगा। – rpetrich

+0

बिल्कुल सही। यह सुचारू रूप से काम किया। धन्यवाद! – crash

20

यह एक पुराना सवाल है लेकिन किसी को भी ऐसा करने का एक अद्यतन तरीका ढूंढ रहा है।

अपने ViewController कि अपने ViewController के मीटर फ़ाइल में एक प्रतिनिधि के रूप UITextView रखती है और सिर्फ जोड़ने असाइन करें:

-(BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange{ 

    //Do something with the URL 
    NSLog(@"%@", URL); 


    return NO; 
} 
+0

मेरा मानना ​​है कि यह आईओएस 7 के रूप में उचित तरीका है। इससे मेरी मदद की, तो धन्यवाद! – ajfigueroa

+0

@ निहाद धन्यवाद बहुत पुराना समाधान से यह साफ और आसान लगता है। – sunny

2
स्विफ्ट 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 
    } 

} 
संबंधित मुद्दे