gpt4 book ai didi

ios - 如何只显示一次登录屏幕?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:35:40 24 4
gpt4 key购买 nike

当用户首次打开应用程序时,我会显示登录屏幕。用户成功登录后,我将 session ID 存储在 NSUserDefaults 中。然后下次当用户打开应用程序时,我会检查是否设置了 session ID。如果已设置,那么我不想显示登录屏幕。

所以第一次我想显示 main_controller 但是当用户在成功登录后第二次打开应用程序时我想显示 test_controller.rb

问题

我应该在哪里控制这个流程?

我不确定如何在应用程序打开时第二次检查 session ID。

这是我的app_delegate.rb

class AppDelegate
attr_reader :window

def application(application, didFinishLaunchingWithOptions:launchOptions)
@window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)

main_controller = MainController.alloc.initWithNibName(nil, bundle: nil)
@window.rootViewController = UINavigationController.alloc.initWithRootViewController(main_controller)

@window.makeKeyAndVisible
true
end
end

主 Controller .rb

class MainController < UIViewController
def viewDidLoad
super

self.edgesForExtendedLayout = UIRectEdgeNone

rmq.stylesheet = MainStylesheet
init_nav
rmq(self.view).apply_style :root_view

@username = rmq.append(UITextField, :username).focus.get
@password = rmq.append(UITextField, :password).focus.get
@sessionId = NSUserDefaults.standardUserDefaults
puts @sessionId["sessionId"]
if @sessionId["sessionId"] == nil
rmq.append(UIButton, :submit_button).on(:touch) do |sender|
puts "pressed #{@username.text} #{@password.text}"
login_user (@username.text, @password.text)
end
end
end

def login_user(uname, pwd)
client = AFMotion::Client.build("http://localhost:9000/") do
header "Accept", "application/json"
header "Content-Type", "application/json"
request_serializer :json
response_serializer :json
end

client.post("user/signup", username: uname, password: pwd) do |result|
@sessionId["sessionId"] = result.object["sessionId"]
end
end

def init_nav
self.title = "Main"
end
end

最后是 test_controller.rb

class TestController < UIViewController

def viewDidLoad
super

# Sets a top of 0 to be below the navigation control
self.edgesForExtendedLayout = UIRectEdgeNone

rmq.stylesheet = MainStylesheet
init_nav
rmq(self.view).apply_style :root_view

end

def init_nav
self.title = "TEST"
end
end

最佳答案

我想你可以使用像这样的辅助类

AuthManager.rb

class AuthManager
attr_accessor :sessionId

def self.instance
Dispatch.once { @instance ||= new}
@instance
end

def init
@keychain = KeychainItemWrapper.alloc.initWithIdentifier('EthanApp', accessGroup: nil)
self.sessionId = @keychain.objectForKey KSecAttrAccount
end

def need_login?
return (self.sessionId.nil? || self.sessionId.empty?)
end

def login(username, password)
client = AFMotion::Client.build('http://localhost:9000/') do
header 'Accept', 'application/json'
header 'Content-Type', 'application/json'
request_serializer :json
response_serializer :json
end
client.post('user/signup', username: uname, password: pwd) do |result|
ap result
ap result.object
if result.success?
@keychain.setObject result.object[:sessionId], forKey: KSecAttrAccount
else
App.alert('login failed: invalid username and/or password')
end
end
end

def logout
@keychain.setObject nil, forKey: KSecAttrAccount
end
end

将此添加到您的 app_delegate.rb

AuthManager.instance.init # initialize singleton object

主 Controller .rb

# You can control your login screen by call this method
if AuthManager.instance.need_login?
rmq.append(UIButton, :submit_button).on(:touch) do |sender|
AuthManager.instance.login(username, password)
end
else
# Display TestController
end

关于ios - 如何只显示一次登录屏幕?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22461836/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com