AWS Cognito sign in not working (Swift-iOS) - ios

I've integrated cognito into my xcode project. The sign up/password update features are working correctly. However I can't seem to get the sign in process to work. I turned on the logs and I get the following error
{"__type":"NotAuthorizedException","message":"Access Token has expired"}
Domain=com.amazonaws.AWSCognitoIdentityProviderErrorDomain Code=-1000 "Authentication delegate not set" UserInfo={NSLocalizedDescription=Authentication delegate not set}]
I have also implemented the AWSCognitoIdentityInteractiveAuthenticationDelegate delegate in the AppDelegate script.
Here's the AppDelegate code
class AppDelegate: UIResponder, UIApplicationDelegate {
class func defaultUserPool() -> AWSCognitoIdentityUserPool {
return AWSCognitoIdentityUserPool(forKey: userPoolID)
}
var window: UIWindow?
var loginViewController: LoginVC?
var navigationController: UINavigationController?
var storyboard: UIStoryboard?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
// Warn user if configuration not updated
if (CognitoIdentityUserPoolId == "us-east-1_TavWWBZtI") {
let alertController = UIAlertController(title: "Invalid Configuration",
message: "Please configure user pool constants in Constants.swift file.",
preferredStyle: .alert)
let okAction = UIAlertAction(title: "Ok", style: .default, handler: nil)
alertController.addAction(okAction)
self.window?.rootViewController!.present(alertController, animated: true, completion: nil)
//print("Please configure user pool constants in Constants.swift file.")
}
// setup logging
AWSDDLog.sharedInstance.logLevel = .verbose
AWSDDLog.add(AWSDDTTYLogger.sharedInstance)
// setup service configuration
let serviceConfiguration = AWSServiceConfiguration(region: CognitoIdentityUserPoolRegion, credentialsProvider: nil)
// create pool configuration
let poolConfiguration = AWSCognitoIdentityUserPoolConfiguration(clientId: CognitoIdentityUserPoolAppClientId,
clientSecret: CognitoIdentityUserPoolAppClientSecret,
poolId: CognitoIdentityUserPoolId)
// initialize user pool client
AWSCognitoIdentityUserPool.register(with: serviceConfiguration, userPoolConfiguration: poolConfiguration, forKey: AWSCognitoUserPoolsSignInProviderKey)
// fetch the user pool client we initialized in above step
let pool = AWSCognitoIdentityUserPool(forKey: AWSCognitoUserPoolsSignInProviderKey)
self.storyboard = UIStoryboard(name: "Main", bundle: nil)
pool.delegate = self
return AWSMobileClient.sharedInstance().interceptApplication(
application, didFinishLaunchingWithOptions:
launchOptions)
//return true
}
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
if let navigationController = self.window?.rootViewController as? UINavigationController {
if navigationController.visibleViewController is SummaryReportVC ||
navigationController.visibleViewController is GoalStatusReportVC || navigationController.visibleViewController is YearTotalsReportVC || navigationController.visibleViewController is DailyActivityReportVC {
return UIInterfaceOrientationMask.all
} else {
return UIInterfaceOrientationMask.portrait
}
}
return UIInterfaceOrientationMask.portrait
}
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}
extension AppDelegate: AWSCognitoIdentityInteractiveAuthenticationDelegate {
func startPasswordAuthentication() -> AWSCognitoIdentityPasswordAuthentication {
print("Calling signin VC from app delegate")
if (self.navigationController == nil) {
self.navigationController = self.storyboard?.instantiateViewController(withIdentifier: "NCFirst") as? UINavigationController
}
if (self.loginViewController == nil) {
self.loginViewController = self.navigationController?.viewControllers[0] as? LoginVC
}
DispatchQueue.main.async {
self.navigationController!.popToRootViewController(animated: true)
if (!self.navigationController!.isViewLoaded
|| self.navigationController!.view.window == nil) {
self.window?.rootViewController?.present(self.navigationController!,
animated: true,
completion: nil)
}
}
return self.loginViewController!
}
}
Here's my LoginVC code
class LoginVC: UIViewController {
#IBOutlet weak var loginButton: UIButton!
#IBOutlet weak var forgotPasswordLabel: UILabel!
#IBOutlet weak var signUpLabel: UILabel!
#IBOutlet weak var emailTF: UITextField!
#IBOutlet weak var passwordTF: UITextField!
var passwordAuthenticationCompletion: AWSTaskCompletionSource<AWSCognitoIdentityPasswordAuthenticationDetails>?
let pool = AWSCognitoIdentityUserPool(forKey: AWSCognitoUserPoolsSignInProviderKey)
var usernameText: String?
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.tintColor = UIColor.white
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
self.navigationController!.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController!.navigationBar.shadowImage = UIImage()
self.navigationController!.navigationBar.isTranslucent = true
loginButton.addTarget(self, action: #selector(loginUser), for: .touchUpInside)
loginButton.layer.cornerRadius = 18
emailTF.addPadding(.left(35))
passwordTF.addPadding(.left(35))
let tap = UITapGestureRecognizer(target: self, action: #selector(goToForgotPasswordVC))
let tap2 = UITapGestureRecognizer(target: self, action: #selector(goToSignUp1VC))
forgotPasswordLabel.isUserInteractionEnabled = true
forgotPasswordLabel.addGestureRecognizer(tap)
signUpLabel.isUserInteractionEnabled = true
signUpLabel.addGestureRecognizer(tap2)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.passwordTF.text = nil
self.emailTF.text = usernameText
}
#objc func loginUser() {
print("Got inside Login func")
if (self.emailTF.text != nil && self.passwordTF.text != nil) {
print("Calling login method now")
let authDetails = AWSCognitoIdentityPasswordAuthenticationDetails(username: self.emailTF.text!, password: self.passwordTF.text! )
self.passwordAuthenticationCompletion?.set(result: authDetails)
} else {
print("Empty fields")
let alertController = UIAlertController(title: "Missing information",
message: "Please enter a valid user name and password",
preferredStyle: .alert)
let retryAction = UIAlertAction(title: "Retry", style: .default, handler: nil)
alertController.addAction(retryAction)
}
}
#objc func goToActivitySessionsVC() {
let storyboard = UIStoryboard(name: "TabBar", bundle: nil)
let destVC = storyboard.instantiateViewController(withIdentifier: "TabBarVC")
self.navigationController?.pushViewController(destVC, animated: true)
self.navigationController?.isNavigationBarHidden = true
}
#objc func goToForgotPasswordVC() {
let storyboard = UIStoryboard(name: "ForgotPassword", bundle: nil)
let destVC = storyboard.instantiateViewController(withIdentifier: "ForgotPasswordVC")
self.navigationController?.pushViewController(destVC, animated: true)
}
#objc func goToSignUp1VC() {
let storyboard = UIStoryboard(name: "SignUp", bundle: nil)
let destVC = storyboard.instantiateViewController(withIdentifier: "SignUp1VC")
self.navigationController?.pushViewController(destVC, animated: true)
}
/* func checkLoginStatus() {
if !AWSSignInManager.sharedInstance().isLoggedIn {
showSignIn()
}
else {
print("Logged In")
AWSSignInManager.sharedInstance().logout(completionHandler: {(result: Any?, error: Error?) in
self.showSignIn()
print("Sign-out Successful");
})
}
}
}
*/
extension LoginVC: AWSCognitoIdentityPasswordAuthentication {
public func getDetails(_ authenticationInput: AWSCognitoIdentityPasswordAuthenticationInput, passwordAuthenticationCompletionSource: AWSTaskCompletionSource<AWSCognitoIdentityPasswordAuthenticationDetails>) {
print("Get details called")
self.passwordAuthenticationCompletion = passwordAuthenticationCompletionSource
DispatchQueue.main.async {
if (self.usernameText == nil) {
self.usernameText = authenticationInput.lastKnownUsername
}
}
}
public func didCompleteStepWithError(_ error: Error?) {
print("Did commplete step with error called")
DispatchQueue.main.async {
if let error = error as NSError? {
let alertController = UIAlertController(title: error.userInfo["__type"] as? String,
message: error.userInfo["message"] as? String,
preferredStyle: .alert)
let retryAction = UIAlertAction(title: "Retry", style: .default, handler: nil)
alertController.addAction(retryAction)
self.present(alertController, animated: true, completion: nil)
print(error.description)
} else {
self.emailTF.text = nil
self.dismiss(animated: true, completion: nil)
print("Got in else")
}
}
}
}
One other thing to note is that getDetails never gets called and so does the didCompleteStepWithError method. When I click the sign in button, nothing happens.

