gpt4 book ai didi

ios - 如果IOS信息不正确,如何阻止用户通过登录页面

转载 作者:行者123 更新时间:2023-12-01 15:48:53 26 4
gpt4 key购买 nike

在我的IOS应用中,我想向尝试使用错误凭据登录的用户发送消息,并通过弹出窗口通知他们。目前,我的数据库可以识别登录错误,但是我的快速代码只有在取消登录页面并进入应用程序后才能看到错误状态。

访问数据库的flask / python代码如下所示:

@app.route('/login', methods=['GET', 'POST'])
def login():
mydb = mysql.connector.connect(host="localhost", user="root", passwd="Pass", database = "events")
if request.method == 'POST':
mycursor = mydb.cursor()
username = request.form['username']
password = request.form['password']

mycursor.execute('SELECT* FROM accounts WHERE username = %s AND password = %s', (username, password,))
account = mycursor.fetchone()

if account:

try:
mydb.commit()
mydb.close()
except e:
# Rollback in case there is any error
print("Error: ", e)
mydb.rollback()

return make_response("Success!", 200)
else:
return make_response("username/password combination dne", 500)

与我的应用程序内的数据库联系的swiftui代码如下所示:
struct LogInView: View {

@State var username: String = ""
@State var password: String = ""
@State var email: String = "test@gmail.com"
@Binding var didLogin: Bool
@Binding var needsAccount: Bool
@State var errorString: String = ""


func send(_ sender: Any, completion: @escaping (String) -> Void) {

let request = NSMutableURLRequest(url: NSURL(string: "http://localhost/login")! as URL)
request.httpMethod = "POST"

self.username = "\(self.username)"
self.password = "\(self.password)"
self.email = "\(self.email)"

let postString = "username=\(self.username)&password=\(self.password)&c=\(self.email)"

request.httpBody = postString.data(using: String.Encoding.utf8)

let task = URLSession.shared.dataTask(with: request as URLRequest) { data, response, error in


if error != nil {
print("error=\(String(describing: error))")
//put variable that triggers error try again view here
self.didLogin = false
self.errorString = String(describing: error)
completion(self.errorString)
return
}else{
self.didLogin = true
completion(String(describing: error))
}

print("response = \(String(describing: response))")

let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
print("responseString = \(String(describing: responseString))")

if let httpResponse = response as? HTTPURLResponse {
self.errorString = String(httpResponse.statusCode)

}


}
task.resume()


}


var body: some View {
VStack{
Spacer()
WelcomeText()
UserImage()
TextField("Username", text: $username)
.padding()
.background(Color(.lightGray))
.cornerRadius(5.0)
.padding(.bottom, 20)
SecureField("Password", text: $password)
.padding()
.background(Color(.lightGray))
.cornerRadius(5.0)
.padding(.bottom, 20)
Button(action: {
self.send((Any).self){ array in
self.errorString = array
}/*
if self.errorString == "500"{
self.didLogin = false
}
else{
self.didLogin = true
}
}*/
},
label: {Text("LOGIN")
.font(.headline)
.foregroundColor(.white)
.padding()
.frame(width: 220, height: 60)
.background(Color.orange)
.cornerRadius(15.0)})
.shadow(radius: 5)
.padding(.bottom, 10)
Button(action: {
self.needsAccount = true
}, label: {Text("Not a member yet? Sign up here")})
Spacer()
}.padding().background(Color.white).edgesIgnoringSafeArea(.all)
}
}

ContentView:
import SwiftUI
import Mapbox
import CoreLocation

