簡體   English   中英

如何在swift中登錄viewController后設置swrevealController

[英]how to set swrevealController after my login viewController in swift

我正在開發一個應用程序,其中我在登錄頁面中具有登錄頁面和儀表板頁面,用戶可以使用facebook登錄,登錄后我導航用戶以控制面板一切正常

但是在儀表板中,我將添加側菜單(通過使用SWRevealViewcontroller),如果我將swrevealviewcontroller設置為intialView,則可以,但是我看不到登錄頁面

首先,我必須帶上登錄頁面,然后將其移至儀表板(其中包含側菜單項)。 在這里,我使用了兩個navigationControllers,一個用於登錄,另一個用於swrevealViewcontroller

我現在不知道如何從登錄頁面移動到儀表板(其中包含側菜單項)

這是我的應用程序代表

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject:AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        print("at delegate")
        var configureError: NSError?
        GGLContext.sharedInstance().configureWithError(&configureError)
        assert(configureError == nil, "Error configuring Google services: \(configureError)")
        GIDSignIn.sharedInstance().delegate = self


        if NSUserDefaults.standardUserDefaults().boolForKey("kUserLogin")
        {
            let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
            let root:UIViewController = mainStoryboardIpad.instantiateViewControllerWithIdentifier("RevealView")
            self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
            self.window?.rootViewController = root
            self.window?.makeKeyAndVisible()
        }
        else
        {
            let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
            let initialViewControlleripad : UIViewController = mainStoryboardIpad.instantiateViewControllerWithIdentifier("loginView") as UIViewController
            let nav:UINavigationController = UINavigationController.init(rootViewController: initialViewControlleripad)
            self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
            self.window?.rootViewController = nav
            self.window?.makeKeyAndVisible()
        }

            return  FBSDKApplicationDelegate.sharedInstance().application(application,

                                                                          didFinishLaunchingWithOptions:launchOptions) || true
    }

這是我的登錄按鈕功能

 func loginButton(loginButton: FBSDKLoginButton!,didCompleteWithResult result : FBSDKLoginManagerLoginResult!, error: NSError!)

    {
         print("come to ")
        FBSDKGraphRequest.init(graphPath: "me", parameters: ["fields":"name,picture.type(large),gender,email"]).startWithCompletionHandler { (connection, result, error) -> Void in
            print("come to print")
            let strName: String = (result.objectForKey("name") as? String)!
            print("before getting token")
           var token = FBSDKAccessToken.currentAccessToken().tokenString
            print(token)
            print("after getting token")
            print(result)
            let Gender : String = (result.objectForKey("gender") as? String)!
            let Id = (result.objectForKey("id") as? String)!
             let deviceId = UIDevice.currentDevice().identifierForVendor?.UUIDString
          let UUID = NSUUID().UUIDString
            print(UUID)
            var DeviceId = deviceId
            print(DeviceId)
            print(Id)
            print(Gender)
             print(strName)
           var strEmail : String
            if ((result.objectForKey("email") as? String) != nil){
                strEmail = (result.objectForKey("email") as? String)!
            }else {
                strEmail = "not available"
            }
            //let strEmail: String = (result.objectForKey("email") as? String)!
            var userID = result["id"] as! NSString
            var facebookProfileUrl = "http://graph.facebook.com/\(userID)/picture?type=large"
           // print(strEmail)
            let request = NSMutableURLRequest(URL: NSURL(string: "http://projects.svirtzone.com/wified/api/authentication.php")!)
            request.HTTPMethod = "POST"
            let postString = "username=\(strName)&email=\(strEmail)&action=auth&pic=\(facebookProfileUrl)&dob=1989/05/05&logintype=Facebook&gender=\(Gender)&device_type=Mac&facebookid=\(Id)&agerange=15&deviceid=\(deviceId)&accesstoken=\(token)"
            request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
            let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in
                guard error == nil && data != nil else {                                                          // check for fundamental networking error
                    print("error=\(error)")
                    return
                }
                if let httpStatus = response as? NSHTTPURLResponse where httpStatus.statusCode != 200 {           // check for http errors
                    print("statusCode should be 200, but is \(httpStatus.statusCode)")
                    print("response = \(response)")
                }

                let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
                print("responseString = \(responseString)")
            }
            task.resume()
                              }

        if(error != nil){

            print(error.localizedDescription)
            return
        }
       print("before main view")
        let defaults = NSUserDefaults.standardUserDefaults()
        if !defaults.boolForKey("kUserLogin")
        {
            print("inside if***")
            defaults.setBool(true, forKey: "kUserLogin")
        }
        NSUserDefaults.standardUserDefaults().setBool(false, forKey: "kUserLogin")
        let app = UIApplication.sharedApplication().delegate as! AppDelegate
        let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let initialViewControlleripad : UIViewController = mainStoryboardIpad.instantiateViewControllerWithIdentifier("RevealView") as UIViewController
        let nav:UINavigationController = UINavigationController.init(rootViewController: initialViewControlleripad)
        app.window = UIWindow(frame: UIScreen.mainScreen().bounds)
        app.window?.rootViewController = nav
        app.window?.makeKeyAndVisible()     

    }

