Expected declaration in Swift if/else statement - ios

if userData != nil {
self.initUserInfo()
self.retreiveOrderCount()
self.retreiveWatchCount()
self.retreiveFollowingCount()
} else {
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewControllerWithIdentifier("loginview")
self.presentViewController(controller, animated: true, completion: nil)
}
}
This is the code block that is giving me the expected declaration.

There is an extra bracket at the end. Remove the bracket just like bellow:
if userData != nil {
self.initUserInfo()
self.retreiveOrderCount()
self.retreiveWatchCount()
self.retreiveFollowingCount()
} else {
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewControllerWithIdentifier("loginview")
self.presentViewController(controller, animated: true, completion: nil)
}

Related

Swift: Thread 1: signal SIGABRT after storyBoard.instantiateViewController

I want switch user to another page
I use this code :
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let vcAboutUs = storyBoard.instantiateViewController(withIdentifier: "ViewControllerIndex")
self.present(vcAboutUs, animated: true, completion: nil)
But after run project i am getting Thread 1: signal SIGABRT error and simulator crashed
Image : https://i.stack.imgur.com/d1uIT.jpg
Full code :
DispatchQueue.global(qos: .background).async {
let getJsonLogin = GetData()
let result = getJsonLogin.getJson(urlGet: AppDelegate.ADDRESS+"/index.php", urlData: "mobile="+mobile+"&code="+getCode+"") { result in
DispatchQueue.main.async {
if(result == nil){
self.progressText.text = "Error";
} else {
if((result!["error"]!) == "true"){
self.progressTextCode.text = result!["message"]!;
} else {
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let vcAboutUs = storyBoard.instantiateViewController(withIdentifier: "ViewControllerIndex")
self.present(vcAboutUs, animated: true, completion: nil)
}
}
}
}
Did you add the name of VC to the storyboard?

how to present and dismiss at same time

This is my Code
#IBAction func backButton(_ sender: UIButton) {
self.dismiss(animated: true, completion: { () -> Void in
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "RequestItemPackageDetails") as! RequestItemPackageDetails
self.present(vc, animated: true, completion: nil)
})
}
this is not working only dissmising
I dont't understand your questions well. I think you want to present a new ViewController. You can use this:
UIView.animate{duration: 0.5, animations: {
(UIApplication.shared.delegate as! AppDelegate).window!.rootViewController = NextViewController()
}}
Problem is that you are dismissing self and then trying to present a new view controller on that exact same self - but at that moment self is already dismissed. You need to present that new view controller from the parent of the current view controller. I believe the best way is to setup a delegate. So the controller that you showed us and that you want to dismiss will have this:
protocol FirstDelegate: class {
func firstDismiss(_ first: First)
}
class First: UIViewController {
weak var delegate: FirstDelegate?
#IBAction func backButton(_ sender: UIButton) {
// this will tell the delegate to dismiss this contorller and present the other one
delegate?.firstDismiss(self)
}
// rest of the code omitted
}
Next, you will have to setup this in the parent that presents First view controller (here I assume that presentFirst method presents the First view controller):
class ParentViewController: UIViewController, FirstDelegate {
// rest of the code
func presentFirst() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let firstVC = storyboard.instantiateViewController(withIdentifier: "First") as! RequestItemPackageDetails
// set delegate to first:
firstVC.delegate = self
self.present(firstVC, animated: true, completion: nil)
}
func firstDismiss(_ first: First) {
first.dismiss(animated: true, completion: { () -> Void in
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "RequestItemPackageDetails") as! RequestItemPackageDetails
self.present(vc, animated: true, completion: nil)
})
}
}
Not quite sure with your Qns, but try this one out.
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "RequestItemPackageDetails") as! RequestItemPackageDetails
vc.previousVC = self
self.present(vc, animated: true)
RequestItemPackageDetails.swift
var previousVC : UIViewController!
override viewDidLoad() {
previousVC.dismiss(animated: false, completion: nil)
}
You can dismiss self and then present another view from rootViewController
self.dismiss(animated: true, completion: { () -> Void in
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "RequestItemPackageDetails") as! RequestItemPackageDetails
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window!.rootViewController?.present(vc, animated: true, completion: nil)
})

