当应用程序未 运行 时,Spotlight 搜索不起作用
Spotlight search doesn't work when app not running
我想先告诉UIViewController
Spotlight打开了它。我试着在 NSNotificationCenter
之前完成。但是我尝试了几种方法,但当我将我的密钥设置为 "spotlightOpen" 时,它们并没有成功。当我使用像 UIApplicationDidFinishLaunchingNotification
这样的标准名称时,它对我有用。下面我写了几个我试过的方法
在
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
NSNotificationCenter.defaultCenter().postNotificationName("selectSong", object: nil)
return true
}
在第一个控制器中
NSNotificationCenter.defaultCenter().addObserverForName("selectSong", object: nil, queue: NSOperationQueue.mainQueue()) { (NSNotification) -> Void in
print("Song table is loaded")
}
我还是在第一个控制器中做到了。但它对我也不起作用。
NSNotificationCenter.defaultCenter().addObserver(self, selector: "selectedSong", name: "selectSong", object: nil)
func application(application: UIApplication, continueUserActivity userActivity: NSUserActivity, restorationHandler: ([AnyObject]?) -> Void) -> Bool {
print("continueUserActivity")
userDefault.setBool(true, forKey: "applicationDelegateOpen")
if userActivity.activityType == CSSearchableItemActionType {
print("CSSearchableItemActionType")
if let identifier = userActivity.userInfo?[CSSearchableItemActivityIdentifier] {
userDefault.setValue(identifier, forKey: "spotlightIdentifier")
userDefault.setBool(true, forKey: "spotlightBool")
print(identifier)
return true
}
}
return false
}
很可能在冷启动时您的视图控制器尚未初始化。您需要暂时保存该通知数据,直到调用 loadView
或 viewDidLoad
之类的东西。
在 UIApplicationDelegate
的 continueUserActivity
回调中,如果您使用 CoreSpotlight
API.如果您正在使用 NSUserActivity
API,您也可以在此委托回调中处理它们。
您使用您的歌曲 ID 创建了一个核心焦点项目:
let item = CSSearchableItem(uniqueIdentifier: songId, domainIdentifier: "com.mycompany.song", attributeSet: attributeSet)
// ... Save it and all that
您可以使用以下方式处理聚光灯启动:
func application(application: UIApplication, continueUserActivity userActivity: NSUserActivity, restorationHandler: ([AnyObject]?) -> Void) -> Bool {
if userActivity.activityType == CSSearchableItemActionType {
if let songId = userActivity.userInfo?[CSSearchableItemActivityIdentifier] as? String {
// Do notification with songId
// Or let view controllers know about selected songId
}
}
return true
}
我做到了。对我有用
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
self.tableView.reloadData()
self.tabBarController!.tabBar.hidden = false
fetchFilesFromFolder()
//
checkSpotlightResult()
}
func checkSpotlightResult() {
print("checkSpotlightResult")
// print(arrayForCheckSpot)
// userDefault.setValue(identifier, forKey: "spotlightIdentifier")
// userDefault.setBool(true, forKey: "spotlightBool")
// var boolCheckSpot: Bool!
// var identifierCheckSpot: String!
boolCheckSpot = userDefault.boolForKey("spotlightBool")
if boolCheckSpot != nil {
if boolCheckSpot == true {
identifierCheckSpot = userDefault.valueForKey("spotlightIdentifier") as! String
if arrayForCheckSpot.contains(identifierCheckSpot) {
// print("Array title contains \(identifierCheckSpot)")
let index = arrayForCheckSpot.indexOf(identifierCheckSpot)!
let myIndexPath = NSIndexPath(forRow: index, inSection: 0)
print(myIndexPath)
self.tableView.selectRowAtIndexPath(myIndexPath, animated: true, scrollPosition: .None)
self.performSegueWithIdentifier("listenMusic", sender: self)
userDefault.setBool(false, forKey: "spotlightBool")
}
}
}
}
我想先告诉UIViewController
Spotlight打开了它。我试着在 NSNotificationCenter
之前完成。但是我尝试了几种方法,但当我将我的密钥设置为 "spotlightOpen" 时,它们并没有成功。当我使用像 UIApplicationDidFinishLaunchingNotification
这样的标准名称时,它对我有用。下面我写了几个我试过的方法
在
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
NSNotificationCenter.defaultCenter().postNotificationName("selectSong", object: nil)
return true
}
在第一个控制器中
NSNotificationCenter.defaultCenter().addObserverForName("selectSong", object: nil, queue: NSOperationQueue.mainQueue()) { (NSNotification) -> Void in
print("Song table is loaded")
}
我还是在第一个控制器中做到了。但它对我也不起作用。
NSNotificationCenter.defaultCenter().addObserver(self, selector: "selectedSong", name: "selectSong", object: nil)
func application(application: UIApplication, continueUserActivity userActivity: NSUserActivity, restorationHandler: ([AnyObject]?) -> Void) -> Bool {
print("continueUserActivity")
userDefault.setBool(true, forKey: "applicationDelegateOpen")
if userActivity.activityType == CSSearchableItemActionType {
print("CSSearchableItemActionType")
if let identifier = userActivity.userInfo?[CSSearchableItemActivityIdentifier] {
userDefault.setValue(identifier, forKey: "spotlightIdentifier")
userDefault.setBool(true, forKey: "spotlightBool")
print(identifier)
return true
}
}
return false
}
很可能在冷启动时您的视图控制器尚未初始化。您需要暂时保存该通知数据,直到调用 loadView
或 viewDidLoad
之类的东西。
在 UIApplicationDelegate
的 continueUserActivity
回调中,如果您使用 CoreSpotlight
API.如果您正在使用 NSUserActivity
API,您也可以在此委托回调中处理它们。
您使用您的歌曲 ID 创建了一个核心焦点项目:
let item = CSSearchableItem(uniqueIdentifier: songId, domainIdentifier: "com.mycompany.song", attributeSet: attributeSet)
// ... Save it and all that
您可以使用以下方式处理聚光灯启动:
func application(application: UIApplication, continueUserActivity userActivity: NSUserActivity, restorationHandler: ([AnyObject]?) -> Void) -> Bool {
if userActivity.activityType == CSSearchableItemActionType {
if let songId = userActivity.userInfo?[CSSearchableItemActivityIdentifier] as? String {
// Do notification with songId
// Or let view controllers know about selected songId
}
}
return true
}
我做到了。对我有用
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
self.tableView.reloadData()
self.tabBarController!.tabBar.hidden = false
fetchFilesFromFolder()
//
checkSpotlightResult()
}
func checkSpotlightResult() {
print("checkSpotlightResult")
// print(arrayForCheckSpot)
// userDefault.setValue(identifier, forKey: "spotlightIdentifier")
// userDefault.setBool(true, forKey: "spotlightBool")
// var boolCheckSpot: Bool!
// var identifierCheckSpot: String!
boolCheckSpot = userDefault.boolForKey("spotlightBool")
if boolCheckSpot != nil {
if boolCheckSpot == true {
identifierCheckSpot = userDefault.valueForKey("spotlightIdentifier") as! String
if arrayForCheckSpot.contains(identifierCheckSpot) {
// print("Array title contains \(identifierCheckSpot)")
let index = arrayForCheckSpot.indexOf(identifierCheckSpot)!
let myIndexPath = NSIndexPath(forRow: index, inSection: 0)
print(myIndexPath)
self.tableView.selectRowAtIndexPath(myIndexPath, animated: true, scrollPosition: .None)
self.performSegueWithIdentifier("listenMusic", sender: self)
userDefault.setBool(false, forKey: "spotlightBool")
}
}
}
}