Open website with SFSafariViewController from auto-detected UITextView link

I have a UIViewControllerc UITextViewthat automatically detects hyperlinks in text. It works correctly, but I would use SFSafariViewControllerit to open links, to stay inside my application, and not open a separate browser, which is out of the box behavior.

Below I will talk about this below, but websites still open a separate Safari browser, and not inside my application. I do not receive any errors or warnings, but the detected sites are still open in a separate browser, and not in my application. The method UITextViewDelegatedoes not seem to be called (I ran the log statement for verification).

I looked at UITextViewDelegate, and I think I want to use this method to open a website discovered with UITextView:

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
    // Fill code in here to use SFSafariViewController
    return YES;
}

What i have done so far:

1) Imported Safari services, made a delegate declaration and declared a delegate property in MyViewController. h

@import SafariServices;

@interface MyViewController : UIViewController <UITextViewDelegate, SFSafariViewControllerDelegate>

@property(nonatomic, weak, nullable) id< SFSafariViewControllerDelegate, SFSafariViewControllerDelegate > delegate;

2) Added a delegate section to my .m file and tried to populate this method with the stub above:

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
    SFSafariViewController *websiteToOpen = [[SFSafariViewController alloc]initWithURL:URL entersReaderIfAvailable:YES];
    websiteToOpen.delegate = self;
    [self presentViewController:websiteToOpen animated:YES completion:nil];
    return YES;
}

I am sure that it hurts me 100%, but I can’t overcome the finish line. What am I missing?

+4
source share
3 answers

if someone is looking for an implementation of Swift 3 .

Solution that works:

1.Using Safari Services

import SafariServices

2. , , textView

let myLink = "google.com"
let myText = "here is my link: \(myLink)"
let myAttributedText = NSMutableAttributedString(string: myText)
let rangeOfMyLink = myText.range(of: myLink)
if rangeOfMyLink.location != NSNotFound {
    myAttributedText.addAttributes([NSLinkAttributeName: myLink], range: rangeOfMyLink)
}
myTextView.attributedText = myAttributedText
myTextView.delegate = self

3. VC:

class MyViewController: UIViewController, UITextViewDelegate

4. :

func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool {
    let safariVC = SFSafariViewController(url: URL)
    present(safariVC, animated: true, completion: nil)
    return false
}

, !

+5

YES , , , URL ( Safari). , NO .

:

  • Safari URL http/https. , URL.scheme Safari.
  • textView:shouldInteractWithURL:inRange: , URL-...

    , - URL- - .

    ... . "" - .., , , , .

UITapGesturRecognizer UITextView, .

+3

, .

-, delegate. viewDidLoad:

myTextView.delegate = self;

< <22 > URL-:

- (BOOL)textView:(UITextView *)textView 
        shouldInteractWithURL:(NSURL *)url 
        inRange:(NSRange)characterRange
{
    [self presentViewController:websiteToOpen animated:YES completion:nil];
    return NO;
}

, , .. "". - ...

+1
source

Source: https://habr.com/ru/post/1610680/


All Articles