I have a UIViewController
c UITextView
that automatically detects hyperlinks in text. It works correctly, but I would use SFSafariViewController
it 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 UITextViewDelegate
does 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 {
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?