xcode MarketingCloudSDK在Beta版TestFlight中崩溃SwiftUI应用程序:“_dispatch_once”

s5a0g9ez  于 2023-06-07  发布在  Swift
关注(0)|答案(1)|浏览(177)

MarketingCloudSDK在Beta TestFlight中导致SwiftUI应用崩溃

我们已经通过SalesForce Marketing Cloud实施了推送通知,在与内部测试人员进行了一些beta测试后,SFMCSDK似乎导致我们的应用程序因“_dispatch_once”而间歇性崩溃。不确定这是我们实现SDK的方式的问题,还是SDK本身的错误,当我们在项目中打开崩溃时,我们只得到以下信息:
TestFlight metrics include # crashes
crash details from Xcode Organizer
Screenshot of crash in project
我们已经按照文档设置了SDK /推送通知,标准推送通知按预期工作。我们有一个问题,从一个终止的应用程序状态接收/阅读openDirect URL(在前台/后台工作),但我们删除了这些URL,希望他们导致崩溃,似乎他们没有。
我还发现了一篇文章,说在Xcode Build Settings中设置'Other Linker Flags'会导致'_dispatch_once'崩溃,但我们检查了,我们没有定义任何。
我们通过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)
    }
}

任何见解将是最赞赏!

5kgi1eie

5kgi1eie1#

这是因为在启用数据保护时从后台访问密钥链。在SDK init之前添加下面的代码,我们应该不会再看到崩溃了。SFMCsdk.setKeychainAccessErrorsAreFatal(errorsAreFatal:false)
看看我们的学习app:https://github.com/salesforce-marketingcloud/MarketingCloudSDK-iOS/tree/spm/examples/LearningApp来看看这个是如何实现的。
谢谢Prakashini

相关问题