I put Reachability.swift file in my app, when Internet reachability is changed, I want to alert to user that internet connection is not available.
this is my code.
import UIKit
import Parse
class ViewController: UIViewController {
var reachability : Reachability?
var myAlert = UIAlertController()
#IBOutlet weak var label: UILabel!
#IBOutlet weak var textField: UITextField!
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(true)
do {
let reachability = try Reachability.reachabilityForInternetConnection()
self.reachability = reachability
} catch ReachabilityError.FailedToCreateWithAddress(let address) {
}
catch {}
NSNotificationCenter.defaultCenter().addObserver(self, selector: "HeyUserInternetDoesntWork", name: ReachabilityChangedNotification, object: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func saveButtonTapped(sender: AnyObject) {
let save = PFObject(className: "Practice")
save["text"] = textField.text
save.saveInBackgroundWithBlock { (success: Bool, error: NSError?) -> Void in
print("Object has been saved.")
}
}
dynamic func HeyUserInternetDoesntWork() {
if reachability!.isReachable() {
} else {
myAlert = UIAlertController(title: "No internet", message: "no good", preferredStyle: UIAlertControllerStyle.Alert)
let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)
myAlert.addAction(okAction) }
}
}
this is not working, I got an error that
Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behaviour
I don't understand what this mean.
If I put code that print("unreachable") will work fine.
My question
what is meaning of that error?
How I can make my alert works?
If there is other way to let user know Internet connectivity,
please let me know.
try to add this line under alert
(self.presentViewController(myAlert, animated: true, completion: nil))
Related
I tried the moved the code around a bit. The errors I got was use of local variable displayMessage before its declaration. So I moved the func displayAlertMessage above the display an alert Message comment, the new error is use of unresolved identifier 'displayAlertMessage'
//
// RegisterPageViewController.swift
// UserLoginandRegistration
//
// Created by Iyah Chulo on 17/11/2017.
// Copyright © 2017 Iyah Chulo. All rights reserved.
//
import UIKit
class RegisterPageViewController: UIViewController {
#IBOutlet weak var userEmailTextField: UITextField!
#IBOutlet weak var userPasswordTextField: UITextField!
#IBOutlet weak var ReenterPasswordTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func RegisterButtonTapped(_ sender: Any) {
let userEmail = userEmailTextField.text;
let userPassword = userPasswordTextField.text;
let userReenterPassword = ReenterPasswordTextField.text;
// Check for empty fields
if((userEmail?.isEmpty)! || (userPassword?.isEmpty)! ||
(userReenterPassword?.isEmpty)!)
{
func displayAlertMessage(userMessage: String) { let myAlert = UIAlertController(title:"Alert", message: userMessage, preferredStyle:
UIAlertControllerStyle.alert);
let okAction = UIAlertAction(title:"Ok", style:
UIAlertActionStyle.default, handler:nil)
myAlert.addAction(okAction);
self.present(myAlert, animated: true,
completion:nil)
}
//Display alert message
displayAlertMessage(userMessage: "All fields are required")
return;
}
//Check if passwords match
if(userPassword != userReenterPassword)
{
// Display an alert message
displayAlertMessage(userMessage: "Passwords do not match")
return;
}
// Store data
UserDefaults.standard.set(userEmail, forKey:"userEmail")
UserDefaults.standard.set(userEmail, forKey:"userPassword")
UserDefaults.standard.synchronize()
// Display alert message with confirmation
var myAlert = UIAlertController(title:"Alert", message: "Registration is successful.Thank you!", preferredStyle:
UIAlertControllerStyle.alert);
let okAction = UIAlertAction(title:"Ok", style:
UIAlertActionStyle.default) { action in
self.dismiss(animated: true, completion:nil)
}
}
}
Your displayAlertMessage function definition needs to be outside of the other functions in your class.
Also, please note that Swift does not require semicolons!
Try this:
import UIKit
class RegisterPageViewController: UIViewController {
#IBOutlet weak var userEmailTextField: UITextField!
#IBOutlet weak var userPasswordTextField: UITextField!
#IBOutlet weak var ReenterPasswordTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func RegisterButtonTapped(_ sender: Any) {
let userEmail = userEmailTextField.text
let userPassword = userPasswordTextField.text
let userReenterPassword = ReenterPasswordTextField.text
// Check for empty fields
if((userEmail?.isEmpty)! || (userPassword?.isEmpty)! ||
(userReenterPassword?.isEmpty)!)
{
//Display alert message
displayAlertMessage(userMessage: "All fields are required")
return;
}
//Check if passwords match
if(userPassword != userReenterPassword)
{
// Display an alert message
displayAlertMessage(userMessage: "Passwords do not match")
return;
}
// Store data
UserDefaults.standard.set(userEmail, forKey:"userEmail")
UserDefaults.standard.set(userEmail, forKey:"userPassword")
UserDefaults.standard.synchronize()
// Display an alert message
displayAlertMessage(userMessage: "Registration is successful.Thank you!")
}
func displayAlertMessage(userMessage: String) {
let myAlert = UIAlertController(title:"Alert", message: userMessage, preferredStyle: UIAlertControllerStyle.alert)
let okAction = UIAlertAction(title:"Ok", style: UIAlertActionStyle.default) {
action in
self.dismiss(animated: true, completion:nil)
}
myAlert.addAction(okAction);
self.present(myAlert, animated: true, completion: nil)
}
}
I have followed the sample here
https://github.com/awslabs/aws-sdk-ios-samples/tree/master/CognitoYourUserPools-Sample
To integrate interactive cognito login to my iOS app. This is all working well, but when a new user is created in the pool, they initially have a FORCE_CHANGE_PASSWORD status.
For android you can follow the procedure below
http://docs.aws.amazon.com/cognito/latest/developerguide/using-amazon-cognito-user-identity-pools-android-sdk-authenticate-admin-created-user.html
But for iOS I can't find out how to do this. Using the sample, if I attempt to login with a user in FORCE_CHANGE_PASSWORD status, I see the following output in the console logs (with some values removed for brevity):
{"ChallengeName":"NEW_PASSWORD_REQUIRED","ChallengeParameters":{"requiredAttributes":"[]","userAttributes":"{\"email_verified\":\"true\",\"custom:autoconfirm\":\"Y\","Session":"xyz"}
The following is the code from the SignInViewController from the sample detailed above.
import Foundation
import AWSCognitoIdentityProvider
class SignInViewController: UIViewController {
#IBOutlet weak var username: UITextField!
#IBOutlet weak var password: UITextField!
var passwordAuthenticationCompletion: AWSTaskCompletionSource<AWSCognitoIdentityPasswordAuthenticationDetails>?
var usernameText: String?
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.password.text = nil
self.username.text = usernameText
self.navigationController?.setNavigationBarHidden(true, animated: false)
}
#IBAction func signInPressed(_ sender: AnyObject) {
if (self.username.text != nil && self.password.text != nil) {
let authDetails = AWSCognitoIdentityPasswordAuthenticationDetails(username: self.username.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 SignInViewController: AWSCognitoIdentityPasswordAuthentication {
public func getDetails(_ authenticationInput: AWSCognitoIdentityPasswordAuthenticationInput, passwordAuthenticationCompletionSource: AWSTaskCompletionSource<AWSCognitoIdentityPasswordAuthenticationDetails>) {
self.passwordAuthenticationCompletion = passwordAuthenticationCompletionSource
DispatchQueue.main.async {
if (self.usernameText == nil) {
self.usernameText = authenticationInput.lastKnownUsername
}
}
}
public func didCompleteStepWithError(_ error: 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.username.text = nil
self.dismiss(animated: true, completion: nil)
}
}
}
}
When didCompleteStepWithError executes, error is null where I would expect it to indicate something to tell us that the user is required to change password.
My question is really how to catch the "ChallengeName":"NEW_PASSWORD_REQUIRED" json that is output to the console?
Sorted this now. Posting it here in case it helps anyone else.
First, you need to create a view controller that will allow the user to specify the new password. The ViewController should have a AWSTaskCompletionSource<AWSCognitoIdentityNewPasswordRequiredDetails> as a property:
var newPasswordCompletion: AWSTaskCompletionSource<AWSCognitoIdentityNewPasswordRequiredDetails>?
Following the pattern in the sample referred to in the question, I then implemented AWSCognitoIdentityNewPasswordRequired as an extension to the view controller as follows:
extension NewPasswordRequiredViewController: AWSCognitoIdentityNewPasswordRequired {
func getNewPasswordDetails(_ newPasswordRequiredInput: AWSCognitoIdentityNewPasswordRequiredInput, newPasswordRequiredCompletionSource:
AWSTaskCompletionSource<AWSCognitoIdentityNewPasswordRequiredDetails>) {
self.newPasswordCompletion = newPasswordRequiredCompletionSource
}
func didCompleteNewPasswordStepWithError(_ error: Error?) {
if let error = error as? NSError {
// Handle error
} else {
// Handle success, in my case simply dismiss the view controller
self.dismiss(animated: true, completion: nil)
}
}
}
Again following the design of the sample detailed in the question, add a function to the AWSCognitoIdentityInteractiveAuthenticationDelegate extension to AppDelegate:
extension AppDelegate: AWSCognitoIdentityInteractiveAuthenticationDelegate {
func startPasswordAuthentication() -> AWSCognitoIdentityPasswordAuthentication {
//Existing implementation
}
func startNewPasswordRequired() -> AWSCognitoIdentityNewPasswordRequired {
// Your code to handle how the NewPasswordRequiredViewController is created / presented, the view controller should be returned
return self.newPasswordRequiredViewController!
}
}
The perfect example to implement the ResetPassword when u create the user in Cognito Console.
But, Integrating this ResetPassword mechanism to your existing app is your responsibility.
https://github.com/davidtucker/CognitoSampleApplication/tree/article1/CognitoApplication
I'm having a problem with the alert not displaying. I know it works because it prints to my console. I also tested the alertpopup in a viewDidAppear method and it worked. I've read some post and it maybe because of me trying to perform a segue and save a userdefault at the same time. I keep getting "Warning: Attempt to present... whose view is not in the window hierarchy". Any way to fix it or an explanation of what I'm doing wrong?
import UIKit
class StartMenuViewController : UIViewController
{
#IBOutlet var nameInput: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
#IBAction func startAction(_ sender: Any)
{
if nameInput.text == ""
{
print("This is working")
createAlert(title: "Warning", message: "Need to enter name")
}
UserDefaults.standard.set(nameInput.text, forKey: "UserName")
}
override func viewDidAppear(_ animated: Bool)
{
if let name = UserDefaults.standard.object(forKey: "UserName") as? String
{
nameInput.text = name
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func createAlert (title: String, message: String)
{
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: { (action) in
alert.dismiss(animated: true, completion: nil)
}))
self.present(alert, animated: true, completion: nil)
}
}
I am attempting to change views in the simulator via a button click but keep getting the error:
Thread 1: signal SIGABRT
I am aware that this usually occurs when there is a disconnect with outlets or actions but I have checked and they all seem to be linked.
AppDelegate.swift
import UIKit
import Parse
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
private func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
// Parse configuration in Heroku
let configuration = ParseClientConfiguration {
// accessing Heroku App via ID and Keys
$0.applicationId = "truegramIDDB91"
$0.clientKey = "truegramKeyDB91"
$0.server = "http://truegram.herokuapp.com/parse"
}
// call login function
login()
Parse.initialize(with: configuration)
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:.
}
func login() {
// remember user's login
let username : String? = UserDefaults.standard.string(forKey: "username")
// if logged in
if username != nil {
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let myTabBar = storyboard.instantiateViewController(withIdentifier: "tabBar") as! UITabBarController
window?.rootViewController = myTabBar
}
}
}
SignInVC.swift
import UIKit
import Parse
class signInVC: UIViewController {
// textfields
#IBOutlet weak var usernameTxt: UITextField!
#IBOutlet weak var passwordTxt: UITextField!
#IBOutlet weak var label: UILabel!
// sign in button view
#IBOutlet weak var signinBtnBlock: UIView!
// buttons
#IBOutlet weak var signInBtn: UIButton!
#IBOutlet weak var signUpBtn: UIButton!
#IBOutlet weak var forgotPasswordBtn: UIButton!
// default function
override func viewDidLoad() {
super.viewDidLoad()
// font of label (Clubbr)
label.font = UIFont(name: "gillsans-light", size: 22)
}
// clicked sign in button
#IBAction func signInBtnClick(sender: AnyObject) {
print("sign in pressed")
// hide keyboard
self.view.endEditing(true)
// if text fields are empty
if usernameTxt.text!.isEmpty || passwordTxt.text!.isEmpty {
// show alert message
let alert = UIAlertController(title: "Please", message: "fill in fields", preferredStyle: UIAlertControllerStyle.alert)
let ok = UIAlertAction(title: "OK", style: UIAlertActionStyle.cancel, handler: nil)
alert.addAction(ok)
self.present(alert, animated: true, completion: nil)
}
// login functions
PFUser.logInWithUsername(inBackground: usernameTxt.text!, password: passwordTxt.text!) { (user: PFUser?, error: Error?) -> Void in
if error == nil {
// remember user or save in app memory (did the user log in or not)
UserDefaults.standard.set(user!.username, forKey: "username")
UserDefaults.standard.synchronize()
// Call login function from AppDelegate.swift class
let appDelegate : AppDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.login()
} else {
// show alert message
let alert = UIAlertController(title: "Error", message: error!.localizedDescription, preferredStyle: UIAlertControllerStyle.alert)
let ok = UIAlertAction(title: "OK", style: UIAlertActionStyle.cancel, handler: nil)
alert.addAction(ok)
self.present(alert, animated: true, completion: nil)
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
SignUpVc.swift
import UIKit
import Parse
class signUpVC: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
// profile image
#IBOutlet weak var avaImg: UIImageView!
// textfields
#IBOutlet weak var firstnameTxt: UITextField!
#IBOutlet weak var lastnameTxt: UITextField!
#IBOutlet weak var emailTxt: UITextField!
#IBOutlet weak var passwordTxt: UITextField!
#IBOutlet weak var confirmTxt: UITextField!
// buttons
#IBOutlet weak var loginBtn: UIButton!
#IBOutlet weak var signupBtn: UIButton!
#IBOutlet weak var businessSignupBtn: UIButton!
// scroll view
#IBOutlet weak var scrollView: UIScrollView!
// resets scroll view to default size
var scrollViewHeight : CGFloat = 0
// keyboard frame size
var keyboard = CGRect()
// default func
override func viewDidLoad() {
super.viewDidLoad()
// check notifications if keyboard is shown or not
NotificationCenter.default.addObserver(self, selector: Selector(("showKeyboard:")), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: Selector(("hideKeyboard:")), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
// declare hide keyboard tap
let hideTap = UITapGestureRecognizer(target: self, action: Selector(("hideKeyboardTap:")))
hideTap.numberOfTapsRequired = 1
self.view.isUserInteractionEnabled = true
self.view.addGestureRecognizer(hideTap)
// profile image circle shape and custom border colour
avaImg.layer.borderWidth = 2
avaImg.layer.borderColor = UIColor.init(red: 90/255, green: 187/255, blue: 181/255, alpha: 1).cgColor
avaImg.layer.cornerRadius = avaImg.frame.height/2
avaImg.clipsToBounds = true
// declare select image tap
let avaTap = UITapGestureRecognizer(target: self, action: Selector(("loadImg:")))
avaTap.numberOfTapsRequired = 1
avaImg.isUserInteractionEnabled = true
avaImg.addGestureRecognizer(avaTap)
}
// call picker to select image
func loadImg(recognizer: UITapGestureRecognizer) {
let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = .photoLibrary
picker.allowsEditing = true
present(picker, animated: true, completion: nil)
}
// connect selected image to our image view
private func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
avaImg.image = info[UIImagePickerControllerEditedImage] as? UIImage
self.dismiss(animated: true, completion: nil)
}
// hide keyboard if tapped
func hideKeyboardTap(recognizer: UITapGestureRecognizer) {
self.view.endEditing(true)
}
// show keyboard
func showKeyboard(notification: NSNotification) {
// define keyboard sizes
keyboard = ((notification.userInfo?[UIKeyboardFrameEndUserInfoKey]! as AnyObject).cgRectValue)!
//move up UI
UIView.animate(withDuration: 0.4) { () -> Void in
self.scrollView.frame.size.height = self.scrollViewHeight - self.keyboard.height
}
}
// hide keyboard
func hideKeyboard(notification: NSNotification) {
//move down UI
UIView.animate(withDuration: 0.4) { () -> Void in
self.scrollView.frame.size.height = self.view.frame.height
}
}
// clicked sign up
#IBAction func signupBtnClick(sender: AnyObject) {
print("sign up pressed")
// dismiss keyboard
self.view.endEditing(true)
// if fields are empty
if (emailTxt.text!.isEmpty || passwordTxt.text!.isEmpty || confirmTxt.text!.isEmpty || firstnameTxt.text!.isEmpty || lastnameTxt.text!.isEmpty) {
// alert pop up message
let alert = UIAlertController(title: "Please", message: "fill all fields", preferredStyle: UIAlertControllerStyle.alert)
let ok = UIAlertAction(title: "OK", style: UIAlertActionStyle.cancel, handler: nil)
alert.addAction(ok)
self.present(alert, animated: true, completion: nil)
}
// if incorrect password
if passwordTxt.text != confirmTxt.text {
// alert pop up message
let alert = UIAlertController(title: "Attention", message: "incorrect password", preferredStyle: UIAlertControllerStyle.alert)
let ok = UIAlertAction(title: "OK", style: UIAlertActionStyle.cancel, handler: nil)
alert.addAction(ok)
self.present(alert, animated: true, completion: nil)
}
// send data to related columns in server
let user = PFUser()
user.username = emailTxt.text?.lowercased()
user.email = emailTxt.text?.lowercased()
user.password = passwordTxt.text
user["firstname"] = firstnameTxt.text?.lowercased()
user["lastname"] = lastnameTxt.text?.lowercased()
// convert our image for sending to server
let avaData = UIImageJPEGRepresentation(avaImg.image!, 0.5)
let avaFile = PFFile(name: "ava.jpg", data: avaData!)
user["ava"] = avaFile
// save data in server
user.signUpInBackground { (success: Bool, error: Error?) -> Void in
if success {
print("registered")
// remember logged user
UserDefaults.standard.set(user.username, forKey: "username")
UserDefaults.standard.synchronize()
// calls login func from AppDelegate.swift class
let appDelegate : AppDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.login()
} else {
// show alert message
let alert = UIAlertController(title: "Error", message: error!.localizedDescription, preferredStyle: UIAlertControllerStyle.alert)
let ok = UIAlertAction(title: "OK", style: UIAlertActionStyle.cancel, handler: nil)
alert.addAction(ok)
self.present(alert, animated: true, completion: nil)
}
}
}
// clicked return to log in
#IBAction func loginBtnClick(sender: AnyObject) {
self.dismiss(animated: true, completion: nil)
// dismiss keyboard
self.view.endEditing(true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
ResetPasswordVC.swift
import UIKit
import Parse
class resetPasswordVC: UIViewController {
// text field
#IBOutlet weak var emailTxt: UITextField!
// buttons
#IBOutlet weak var cancelBtn: UIButton!
#IBOutlet weak var resetBtn: UIButton!
// clicked cancel button
#IBAction func cancelBtnClick(sender: AnyObject) {
self.dismiss(animated: true, completion: nil)
// dismiss keyboard
self.view.endEditing(true)
}
// clicked reset button
#IBAction func resetBtnClick(sender: AnyObject) {
// hide keyboard
self.view.endEditing(true)
// if email text field is empty
if emailTxt.text!.isEmpty {
// show alert message
let alert = UIAlertController(title: "Email field", message: "is empty", preferredStyle: UIAlertControllerStyle.alert)
let ok = UIAlertAction(title: "OK", style: UIAlertActionStyle.cancel, handler: nil)
alert.addAction(ok)
self.present(alert, animated: true, completion: nil)
}
// request for resetting password
PFUser.requestPasswordResetForEmail(inBackground: emailTxt.text!) { (success: Bool, error: Error?) -> Void in
if success {
// show alert message
let alert = UIAlertController(title: "Reset Password Email", message: "has been sent to the email address associated with your account", preferredStyle: UIAlertControllerStyle.alert)
// of ok is pressed call self.dismiss.. function
let ok = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: { (UIAlertAction) -> Void in
self.dismiss(animated: true, completion: nil)
})
alert.addAction(ok)
self.present(alert, animated: true, completion: nil)
} else {
print(error?.localizedDescription)
}
}
}
// default func
override func viewDidLoad() {
super.viewDidLoad()
// tap to hide keyboard
let hideTap = UITapGestureRecognizer(target: self, action: Selector(("hideKeyboard:")))
hideTap.numberOfTapsRequired = 1
self.view.isUserInteractionEnabled = true
self.view.addGestureRecognizer(hideTap)
}
// hide keyboard function
func hideKeyboard(recognizer: UITapGestureRecognizer) {
self.view.endEditing(true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I am sure the issue is something fairly obvious but I have only been working with Swift for 3 months and it is my first programming language. I would really appreciate the help.
Thanks for your time.
SAME ERROR
Hey guys, I've been trying my best to sort this myself but cannot get to grips with it. After the initial response from #Vadian solved the issue of the IBActions, I have still been receiving the same SIGABRT error.
Error Info
2016-10-06 17:18:33.709206 Truegram[13130:1026640] [MobileAssetError:29] Unable to copy asset information from https://mesu.apple.com/assets/ for asset type com.apple.MobileAsset.TextInput.SpellChecker
sign up pressed
2016-10-06 17:18:53.120 Truegram[13130:1026284] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'The class PFUser must be registered with registerSubclass before using Parse.'
I think the above is most relevant to the error and this seems to be vindicated as the error occurs after pressing 'sign up'
I have also added another sign up page, specific to businesses that might be causing the issue.
BusinessSignUp.swift
import UIKit
import Parse
class BusinessSignUpVC: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
// profile image
#IBOutlet weak var BusinessAvaImg: UIImageView!
// textfields
#IBOutlet weak var confirmTxt: UITextField!
#IBOutlet weak var passwordTxt: UITextField!
#IBOutlet weak var AddressTxt: UITextView!
#IBOutlet weak var contactNumTxt: UITextField!
#IBOutlet weak var emailTxt: UITextField!
#IBOutlet weak var businessNameTxt: UITextField!
// buttons
#IBOutlet weak var loginBtn: UIButton!
#IBOutlet weak var signupBtn: UIButton!
#IBAction func loginBtnClick(_ sender: AnyObject) {
}
// keyboard frame size
var keyboard = CGRect()
override func viewDidLoad() {
super.viewDidLoad()
// check notifications if keyboard is shown or not
NotificationCenter.default.addObserver(self, selector: #selector(BusinessSignUpVC.showKeyboard(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: Selector(("hideKeyboard:")), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
// declare hide keyboard tap
let hideTap = UITapGestureRecognizer(target: self, action: #selector(BusinessSignUpVC.hideKeyboardTap(_:)))
hideTap.numberOfTapsRequired = 1
self.view.isUserInteractionEnabled = true
self.view.addGestureRecognizer(hideTap)
// profile image circle shape and custom border colour
BusinessAvaImg.layer.borderWidth = 2
BusinessAvaImg.layer.borderColor = UIColor.init(red: 90/255, green: 187/255, blue: 181/255, alpha: 1).cgColor
BusinessAvaImg.layer.cornerRadius = BusinessAvaImg.frame.height/2
BusinessAvaImg.clipsToBounds = true
// declare select image tap
let avaTap = UITapGestureRecognizer(target: self, action: #selector(BusinessSignUpVC.loadImg(_:)))
avaTap.numberOfTapsRequired = 1
BusinessAvaImg.isUserInteractionEnabled = true
BusinessAvaImg.addGestureRecognizer(avaTap)
}
// call picker to select image
func loadImg(_ recognizer: UITapGestureRecognizer) {
let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = .photoLibrary
picker.allowsEditing = true
present(picker, animated: true, completion: nil)
}
// connect selected image to our image view
private func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
BusinessAvaImg.image = info[UIImagePickerControllerEditedImage] as? UIImage
self.dismiss(animated: true, completion: nil)
}
// hide keyboard if tapped
func hideKeyboardTap(_ recognizer: UITapGestureRecognizer) {
self.view.endEditing(true)
}
// show keyboard
func showKeyboard(_ notification: NSNotification) {
// define keyboard sizes
keyboard = ((notification.userInfo?[UIKeyboardFrameEndUserInfoKey]! as AnyObject).cgRectValue)!
// Do any additional setup after loading the view.
}
// clicked sign up
#IBAction func signupBtnClick(_ sender: AnyObject) {
print("sign up pressed")
// dismiss keyboard
self.view.endEditing(true)
// if fields are empty
if (emailTxt.text!.isEmpty || passwordTxt.text!.isEmpty || confirmTxt.text!.isEmpty || businessNameTxt.text!.isEmpty || AddressTxt.text!.isEmpty || contactNumTxt.text!.isEmpty) {
// alert pop up message
let alert = UIAlertController(title: "Please", message: "fill all fields", preferredStyle: UIAlertControllerStyle.alert)
let ok = UIAlertAction(title: "OK", style: UIAlertActionStyle.cancel, handler: nil)
alert.addAction(ok)
self.present(alert, animated: true, completion: nil)
}
// if incorrect password
if passwordTxt.text != confirmTxt.text {
// alert pop up message
let alert = UIAlertController(title: "Attention", message: "incorrect password", preferredStyle: UIAlertControllerStyle.alert)
let ok = UIAlertAction(title: "OK", style: UIAlertActionStyle.cancel, handler: nil)
alert.addAction(ok)
self.present(alert, animated: true, completion: nil)
}
// send data to related columns in server
let businessUser = PFUser()
businessUser.username = businessNameTxt.text?.lowercased()
businessUser.email = emailTxt.text?.lowercased()
businessUser.password = passwordTxt.text
businessUser["BusinessUser"] = businessNameTxt.text?.lowercased()
businessUser["Address"] = AddressTxt.text?.lowercased()
businessUser["ContactNumber"] = contactNumTxt.text?.lowercased()
// convert our image for sending to server
let avaData = UIImageJPEGRepresentation(BusinessAvaImg.image!, 0.5)
let avaFile = PFFile(name: "ava.jpg", data: avaData!)
businessUser["ava"] = avaFile
// save data in server
businessUser.signUpInBackground { (success: Bool, error: Error?) -> Void in
if success {
print("registered")
// remember logged user
UserDefaults.standard.set(businessUser.username, forKey: "username")
UserDefaults.standard.synchronize()
// calls login func from AppDelegate.swift class
let appDelegate : AppDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.login()
} else {
// show alert message
let alert = UIAlertController(title: "Error", message: error!.localizedDescription, preferredStyle: UIAlertControllerStyle.alert)
let ok = UIAlertAction(title: "OK", style: UIAlertActionStyle.cancel, handler: nil)
alert.addAction(ok)
self.present(alert, animated: true, completion: nil)
}
}
}
// clicked return to log in
#IBAction func businessLoginBtnClick(_ sender: AnyObject) {
self.dismiss(animated: true, completion: nil)
// dismiss keyboard
self.view.endEditing(true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
This has now taken me a couple of days attempting to fix, so I would be very appreciative if anyone could help.
The crucial information is
[Truegram.signUpVC signupBtnClick:]: unrecognized selector sent to instance 0x7f9d0700cff0'
In Swift 3 IBActions are supposed to be declared with an underscore to omit the parameter label.
#IBAction func signupBtnClick(_ sender: AnyObject)
PFCloud.callFunctionInBackground("updatePositions", withParameters: ["username" : username, "location" : locationTitle, "instructor" : instructorSwitch.on, "guard" : guardSwitch.on, "sup" : supSwitch.on]) {
(positions: AnyObject!, error: NSError!) -> Void in
if error == nil {
self.currentUser.setValue(self.instructorSwitch.on, forKey: (self.shortTitle + "Instructor"))
self.currentUser.setValue(self.guardSwitch.on, forKey: (self.shortTitle + "Guard"))
self.currentUser.setValue(self.supSwitch.on, forKey: (self.shortTitle + "Sup"))
self.currentUser.save(nil)
self.navigationController?.popToRootViewControllerAnimated(true)
}
else {
let errorAlert = UIAlertController (title: "Error", message: "There was an error while processing your request. This may be because the app could not connect to the internet. Please try again.", preferredStyle: UIAlertControllerStyle.Alert)
let actionCancel = UIAlertAction (title: "Dismiss", style: .Cancel, handler: nil)
errorAlert.addAction(actionCancel)
self.presentViewController(errorAlert, animated: true, completion: nil)
}
When I run the above code, my goal to is to update the Parse Objects in my Data Browser and at the same time edit the same objects in currentUser.
As it stands now, the Login VC is shown if no one is logged in (based on a boolean value in Core Data). When a user logs in they are taken to the apps Home Page (and the boolean value is changed). If they were to shutdown the app and restart, they would be taken to the Home Page rather than the Login VC. I have a "dummy" VC where the Core Data entity is updated (I know it sounds redundant but it's there for a purpose that is unrelated to my issue).
Login VC
#IBOutlet var fullName: UITextField!
#IBOutlet var password: UITextField!
#IBOutlet var helpButton: UIButton!
#IBOutlet var loginButton: UIButton!
var nameID: String!
var passwordStatus: Bool?
var username: String?
var userID: String?
var objectID: String!
var currentUser = PFUser.currentUser()
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.hidesBackButton = true
checkInternetConnection()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
self.view.endEditing(true)
}
#IBAction func LoginTapped(sender: AnyObject) {
if (fullName.text.isEmpty || password.text.isEmpty) {
let alert = UIAlertView()
alert.title = "Error"
alert.message = "You have not completed the required fields!"
alert.addButtonWithTitle("Dismiss")
alert.show()
}
else {
PFUser.logInWithUsernameInBackground(fullName.text, password: password.text) {
(user: PFUser!, error: NSError!) -> Void in
if (user != nil) {
self.currentUser = user
self.currentUser.save()
//println(self.currentUser.objectForKey("username") as String!)
if (self.currentUser.objectForKey("passwordChanged") as Bool! == nil || self.currentUser.objectForKey("passwordChanged") as Bool! == false) {
self.performSegueWithIdentifier("password", sender: self)
}
else {
self.performSegueWithIdentifier("skipPassword", sender: self)
}
}
else {
let errorAlert = UIAlertController (title: "Error", message: "Profile not found. Please check your username and password.", preferredStyle: UIAlertControllerStyle.Alert)
let actionCancel = UIAlertAction (title: "Dismiss", style: .Cancel, handler: nil)
errorAlert.addAction(actionCancel)
self.presentViewController(errorAlert, animated: true, completion: nil)
}
}
}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "password") {
var svc = segue.destinationViewController as AccountSetup
svc.username = fullName.text
}
else if (segue.identifier == "skipPassword") {
var svc = segue.destinationViewController as Agree
svc.password = password.text
}
}
The only place that there is a chance for the user to log out is in the app's Settings controller:
Settings VC
var choice: String!
var currentEmail: String!
var sendEmail: String!
var email: UITextField!
var listArray = NSArray (objects: "-", "-", "-", "1", "2", "3", "4", "5", "6")
var currentUser = PFUser.currentUser()
var res : NSManagedObject!
var context : NSManagedObjectContext!
override func viewDidLoad() {
super.viewDidLoad()
currentEmail = currentUser.objectForKey("email") as String!
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 4
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Return the number of rows in the section.
switch(section) {
case 0: return 2
case 1: return 9
case 2: return 4
case 3: return 1
default: fatalError("Unknown number of sections")
}
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
choice = listArray.objectAtIndex(indexPath.row) as NSString
if (indexPath.row >= 3) {
performSegueWithIdentifier("positions", sender: self)
}
else if (indexPath.row == 1) {
var passwordAlert = UIAlertController (title: "Change Password", message: "For security reasons, you can not change your password through the app. We will send an email to " + currentEmail + ". If you rather us send it to another email, enter it below. Otherwise, click 'Send'.", preferredStyle: UIAlertControllerStyle.Alert)
let standardCancel = UIAlertAction (title: "Dismiss", style: .Cancel, handler: nil)
let actionSubmit = UIAlertAction (title: "Send", style: .Default) { (action) in
if (self.email.text.isEmpty) {
self.sendEmail = self.currentEmail
}
else {
self.sendEmail = self.email.text
}
PFUser.requestPasswordResetForEmailInBackground(self.sendEmail){
(success: Bool!, error: NSError!) -> Void in
if (success == true) {
let emailAlert = UIAlertController (title: "Password", message: "An email containing information on how to change your password has been sent to " + self.sendEmail + ".", preferredStyle: UIAlertControllerStyle.Alert)
emailAlert.addAction(standardCancel)
self.presentViewController(emailAlert, animated: true, completion: nil)
}
else {
let errorAlert = UIAlertController (title: "Error", message: "There was an error while processing your request. This may be because the email is invalid or the app could not connect to the internet. Please try again.", preferredStyle: UIAlertControllerStyle.Alert)
errorAlert.addAction(standardCancel)
self.presentViewController(errorAlert, animated: true, completion: nil)
}
}
}
let actionCancel = UIAlertAction (title: "Cancel", style: .Cancel, handler: nil)
passwordAlert.addTextFieldWithConfigurationHandler {
(textField) in textField.placeholder = "Email"
self.email = textField
}
passwordAlert.addAction(actionSubmit)
passwordAlert.addAction(actionCancel)
self.presentViewController(passwordAlert, animated: true, completion: nil)
}
}
#IBAction func logoutPressed (sender: AnyObject) {
PFUser.logOut()
//code to change Core Data boolean value to start on Login is here but there is no need to put it (unrelated)
tabBarController?.tabBar.hidden = true
performSegueWithIdentifier("goToLogin", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "positions") {
var svc = segue.destinationViewController as UpdatePositions;
svc.locationTitle = choice
println (choice)
}
}
Like I've mentioned before, everything works up until the app is shutdown where the authentication is lost.
Ends up that there was an error in the 1.6.0 Parse SDK. Works fine with 1.6.5.