修复iOS 14闪退的问题, 优化一些问题

This commit is contained in:
hext 2022-01-30 01:02:44 +08:00
parent 5b4371ac8f
commit aa348feace
13 changed files with 68 additions and 22 deletions

View File

@ -20,8 +20,12 @@ class AppDelegate: NSObject, UIApplicationDelegate, UNUserNotificationCenterDele
application.registerForRemoteNotifications()
Task {
// APP, , . ()
_ = try await HttpRequest.fake()
let notFirstStart = UserDefaults.standard.bool(forKey: "PushDeer_notFirstStart")
if !notFirstStart {
// APP, , . ()
_ = try await HttpRequest.fake()
UserDefaults.standard.set(true, forKey: "PushDeer_notFirstStart")
}
}
return true
@ -33,6 +37,10 @@ class AppDelegate: NSObject, UIApplicationDelegate, UNUserNotificationCenterDele
AppState.shared.deviceToken = deviceTokenString
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("didFailToRegisterForRemoteNotificationsWithError: ", error)
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification) async -> UNNotificationPresentationOptions {
print("willPresent:", notification.request.content.userInfo)
Task {

View File

@ -23,7 +23,7 @@ struct TokenContent: Codable{
struct UserInfoContent: Codable{
let id: Int
let name: String
let email: String
let email: String?
let apple_id: String
let wechat_id: String?
let level: Int

View File

@ -20,7 +20,7 @@ class AppState: ObservableObject {
/// key
@Published var keys: [KeyItem] = []
///
// @Published var messages: [MessageItem] = []
// @Published var messages: [MessageItem] = []
/// tab
@Published var tabSelectedIndex: Int {
didSet {
@ -79,13 +79,18 @@ class AppState: ObservableObject {
} catch {
print(error)
//
throw NSError(domain: NSLocalizedString("登录失败", comment: "AppleId登录失败时提示") + "\n\(error.localizedDescription)", code: -4, userInfo: [NSLocalizedDescriptionKey: NSLocalizedString("登录失败", comment: "AppleId登录失败时提示") + "(-4)\n\(error.localizedDescription)"])
}
} else {
// Apple
throw NSError(domain: NSLocalizedString("登录失败", comment: "AppleId登录失败时提示"), code: -3, userInfo: [NSLocalizedDescriptionKey: NSLocalizedString("登录失败", comment: "AppleId登录失败时提示") + "(-3)"])
}
case let .failure(error):
print(error)
// Apple
throw NSError(domain: NSLocalizedString("登录失败", comment: "AppleId登录失败时提示") + "\n\(error.localizedDescription)", code: -2, userInfo: [NSLocalizedDescriptionKey: NSLocalizedString("登录失败", comment: "AppleId登录失败时提示") + "(-2)\n\(error.localizedDescription)"])
}
//
throw NSError(domain: NSLocalizedString("登录失败", comment: "AppleId登录失败时提示"), code: -1, userInfo: nil)
}
}

View File

@ -26,9 +26,9 @@ struct HttpRequest {
continuation.resume(returning: content)
} else if result.code == 80403 {
AppState.shared.token = ""
continuation.resume(throwing: NSError(domain: result.error ?? NSLocalizedString("登录过期", comment: "token失效时提示"), code: result.code, userInfo: nil))
continuation.resume(throwing: NSError(domain: result.error ?? NSLocalizedString("登录过期", comment: "token失效时提示"), code: result.code, userInfo: [NSLocalizedDescriptionKey: result.error ?? NSLocalizedString("登录过期", comment: "token失效时提示")]))
} else {
continuation.resume(throwing: NSError(domain: result.error ?? NSLocalizedString("接口报错", comment: "接口报错时提示"), code: result.code, userInfo: nil))
continuation.resume(throwing: NSError(domain: result.error ?? NSLocalizedString("接口报错", comment: "接口报错时提示"), code: result.code, userInfo: [NSLocalizedDescriptionKey: result.error ?? NSLocalizedString("接口报错", comment: "接口报错时提示")]))
}
} catch {
print(error)

View File

@ -31,7 +31,9 @@ struct PersistenceController {
* The store could not be migrated to the current model version.
Check the error message to determine what the actual problem was.
*/
fatalError("Unresolved error \(error), \(error.userInfo)")
// fatalError("Unresolved error \(error), \(error.userInfo)")
print("Unresolved error \(error), \(error.userInfo)")
HToast.showError("数据库初始化失败!\n\(error.localizedDescription)")
}
})
container.viewContext.automaticallyMergesChangesFromParent = true

View File

@ -42,7 +42,7 @@ struct DeletableView<Content : View> : View {
DragGesture()
.onChanged({ value in
let width = value.translation.width
print("onChanged", width)
// print("onChanged", width)
let endX = isShowDelete ? offsetMaxX : 0.0
if width < endX {
offsetX = width - endX

View File

@ -25,8 +25,14 @@ struct EditableText: View {
var body: some View {
if #available(iOS 15.0, *) {
textField()
.submitLabel(.done)
Group {
// https://stackoverflow.com/questions/70506330/swiftui-app-crashes-with-different-searchbar-viewmodifier-on-ios-14-15/70603710#70603710
// !!!: Group , , Xcode 13.2 bug, if , .
if #available(iOS 15.0, *) {
textField()
.submitLabel(.done)
}
}
} else {
textField()
}

View File

@ -89,7 +89,10 @@ struct DeviceItemView: View {
if deviceName.contains("mac") {
return "macwindow"
}
return "ipad.and.iphone"
if #available(iOS 15.0, *) {
return "ipad.and.iphone"
}
return "laptopcomputer.and.iphone"
}
}

View File

@ -37,6 +37,7 @@ struct LoginView: View {
//
} catch {
showLoading = false
HToast.showError(error.localizedDescription)
}
}
)

View File

@ -15,7 +15,11 @@ struct MainView: View {
TabView.init(selection: $store.tabSelectedIndex) {
DeviceListView()
.tabItem {
Label("设备",systemImage: "ipad.and.iphone")
if #available(iOS 15.0, *) {
Label("设备",systemImage: "ipad.and.iphone")
} else {
Label("设备",systemImage: "laptopcomputer.and.iphone")
}
}
.tag(0)

View File

@ -14,7 +14,7 @@ struct MessageListView: View {
@FetchRequest(sortDescriptors: [NSSortDescriptor(keyPath: \MessageModel.created_at, ascending: false)], animation: .default)
private var messages: FetchedResults<MessageModel>
var body: some View {
BaseNavigationView(title: "消息") {
ScrollView {
@ -78,12 +78,16 @@ struct TestPushView: View {
store.keys = try await HttpRequest.getKeys().keys
}
if let keyItem = store.keys.first {
_ = try await HttpRequest.push(pushkey: keyItem.key, text: testText, desp: "", type: "")
testText = ""
HToast.showSuccess(NSLocalizedString("推送成功", comment: ""))
let messageItems = try await HttpRequest.getMessages().messages
withAnimation(.easeOut) {
try? MessageModel.saveAndUpdate(messageItems: messageItems)
do {
_ = try await HttpRequest.push(pushkey: keyItem.key, text: testText, desp: "", type: "")
testText = ""
HToast.showSuccess(NSLocalizedString("推送成功", comment: ""))
let messageItems = try await HttpRequest.getMessages().messages
withAnimation(.easeOut) {
try? MessageModel.saveAndUpdate(messageItems: messageItems)
}
} catch {
HToast.showError(error.localizedDescription)
}
} else {
HToast.showError(NSLocalizedString("推送失败, 请先添加一个Key", comment: ""))

View File

@ -15,7 +15,7 @@ struct SettingsView: View {
var body: some View {
BaseNavigationView(title: "设置") {
VStack {
SettingsItemView(title: NSLocalizedString("登录为", comment: "") + " \(store.userInfo?.name ?? "--")", button: NSLocalizedString("退出", comment: "退出登录按钮上的文字")) {
SettingsItemView(title: NSLocalizedString("登录为", comment: "") + " " + userName(), button: NSLocalizedString("退出", comment: "退出登录按钮上的文字")) {
store.token = ""
}
.padding(EdgeInsets(top: 18, leading: 20, bottom: 0, trailing: 20))
@ -45,6 +45,18 @@ struct SettingsView: View {
}
}
}
func userName() -> String {
if let name = store.userInfo?.name {
if name.isEmpty {
return NSLocalizedString("苹果用户", comment: "")
} else {
return name
}
} else {
return "--"
}
}
}
struct SettingsView_Previews: PreviewProvider {

View File

@ -109,3 +109,4 @@
/* No comment provided by engineer. */
"自定义服务器" = "Custom server";
"苹果用户" = "Apple User";