struct ContentView: View {
@ObservedObject var annotationsVM: AnnotationsVM //= AnnotationsVM()
@ObservedObject var VModel: ViewModel //= ViewModel()
@ObservedObject var locationManager: LocationManager //= LocationManager()
@ObservedObject var data: DataFetcher
// @ObservedObject var mapViewCoordinator = MapViewCoordinator()

init() {
let vm = ViewModel()
VModel = vm
annotationsVM = AnnotationsVM(VModel: vm)
locationManager = LocationManager()
data = DataFetcher()
}


var userLatitude: CLLocationDegrees {
return (locationManager.lastLocation?.latitude ?? 0)
}

var userLongitude: CLLocationDegrees {
return (locationManager.lastLocation?.longitude ?? 0)
}

var lat: Double {
return (VModel.lat ?? 0)
}
var long: Double {
return (VModel.lon ?? 0)
}

var Userlat: Double {
return (VModel.userLatitude)
}
var Userlon: Double {
return (VModel.userLongitude)
}

//@State var searchedLocation: String = ""
@State private var annotationSelected: Bool = false
@State private var renderingMap: Bool = true
@State private var searchedText: String = ""
@State private var showResults: Bool = false
@State private var events: [eventdata] = []
@State private var showMoreDetails: Bool = false
@State private var didLogin: Bool = false
@State private var needsAccount: Bool = false
@State private var selectedAnnotation: MGLAnnotation? = nil


var body: some View {

VStack{
ZStack(alignment: Alignment(horizontal: .leading, vertical: .top)){



MapView(annotationSelected: $annotationSelected, renderingMap: $renderingMap, visited: $annotationsVM.visited, showMoreDetails: $showMoreDetails, selectedAnnotation: $selectedAnnotation, VModel: VModel, locationManager: locationManager, aVM: annotationsVM, data: data, annos: $annotationsVM.annos)
.edgesIgnoringSafeArea(.all)

if showResults == true && searchedText.count >= 1 {
Text("").frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity).background(Color.white).edgesIgnoringSafeArea(.all)
//this is pretty ghetto but whatever
}

VStack{
HStack(alignment: .top){

if showResults == false {
SettingsButton()
}

Spacer()

SearchBar(annotation: annotationsVM, VModel: VModel, searchedText: $searchedText, showResults: $showResults, showMoreDetails: $showMoreDetails)
// SearchBar(annotation: annotationsVM) { sender in
// self.searchedLocation = sender.searchText.text
// }

Spacer()

if showResults == false {
MessageButton()
}

}.padding()

//Update Annotation Button
// Button (action: {
// let delayInSeconds = 1.5
// DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + delayInSeconds) {
// self.annotationsVM.addNextAnnotation(address: "22 Sunset Ave, East Quogue, NY")
//
// print("\(self.annotationsVM.annos)")
// print("User Coords: \(self.VModel.userLatitude), \(self.VModel.userLongitude)")
// }
// }, label: {Text("Press to update annotation")})

if showResults == true && searchedText.count >= 1 {
SearchResults(VModel: VModel, annotation: annotationsVM, showResults: $showResults, searchedText: $searchedText)
}

Spacer()

HStack(alignment: .bottom) {
if renderingMap {
Text("The Map is Rendering...")
}
}

//Side Note: If the Create Event Button is pressed, the currently selected annotation is unselected, so that'll need to be fixed
HStack(alignment: .bottom) {
if annotationSelected {
CreateEventButton(annotation: annotationsVM, annotationSelected: $annotationSelected)
}
}.padding()
}
VStack {
Spacer()
HStack {
Spacer()
if annotationsVM.annotationPlacementFailed == true {
AnnotationPlacementErrorView(annotationPlacementFailed: $annotationsVM.annotationPlacementFailed, annotation: annotationsVM, searchedText: $searchedText)
}
Spacer()
}
Spacer()
}
VStack {
Spacer()
HStack{
Spacer()
if self.showMoreDetails == true {
MoreDetailsView(searchedText: $searchedText, showMoreDetails: $showMoreDetails, selectedAnnotation: $selectedAnnotation)
//Instead of passing in searchedText, we need to pass in the mapView...idk how though
}
Spacer()
}
Spacer()
}
if self.didLogin == false {
LogInView(didLogin: $didLogin, needsAccount: $needsAccount)
}

if self.needsAccount == true {
SignUpView(didLogin: $didLogin, needsAccount: $needsAccount)
}
}
}
}
}

我不确定这是数据库//服务器问题还是swiftui / httpresponse问题。非常感谢任何见解

最佳答案

如何关闭登录页面?假设您使用didLogin进行检查,则应将didLogin默认为false。因此,直到didLogin = true,用户才可以登录,这意味着它将一直等到您从http请求中获得响应。像这样的东西:

@State private var didLogin: Bool = false

if didLogin {
ShowSomeViewAfterLogin()
} else {
ShowLogin()
}

关于ios - 如果IOS信息不正确,如何阻止用户通过登录页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62234003/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com