-1
그래서 내 앱에 간단한 로그인 시스템을 만드는 방법에 대한 Belal Khan의 가이드를 따르고있었습니다. 나는 튜토리얼을 따라 그것은 내가 로그 아웃 버튼을로그 아웃 스위프트에서 앱이 다운 됨
로그인 VC 존재 로그인 VC와 ProfileVC에 대한 코드를 게시 할 예정입니다 로그 아웃하려는 지점까지 아름답게 작동합니다
import UIKit
import Alamofire
class ViewController: UIViewController {
//The login script url make sure to write the ip instead of localhost
//you can get the ip using ifconfig command in terminal
let URL_USER_LOGIN = "http://madrasati.site/userReg/v1/login.php"
//the defaultvalues to store user data
//the defaultvalues to store user data
let defaultValues = UserDefaults.standard
//the connected views
//don't copy instead connect the views using assistant editor
@IBOutlet weak var labelMessage: UILabel!
@IBOutlet weak var textFieldUserName: UITextField!
@IBOutlet weak var textFieldPassword: UITextField!
//the button action function
@IBAction func buttonLogin(_ sender: UIButton) {
//getting the username and password
let parameters: Parameters=[
"username":textFieldUserName.text!,
"password":textFieldPassword.text!
]
//making a post request
Alamofire.request(URL_USER_LOGIN, method: .post, parameters: parameters).responseJSON
{
response in
//printing response
print(response)
//getting the json value from the server
if let result = response.result.value {
let jsonData = result as! NSDictionary
//if there is no error
if(!(jsonData.value(forKey: "error") as! Bool)){
//getting the user from response
let user = jsonData.value(forKey: "user") as! NSDictionary
//getting user values
let userId = user.value(forKey: "id") as! Int
let userName = user.value(forKey: "username") as! String
let userEmail = user.value(forKey: "email") as! String
let userPhone = user.value(forKey: "phone") as! String
//saving user values to defaults
self.defaultValues.set(userId, forKey: "userid")
self.defaultValues.set(userName, forKey: "username")
self.defaultValues.set(userEmail, forKey: "useremail")
self.defaultValues.set(userPhone, forKey: "userphone")
//switching the screen
let profileViewController = self.storyboard?.instantiateViewController(withIdentifier: "ProfileViewController") as! ProfileViewController
self.navigationController?.pushViewController(profileViewController, animated: true)
self.dismiss(animated: false, completion: nil)
}else{
//error message in case of invalid credential
self.labelMessage.text = "Invalid username or password"
}
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
//hiding the navigation button
let backButton = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.plain, target: navigationController, action: nil)
navigationItem.leftBarButtonItem = backButton
// Do any additional setup after loading the view, typically from a nib.
//if user is already logged in switching to profile screen
if defaultValues.string(forKey: "username") != nil{
let profileViewController = self.storyboard?.instantiateViewController(withIdentifier: "ProfileViewcontroller") as! ProfileViewController
self.navigationController?.pushViewController(profileViewController, animated: true)
}
}
}
ProfileViewController는 :
2017-11-30 22:03:33.987292+0400 registerationtest[40711:1152571] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Storyboard() doesn't contain a view controller with identifier 'ViewController'' *** First throw call stack: ( 0 CoreFoundation 0x0000000105e981ab __exceptionPreprocess + 171 1 libobjc.A.dylib 0x000000010552df41 objc_exception_throw + 48 2 UIKit 0x0000000106c2b1f3 -[UIStoryboard instantiateInitialViewController] + 0 3 registerationtest 0x0000000104a64221 _T017registerationtest21ProfileViewControllerC12buttonLogoutySo8UIButtonCF + 913 4 registerationtest 0x0000000104a6482c _T017registerationtest21ProfileViewControllerC12buttonLogoutySo8UIButtonCFTo + 60 5 UIKit 0x0000000106315275 -[UIApplication sendAction:to:from:forEvent:] + 83 6 UIKit 0x00000001064924a2 -[UIControl sendAction:to:forEvent:] + 67 7 UIKit 0x00000001064927bf -[UIControl _sendActionsForEvents:withEvent:] + 450 8 UIKit 0x00000001064916ec -[UIControl touchesEnded:withEvent:] + 618 9 UIKit 0x000000010638abbb -[UIWindow _sendTouchesForEvent:] + 2807 10 UIKit 0x000000010638c2de -[UIWindow sendEvent:] + 4124 11 UIKit 0x000000010632fe36 -[UIApplication sendEvent:] + 352 12 UIKit 0x0000000106c72434 __dispatchPreprocessedEventFromEventQueue + 2809 13 UIKit 0x0000000106c75089 __handleEventQueueInternal + 5957 14 CoreFoundation 0x0000000105e3b231 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17 15 CoreFoundation 0x0000000105edae41 __CFRunLoopDoSource0 + 81 16 CoreFoundation 0x0000000105e1fb49 __CFRunLoopDoSources0 + 185 17 CoreFoundation 0x0000000105e1f12f __CFRunLoopRun + 1279 18 CoreFoundation 0x0000000105e1e9b9 CFRunLoopRunSpecific + 409 19 GraphicsServices 0x000000010e1809c6 GSEventRunModal + 62 20 UIKit 0x00000001063135e8 UIApplicationMain + 159 21 registerationtest 0x0000000104a63827 main + 55 22 libdyld.dylib 0x000000010a3bed81 start + 1 23 ??? 0x0000000000000001 0x0 + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb):
import UIKit
class ProfileViewController: UIViewController {
//label again don't copy instead connect
@IBOutlet weak var labelUserName: UILabel!
//button
@IBAction func buttonLogout(_ sender: UIButton) {
//removing values from default
UserDefaults.standard.removePersistentDomain(forName: Bundle.main.bundleIdentifier!)
UserDefaults.standard.synchronize()
//switching to login screen
let loginViewController = self.storyboard?.instantiateViewController(withIdentifier: "ViewController") as! ViewController
self.navigationController?.pushViewController(loginViewController, animated: true)
self.dismiss(animated: false, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
//hiding back button
let backButton = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.plain, target: navigationController, action: nil)
navigationItem.leftBarButtonItem = backButton
//getting user data from defaults
let defaultValues = UserDefaults.standard
if let name = defaultValues.string(forKey: "username"){
//setting the name to label
labelUserName.text = name
}else{
//send back to login view controller
}
}
}
이 내가지고있어 오류입니다
편집 : 오류가이 라인을 참조한다
2017-11-30 22:05:22.424198+0400 registerationtest[40745:1154215] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Storyboard() doesn't contain a view controller with identifier 'ViewController'' *** First throw call stack: ( 0 CoreFoundation 0x000000010cb6d1ab __exceptionPreprocess + 171 1 libobjc.A.dylib 0x000000010c202f41 objc_exception_throw + 48 2 UIKit 0x000000010d9001f3 -[UIStoryboard instantiateInitialViewController] + 0 3 registerationtest 0x000000010b739221 _T017registerationtest21ProfileViewControllerC12buttonLogoutySo8UIButtonCF + 913 4 registerationtest 0x000000010b73982c _T017registerationtest21ProfileViewControllerC12buttonLogoutySo8UIButtonCFTo + 60 5 UIKit 0x000000010cfea275 -[UIApplication sendAction:to:from:forEvent:] + 83 6 UIKit 0x000000010d1674a2 -[UIControl sendAction:to:forEvent:] + 67 7 UIKit 0x000000010d1677bf -[UIControl _sendActionsForEvents:withEvent:] + 450 8 UIKit 0x000000010d1666ec -[UIControl touchesEnded:withEvent:] + 618 9 UIKit 0x000000010d05fbbb -[UIWindow _sendTouchesForEvent:] + 2807 10 UIKit 0x000000010d0612de -[UIWindow sendEvent:] + 4124 11 UIKit 0x000000010d004e36 -[UIApplication sendEvent:] + 352 12 UIKit 0x000000010d947434 __dispatchPreprocessedEventFromEventQueue + 2809 13 UIKit 0x000000010d94a089 __handleEventQueueInternal + 5957 14 CoreFoundation 0x000000010cb10231 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17 15 CoreFoundation 0x000000010cbafe41 __CFRunLoopDoSource0 + 81 16 CoreFoundation 0x000000010caf4b49 __CFRunLoopDoSources0 + 185 17 CoreFoundation 0x000000010caf412f __CFRunLoopRun + 1279 18 CoreFoundation 0x000000010caf39b9 CFRunLoopRunSpecific + 409 19 GraphicsServices 0x0000000114dca9c6 GSEventRunModal + 62 20 UIKit 0x000000010cfe85e8 UIApplicationMain + 159 21 registerationtest 0x000000010b738827 main + 55 22 libdyld.dylib 0x0000000111001d81 start + 1 23 ??? 0x0000000000000001 0x0 + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)
내가 그 통지를하지 않았지만, 같은 스토리 보드에서 2 개 viewControllers가 있습니까 지금은 그것이 ID 부여했고 여전히 나에게 오류 – SwiftyDude
을주고? – creeperspeak
nvm 지금은 대단히 감사합니다. 문제는 내 Xcode가 도청 당했고 제대로 구축되지 않고 모든 것이 좋지 않다는 것이 었습니다. 고마워요! – SwiftyDude