返回列表

# 259-Targeting Content with Multiple Windows

# Activate scene

当外部事件的 target content Identifier 对应上 scene activationConditions 时,激活对应的 scene。

class UIScene {
  /// The conditions that define when UIKit activates the scene object.
  var activationConditions: UISceneActivationConditions { get set }
}

class UISceneActivationConditions {
  /// The set of conditions for which UIKit chooses to activate this scene over others.
  var prefersToActivateForTargetContentIdentifierPredicate: NSPredicate { get set }
  
  /// Conditions for which UIKit can activate the scene if a better alternative doesn't exist.
  var canActivateForTargetContentIdentifierPredicate: NSPredicate { get set }
}
1
2
3
4
5
6
7
8
9
10
11
12

Notification

通过 APNS “target-content-id” key

UIApplicationShortcutItem 和 NSUserActivity

/// The object that determines which scene handles the quick action.
var targetContentIdentifier: Any? { get }
1
2

# Restrictions

NSPredicate has three restrictions:

  1. Block-based predicates aren't allowed
  2. Regular expression-based predicates aren't allowed, recommend using like
  3. The only keypath you can reference is “self”

# Demo

# Clown Town

// self is target content identifier?
let crustyPredicate = NSPredicate(format: "self == 'http://clowntown.example.com/clown/
Crusty'")
let conditions = scene.activationConditions
 
conditions.prefersToActivateForTargetContentIdentifierPredicate = crustyPredicate
conditions.canActivateForTargetContentIdentifierPredicate = crustyPredicate
1
2
3
4
5
6
7

# Safari

let canPredicate = NSPredicate(value: true)
let subpredicates = []
for tab in openTabs {
  subpredicates.append(NSPredicate(format: "self == %@", tab.targetContentIdentifier))
}
let prefersPredicate = NSCompoundPredicate(orPredicateWithSubpredicates: subpredicates)
1
2
3
4
5
6