To start, error 401 means “Unauthorized”, meaning that the request failed but could succeed if you authenticated properly.
Web sites usually authenticate requests in one of three ways:
Cookies
HTTP authentication (Basic, Digest, and so on)
Some custom scheme, like a custom HTTP header
The first approach is by far the most common, but you’ll have to confirm which approach is in play before moving on. There may be helpful hints in the 401 response itself (for example, a
WWW-Authenticate
header), or you may need to look at how the web browser is doing its authentication.
If, as I suspect, the site is using cookies, you’ll have to copy the cookies from the web view to your native request. This can be a bit tricky. There is, alas, no way to set up an WKWebView to share its cookie context with an NSURLSession (although that would make a fine enhancement request; if you do file such a request, please post your bug number, just for the record). In most cases you can get the required cookies from a web view by running JavaScript code in that web view (using
-evaluateJavaScript:completionHandler:
) to return the JavaScript
document.cookie
value.
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"