The AWS documentation is quite confusing. After much trial and error, I was able to successfully set up Cognito, sign up, authenticate on log in, and un-authenticate on sign out. To be quite honest, I don't fully understand why I call certain things. To the best of my ability, I will explain here.
Here's how Cognito works.. First it assumes that the user is already logged in and authenticated. It checks to see if this is true. This is the reason why the entry point for your storyboard is the view controller that users see after they are logged in. This is all done with the code that runs in your AppDelegate on launch. More on what you need to fix in that below.
If the user is not logged in, startPasswordAuthentication() will be called. In your code, (as it should be) this defined in the extension of AppDelegate for the protocol AWSCognitoIdentityInteractiveAuthenticationDelegate. Furthermore, startPasswordAuthentication() is called every time the user needs to log in. If the user is already logged in once the app starts, this is not called.
Another note on your question - getDetails is only called on launch if the user is not signed in. If on launch the user is not signed in, then this is called. It is also called if you are signed in and then you sign out.
So make sure the entry point for your storyboard is the logged-in screen.
On the statement that follows I am not entirely sure, so feel free to correct me if so: AWS automatically accesses the entry point upon successful log-in. Everything you are going in your #objc func loginUser() looks correct to me. That's what I have. But make sure your entry point is not the log-in screen but rather what shows after successful log in. Here is a picture of my storyboard:
Try adding the following. I am not quite sure why exactly this works, but it results in proper authentication:
In your AppDelegate, right after your variable for the storyboard, add a boolean isInitialized as such:
var isInitialized : Bool = false
Then add this code after you set up your Cognito configuration. (right before your return statement in didFinishLaunchingWithOptions) :
let didFinishLaunching = AWSSignInManager.sharedInstance().interceptApplication(application, didFinishLaunchingWithOptions: launchOptions)
if (!self.isInitialized) {
AWSSignInManager.sharedInstance().resumeSession(completionHandler: { (result: Any?, error: Error?) in
print("Result: \(String(describing: result)) \n Error:\(String(describing: error))")
})
self.isInitialized = true
}
Now replace the return statement you currently have for didFinishLaunching with the following:
return didFinishLaunching
Make sure you have this delegate set in your viewDidLoad() method for your login screen (Note you have to import AWSAuthCore):
AWSSignInManager.sharedInstance().delegate = self
and implement the protocol in your log-in VC as such:
extension LoginViewController : AWSSignInDelegate {
func onLogin(signInProvider: AWSSignInProvider, result: Any?, error: Error?) {
if error == nil {
}
}
}
Add these variables as class variables to your view controller that users see after they are logged in. They are referenced below.
var user : AWSCognitoIdentityUser?
var userAttributes : [AWSCognitoIdentityProviderAttributeType]?
/*
* :name: defaultUserPool
* :description: Returns the cognito identity pool for the global pool ID.
* :return: A Cognito Identity pool instantiation
*/
class func defaultUserPool() -> AWSCognitoIdentityUserPool {
return AWSCognitoIdentityUserPool(forKey: userPoolID)
}
Finally, make sure that you are checking the user attributes in the initial screen in viewWillAppear(). For example call the function fetchUserAttributes in this method:
func fetchUserAttributes() {
self.user = AppDelegate.defaultUserPool().currentUser()
self.user?.getDetails().continueOnSuccessWith(block: { [weak self = self] (task) -> Any? in
AWSSignInManager.sharedInstance().resumeSession(completionHandler: { (result: Any?, error: Error?) in
print("Result: \(String(describing: result)) \n Error:\(String(describing: error))")
})
guard task.result != nil else {
// alert error
return nil
}
self?.username = self?.user?.username
self?.userAttributes = task.result?.userAttributes
self?.userAttributes?.forEach({ (attribute) in
print("Name: " + attribute.name!)
})
DispatchQueue.main.async {
self?.setAttributeValues()
}
}
return nil
})
}
func resetAttributeValues() {
self.user = nil
self.userAttributes = nil
}
Finally, here is my code for signing out:
let comp = { [weak self = self] (_ result: Any?, _ error: Error?) -> Void in
if error == nil {
self?.user?.signOut()
self?.resetAttributeValues()
self?.fetchUserAttributes()
}
}
AWSSignInManager.sharedInstance().logout(completionHandler: comp)
I hope this helps. I understand this is really confusing, and to be honest, I was quite confused just writing this.. Good luck and feel free to message me with any questions.

