在Turbolinks-ios使用Devise的网页页面进行登录需要注意将表单变成ajax形式提交。
Rails部分
Rails的页面中,中使用ajax提交登录表单。
overwrite devise的session的create方法。结束时候渲染create.js.erb
模版。
create.js.erb
中使用Turbolinks.visit(after_url, {action: "replace"})
方法转向登录后的页面。
Swift 部分
- delegate页面中的turbolinks的链接时候对应的处理。
1 2 3 4 5 6 7 8 9 10 11
| extension ViewController: SessionDelegate { func session(_ session: Session, didProposeVisitToURL URL: URL, withAction action: Action) { visit(URL: URL as NSURL, action: action) } func session(_ session: Session, didFailRequestForVisitable visitable: Visitable, withError error: NSError) { let alert = UIAlertController(title: "Error", message: error.localizedDescription, preferredStyle: .alert) alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil)) present(alert, animated: true, completion: nil) } }
|
visit
函数中按传来的action进行navigationController stack的替换。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| func visit(URL: NSURL, action: Action) { let visitable = WebViewController() visitable.visitableURL = URL as URL! switch action { case .Advance: pushViewController(visitable, animated: true) break case .Replace: popViewController(animated: false) pushViewController(visitable, animated: false) break default: pushViewController(visitable, animated: true) break } session.visit(visitable) }
|
要特别注意的是处理replace的时候我们需要把登录页面从navigationController的历史里替换掉,否则我们登录之后点击返回上一页会返回登录页面的快照。
另外注意animated: false
,这样用户就看不到我们替换的过程。