问题
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?
回答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.
}
}
回答2:
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.
回答3:
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:
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) } } }
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.
来源:https://stackoverflow.com/questions/38395104/refresh-safari-page-with-openurl