You need to call getDetails.
In the sample app, they call getDetails in UserDetailTableViewController.
Try this line of code below pool.delegate = self in AppDelegate.
self.pool?.currentUser()?.getDetails()
I referred AWS Cognito User Pools in iOS (Swift) app

Related

How to make a global timer for the whole app

I want to make a timer that start on app load and when it finishes to alert a text and restart the app when alert is closed(when click the ok button of the alert). Have tried with a class timer but I can't send alert and reload the app. I need it to be working in the whole app, and when move to other view not restart it countdown. Is there any easy way without using CocoaPods.
import Foundation
class GlobalTokenTimer {
static let sharedTimer: GlobalTokenTimer = {
let timer = GlobalTokenTimer()
return timer
}()
var internalTimer: Timer?
var jobs = [() -> Void]()
func startTimer(withInterval interval: Double, andJob job: #escaping () -> Void) {
if internalTimer != nil {
internalTimer?.invalidate()
}
jobs.append(job)
internalTimer = Timer.scheduledTimer(timeInterval: interval, target: self, selector: #selector(doJob), userInfo: nil, repeats: true)
}
func stopTimer() {
guard internalTimer != nil else {
print("No timer active, start the timer before you stop it.")
return
}
jobs = [()->()]()
internalTimer?.invalidate()
}
#objc func doJob() {
guard jobs.count > 0 else{return}
}
}
Your timer is static and will work while app is alive. Implement some function that will show alert on topViewController of your application.
extension UIApplication {
// function that lets you get topViewController of the app
func topViewController(controller: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
if let navigationController = controller as? UINavigationController {
return topViewController(controller: navigationController.visibleViewController)
}
if let tabController = controller as? UITabBarController {
if let selected = tabController.selectedViewController {
return topViewController(controller: selected)
}
}
if let presented = controller?.presentedViewController {
return topViewController(controller: presented)
}
return controller
}
}
Inside your app delegate start your timer and show alert. In iOS you can't restart you app, but can set newly created MyStartViewController which is your home page for example and restart logic of your app.
UPD:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func startTimer() {
GlobalTokenTimer.sharedTimer.startTimer(withInterval: 10) {
let alertVC = UIAlertController(title: "Restart your app!", message: "Select ok!", preferredStyle: .alert)
alertVC.addAction(UIAlertAction(title: "Restart", style: .default) { [weak self] _ in
self?.window?.rootViewController = MyStartViewController()
startTimer()
})
application.topViewController()?.present(alertVC, animated: true)
}
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
startTimer()
}
}
Set your timer repeat: false and it would stop after finishing once.
internalTimer = Timer.scheduledTimer(timeInterval: interval, target: self, selector: #selector(doJob), userInfo: nil, repeats: false)
UPD:
You do nothing in your doJob function, change it to this.
#objc func doJob() {
guard jobs.count > 0 else { return }
jobs.forEach { $0() }
}

I can't get this delegate to fire and I'm not sure what I'm missing

