I want to tell first UIViewController
that Spotlight opened it. I try to make it by NSNotificationCenter
. But I tried several methods and they don't it when I make my key like "spotlightOpen". When I use standard name like UIApplicationDidFinishLaunchingNotification
it works for me. Below I wrote several methods which I tried
In
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
NSNotificationCenter.defaultCenter().postNotificationName("selectSong", object: nil)
return true
}
In First controller
NSNotificationCenter.defaultCenter().addObserverForName("selectSong", object: nil, queue: NSOperationQueue.mainQueue()) { (NSNotification) -> Void in
print("Song table is loaded")
}
Still I made it in the first controller. But it too didn't work for me.
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
}
I made it. It works for me
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")
}
}
}
}