I have a UIViewController
with a UITextView
that auto-detects hyperlinks in its text. It works properly, but I'd to use SFSafariViewController
to open the links so I stay "inside" my app, rather than opening a separate browser, which is the "out of the box" behavior.
I've taken the steps outline below, but websites still open a separate Safari browser, rather than inside my app. I get no errors or warnings, but the websites detected still open in a separate browser, not within my app. The UITextViewDelegate
method doesn't appear to be getting called (I threw a log statement in to check).
I looked at UITextViewDelegate
and I think I want to use this method to open the website that's detected by the UITextView
:
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
// Fill code in here to use SFSafariViewController
return YES;
}
What I've done so far:
1) Imported SafariServices, made 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 fill in this method from 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'm certain it's 100% me mucking this up, but I'm unable to get over the finish line. What am I missing?