菜鸟教程小白 发表于 2022-12-12 20:29:50

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


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

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

<p><strong>问题</strong></p>

<p>我应该在哪里控制这个流程?</p>

<p>我不确定如何在第二次打开应用程序时检查 sessionID。 </p>

<p>这是我的
app_delegate.rb</p>

<pre><code>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
</code></pre>

<p>main_controller.rb</p>

<pre><code>class MainController &lt; 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[&#34;sessionId&#34;]
    if @sessionId[&#34;sessionId&#34;] == nil
      rmq.append(UIButton, :submit_button).on(:touch) do |sender|
      puts &#34;pressed #{@username.text} #{@password.text}&#34;
      login_user (@username.text, @password.text)
      end
    end
end

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

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

def init_nav
    self.title = &#34;Main&#34;
end
end
</code></pre>

<p>最后是 <code>test_controller.rb</code></p>

<pre><code>class TestController &lt; 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 = &#34;TEST&#34;
end
end
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我认为您可以使用类似这样的某种辅助类</p>

<h3>AuthManager.rb</h3>

<pre><code>class AuthManager
attr_accessor :sessionId

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

def init
    @keychain = KeychainItemWrapper.alloc.initWithIdentifier(&#39;EthanApp&#39;, 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(&#39;http://localhost:9000/&#39;) do
      header &#39;Accept&#39;, &#39;application/json&#39;
      header &#39;Content-Type&#39;, &#39;application/json&#39;
      request_serializer :json
      response_serializer :json
    end
    client.post(&#39;user/signup&#39;, username: uname, password: pwd) do |result|
      ap result
      ap result.object
      if result.success?
      @keychain.setObject result.object[:sessionId], forKey: KSecAttrAccount
      else
      App.alert(&#39;login failed: invalid username and/or password&#39;)
      end
    end
end

def logout
    @keychain.setObject nil, forKey: KSecAttrAccount
end
end
</code></pre>

<h3>将此添加到您的 app_delegate.rb</h3>

<pre><code>AuthManager.instance.init # initialize singleton object
</code></pre>

<h3>main_controller.rb</h3>

<pre><code># 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
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何只显示一次登录屏幕?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/22461836/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/22461836/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何只显示一次登录屏幕?