sathya chinnasamy sathya chinnasamy - 4 months ago 31
iOS Question

how to set swrevealController after my login viewController in swift

I am developing an app in which I have login page and dashboard page in login page user can login with facebook,after login I navigate the user to dashboard everything works fine till now

But in dashboard I am going to add side menu (by using SWRevealViewcontroller) If I give swrevealviewcontroller as intialView It works but I could not see the login page

First I have to bring login page then it should move to dashboard(which contains side menu item). Here I am using two navigationControllers one is to login and another one is for swrevealViewcontroller

I don't know how to move from login page to dashboard (which contains side menu item) now

This is my app delegate

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
}


this is my loginbutton function

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()

}


this is my main viewController(I set this with swrevealViewController) here I have side menu item "Menu" this Item is disabling now

@IBOutlet var Menu: UIBarButtonItem!

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

}


Can any one help me please ?

Answer

Try this I hope it would be helpful!!!

After the login successfully set flag value in LoginViewController

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

When You log out set the flag value and make Login Screen as root

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()

Step-2 Set Rootviewcontroller as 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
    }