【问题标题】:Check if app is launched/openend by a SharePlay session检查应用程序是否由 SharePlay 会话启动/打开
【发布时间】:2022-01-02 23:03:00
【问题描述】:

当用户在 Facetime 通话中并接受 SharePlay 请求时,我想推送特定的ViewController。所以我认为当用户点击“接受”时收到通知是一样的。

import UIKit
import UserNotifications
        
    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {
    
    
    
        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
            // Override point for customization after application launch.
            
            // to perform action when notification is tapped
            UNUserNotificationCenter.current().delegate = self
            
            registerForPushNotifications()
            
            return true
        }
    
     func registerForPushNotifications() {
               UNUserNotificationCenter.current()
                   .requestAuthorization(options: [.alert, .sound, .badge]) {
                       [weak self] granted, error in
                       
                       print("Permission granted: \(granted)")
                       guard granted else { return }
                       self?.getNotificationSettings()
               }
           }
           
           func getNotificationSettings() {
               UNUserNotificationCenter.current().getNotificationSettings { settings in
                   print("Notification settings: \(settings)")
                   guard settings.authorizationStatus == .authorized else { return }
                   DispatchQueue.main.async {
                       UIApplication.shared.registerForRemoteNotifications()
                   }
               }
           }
    
    extension AppDelegate : UNUserNotificationCenterDelegate {
        func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
            
            let application = UIApplication.shared
            
            if(application.applicationState == .active){
                print("user tapped the notification bar when the app is in foreground")
                
            }
            
            if(application.applicationState == .inactive)
            {
                print("user tapped the notification bar when the app is in background")
            }
            
            guard let rootViewController = (UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate)?.window?.rootViewController else {
                return
            } 
// Do some work 


            completionHandler()
        }
    }


   

但事实并非如此。日志中没有打印任何内容。是否有其他方法可以通过接受 SharePlay 请求来了解应用程序(在后台或终止)何时启动或打开?

【问题讨论】:

    标签: swift ios15


    【解决方案1】:

    因此,如果有人接受 SharePlay 请求,就会加入会话。之后,您可以切换到特定的 ViewController。例如,您可以在

    中调用此函数
    func sceneWillEnterForeground(_ scene: UIScene) {
    
        if #available(iOS 15, *) {
            let _ = CoordinationManager.shared
        }
    }
    

    CoordinationManager 可能看起来像这样

        class CoordinationManager {
            
            static let shared = CoordinationManager()
            
            private var subscriptions = Set<AnyCancellable>()
            
            // Published values that the player, and other UI items, observe
            @Published var groupSession: GroupSession<MovieWatchingActivity>?
            @Published var enquedMovie: Movies?
            
            private init() {
                
                Task {
                    // await new sessions to watch movies together
                    for await groupSession in MovieWatchingActivity.sessions() {
                        
                        //set the app's active group session
                        self.groupSession = groupSession
                        
                        //remove previous subscriptions
                        subscriptions.removeAll()
                        
                        //observe changes to the session state
                        groupSession.$state.sink { [weak self] state in
                            if case .invalidated = state {
                                // set the groupSession to nil to publish
                                // the invalidated session state
                                self?.groupSession = nil
                                self?.subscriptions.removeAll()
                            }
                        }.store(in: &subscriptions)
                        
                        // join the session to participate in playback coordination
                        groupSession.join()
                        
                        // navigate user to correct view controller to show videos
                        guard let windowScene = await UIApplication.shared.connectedScenes.first as? UIWindowScene,
                              let sceneDelegate = await windowScene.delegate as? SceneDelegate,
                              let navigationController = await sceneDelegate.window?.rootViewController as? UINavigationController else {
                                  return
                              }
                        DispatchQueue.main.async {
                            navigationController.pushViewController(GroupMovieController(), animated: true)
                        }
                        
                        // observe when the local user or a remote participant starts an activity
                        groupSession.$activity.sink { [weak self] activity in
                            // set the movie to enqueue it in the player
                            self?.enquedMovie = activity.movie
                        }.store(in: &subscriptions)
                    }
                }
            }
    ... 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-29
      • 1970-01-01
      • 2019-07-19
      相关资源
      最近更新 更多