我有一个使用 JSON 数据填充的 tableView。此时,只要 JSON 有更新,用户就需要下拉刷新 tableView 并加载新数据。
是否可以在用户手机上退出或完全未打开应用程序时在后台刷新数据,并向用户发送本地通知说有新数据?
如何做到这一点?我更关注本地通知。
我目前如何让我的应用程序读取 JSON 的示例:
private func fetchJSON() {
guard let url = URL(string: "https://example.com/example/example.php"),
let value = name.addingPercentEncoding(withAllowedCharacters: .urlQueryValueAllowed)
else { return }
var request = URLRequest(url: url)
URLSession.shared.dataTask(with: request) { data, _, error in
guard let data = data else { return }
do {
self.structure = try JSONDecoder().decode([structure].self,from:data)
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
catch {
print(error)
}
}.resume()
}
来自地址 example.com/example/example.php 的 api 的示例如下:
[{"person":"Jackson","number":"1928192",
"position":"driver"},{"person":"Jeff","number":"293829","position":"driver"}]
最佳答案
第 1 步:- 将 json 存储在 UserDefault 或 CoreData 中
如果您使用的是 UserDefault,我个人更喜欢 PropertyListEncoder&Decoder。
第 2 步:- 启用后台功能
第 3 步:- 在您的应用中实现本地通知代码。
关于json - 从关闭的应用程序发送有关新 JSON 数据的本地通知? | swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57040970/