2015-10-09 48 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

相關問題