当应用未运行时,Spotlight搜索不起作用

时间:2015-10-09 15:01:06

标签: ios swift2 nsnotificationcenter nsnotification

我想首先告诉Spotlight打开它UIViewController。我试图通过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
}

3 个答案:

答案 0 :(得分:0)

在冷启动时,您的视图控制器很可能尚未初始化。您需要暂时保存该通知数据,直到调用loadViewviewDidLoad为止。

答案 1 :(得分:0)

continueUserActivity的{​​{1}}回调中,如果您使用UIApplicationDelegate API,则可以为聚光灯操作处理CSSearchableItemActionType类型的NSUserActivity。如果您使用的是CoreSpotlight API,则还可以处理此委托回调中的那些。

您使用歌曲ID创建核心聚光灯项目:

NSUserActivity

您可以使用以下方式处理聚光灯启动:

let item = CSSearchableItem(uniqueIdentifier: songId, domainIdentifier: "com.mycompany.song", attributeSet: attributeSet)
// ... Save it and all that

答案 2 :(得分:0)

我做到了。它对我有用

 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")
                        }
                    }
                }
            }
相关问题