I've got a login screen that authenticates with Amazon Cognito using the SDK. Once complete, it's supposed to call a delegate (or extension in iOS Swift). It is supposed to call the didCompleteWithError or the getDetails methods.
I've tried taking the examples I've been trying to follow this example but I haven't had luck (https://github.com/awslabs/aws-sdk-ios-samples/tree/master/CognitoYourUserPools-Sample/Swift). Any ideas? See my code for just the log in screen and AppDelegate.swift below. What am I doing wrong?
// LoginViewController.swift
import UIKit
import AWSCognitoIdentityProvider
import AWSCognitoAuth
import AWSMobileClient
import AWSUserPoolsSignIn
import AWSAuthUI
class LoginViewController: BaseViewController {
#IBOutlet var password: UITextField!
#IBOutlet var email: UITextField!
var pool: AWSCognitoIdentityUserPool?
var passwordAuthenticationCompletion: AWSTaskCompletionSource<AWSCognitoIdentityPasswordAuthenticationDetails>?
var usernameText: String?
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.password.text = nil
self.email.text = usernameText
self.navigationController?.setNavigationBarHidden(true, animated: false)
}
#IBAction func login_Tap(_ sender: Any) {
if (self.email.text != nil && self.password.text != nil) {
let authDetails = AWSCognitoIdentityPasswordAuthenticationDetails(username: self.email.text!, password: self.password.text! )
self.passwordAuthenticationCompletion?.set(result: authDetails)
} else {
let alertController = UIAlertController(title: "Missing information",
message: "Please enter a valid user name and password",
preferredStyle: .alert)
let retryAction = UIAlertAction(title: "Retry", style: .default, handler: nil)
alertController.addAction(retryAction)
}
}
}
extension LoginViewController: AWSCognitoIdentityPasswordAuthentication {
public func getDetails(_ authenticationInput: AWSCognitoIdentityPasswordAuthenticationInput, passwordAuthenticationCompletionSource: AWSTaskCompletionSource<AWSCognitoIdentityPasswordAuthenticationDetails>) {
print(passwordAuthenticationCompletionSource)
self.passwordAuthenticationCompletion = passwordAuthenticationCompletionSource
DispatchQueue.main.async {
if (self.usernameText == nil) {
self.usernameText = authenticationInput.lastKnownUsername
}
}
}
public func didCompleteStepWithError(_ error: Error?) {
print(error)
DispatchQueue.main.async {
if let error = error as NSError? {
let alertController = UIAlertController(title: error.userInfo["__type"] as? String,
message: error.userInfo["message"] as? String,
preferredStyle: .alert)
let retryAction = UIAlertAction(title: "Retry", style: .default, handler: nil)
alertController.addAction(retryAction)
self.present(alertController, animated: true, completion: nil)
} else {
self.email.text = nil
self.dismiss(animated: true, completion: nil)
}
}
}
}
AppDelegate.swift I have this
//
// AppDelegate.swift
import UIKit
import AWSCognitoAuth
import AWSSNS
import AWSCognitoIdentityProvider
import UserNotifications
import ESTabBarController_swift
import AWSMobileClient
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
var window: UIWindow?
var navigationController: UINavigationController?
var storyboard: UIStoryboard?
var loginViewController: LoginViewController?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// setup logging
AWSDDLog.sharedInstance.logLevel = .verbose
// setup service configuration
let serviceConfiguration = AWSServiceConfiguration(region: AWSRegionType.USEast1, credentialsProvider: nil)
// create pool configuration
let poolConfiguration = AWSCognitoIdentityUserPoolConfiguration(clientId: Constants.APIKeys.AWSClientID,
clientSecret: Constants.APIKeys.AWSSecret,
poolId: Constants.APIKeys.AWSPoolID)
// initialize user pool client
AWSCognitoIdentityUserPool.register(with: serviceConfiguration, userPoolConfiguration: poolConfiguration, forKey: "UserPool")
// fetch the user pool client we initialized in above step
let pool = AWSCognitoIdentityUserPool(forKey: "UserPool")
self.window = UIWindow(frame: UIScreen.main.bounds)
self.storyboard = UIStoryboard(name: "LaunchScreen", bundle: nil)
pool.delegate = self
AppController.sharedInstance.launchInWindow(aWindow: self.window)
return true
}
//MARK: Push Notification
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
/// Attach the device token to the user defaults
var token = ""
for i in 0..<deviceToken.count {
token = token + String(format: "%02.2hhx", arguments: [deviceToken[i]])
}
print(token)
UserDefaults.standard.set(token, forKey: "deviceTokenForSNS")
/// Create a platform endpoint. In this case, the endpoint is a
/// device endpoint ARN
let sns = AWSSNS.default()
let request = AWSSNSCreatePlatformEndpointInput()
request?.token = token
request?.platformApplicationArn = Constants.APIKeys.AWSSSNSARN
sns.createPlatformEndpoint(request!).continueWith(executor: AWSExecutor.mainThread(), block: { (task: AWSTask!) -> AnyObject? in
if task.error != nil {
print("Error: \(String(describing: task.error))")
} else {
let createEndpointResponse = task.result! as AWSSNSCreateEndpointResponse
if let endpointArnForSNS = createEndpointResponse.endpointArn {
print("endpointArn: \(endpointArnForSNS)")
Settings.setPushArn(endpointArnForSNS)
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "RegisteredForPush"), object: nil)
}
}
return nil
})
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
let visible = window?.visibleViewController()
if let data = userInfo["aps"] as? [AnyHashable: Any] {
if let route = data["route"] as? String {
switch route {
case "matchRequested":
var projectId = ""
if let project = data["projectId"] as? String {
projectId = project
}
var matchId = ""
if let match = data["matchId"] as? String {
matchId = match
}
let projectMatches = MatchRequestedViewController(withNotificationPayload: projectId, matchId: matchId)
visible?.navigationController?.pushViewController(projectMatches, animated: true)
case "projectDetails":
var projectId = ""
if let project = data["projectId"] as? String {
projectId = project
}
let projectMatches = ProjectDetailsViewController(withProject: TERMyProject(), orProjectId: projectId)
visible?.navigationController?.pushViewController(projectMatches, animated: true)
case "matched":
var projectId = ""
if let project = data["projectId"] as? String {
projectId = project
}
var matchId = ""
if let match = data["matchId"] as? String {
matchId = match
}
var originProject: TERMyProject = TERMyProject()
var matchedProject: TERMatchedProject = TERMatchedProject()
AppController.sharedInstance.AWSClient?.projectsGet(id:projectId).continueWith(block: { (task: AWSTask) -> Any? in
if let error = task.error {
print("Error: \(error)")
} else if let result = task.result {
if result is NSDictionary {
DispatchQueue.main.async {
let array = [result]
let parsedProject: [TERMyProject] = TERMyProject.parseFromAPI(array:array as! [NSDictionary])
for project in parsedProject {
originProject = project
}
// self.initialSetup()
}
AppController.sharedInstance.AWSClient?.projectsGet(id:matchId).continueWith(block: { (task: AWSTask) -> Any? in
if let error = task.error {
print("Error: \(error)")
} else if let result = task.result {
if result is NSDictionary {
DispatchQueue.main.async {
let array = [result]
let parsedProject: [TERMatchedProject] = TERMatchedProject.parseFromAPI(array:array as! [NSDictionary])
for project in parsedProject {
matchedProject = project
}
let projectMatches = MatchedViewController(withProject: originProject, match: matchedProject, isComplete: false)
visible?.navigationController?.pushViewController(projectMatches, animated: true)
}
}
}
return nil
})
}
}
return nil
})
default:
break
}
}
}
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print(error.localizedDescription)
}
// Called when a notification is delivered to a foreground app.
#available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
print("User Info = ",notification.request.content.userInfo)
completionHandler([.alert, .badge, .sound])
}
//MARK: Boiler-plate methods
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(_ application: UIApplication) {
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}
extension AppDelegate: AWSCognitoIdentityInteractiveAuthenticationDelegate {
func startPasswordAuthentication() -> AWSCognitoIdentityPasswordAuthentication {
if (self.navigationController == nil) {
self.navigationController = self.storyboard?.instantiateViewController(withIdentifier: "LoginViewController") as? UINavigationController
}
if (self.loginViewController == nil) {
self.loginViewController = self.navigationController?.viewControllers[0] as? LoginViewController
}
DispatchQueue.main.async {
self.navigationController!.popToRootViewController(animated: true)
if (!self.navigationController!.isViewLoaded
|| self.navigationController!.view.window == nil) {
self.window?.rootViewController?.present(self.navigationController!,
animated: true,
completion: nil)
}
}
return self.loginViewController!
}
}
// MARK:- AWSCognitoIdentityRememberDevice protocol delegate
extension AppDelegate: AWSCognitoIdentityRememberDevice {
func didCompleteStepWithError(_ error: Error?) {
}
func getRememberDevice(_ rememberDeviceCompletionSource: AWSTaskCompletionSource<NSNumber>) {
}
}
extension UIWindow {
func visibleViewController() -> UIViewController? {
if let rootViewController: UIViewController = self.rootViewController {
return UIWindow.getVisibleViewControllerFrom(vc: rootViewController)
}
return nil
}
class func getVisibleViewControllerFrom(vc:UIViewController) -> UIViewController {
switch(vc){
case is UINavigationController:
let navigationController = vc as! UINavigationController
return UIWindow.getVisibleViewControllerFrom( vc: navigationController.visibleViewController!)
break;
case is UITabBarController:
let tabBarController = vc as! UITabBarController
return UIWindow.getVisibleViewControllerFrom(vc: tabBarController.selectedViewController!)
break;
default:
if let presentedViewController = vc.presentedViewController {
//print(presentedViewController)
if let presentedViewController2 = presentedViewController.presentedViewController {
return UIWindow.getVisibleViewControllerFrom(vc: presentedViewController2)
}
else{
return vc;
}
}
else{
return vc;
}
break;
}
}
}

