2015-10-09 52 views
0

我想先告诉Spotlight打开它,然后第一个UIViewController。我试图通过NSNotificationCenter。但是我尝试了几种方法,但是当我使我的密钥像“spotlightOpen”那样时,他们不会。当我使用标准名称如UIApplicationDidFinishLaunchingNotification它适用于我。下面我写了几种方法,我试过当应用程序未运行时,Spotlight搜索不起作用

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 
} 

回答

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") 
         } 
        } 
       } 
      } 
0

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

+0

我仍然尝试当第一个UIViewController加载我通知通知第二个UIViewController,但我得到了同样的结果。 – Alexander

0

continueUserActivity回调UIApplicationDelegate,您可以处理NSUserActivity CSSearchableItemActionType类型的聚光灯行动,如果您使用的是CoreSpotlight API。如果您使用的是NSUserActivity API,那么您也可以在此代理回调中处理这些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 
} 
+0

是的,我做到了。但是,当应用程序不启动时,我想打开特定的歌曲,但它不起作用。它只是打开应用程序。 – Alexander

+0

当应用程序处于后台时,我可以从聚光灯下打开特定文件,但当应用程序未启动时,此方法只会打开应用程序。 – Alexander

+0

它的作品,但只有应用程序在后台。 – Alexander

相关问题