升级OneSignal iOS 1.13.3至2.8.2后遇代码错误,寻求技术帮助
Hey there, let's work through those errors you ran into after upgrading from OneSignal SDK 1.13.3 to 2.8.2. The API changed quite a bit between these versions, so your old code needs some targeted adjustments:
1. "Argument passed to call that takes no arguments" (Initialization Error)
In SDK 1.x, you initialized OneSignal with a constructor that accepted launch options, app ID, and a notification handler. But in 2.x, this constructor was completely removed—instead, you configure OneSignal using static methods and set up notification handling via dedicated closures.
Replace your old initialization code with this updated version:
// Set your app ID first OneSignal.setAppId(appID) // Pass launch options if your app relies on them OneSignal.setLaunchOptions(launchOptions) // Handle notifications received while the app is active in the foreground OneSignal.notificationWillShowInForeground = { notification in // Map the new notification object to your existing handler logic let userInfo = notification.payload.additionalData ?? [:] NotificationHandler.handleNotification(userInfo: userInfo, appWasActive: true) } // Optional: Add logic to handle notifications when the user taps on them OneSignal.notificationOpened = { result in let userInfo = result.notification.payload.additionalData ?? [:] NotificationHandler.handleNotification(userInfo: userInfo, appWasActive: false) }
The core change here is that 2.x uses static configuration methods (setAppId, setLaunchOptions) instead of a constructor, and notification handling is managed via closure properties rather than passing a handler during initialization.
2. "Type 'OneSignal' has no member 'defaultClient'"
The defaultClient() method was deprecated and removed in 2.x—all SDK operations now use static methods or properties directly on the OneSignal class.
Your old line:
OneSignal.defaultClient().enable(inAppAlertNotification: false)
Replace it with this 2.x equivalent to disable in-app alert notifications:
OneSignal.inAppMessagesEnabled = false
If you were targeting a specific type of in-app alert, you can cross-reference the 2.8.2 SDK docs for more granular controls, but this line replicates the core behavior of disabling in-app alert notifications.
内容的提问来源于stack exchange,提问作者Igor Jorin

