OStack程序员社区-中国程序员成长平台

标题: ios - 如何只显示一次登录屏幕? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 20:29
标题: ios - 如何只显示一次登录屏幕?

当用户首次打开应用程序时,我会显示登录屏幕。用户成功登录后,我在 NSUserDefaults 中存储了一个 session ID。然后下次用户打开应用程序时,我检查是否设置了 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

main_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



Best Answer-推荐答案


我认为您可以使用类似这样的某种辅助类

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

main_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/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4