首页 > 解决方案 > SKCloudServiceController.requestAuthorization 在授权时卡住

问题描述

我指的是以下SKCloudServiceController.requestAuthorization方法。一旦状态被授权,我想更新@State var showStart = false以便可以将视图推送到下一个。

  if (showStart) {
        NavigationLink(destination: Main(), isActive: $showStart) {
            EmptyView()
        }.hidden()
    }
    SKCloudServiceController.requestAuthorization { (status) in
        if status == .authorized {
            print(AppleMusicAPI().fetchStorefrontID())
            showStart = true
        }
    }

但在此运行并授权状态后,应用程序冻结并且不会更改 showStart。

标签: objective-cswiftxcodecontrollerapple-musickit

解决方案


通过实现以下功能来修复。它允许SKCloudServiceController.requestAuthorization完成,然后在完成后设置showStart为 true。

func requestAccess(_ completion: @escaping(Bool) -> Void) {
    SKCloudServiceController.requestAuthorization { (status) in
        switch status {
        case .authorized:
            completion(true)
        case .denied, .notDetermined, .restricted:
            completion(false)
        @unknown default:
            completion(false)
        }
    }
}
requestAccess { (true) in
    showStart = true
}

推荐阅读