Unable to Instantiate View Controller

I have a function, whereby I check if the UserDefaults are set and if not a new View Controller opens and presents a login screen which will set the user defaults.
My problem is the view controller does not Instantiate but I get a print "User not registered"
func checkUserAccount() {
let defaults = UserDefaults.standard
let accountName = defaults.bool(forKey: "PUserAccountName")
let accountPassword = defaults.bool(forKey: "PUserAccountPassword")
if accountName == true && accountPassword == true {
print("User Registered")
} else {
let storyboard: UIStoryboard = UIStoryboard(name: "PolTRiM", bundle: nil)
let vc: StudentLoginVC = storyboard.instantiateViewController(withIdentifier: "studentLogin") as! StudentLoginVC
vc.modalPresentationStyle = .custom
vc.modalTransitionStyle = .crossDissolve
self.present(vc, animated: true, completion: { _ in })
print("User not registered")
}
}
Any thoughts?
Have you double checked UIStoryBoard name and UIViewController identifier if it's written correctly? Otherwise this code is working for me
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier :"MyViewController") as! UIViewController
self.present(viewController, animated: true)

Moving to another storyboard inside closure

api.postUserLogin(Parameters: params) { (json) in
print(json)
if (json["status"].string == "true") {
self.user.id = json["data"]["id"].string
self.user.verify_status = json["data"]["verify_status"].string
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "MainViewController") as UIViewController
present(vc, animated: true, completion: nil)
}
}
Try this code:
func callViewController(){
let viewController: UIViewController = UIStoryboard(name:"STORYBOARD_NAME",bundle:nil).instantiateViewController(withIdentifier: "MainViewController")
let window :UIWindow = UIApplication.shared.keyWindow!
window.rootViewController = viewController
window.makeKeyAndVisible()
}

Touch Id autotrigger success when i dismiss from another view

the process is :
1 the user login with touch id
2 the user logout (logout dismiss to login)
then the app autotrigger touch id with success response (this not happen in the emulators) HELP!!
this is my listener on login viewcontroller
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if authenticationContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) && DataContainerSingleton.sharedDataContainer.fingerPrintEnabled == "SI"{
self.touchIdListener()
}
}
with
func touchIdListener(){
authenticationContext.evaluatePolicy(
.deviceOwnerAuthenticationWithBiometrics,
localizedReason: self.valFromCurrentLanguaje(valor: "LOGIN_FINGER_MSG"),
reply: { [unowned self] (success, error) -> Void in
if( success ) {
let keychain = KeychainSwift()
let data = keychain.get(Constants.USER_KEY)?.components(separatedBy: ",")
DispatchQueue.main.async {
self.showProgress(text: Constants.EMPTY_STRING)
}
self.fromTouchId = true
self.loginBL.startLogin(email: (data?[0])!, password: data?[1])
}else {
// Check if there is an error
if let error = error {
print("\(error.localizedDescription)")
}
}
})
}
So the solution was change the dismiss
self.dismiss(animated: true, completion: nil)
for a new instance
var storyboard = UIStoryboard()
let vc : LoginViewController
if (Constants.IS_IPAD) {
storyboard = UIStoryboard.init(name: "LoginIpad", bundle: nil)
vc = storyboard.instantiateViewController(withIdentifier: "ipadViewController") as! LoginViewController
} else {
storyboard = UIStoryboard.init(name: "Login", bundle: nil)
vc = storyboard.instantiateViewController(withIdentifier: "iphoneViewController") as! LoginViewController
}
let navigationController = UINavigationController(rootViewController: vc)
self.present(navigationController, animated: true, completion: nil)

Resources