Error: Value of type 'AppDelegate?' has no member 'present'

I'm getting an error of
Value of type 'AppDelegate?' has no member 'present'
when I tried to keep my users logged in even when the app was quit.
Here are my view controllers, any idea how I'd keep my users logged in? And why am I getting this error?
I'm using Firebase in my code as my Database.
LoginViewController.swift
import UIKit
import Firebase
class LoginViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var emailField: UITextField!
#IBOutlet weak var pwField: UITextField!
func createAlert(title:String, message:String) {
let alert=UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "error" , style: UIAlertActionStyle.default, handler: { (action) in alert.dismiss(animated: true, completion: nil)
}))
self.present(alert, animated:true, completion: nil)
}
/* #IBAction func emailKeyboardField(_ sender: Any) {
hideKeyboard()
}
func hideKeyboard() {
emailField.resignFirstResponder()
pwField.resignFirstResponder()
}
*/
override func viewDidLoad() {
super.viewDidLoad()
emailField.delegate = self
// Do any additional setup after loading the view.
}
func emailKeyField(_ emailField: UITextField) -> Bool {
self.view.endEditing(true)
return true
}
#IBAction func loginPressed(_ sender: Any) {
guard emailField.text != "", pwField.text != "" else {return}
FIRAuth.auth()?.signIn(withEmail: emailField.text!, password: pwField.text!, completion: { (user, error) in
if let error = error {
print(error.localizedDescription)
self.createAlert(title: "Error", message: "We could not locate your account. Check your email and password.")
}
if let user = user {
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "usersVC")
self.present(vc, animated: true, completion: nil)
}
})
}
}
AppDelegate.swift
import UIKit
import Firebase
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
var window: UIWindow?
var actIdc = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge)
var container: UIView!
private let auth = FIRAuth.auth()
class func instance() -> AppDelegate {
return UIApplication.shared.delegate as! AppDelegate
}
func showActivityIndicator() {
if let window = window {
container = UIView()
container.frame = window.frame
container.center = window.center
container.backgroundColor = UIColor(white: 0, alpha: 0.8)
actIdc.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
actIdc.hidesWhenStopped = true
actIdc.center = CGPoint(x: container.frame.size.width / 2, y: container.frame.size.height / 2)
container.addSubview(actIdc)
window.addSubview(container)
actIdc.startAnimating()
}
}
func dismissActivityIndicatos() {
if let _ = window {
container.removeFromSuperview()
}
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
auth?.addStateDidChangeListener { [weak self] (_, user) in
if let user = user {
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "usersVC")
self.present(vc, animated: true, completion: nil) //error on this line
// user is already logged in
} else {
// user is not logged in
}
}
FIRApp.configure()
return true
}
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}
Any and all help would be appreciated.
App delegate is not of type ViewController, hence it doesn't have a present method to show views on it. Instead what you can do is show a view using it's window like:
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let homeC = storyboard.instantiateViewController(withIdentifier: "YOUR_VIEWCONTROLLER_IDENTIFIER_IN_STORYBOARD") as? HomeC
if homeC != nil {
homeC!.view.frame = (self.window!.frame)
self.window!.addSubview(homeC!.view)
self.window!.bringSubview(toFront: homeC!.view)
}

Unexpected quirky behavior from socket.io in Swift

