Я пытаюсь создать устройство, работающее с turbolinks-iOS, и мне кажется, что я что-то упустил.
На iOS я пытаюсь показать защищенный вид: localhost:3000/lists
Ожидаемый результат - открытие моего AuthentificationController, но вместо этого представление загружается нормально.
На стороне Rails сервер отвечает:
Started GET "/lists" for 127.0.0.1 at 2019-03-29 15:06:35 +0100
Processing by ListsController#index as HTML
Completed 401 Unauthorized in 10ms (ActiveRecord: 0.0ms)
Started GET "/account/sign_in" for 127.0.0.1 at 2019-03-29 15:06:36 +0100
Processing by Users::SessionsController#new as HTML
...
Полный ApplicationController (остальные файлы такие же, как TurbolinksDemo https://github.com/turbolinks/turbolinks-ios/blob/master/TurbolinksDemo)
import UIKit
import WebKit
import Turbolinks
class ApplicationController: UINavigationController {
fileprivate let url = URL(string: "http://localhost:3000")!
fileprivate let webViewProcessPool = WKProcessPool()
fileprivate var application: UIApplication {
return UIApplication.shared
}
fileprivate lazy var webViewConfiguration: WKWebViewConfiguration = {
let configuration = WKWebViewConfiguration()
configuration.processPool = self.webViewProcessPool
configuration.applicationNameForUserAgent = "App"
return configuration
}()
fileprivate lazy var session: Session = {
let session = Session(webViewConfiguration: self.webViewConfiguration)
session.delegate = self
return session
}()
override func viewDidLoad() {
super.viewDidLoad()
navigationBar.isTranslucent = false
presentVisitableForSession(session, url: url.appendingPathComponent("lists"))
}
fileprivate func presentVisitableForSession(_ session: Session, url: URL, action: Action = .Advance) {
let visitable = MyViewController(url: url)
print("visited url: \(url)")
if action == .Advance {
pushViewController(visitable, animated: true)
} else if action == .Replace {
popViewController(animated: false)
pushViewController(visitable, animated: false)
}
session.visit(visitable)
}
fileprivate func presentAuthenticationController() {
let authenticationController = AuthenticationController()
authenticationController.delegate = self
authenticationController.webViewConfiguration = webViewConfiguration
authenticationController.url = url.appendingPathComponent("account/sign_in")
authenticationController.title = "Sign in"
let authNavigationController = UINavigationController(rootViewController: authenticationController)
present(authNavigationController, animated: true, completion: nil)
}
}
extension ApplicationController: SessionDelegate {
func session(_ session: Session, didProposeVisitToURL URL: Foundation.URL, withAction action: Action) {
presentVisitableForSession(session, url: URL, action: action)
}
func session(_ session: Session, didFailRequestForVisitable visitable: Visitable, withError error: NSError) {
NSLog("ERROR: %@", error)
guard let myViewController = visitable as? MyViewController, let errorCode = ErrorCode(rawValue: error.code) else { return }
switch errorCode {
case .httpFailure:
let statusCode = error.userInfo["statusCode"] as! Int
switch statusCode {
case 401:
presentAuthenticationController()
case 404:
myViewController.presentError(.HTTPNotFoundError)
default:
myViewController.presentError(Error(HTTPStatusCode: statusCode))
}
case .networkFailure:
myViewController.presentError(.NetworkError)
}
}
func sessionDidStartRequest(_ session: Session) {
application.isNetworkActivityIndicatorVisible = true
}
func sessionDidFinishRequest(_ session: Session) {
application.isNetworkActivityIndicatorVisible = false
}
}
extension ApplicationController: AuthenticationControllerDelegate {
func authenticationControllerDidAuthenticate(_ authenticationController: AuthenticationController) {
session.reload()
dismiss(animated: true, completion: nil)
}
}
Что мне не хватает?
Спасибо.