You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MarketingCloudSDK致SwiftUI TestFlight Beta版应用间歇性崩溃(_dispatch_once)

Salesforce MarketingCloud推送间歇性崩溃(_dispatch_once相关)问题求助

我们基于Salesforce MarketingCloud实现推送通知功能,在内部Beta测试阶段发现应用会因_dispatch_once触发间歇性崩溃,目前无法确定是自身代码实现问题还是SFMCSDK的Bug。仅能获取到TestFlight崩溃统计、Xcode Organizer崩溃详情、项目内崩溃截图这类日志相关截图信息。

已完成的排查动作:

  • 严格遵循官方文档完成SDK及推送配置,标准推送功能正常运行
  • 移除了疑似引发问题的openDirect链接,崩溃现象依然存在
  • 检查Xcode构建设置中的Other Linker Flags,确认未配置该选项

以下是我们在SwiftUI中通过AppDelegate实现的核心代码:

import SFMCSDK
import MarketingCloudSDK
import Foundation
import SwiftUI
import UIKit

class AppDelegate: NSObject, UIApplicationDelegate, ObservableObject, URLHandlingDelegate {

    let appId = "correct appId String"
    let accessToken = "correct accessToken String"
    let appEndpoint = URL(string:"correct marketingCloud appEndpoint URL")!
    let mid = "correct marketing cloud ID"

    func configureSDK() {
        #if DEBUG
            SFMCSdk.setLogger(logLevel: .debug)
        #endif

        let mobilePushConfiguration = PushConfigBuilder(appId: appId)
            .setAccessToken(accessToken)
            .setMarketingCloudServerUrl(appEndpoint)
            .setDelayRegistrationUntilContactKeyIsSet(true)
            .setMid(mid)
            .setInboxEnabled(false)
            .setLocationEnabled(false)
            .setAnalyticsEnabled(true)
            .build()

        let completionHandler: (OperationResult) -> () = { result in
            if result == .success {
                // module is fully configured and ready for use
                SFMCSdk.mp.setURLHandlingDelegate(self)
            } else if result == .error {
                // module failed to initialize, check logs for more details
            } else if result == .cancelled {
                // module initialization was cancelled
            } else if result == .timeout {
                // module failed to initialize due to timeout, check logs for more details
            }
        }

        SFMCSdk.initializeSdk(ConfigBuilder().setPush(config: mobilePushConfiguration,
          onCompletion: completionHandler).build())
    }
    
    func registerPush(contactID:String?) {
        #if !targetEnvironment(simulator)
            #if DEBUG
            SFMCSdk.identity.setProfileId("developer@developer.com")
            setupMobilePush()
            #else
            if let contactKey = contactID {
                SFMCSdk.identity.setProfileId(contactKey)
                setupMobilePush()
            }
            #endif
        #endif
    }
    
    func sfmc_handleURL(_ url: URL, type: String) {
        print(url.description)
    }
}

extension AppDelegate: UNUserNotificationCenterDelegate {
    func setupMobilePush() {

        DispatchQueue.main.async {
            UNUserNotificationCenter.current().delegate = self

            // Request authorization from the user for push notification alerts.
            UNUserNotificationCenter.current().requestAuthorization(
             options: [.alert, .sound, .badge], completionHandler: {
                (_ granted: Bool, _ error: Error?) -> Void in
                if error == nil {
                    if granted == true {
                        // Logic if authorized
                    }
                } else {
                    print(error!.localizedDescription)
                }
            })

            UIApplication.shared.registerForRemoteNotifications()
        }

    }
    
    // SDK: REQUIRED IMPLEMENTATION
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        self.configureSDK()
        return true
    }

    
    // MobilePush SDK: REQUIRED IMPLEMENTATION
    func application(_ application: UIApplication,
      didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        SFMCSdk.mp.setDeviceToken(deviceToken)
    }

    // MobilePush SDK: REQUIRED IMPLEMENTATION
    func application(_ application: UIApplication,
     didFailToRegisterForRemoteNotificationsWithError error: Error) {
        print(error)
    }

    // MobilePush SDK: REQUIRED IMPLEMENTATION
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo:
     [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping
    (UIBackgroundFetchResult) -> Void) {
        SFMCSdk.mp.setNotificationUserInfo(userInfo)
        completionHandler(.newData)
    }
    
    // MobilePush SDK: REQUIRED IMPLEMENTATION
    @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response:
     UNNotificationResponse, withCompletionHandler completionHandler: @escaping () ->
     Void) {
        SFMCSdk.mp.setNotificationRequest(response.notification.request)
        completionHandler()
    }

    // MobilePush SDK: REQUIRED IMPLEMENTATION
    @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent
     notification: UNNotification, withCompletionHandler completionHandler: @escaping
      (UNNotificationPresentationOptions) -> Void) {
        completionHandler(.alert)
    }
}

恳请提供相关技术见解!

内容的提问来源于stack exchange,提问作者Michael Reinders

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 04:30:04