在应用程序关闭时从服务器获取数据
Get data from the server while app is closed
最近我进入了swift的世界,我想了解以下内容:
如何在应用程序关闭时从数据库中获取数据?
例如,使用这段代码和 class 的相应解析,我获得了在我的应用程序中进行登录的数据,更改一些代码我可以从我的数据库中获得其他任何东西,到目前为止效果很好。但我今天需要的是能够 运行 一个类似于我展示的代码,但是当应用程序关闭时,并在收到数据时显示一个通知。
有什么帮助吗?
@IBAction func inicioSeccion(_ sender: Any) {
username = usernameText.text
password = passwordText.text
if(username!.isEmpty || password!.isEmpty){
showToas(message: "Please check the empty fields")
}else{
self.forData { (data) in
let parser = XMLParser(data: data)
let parserDelegate = ParserDelegate()
parser.delegate = parserDelegate
guard
parser.parse(),
let result:[UserXml] = parserDelegate.Users
else { return }
if result.count == 1
{
self.idUser = result[0].idUser
self.nombreUser = result[0].nombreUsuario
self.saveData()
DispatchQueue.main.async {
self.performSegue(withIdentifier: "welcomeConexion", sender: self)
}
}
}
}
}
func forData(completion: @escaping (Data) -> ()) {
let is_SoapMessage1 = String(format: "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'><soap:Body><WSLoginUser xmlns='http://tempuri.org/'><username>%@</username><password>%@</password></WSLoginUser></soap:Body></soap:Envelope>",username!,password!)
//showToas(message: "\(username!)"+" "+"\(password!)")
let is_URL1: String = "http://10.0.0.160/WebService.asmx"
let lobj_Request = NSMutableURLRequest(url: NSURL(string: is_URL1)! as URL)
let session = URLSession.shared
lobj_Request.httpMethod = "POST"
lobj_Request.httpBody = is_SoapMessage1.data(using: String.Encoding.utf8)
lobj_Request.addValue("10.0.0.160", forHTTPHeaderField: "Host")
lobj_Request.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
lobj_Request.addValue(String(is_SoapMessage1.count), forHTTPHeaderField: "Content-Length")
//lobj_Request.addValue("223", forHTTPHeaderField: "Content-Length")
lobj_Request.addValue("http://tempuri.org/WSLoginUser", forHTTPHeaderField: "SOAPAction")
let task = session.dataTask(with: lobj_Request as URLRequest, completionHandler: {data, response, error -> Void in
//print("Response: \(response!)")
var strData: String = String(data: data!, encoding: .ascii)!
strData = strData.replacingOccurrences(of: "\r", with: "\n")
if let data = data, error == nil {
completion(data)
} else {
print("error=\(error!.localizedDescription)")
}
})
task.resume()
}
您可以使用 background fetch 创建它:
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions:
[UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
// Fetch data once an hour.
UIApplication.shared.setMinimumBackgroundFetchInterval(3600)
// Other initialization…
return true
}
func application(_ application: UIApplication,
performFetchWithCompletionHandler completionHandler:
@escaping (UIBackgroundFetchResult) -> Void) {
// Check for new data.
if let newData = fetchUpdates() {
addDataToFeed(newData: newData)
completionHandler(.newData)
}
completionHandler(.noData)
}
但用户可以为您的应用程序禁止此功能。
最近我进入了swift的世界,我想了解以下内容: 如何在应用程序关闭时从数据库中获取数据? 例如,使用这段代码和 class 的相应解析,我获得了在我的应用程序中进行登录的数据,更改一些代码我可以从我的数据库中获得其他任何东西,到目前为止效果很好。但我今天需要的是能够 运行 一个类似于我展示的代码,但是当应用程序关闭时,并在收到数据时显示一个通知。 有什么帮助吗?
@IBAction func inicioSeccion(_ sender: Any) {
username = usernameText.text
password = passwordText.text
if(username!.isEmpty || password!.isEmpty){
showToas(message: "Please check the empty fields")
}else{
self.forData { (data) in
let parser = XMLParser(data: data)
let parserDelegate = ParserDelegate()
parser.delegate = parserDelegate
guard
parser.parse(),
let result:[UserXml] = parserDelegate.Users
else { return }
if result.count == 1
{
self.idUser = result[0].idUser
self.nombreUser = result[0].nombreUsuario
self.saveData()
DispatchQueue.main.async {
self.performSegue(withIdentifier: "welcomeConexion", sender: self)
}
}
}
}
}
func forData(completion: @escaping (Data) -> ()) {
let is_SoapMessage1 = String(format: "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'><soap:Body><WSLoginUser xmlns='http://tempuri.org/'><username>%@</username><password>%@</password></WSLoginUser></soap:Body></soap:Envelope>",username!,password!)
//showToas(message: "\(username!)"+" "+"\(password!)")
let is_URL1: String = "http://10.0.0.160/WebService.asmx"
let lobj_Request = NSMutableURLRequest(url: NSURL(string: is_URL1)! as URL)
let session = URLSession.shared
lobj_Request.httpMethod = "POST"
lobj_Request.httpBody = is_SoapMessage1.data(using: String.Encoding.utf8)
lobj_Request.addValue("10.0.0.160", forHTTPHeaderField: "Host")
lobj_Request.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
lobj_Request.addValue(String(is_SoapMessage1.count), forHTTPHeaderField: "Content-Length")
//lobj_Request.addValue("223", forHTTPHeaderField: "Content-Length")
lobj_Request.addValue("http://tempuri.org/WSLoginUser", forHTTPHeaderField: "SOAPAction")
let task = session.dataTask(with: lobj_Request as URLRequest, completionHandler: {data, response, error -> Void in
//print("Response: \(response!)")
var strData: String = String(data: data!, encoding: .ascii)!
strData = strData.replacingOccurrences(of: "\r", with: "\n")
if let data = data, error == nil {
completion(data)
} else {
print("error=\(error!.localizedDescription)")
}
})
task.resume()
}
您可以使用 background fetch 创建它:
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions:
[UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
// Fetch data once an hour.
UIApplication.shared.setMinimumBackgroundFetchInterval(3600)
// Other initialization…
return true
}
func application(_ application: UIApplication,
performFetchWithCompletionHandler completionHandler:
@escaping (UIBackgroundFetchResult) -> Void) {
// Check for new data.
if let newData = fetchUpdates() {
addDataToFeed(newData: newData)
completionHandler(.newData)
}
completionHandler(.noData)
}
但用户可以为您的应用程序禁止此功能。