As per title, I'm having some trouble dealing with socket.io. It connects really well and accordingly in the first view controller but weird things happen when it comes to second controller.
Here's the code:
First Controller: I have declared some global variable for connection purposes between both view controller.
import UIKit
import SocketIOClientSwift
import SwiftyJSON
import CoreData
//declare some global variable
var patientCoreData = [NSManagedObject]()
var numberOfUsersExisting:Int = 0 //assign to 0 by default
var appUserData: Patient? //for specific user
var pSample: Array<Patient> = [] //for all user
//initiate socket globally
let socket = SocketIOClient(socketURL: "localhost:3000", options: [
"reconnects": true
])
func reportStatus(){
socket.on("connect") {data, ack in
print("Report status: View Controller connected")
socket.emit("click", "Client app connected")
}
}
func readDataFromSocket(completion: (data:AnyObject)-> ()){
socket.on("reply") {data, ack in
print("database replied")
completion(data: data)
}//socket
}//readDataFromSOCKET
func importData(){
reportStatus()
socket.connect()
readDataFromSocket(){ data in
let json = JSON(data)
let nou = json[0].count
if nou > 0 {
print("Test(1st VC): grabbing data from database")
for var i=0; i<nou; ++i{
numberOfUsersExisting = nou
pSample += [Patient(id: json[0][i]["ID"].intValue, name: json[0][i]["Name"].stringValue, gender: json[0][i]["Gender"].stringValue, mileage: json[0][i]["Mileage"].doubleValue)]
pSample.sortInPlace({$0.globalPatientMileAge < $1.globalPatientMileAge})
}
print("Successfully grabbed data")
}else{
print("No user in the database")
numberOfUsersExisting = 0
}
}//readDataFromSocket
}
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout{
let prefs:NSUserDefaults = NSUserDefaults.standardUserDefaults()
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
print("First view appeared")
let prefs = NSUserDefaults.standardUserDefaults()
//if an user has logged in
let isLoggedIn = prefs.integerForKey("ISLOGGEDIN") as Int
if (isLoggedIn != 1){
print("No user currently, so heading to login screen")
socket.disconnect()
self.performSegueWithIdentifier("gotoLogin", sender: self)
}else{
print("ViewDidAppear: An user has been logged in")
let permissionToLoadData = prefs.integerForKey("ISLOGGEDIN")
if (permissionToLoadData != 1) {
print("Please grant permission to get data")
}else{
print("First view: connecting to database")
importData()
}//permission to load data
}
}//end of viewDidAppear
}
Second Controller:
import UIKit
import SocketIOClientSwift
import SwiftyJSON
import CoreData
var nou:Int?
class LoginViewController: UIViewController {
let prefs:NSUserDefaults = NSUserDefaults.standardUserDefaults()
let registeredUserID = NSUserDefaults.standardUserDefaults().stringForKey("registerPatientID")
let appDel:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
func displayAlertMessage(userMessage:String){
let alert = UIAlertController(title: "Alert", message: userMessage, preferredStyle: UIAlertControllerStyle.Alert)
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)
alert.addAction(okAction)
self.presentViewController(alert, animated: true, completion: nil)
}
func successMessage(userMessage:String){
let alert = UIAlertController(title: "Welcome Back", message: userMessage, preferredStyle: UIAlertControllerStyle.Alert)
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)
alert.addAction(okAction)
self.presentViewController(alert, animated: true, completion: nil)
}
#IBOutlet weak var loginPatientID: UITextField!
#IBAction func LoginButton(sender: AnyObject) {
let logInUserID = loginPatientID.text
if (logInUserID!.isEmpty){
displayAlertMessage("Please enter your Patient ID!")
return
}else{
print("Test: requesting login permission from database")
socket.emit("loginRequest", logInUserID!)
print("Test: requested")
socket.on("loginReply") {data, ack in
let jsonLogin = JSON(data)
if jsonLogin[0].intValue == 1{
print("Test: ID Matched, putting up ViewController")
self.prefs.setObject(logInUserID, forKey: "AppUserID")
self.prefs.setInteger(1, forKey: "ISLOGGEDIN")
self.prefs.synchronize()
let permissionToLoadData = self.prefs.integerForKey("ISLOGGEDIN")
if (permissionToLoadData != 1) {
print("Please grant permission to get data")
}else{
print("First view: connecting to database")
importData()
print("Did you import?")
}//permission to load data
self.loginPatientID.resignFirstResponder()
self.dismissViewControllerAnimated(true, completion: nil)
}else if jsonLogin[0].intValue == 0{
self.displayAlertMessage("Sorry, you are not assigned to this program")
}else if jsonLogin[0].intValue == 3{
print("Test: Query problem")
}else{
print("Test: not getting anything from ID database")
}
}//socket.on
}//else
}//login button
override func viewDidLoad() {
super.viewDidLoad()
print("Login View Controller loaded")
}
override func viewDidAppear(animated: Bool) {
socket.connect()
print("LoginVC: establishing connection")
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.view.endEditing(true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
You may have noticed that in First view controller, when the viewDidAppear() is launched, the app will checks if user is login or not. If somebody has already logged in, it's fine. If there is nobody logging in, it will perform a segue(modally segue) to Second view controller.
A login form will be presented in second view controller and once user hits the login button, you might wanna look at the code.
Let's assume that everything goes right until it comes to importData(), the function isn't launched at all but the app just goes on, why?
Here's a screenshot of the console, pay attention to "Did you import?", if the function is launched, the app should return some additional message from 1st view controller.
After struggling for a few days, I think I may have found the correct answer.
Eventually I defined 2 different socket handlers connection as such:
let loginSocket = SocketIOClient(socketURL: "localhost:3000")
let socket = SocketIOClient(socketURL: "localhost:3000", options: [
"reconnects": true
])
for both view controller.
If there is a conclusion I can draw from this conundrum is that we can't use single socket handler for socket methods from different view controller.

swift prepareforsegue with mmdrawercontroller

I have searched every where to find a solution to this error i am getting, i have also tried to use, nsuserdefaults, struct and global var to pass on my vars to other viewcontrollers. I am use mmdrawer and i have set a navigationcontrol on my first viewcontroller that who i named userOview and identifier membersArea. Whenever i try to use prepareforsegue i am getting the following error
Could not cast value of type 'xxxxxxxx.DjInformation' (0x115da8) to 'UINavigationController' (0x3ad405e0).
My appdelegate looks like this
import UIKit
import CoreData
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var centerContainer: MMDrawerController?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let prefs:NSUserDefaults = NSUserDefaults.standardUserDefaults()
let isLoggedIn:Int = prefs.integerForKey("ISLOGGEDIN") as Int
if (isLoggedIn == 1){
var rootViewController = self.window!.rootViewController
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var centerViewController = mainStoryboard.instantiateViewControllerWithIdentifier("memberArea") as! userOverview
var leftViewController = mainStoryboard.instantiateViewControllerWithIdentifier("LeftSideViewController") as! LeftSideViewController
var rightViewController = mainStoryboard.instantiateViewControllerWithIdentifier("RightSideViewController")as! RightSideViewController
var leftSideNav = UINavigationController(rootViewController: leftViewController)
var centerNav = UINavigationController(rootViewController: centerViewController)
var rightNav = UINavigationController(rootViewController: rightViewController)
centerContainer = MMDrawerController(centerViewController: centerNav, leftDrawerViewController: leftSideNav,rightDrawerViewController:rightNav)
centerContainer!.openDrawerGestureModeMask = MMOpenDrawerGestureMode.PanningCenterView;
centerContainer!.closeDrawerGestureModeMask = MMCloseDrawerGestureMode.PanningCenterView;
window!.rootViewController = centerContainer
window!.makeKeyAndVisible()
UIApplication.sharedApplication().setStatusBarHidden(true, withAnimation: UIStatusBarAnimation.None)
}
return true
}
func applicationWillResignActive(application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
func applicationDidEnterBackground(application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
// Saves changes in the application's managed object context before the application terminates.
self.saveContext()
}
// MARK: - Core Data stack
lazy var applicationDocumentsDirectory: NSURL = {
// The directory the application uses to store the Core Data store file. This code uses a directory named "bfd.Be_Fit_Donate" in the application's documents Application Support directory.
let urls = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
return urls[urls.count-1] as! NSURL
}()
lazy var managedObjectModel: NSManagedObjectModel = {
// The managed object model for the application. This property is not optional. It is a fatal error for the application not to be able to find and load its model.
let modelURL = NSBundle.mainBundle().URLForResource("Be_Fit_Donate", withExtension: "momd")!
return NSManagedObjectModel(contentsOfURL: modelURL)!
}()
lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator? = {
// The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it. This property is optional since there are legitimate error conditions that could cause the creation of the store to fail.
// Create the coordinator and store
var coordinator: NSPersistentStoreCoordinator? = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("Be_Fit_Donate.sqlite")
var error: NSError? = nil
var failureReason = "There was an error creating or loading the application's saved data."
if coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil, error: &error) == nil {
coordinator = nil
// Report any error we got.
var dict = [String: AnyObject]()
dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data"
dict[NSLocalizedFailureReasonErrorKey] = failureReason
dict[NSUnderlyingErrorKey] = error
error = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
// Replace this with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog("Unresolved error \(error), \(error!.userInfo)")
abort()
}
return coordinator
}()
lazy var managedObjectContext: NSManagedObjectContext? = {
// Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.) This property is optional since there are legitimate error conditions that could cause the creation of the context to fail.
let coordinator = self.persistentStoreCoordinator
if coordinator == nil {
return nil
}
var managedObjectContext = NSManagedObjectContext()
managedObjectContext.persistentStoreCoordinator = coordinator
return managedObjectContext
}()
// MARK: - Core Data Saving support
func saveContext () {
if let moc = self.managedObjectContext {
var error: NSError? = nil
if moc.hasChanges && !moc.save(&error) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog("Unresolved error \(error), \(error!.userInfo)")
abort()
}
}
}
}
and the view controller i am trying to send the prepareforsegue from is as follows
import UIKit
import AVFoundation
import AVKit
import Foundation
import Social
public var AudioPlayer = AVPlayer()
public var SelectedSongNumber = Int()
public var TrackName = String()
public var TrackImage = String()
public var TrackDJ = String()
class MusicListTableViewController: UITableViewController, AVAudioPlayerDelegate{
#IBOutlet weak var playerView: UIView!
#IBOutlet weak var songName: UILabel!
#IBOutlet weak var playButton: UIButton!
#IBOutlet weak var trackDjName: UILabel!
#IBOutlet weak var imageArtwork: UIImageView!
var trackName = [String]()
var artistLabel = [String]()
var trackUrl = [String]()
var artWork = [String]()
var tags = [String]()
var artistId = [String]()
var djInfo = ""
#IBAction func facebookButton(sender: UIButton) {
if SLComposeViewController.isAvailableForServiceType(SLServiceTypeFacebook){
var facebookSheet:SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
facebookSheet.setInitialText("Share on Facebook")
self.presentViewController(facebookSheet, animated: true, completion: nil)
} else {
var alert = UIAlertController(title: "Accounts", message: "Please login to a Facebook account to share.", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
}
#IBAction func tritterButton(sender: UIButton) {
if SLComposeViewController.isAvailableForServiceType(SLServiceTypeTwitter){
var twitterSheet:SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeTwitter)
twitterSheet.setInitialText("Share on Twitter")
self.presentViewController(twitterSheet, animated: true, completion: nil)
} else {
var alert = UIAlertController(title: "Accounts", message: "Please login to a Twitter account to share.", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
}
#IBAction func myPlayList(sender: UIButton) {
var centerViewController = self.storyboard?.instantiateViewControllerWithIdentifier("myMusicList") as! myMusicList
var centerNavController = UINavigationController(rootViewController: centerViewController)
var appDelegate:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.centerContainer!.centerViewController = centerNavController
appDelegate.centerContainer!.toggleDrawerSide(MMDrawerSide.Right, animated: true, completion: nil)
}
#IBAction func favoriteButton(sender: UIButton) {
var favSong = trackName[SelectedSongNumber]
var alertView:UIAlertView = UIAlertView()
alertView.title = "Nummer Toegevoegd"
alertView.message = "Het nummer \(favSong) is nu toegevoegd aan uw favorieten muziek lijst"
alertView.delegate = self
alertView.addButtonWithTitle("OK")
alertView.show()
}
#IBAction func djInformation(sender: UIButton) {
var centerViewController = self.storyboard?.instantiateViewControllerWithIdentifier("djInformation") as! DjInformation
var centerNavController = UINavigationController(rootViewController: centerViewController)
var appDelegate:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.centerContainer!.centerViewController = centerNavController
appDelegate.centerContainer!.toggleDrawerSide(MMDrawerSide.Right, animated: true, completion: nil)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var artist_label = artistLabel[SelectedSongNumber]
if (segue.identifier == "djInformation" ){
var detailVC = segue.destinationViewController as! UINavigationController
let targetController = detailVC.topViewController as! DjInformation
targetController.djInfo = "hello"
}
}
func getMusicListJSON(){
let urlString = "http://xxxxxxxxxx"
let urlEncodedString = urlString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
let url = NSURL( string: urlEncodedString!)
var task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, innerError) in
let json = JSON(data: data)
let musicArray = json.arrayValue
//NSLog("\(json)")
dispatch_async(dispatch_get_main_queue(), {
for musiclist in musicArray
{
let track_name = musiclist["track_name"].stringValue
let artist = musiclist["artist"].stringValue
let track_url = musiclist["track_url"].stringValue
let art_work = musiclist["artwork"].stringValue
let track_tags = musiclist["tags"].stringValue
let artist_id = musiclist["artist_id"].stringValue
//println( "track_name: \(track_name) artist: \(artist) track_url: \(track_url) artwork: \(art_work) track_tags: \(track_tags) artist_id: \(artist_id)" )
self.trackName.append(track_name)
self.artistLabel.append(artist)
self.trackUrl.append(track_url)
self.artWork.append(art_work)
self.tags.append(track_tags)
self.artistId.append(artist_id)
}
dispatch_async(dispatch_get_main_queue(), {
self.tableView.reloadData()
return
})
})
}
task.resume()
}
override func viewDidLoad() {
super.viewDidLoad()
getMusicListJSON()
playButton.addTarget(self, action: "playButtonTapped:", forControlEvents: .TouchUpInside)
playerView.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height * 0.7)
playButton.hidden = true
var error: NSError?
var success = AVAudioSession.sharedInstance().setCategory(
AVAudioSessionCategoryPlayAndRecord,withOptions: .DefaultToSpeaker, error: &error)
if !success{
NSLog("Failed to set audio session category, Error: \(error)")
}
}
func playButtonTapped(sender: AnyObject){
// set play image to pause wehen video is paused and also back
if AudioPlayer.rate == 0
{
AudioPlayer.play()
playButton.setImage(UIImage(named:"pause"), forState: UIControlState.Normal)
} else {
AudioPlayer.pause()
playButton.setImage(UIImage(named: "play"), forState: UIControlState.Normal)
}
}
#IBAction func slideOutMenu(sender: AnyObject) {
var appDelegate:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.centerContainer!.toggleDrawerSide(MMDrawerSide.Left, animated: true, completion: nil)
}
#IBAction func musicMenu(sender: AnyObject) {
var appDelegate:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.centerContainer!.toggleDrawerSide(MMDrawerSide.Right, animated: true, completion: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBarHidden = true
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return trackName.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("playerCell", forIndexPath: indexPath) as! playerTableViewCell
// Configure the cell...
cell.artistName.text = trackName[indexPath.row]
cell.trackName.text = artistLabel[indexPath.row]
return cell
}
// set the song to play according to the table row selected. Also set the name and artist.
func playSong() {
var playnumber = trackUrl[SelectedSongNumber]
var TrackName = trackName[SelectedSongNumber]
var TrackImage = artWork[SelectedSongNumber]
var TrackDJ = artistLabel[SelectedSongNumber]
var DjId = artistId[SelectedSongNumber]
AudioPlayer = AVPlayer(URL: NSURL(string: playnumber))
AudioPlayer.play()
songName.text = TrackName
trackDjName.text = TrackDJ
load_artwork(TrackImage)
playButton.hidden = false
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
SelectedSongNumber = indexPath.row
playSong()
}
// set the song artwork to display in the player for the current playing song.
func load_artwork(urlString: String){
var imgURL: NSURL = NSURL(string: urlString)!
let request: NSURLRequest = NSURLRequest(URL: imgURL)
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: {(response: NSURLResponse!, data: NSData!,error: NSError!) -> Void in
if error == nil {
self.imageArtwork.image = UIImage(data: data)
}
})
}
}
These are the two codes that i think should be the cause of the problem. But so far i can't find any way to pas any variables and i will be needing that to complete my app. What am i doing wrong here?
I can really use some help here to get this to work. Maybe my navigation controller is correctly setup. As the first viewcontroller should be embedded in it. But i am not sure i have done that properly.
Thanks for the help.
I have found the solution which was very simple if you know what you should do. When you are using mmdrawer and would like to send over data you could just create a prepareforsegue, but you also need to create a segue with overrides the mmdrawer. I find that you use you use the following just like you would do with prepareforsgue
#IBAction func djInformation(sender: UIButton) {
var artist_label = artistLabel[SelectedSongNumber]
var centerViewController = self.storyboard?.instantiateViewControllerWithIdentifier("djInformation") as! DjInformation
centerViewController.djINfo = "\(artist_label)"
var centerNavController = UINavigationController(rootViewController: centerViewController)
var appDelegate:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.centerContainer!.centerViewController = centerNavController
appDelegate.centerContainer!.toggleDrawerSide(MMDrawerSide.Right, animated: true, completion: nil)
}
so basically what you do in prepareforsegue you can also do in button that is initiating the mmdrawercontroller.
Hope this makes sense and helps some one else. I am in no way a pro and i am not sure if this is proper or more something like a hack. but it fast and easy.

Resources