I make request with my data. I want to get token from Instagram API. When I paste my url in browser, I see token. But I need to get it programmatically. How can I do it?
webView.load(URLRequest(url: url))
You just want to read the token from the header? You could try this. Set:
webView.navigationDelegate = self
And then in the WKNavigationDelegate method:
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Void) {
print(navigationAction.request.allHTTPHeaderFields)
}
Related
For example, I write as follows, then it works correctly, that is, the content that comes is displayed as it should and when the user clicks the link, it doesn't redirect anywhere.
extension AuthorizationContentView: WKNavigationDelegate {
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Void) {
if navigationAction.navigationType == .linkActivated {
decisionHandler(.cancel)
} else {
decisionHandler(.allow)
}
}
}
BUT I need to cover all the cases of clicking on the link and not just linkActivated(A link with an href attribute was activated by the user), but if I write just decisionHandler(.cancel), then the content is not displayed and it is unclear why so.
extension AuthorizationContentView: WKNavigationDelegate {
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Void) {
decisionHandler(.cancel)
return
}
}
UPD:
func setupWebView(content: String) {
let supportDarkCSS = "<style>:root { color-scheme: light dark; }</style>"
contentStackView.removeAllArrangedSubviews()
webView.loadHTMLString(content + supportDarkCSS, baseURL: nil)
contentStackView.addArrangedSubview(webView)
}
Ok, so after I understand what you are trying to do, the .linkActivated navigation type does not include all of the cases you are trying to block.
I think the best options is to keep intercept the requests using the func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Void) delegate function.
You can use the URLRequest property on the WKNavigationActioni.e navigationAction.request.
Then you can use the URLRequest metadata, for example, the URL itself (navigationAction.request.url) or the HTTP method, and understand if that request needs to be blocked.
cy) -> Void) {
attempts to
navigationAction.request
setValue("Cookies": ...
in
public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPoli
has no effect
(setCookie likewise has no effect on that webview configuration)
Any ideas? ios 12 and 13
I have a WKWebView in my application, there are some buttons inside the WKWebView on clicking on it, a new page is not loaded. By the look of it, they are trying to open an about_blank page. In the browser, the link is opening in a new page and the URL is loaded.
The way that you do this is by implementing WKUIDelegate and override this method
func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView?
If you return a new WKWebView with the passed configuration, the request will be loaded to a new webview. Since target=_blank tries to open a new window in browser, this method compensates for that in iOS. You could launch a new UIViewController with this new WKWebView where the request for target=_blank page.
Here is a simple example, where I substitute the main WKWebView with new webView, and load the request in same viewcontroller,
extension ViewController: WKNavigationDelegate, WKUIDelegate {
public func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
let newWebView = WKWebView(frame: self.webView.frame,
configuration: configuration)
view.addSubview(newWebView)
return newWebView
}
}
This in someway works like a tab in browser. But, bear in mind that with the change as above, forward and backward do not work, since you have created a new webview. If you want everything to work, I suggest that you load a new viewcontroller with this webview or create a new view where this would be loaded and shown, and you could switch to other main webview.
I went with this approach, even though this approach opens the URL in a device safari browser, anyway those links are pointing to external links, this method helped
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Void) {
if navigationAction.targetFrame == nil {
if let url = navigationAction.request.url {
let app = UIApplication.shared
if app.canOpenURL(url) {
app.open(url, options: [:], completionHandler: nil)
}
}
}
decisionHandler(.allow)
}
Swift Solution
public func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
if navigationAction.targetFrame == nil {
self.webView.load(navigationAction.request)
}
return nil
}
You need to delegate WKUIDelegate
I'm trying to get access_token of an app from Facebook. When user login Facebook, It returns access_token. I can get it on chrome in below section (Screenshot). However, I can't get that response on WkWebview. I think It is because, chrome shows last response of url (redirected one) but WkWebview shows first one.
func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: #escaping (WKNavigationResponsePolicy) -> Void) {
print(navigationResponse.response)
let headers = (navigationResponse.response as! HTTPURLResponse).allHeaderFields
let req = URLRequest(url: navigationResponse.response.url!)
decisionHandler(.allow)
}
I am not sure if i truly understand your question.
func webView(_ webView: WKWebView, decidePolicyFor navigationResponse is used for deciding if a navigation should happen after the response is known.
Try monitor func webView(_ webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!) or func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) if you want to obtain the full callback Url from facebook with the token.
In a WKWebView, every time I tap on a URL, the navigationType is .other. When does navigationType equal .linkActivated?
Very unlikely. Maybe you are interpreting the raw values wrong?
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Void) {
print(navigationAction.navigationType.rawValue)
// -1 "other" seen when assigning the url programatically
// 0 "linkActivated" a link with an href attribute was tapped
// 3 "reload" page was refreshed
}
See the docs where you can click on the enumerations listed and and see their raw values