I have a very simple UIWebView with content from my application bundle. I would like any links in the web view to open in Safari instead of in the web view. Is this possible?
相关问题
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
- Get the NSRange for the visible text after scroll
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- Swift - hide pickerView after value selected
- How do you detect key up / key down events from a
- didBeginContact:(SKPhysicsContact *)contact not in
- Attempt to present UIAlertController on View Contr
The accepted answer does not work.
If your page loads URLs via Javascript, the
navigationType
will beUIWebViewNavigationTypeOther
. Which, unfortunately, also includes background page loads such as analytics.To detect page navigation, you need to compare the
[request URL]
to the[request mainDocumentURL]
.This solution will work in all cases:
The other answers have one problem: they rely on the action you do and not on the link itself to decide whether to load it in Safari or in webview.
Now sometimes this is exactly what you want, which is fine; but some other times, especially if you have anchor links in your page, you want really to open only external links in Safari, and not internal ones. In that case you should check the
URL.host
property of your request.I use that piece of code to check whether I have a hostname in the URL that is being parsed, or if it is embedded html:
You can of course adapt the regular expression to fit your needs.