4

I am opening a web page in Safari using openURL: method, but if that page is already open in Safari and I call openURL:, iOS display the already opened page.

It should refresh or should get open in new tab. Is it possible?

Jacopo Penzo
  • 2,168
  • 2
  • 24
  • 29
Aashish Nagar
  • 1,207
  • 1
  • 14
  • 30

3 Answers3

1

I don't think there's a way to do that.

A possible easy solution, if you can, is implement a SFSafariViewController and open your URL within the app. At this stage you can control it like a UIWebView.

Plus you have a better UX!

- (void)openLink:(NSString *)url 
{
   NSURL *URL = [NSURL URLWithString:url]];

   if (URL) {
      if ([SFSafariViewController class] != nil) {
         SFSafariViewController *sfvc = [[SFSafariViewController alloc] initWithURL:URL];
         sfvc.delegate = self;
         [self presentViewController:sfvc animated:YES completion:nil];
      } 
      else {
         if (![[UIApplication sharedApplication] openURL:url]) {
            NSLog(@"%@%@",@"Failed to open url:",[url description]);
         }
      }
   } 
   else 
   {
      // will have a nice alert displaying soon.
   }
}
Jacopo Penzo
  • 2,168
  • 2
  • 24
  • 29
0

I know it's too late, but maybe this will help the other. I was facing the same issue, but I have resolved the issue using the following steps:

  1. I have cleared the cache everytime, before load the url in safari app. The code for clear cache is:

    URLCache.shared.removeAllCachedResponses()
    URLCache.shared.diskCapacity = 0
    URLCache.shared.memoryCapacity = 0
    
    if let cookies = HTTPCookieStorage.shared.cookies {
        for cookie in cookies {
            let calendar = Calendar.current
            if let twoDaysAgo = calendar.date(byAdding: .day, value: -2, to: Date()){
                HTTPCookieStorage.shared.deleteCookie(cookie)
                HTTPCookieStorage.shared.removeCookies(since:twoDaysAgo)
            }
        }
    }
    
  2. After that I have added the minor delay and then load the url using below code:

    DispatchQueue.main.asyncAfter(deadline: .now() + 1.0, execute: {
        UIApplication.shared.openURL(url)
    })
    

This solution worked for me, I have tested the app in iOS 12.0. Hope so this information will be helpful for others.

quant
  • 2,184
  • 2
  • 19
  • 29
gurmeet kaur
  • 111
  • 1
  • 10
-1

You can reload the web page just after opening it.

Use reload method of web view to reload web page

reload() – is used to reload current web page.

Vishal Sonawane
  • 2,637
  • 2
  • 16
  • 21
  • I am not using any webView. I am using openUrl method.( UIApplication.sharedApplication().openURL(NSURL(string: "https:eworkplaceapps.com")!) ) – Aashish Nagar Jul 15 '16 at 11:41