這是我的主要viewController(我用swrevealViewController設置了它)在這里,我有側邊菜單項“菜單”,此項目現在禁用

 @IBOutlet var Menu: UIBarButtonItem!

    override func viewDidLoad() {
        super.viewDidLoad()        
        Menu.target = self.revealViewController()
        Menu.action = Selector("revealToggle:")
        self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())    

    }

這是我在appDelegate中的Google登錄代碼

func signIn(signIn: GIDSignIn!, didSignInForUser user: GIDGoogleUser!,
            withError error: NSError!) {

    if (error == nil) {
       print("at delegate5")
        // Perform any operations on signed in user here.
        let userId = user.userID                  // For client-side use only!
        let idToken = user.authentication.idToken // Safe to send to the server
        let fullName = user.profile.name
        let givenName = user.profile.givenName
        let familyName = user.profile.familyName
        let email = user.profile.email
        var imageURL = ""
        if user.profile.hasImage {
            imageURL = user.profile.imageURLWithDimension(100).absoluteString
        }

        NSNotificationCenter.defaultCenter().postNotificationName(
            "ToggleAuthUINotification",
            object: nil,
            userInfo: ["statusText": "Signed in user:\n\(fullName)","email" : email, "imageURL" : imageURL])
         print( imageURL)
       let postString = "username=\(fullName)&email=\(email)&action=auth&pic=\(imageURL)&dob=1989/05/05&logintype=Google&gender=female&device_type=Mac&facebookid=no&agerange=15&deviceid=not available&accesstoken=\(idToken)"
        let request = NSMutableURLRequest(URL: NSURL(string: "http://projects.svirtzone.com/wified/api/authentication.php")!)
        request.HTTPMethod = "POST"
                    request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)

        let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in
            guard error == nil && data != nil else {                                                          // check for fundamental networking error
                print("error=\(error)")
                return
            }
            if let httpStatus = response as? NSHTTPURLResponse where httpStatus.statusCode != 200 {           // check for http errors
                print("statusCode should be 200, but is \(httpStatus.statusCode)")
                print("response = \(response)")
            }

            let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
            print("responseString = \(responseString)")
        }
        task.resume()

     } else {

        print("\(error.localizedDescription)")

        NSNotificationCenter.defaultCenter().postNotificationName(
            "ToggleAuthUINotification", object: nil, userInfo: nil)
        let defaults = NSUserDefaults.standardUserDefaults()
        if !defaults.boolForKey("kUserLogin")
        {
            print("inside if***")
            defaults.setBool(true, forKey: "kUserLogin")
        }
               }
  NSUserDefaults.standardUserDefaults().setBool(false, forKey: "kUserLogin")
    let app = UIApplication.sharedApplication().delegate as! AppDelegate
    let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let initialViewControlleripad : UIViewController = mainStoryboardIpad.instantiateViewControllerWithIdentifier("RevealView") as UIViewController
    let nav:UINavigationController = UINavigationController.init(rootViewController: initialViewControlleripad)
    app.window = UIWindow(frame: UIScreen.mainScreen().bounds)
    app.window?.rootViewController = nav
    app.window?.makeKeyAndVisible()   

}

有人可以幫我嗎?

試試這個,希望對您有所幫助!!!

登錄成功后,在LoginViewController中設置標志值

 let defaults = NSUserDefaults.standardUserDefaults()
 if !defaults.boolForKey("kUserLogin")
 {
        defaults.setBool(true, forKey: "kUserLogin")
 }

注銷時,設置標志值並使“登錄屏幕”為根用戶

NSUserDefaults.standardUserDefaults().setBool(false, forKey: "kUserLogin")
let app = UIApplication.sharedApplication().delegate as! AppDelegate
let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewControlleripad : UIViewController = mainStoryboardIpad.instantiateViewControllerWithIdentifier("LoginViewController") as UIViewController
let nav:UINavigationController = UINavigationController.init(rootViewController: initialViewControlleripad)
app.window = UIWindow(frame: UIScreen.mainScreen().bounds)
app.window?.rootViewController = nav
app.window?.makeKeyAndVisible()

步驟2將Rootviewcontroller設置為SVrevealviewcontroller

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    if NSUserDefaults.standardUserDefaults().boolForKey("kUserLogin")
        {
            let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
            let root:UIViewController = mainStoryboardIpad.instantiateViewControllerWithIdentifier("RootViewController")
            self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
            self.window?.rootViewController = root
            self.window?.makeKeyAndVisible()
        }
        else
        {
            let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
            let initialViewControlleripad : UIViewController = mainStoryboardIpad.instantiateViewControllerWithIdentifier("LoginViewController") as UIViewController
            let nav:UINavigationController = UINavigationController.init(rootViewController: initialViewControlleripad)
            self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
            self.window?.rootViewController = nav
            self.window?.makeKeyAndVisible()
        }

        return true
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM