Passing data between controller after method is done executed [SWIFT] - ios

I'm trying to send data across another view controller once a button is pressed (I know this question looks repetitive), however, the button being pressed is processing some data. So when the button is clicked, the other view controller is popped up before the needed actual data is sent. I tried both segue calls (prepare for segue and the calling segue) but none seem to work. Here is my code:
#IBAction func login(sender: Any) {
SparkCloud.sharedInstance().login(withUser: email, password: password) { (error:Error?) -> Void in
if let _ = error {
print("Wrong credentials or no internet connectivity, please try again")
}
else {
print("Logged in")
var myPhoton : SparkDevice?
SparkCloud.sharedInstance().getDevices { (devices:[SparkDevice]?, error:Error?) -> Void in
if let _ = error {
print("Check your internet connectivity")
}
else {
if let d = devices {
for device in d {
myPhoton = device
print(myPhoton!)
}
}
}
}
}
}
}
And the segue:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "loggedIn" {
if let destinationVC = segue.destination as? testViewController {
destinationVC.myPhoton = sentDevice
}
}
}
And the other view controller that is receiving the data:
var myPhoton : SparkDevice?
override func viewDidLoad() {
super.viewDidLoad()
print(myPhoton)
}
I receive 'nil', which indicates that when the data has been set, it was before it got set to the data that I wanted from the server. Can someone help me please?

You can try
#IBAction func login(sender: Any) {
SparkCloud.sharedInstance().login(withUser: email, password: password) { (error:Error?) -> Void in
if let _ = error {
print("Wrong credentials or no internet connectivity, please try again")
}
else {
print("Logged in")
var myPhoton : SparkDevice?
SparkCloud.sharedInstance().getDevices { (devices:[SparkDevice]?, error:Error?) -> Void in
if let _ = error {
print("Check your internet connectivity")
}
else {
if let d = devices {
for device in d {
myPhoton = device
print(myPhoton!)
}
self.performSegue(withIdentifier: "loggedIn", sender: myPhoton)
}
}
}
}
}
}
and remove linking the segue directly to the button action in IB
Edit
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "loggedIn" {
if let destinationVC = segue.destination as? testViewController {
destinationVC.myPhoton = sender as! SparkDevice
}
}
}

Instead of using Segue, you can also try doing it with code, i.e.
#IBAction func login(_ sender: UIButton)
{
SparkCloud.sharedInstance().login(withUser: email, password: password) {
if let _ = error
{
print("Wrong credentials or no internet connectivity, please try again")
}
else
{
print("Logged in")
var myPhoton : SparkDevice?
SparkCloud.sharedInstance().getDevices { (devices:[SparkDevice]?, error:Error?) -> Void in
if let _ = error
{
print("Check your internet connectivity")
}
else
{
if let d = devices
{
for device in d
{
myPhoton = device
print(myPhoton!)
//HERE..!!!!!
DispatchQueue.main.async {[weak self] in
let anotherController = self.storyboard?.instantiateViewController(withIdentifier: "AnotherVC") as! AnotherVC
anotherController.myPhoton = myPhoton
self.navigationController?.pushViewController(anotherController, animated: true)
}
}
}
}
}
}
}
}
In the above code, if you want to push the controller, then use:
self.navigationController?.pushViewController(anotherController, animated: true)
otherwise, if you want to present the controller, then use:
self.present(anotherController, animated: true, completion: nil)
Let me know if you still face any issues.

Related

How to pass data in a closure to another scene

It is my first app in swift. I am using Alamofire for my HTTP request. Coming from Android, I know it is possible to attach serialized object to navcontroller action while navigating from one screen to another.
I want to be able to perform segue after from the viewmodel subscription and attach the resultant token to the segue as I will be using it for verification at the next screen.
I have tried didSet but to no avail.
How can I do this in swift.
//MARK: Register user
#IBAction func registerUser(_ sender: Any) {
let fullName = firstNameTF.text! + " " + lastNameTF.text!
let email = emailTF.text
let password = passwordTF.text
let phone = phoneNumberTF.text
let country = countryDropDown.text
let user = User(name: fullName, email: email, password: password, country: country, phone: phone, token: nil)
var tk = ""{
didSet{
token = tk
}
}
authViewModel.registerUser(user: user).subscribe(onNext: { (AuthResponse) in
print("messaage \(String(describing: AuthResponse.message))")
self.tokens = AuthResponse.token
self.performSegue(withIdentifier: "gotoVerification", sender: self)
}, onError: { (Error) in
print("Error: \(Error.localizedDescription)")
}, onCompleted: nil) {
}.disposed(by: disposeBag)
print("token \(token)")
// AF.request(url, method: .post, parameters: user, encoder: JSONParameterEncoder.default).responseDecodable(of:AuthResponse.self){response in
//
// response.map { (AuthResponse) in
// print("messaage \(String(describing: AuthResponse.message))")
// }
//
// print("user: \(user)")
// print("response \(String(describing: response))")
// }
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let vc = segue.destination as? UserVerification{
//
vc.tokens = token
print("token \(token)")
}
}
You can pass the token as the sender:
self.performSegue(withIdentifier: "gotoVerification", sender: AuthResponse.token)
Then:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let vc = segue.destination as? UserVerification, let token = sender as? String {
vc.tokens = token
print("token \(token)")
}
}

Only instance methods can be declared #IBAction error?

I am facing this error on build for the function shown in the code
Only instance methods can be declared #IBAction
this error is coming up only after I introduced google sign in method for similar functionality , earlier it not an error
#IBAction func SignInButtonAction(_ sender: Any) {
guard let email = emailField.text else { return }
guard let pass = passwordField.text else { return }
Auth.auth().signIn(withEmail: email, password: pass) { user, error in
if error == nil && user != nil {
let setupcheckref = Firestore.firestore().collection("users").document(Auth.auth().currentUser!.uid)
setupcheckref.getDocument{(document, error) in
if let document = document, document.exists{
let dataDescription = document.data().map(String.init(describing:)) ?? "nil"
self.checksetup = document.get("setupComplete") as! Bool
if self.checksetup == true {
if Auth.auth().currentUser!.isEmailVerified {
self.performSegue(withIdentifier: "toLoginFeed", sender: self)
}
else{
print("please verify your email")
try! Auth.auth().signOut()
}
}
else{
self.view.makeToast("Please Setup Your Account!", duration: 2.5)
self.performSegue(withIdentifier: "fromlogintosetup", sender: self)
SVProgressHUD.dismiss()
} }
}
// self.dismiss(animated: false, completion: nil)
} else {
print("Error logging in: \(error!.localizedDescription)")
// self.resetForm()
// SVProgressHUD.dismiss()
}
}
}
That means you can create #IBActions only as instance methods of a class.
You might be creating it of a class.
class VC: UIViewController {
#IBAction func SignInButtonAction(_ sender: Any) {
//your code...
}
}

Passing data from viewcontroler to tab viewed controllers in swift

I have this login form:
import UIKit
class LoginViewController: UIViewController {
var user : LoginUser?
#IBAction func btnLoginPressed(_ sender: Any) {
if self.textFieldLogin.text?.isEmpty ?? true || self.textFieldPassword.text?.isEmpty ?? true {
self.errorLoginMessage(txt: "Error", title: "Error")
} else {
let cms = ServerConnect()
cms.checkUsersLogin(login: self.textFieldLogin.text, password: self.textFieldPassword.text, completion: { (data) in
switch(data) {
case .succes(let data):
var actionToRun: ()-> Void
let decoder = JSONDecoder()
self.user = try? decoder.decode(LoginUser.self, from: data)
dump(self.user)
// we have an user
if ((self.user?.id ) != nil) {
actionToRun = {
self.performSegue(withIdentifier: "toLoginUser", sender: self)
}
}
// we have an error
else if let json = try? JSONSerialization.jsonObject(with: data, options: []),
let dictionary = json as? [String: Any],
let message = dictionary["komunikat"] as? String,
let title = dictionary["error"] as? String {
// we have an error
actionToRun = {
self.errorLoginMessage(txt: message, title: title)
}
}
// default error
else {
actionToRun = {
self.errorLoginMessage(txt: "Podany login lub hasło jest błędny!!", title: "Błąd")
}
}
DispatchQueue.main.async {
actionToRun()
}
case .error(let error):
print("Error 104: \(error)")
}
})
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?){
if segue.identifier == "toLoginUser" {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let tabVC = storyboard.instantiateViewController(withIdentifier: "MainVC") as! UITabBarController
self.present(tabVC, animated: true, completion: {
let vc = tabVC.selectedViewController as! NewsViewController
vc.loggedUser = self.user
})
}
}
func errorLoginMessage(txt: String, title: String){
let alertController = UIAlertController(title: title, message: txt, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Ok", style: .cancel, handler: { (action: UIAlertAction!) in
}))
self.present(alertController, animated: true, completion: nil)
}
}
The object is built correctly.
I would like to pass my object from the login view to tab viewed controllers.
I am receiving the object like this:
class NewsViewController: UIViewController {
var loggedUser : LoginUser?
override func viewDidLoad() {
super.viewDidLoad()
print("check object: \(loggedUser)")
dump(loggedUser)
}
#IBAction func testBtnPressed(_ sender: Any) {
print("check object: \(loggedUser)")
dump(loggedUser)
}
}
Unfortunately this code does not work and I always get nil.
My full source code: https://bitbucket.org/trifek/karta-nauka/src/master/
Can I ask for help?
It is not good idea to pass info view controller to view controller which is used by almost all of your view controller's
Like Login information which contains info that is nearly used by all of the screen
Good idea is to create Global Shared class which will store your all global information which is shared among the application.
public final class AppGlobalManager {
static let sharedManager = AppGlobalManager()
var loggedUser : LoginUser?
}
Now whenever user login you can
AppGlobalManager.sharedManager.loggedUser = object
And when user logout
AppGlobalManager.sharedManager.loggedUser = nil
Hope it is helpful
If you are already using segues in the storyboard, you don't have to get the desired view controller from the UIStoryboard, the segues itself provides the destination view controller.
Example:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "toLoginUser" {
let tabVC = segue.destination as! UITabBarController
// assuming that `NewsViewController` is the first view controller in the tabbar controller:
let destinationViewController = tabVC.viewControllers?[0] as! NewsViewController
destinationViewController.loggedUser = self.user
}
}
Since you are aiming to pass the LoginUser object to one of the tabbar view controllers, you could get it from the tabbar viewControllers array -as mentioned in the code snippet above-.

triggering segue after an ibaction

I'm trying to perform a segue after my IBAction has already happend.
This is my code and as you can see when I press the button I make a get request with alamofire. The problem is that the request is (as I understand) an async method so the segue will unwind and perform eve if the getPlayer method hasn't done what it's supposed to. The only way I could fix it is by putting the perfomrsegue method inside an if statement where I check for the value of person.name, but I have to press the button twice and I just can't figure out how to solve this!
#IBAction func getPlayerPressed(_ sender: UIButton) {
userDefaults.set(tagTextField.text!, forKey: "userTag")
let userTag = userDefaults.string(forKey: "userTag")
getPlayerData(with: userTag!)
performSegue(withIdentifier: "goToPlayerProfile", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destionationVC = segue.destination as! PlayerProfileViewController
destionationVC.playerName = player.name
print(destionationVC.playerName)
print("prepared for segue")
}
func getPlayerData (with tag: String) {
let finalURL = baseURL + tag
Alamofire.request(finalURL, headers: headers).responseJSON { (response) in
if response.result.isSuccess {
print("Got player data!")
let playerJSON = JSON(response.result.value!)
self.player.name = playerJSON["name"].stringValue
print(self.player.name)
} else {
print("Error: \(response.result.error!)")
}
}
Perform segue after async alamofire request is completed.
func getPlayerData (with tag: String) {
let finalURL = baseURL + tag
Alamofire.request(finalURL, headers: headers).responseJSON { (response) in
if response.result.isSuccess {
print("Got player data!")
let playerJSON = JSON(response.result.value!)
self.player.name = playerJSON["name"].stringValue
print(self.player.name)
DispatchQueue.main.async {
self.performSegue(withIdentifier: "goToPlayerProfile", sender: self)
}
} else {
print("Error: \(response.result.error!)")
}
}
}

Swift segue not working?

My Swift segue is not working at all and isn't throwing any errors. The breakpoint shows me that the app lands on this line but nothing happens:
self.performSegueWithIdentifier("SignupSegue", sender: self)
The code block is a login form with Facebook:
if let accessToken: FBSDKAccessToken = FBSDKAccessToken.currentAccessToken() {
PFFacebookUtils.logInInBackgroundWithAccessToken(result.token, block: {
(user: PFUser ? , error : NSError ? ) - > Void in
if user!.isNew {
self.performSegueWithIdentifier("SignupSegue", sender: self)
} else {
self.navigateToInGame(true)
}
})
}
Here's the segue function it should call, but doesn't even get to it:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
if (segue.identifier == "SignupSegue") {
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("SignUpViewController")
self.showViewController(vc, sender: self)
}
}
Any ideas?
Generally, any UI updating has to be in main thread. I think the block for PFFacebookUtils.logInInBackgroundWithAccessToken is still in the background state in above situation. Maybe trigger the showViewController in dispatch_async(dispatch_get_main_queue(), {}) and see if there is any difference.
dispatch_async(dispatch_get_main_queue(), {
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("SignUpViewController")
self.showViewController(vc, sender: self)
})
Ok. I just tried it out. Hope you did all the things regarding StoryBoard Reference.
Me too had the same issue with performSegueWithIdentifier.
Example:
Let take two storyboard main and signup.
1) In main.storyboard create a storyboard reference. Set the storyboardId in the Storyboard Reference as signup and the referencedId as the storyboardId of the scene(viewController) which is in signup.storyboard. Look at this link for a clear picture Storyboard to Storyboard
2) Set the segue identifier between viewController and Storyboard Reference in main.storyboard
3) Since I faced the same problem with performSegueWithIdentifier, I replaced it with shouldPerformSegueWithIdentifier.
override func shouldPerformSegueWithIdentifier(identifier: String, sender: AnyObject?) -> Bool {
if(identifier == "segue_identifier"){
// segue_identifier is the viewController and storyBoard Reference segue identifier.
print("hello")
}
return true;
}
Let me know if you find any issues. It did work for me.
Performing a segue leads to present a new view controller.You don't need to and can't create and show view controller in prepareForSegue.It will look like:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
if (segue.identifier == "SignupSegue") {
let vc = segue.destinationViewController
}
}
Swift 3 solved:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "SignupSegue") {
DispatchQueue.main.async {
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "SignUpViewController")
self.show(vc, sender: self)
}
}
}
You can try this ...
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
if (segue.identifier == "SignupSegue") {
if let destination = segue.destination as? SignUpViewController {
...
}
}
}
The closest I can get to overcome same problem for myself:
Made trigger var segueLogin : Bool = false with initialised value in the Class.
When PFFacebookUtils gets needed values for segue, change trigger to true:
PFFacebookUtils.logInInBackground(withReadPermissions: permissions) {
(user: PFUser?, error: Error?) -> Void in
if let user = user {
if user.isNew {
print("User signed up and logged in through Facebook!")
self.segueLogin = true
} else {
print("User logged in through Facebook!")
self.segueLogin = true
}
} else {
print("Uh oh. The user cancelled the Facebook login.")
self.loginCancelledLabel.alpha = 1
}
}
Then added code to viewDidAppear class. Realised it starts everytime PFFacebookUtils complete. So it checks if returned value is true and performs segue after successful PFFacebookUtils session:
override func viewDidAppear(_ animated: Bool) {
if segueLogin == true {
self.performSegue(withIdentifier: "segueSingup", sender: self)
}
}

Resources