Created by 大刘 liuxing8807@126.com
这里是Apple官网的说明。
A representation of the state of your app at a moment in time.
NSUserActivity是Apple在iOS8之后推出来做Handoff的,它主要用于对app记录用户的操作,后来NSUserActivity主要用来支持:
创建NSUserActivity的流程:
NSUserActivity对象, 类型一般约定为reverse-DNS格式,比如com.myCompany.myApp, type需要在Info.plist中使用key为NSUserActivityTypes配置becomeCurrent()向系统注册支持Handoff
设置isEligibleForHandoff属性为true
支持SiriKit
当SiriKit需要开启app的时候,它会创建一个user activity对象,并且设置userActivityObj.interaction = INInteraction对象,具体可以参见SiriKit Programming Guide.
支持Search Results
在未打开任何app的时候,由上往下拖会启用Spotlight:

需要深一步了解Core Spotlight,可以参见:[图片上传失败...(image-55b42-1655180877531)]是官网对Core Spotlight的说明。
我们做一个简单示例,当在Spotlight中搜索One or two or three的时候启用我们的App, 并使用User activity做一些事情。
新建一个iOS 工程:
class RootViewController: UITableViewController {
private let items: Array<String> = ["One", "Two", "Three"]
private let activity: NSUserActivity = NSUserActivity(activityType: "daliu")
override func viewDidLoad() {
super.viewDidLoad()
// cellClass: AnyClass
self.tableView.register(UITableViewCell.classForCoder(), forCellReuseIdentifier: "cellID")
self.addActivity()
}
func addActivity() -> Void {
// title和keywords都可以在Spotlight搜索结果中出现
self.activity.title = "activity title here"
self.activity.keywords = Set(arrayLiteral: "One", "Two", "Three")
// 是否将用户活动转交到其他设备
// self.activity.isEligibleForHandoff = false
self.activity.isEligibleForSearch = true
// 每个控制器的user activity和搜索结果都是仅当应用曾经被打开过时而创建的
// _activity.eligibleForPublicIndexing = YES;
// 自动被加入到设备的搜索结果索引中
self.activity.becomeCurrent()
}
// MARK: - Table view data source
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.items.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellID", for: indexPath)
cell.textLabel?.text = self.items[indexPath.row]
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
switch indexPath.row {
case 0:
let vc = OneViewController()
self.navigationController?.pushViewController(vc, animated: true)
case 1:
let vc = TwoViewController()
self.navigationController?.pushViewController(vc, animated: true)
case 2:
let vc = ThreeViewController()
self.navigationController?.pushViewController(vc, animated: true)
default:
break
}
}
override func restoreUserActivityState(_ activity: NSUserActivity) {
if activity.title == "One" {
let vc = OneViewController()
self.navigationController?.pushViewController(vc, animated: true)
} else if let _ = activity.title?.elementsEqual("Two") {
let vc = TwoViewController()
self.navigationController?.pushViewController(vc, animated: true)
} else if activity.title?.caseInsensitiveCompare("Three") == ComparisonResult.orderedSame {
let vc = ThreeViewController()
self.navigationController?.pushViewController(vc, animated: true)
}
}
}
AppDelegate.m
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
self.window = UIWindow.init(frame: UIScreen.main.bounds)
let rootController = UIStoryboard.init(name: "RootViewController", bundle: nil).instantiateInitialViewController() as! RootViewController
let nivController = UINavigationController(rootViewController: rootController)
self.window?.rootViewController = nivController
self.window?.makeKeyAndVisible()
return true
}
// 监听在Spotlight中的点击
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
let niv: UINavigationController = self.window?.rootViewController as! UINavigationController
niv.topViewController?.restoreUserActivityState(userActivity)
return true
}
这样,当在Spotlight中搜索one时,就是显示我们的app, 点击进去就进入到了OneViewController.
我在使用我的应用程序中的mapItem实现NSUserActivity时遇到了一些问题。我想要的只是能够在应用程序切换器中使用“获取路线”建议,就像Foursquare所做的那样:我在我的viewDidLoad方法中使用以下代码,在显示我要注册的位置(酒吧)的ViewController中。NSUserActivity*activity=[[NSUserActivityalloc]initWithActivityType:@"co.pubmapper.ViewPub"];CLLocationCoordinate2Dcoords=CLLocationCoordinate2DMake(se
我已经在我们的应用程序中实现了Handoff,当应用程序在前台或后台运行时,它可以很好地用于Web到应用程序的切换,反之亦然。但是,如果应用未运行,那么如果用户从Web到应用切换启动应用,在launchOptions字典中,我得到UIApplicationLaunchOptionsUserActivityDictionaryKey,但缺少对事件的引用.看截图:如您所见,我只获得了NSUserActivity的ID。这是iOS9中的错误吗?有没有办法通过使用id来获取对事件的引用?编辑,这是代码,虽然我认为这不相关-(BOOL)application:(UIApplication*)ap
我有一个关于NSUserActivity的userInfo属性的快速问题。NSString*activity=@"com.test.activity";NSUserActivity*userActivity=[[NSUserActivityalloc]initWithActivityType:activity];userActivity.title=@"Test";userActivity.keywords=[NSSetsetWithArray:@[@"Test"];userActivity.userInfo=@{@"location":location};userActivity.e
我有一个关于NSUserActivity的userInfo属性的快速问题。NSString*activity=@"com.test.activity";NSUserActivity*userActivity=[[NSUserActivityalloc]initWithActivityType:activity];userActivity.title=@"Test";userActivity.keywords=[NSSetsetWithArray:@[@"Test"];userActivity.userInfo=@{@"location":location};userActivity.e
我正在尝试将我自己的事件添加到osx10.11上的Spotlight搜索结果中。但经过多次尝试,我无法通过关键字搜索或标题搜索进入Spotlight搜索结果。{self.userActivity=[[NSUserActivityalloc]initWithActivityType:@"ReverseDNSkeyword"];self.userActivity.title=@"Sometitle";self.userActivity.keywords=[NSSetsetWithArray:@[@"Somekeywords"]];self.userActivity.eligibleForS
我正在尝试将我自己的事件添加到osx10.11上的Spotlight搜索结果中。但经过多次尝试,我无法通过关键字搜索或标题搜索进入Spotlight搜索结果。{self.userActivity=[[NSUserActivityalloc]initWithActivityType:@"ReverseDNSkeyword"];self.userActivity.title=@"Sometitle";self.userActivity.keywords=[NSSetsetWithArray:@[@"Somekeywords"]];self.userActivity.eligibleForS
以下是我试图实现的代码,使应用程序事件和状态可搜索但无法在iOS搜索中显示NSUserActivity*userActivity=[[NSUserActivityalloc]initWithActivityType:@"com.mycompany.activity-type"];userActivity.title=@"Helloworldfrominappsearch";userActivity.keywords=[NSSetsetWithArray:@[@"Hello",@"Welcome",@"search"]];userActivity.userInfo=@{@"id":@"c
以下是我试图实现的代码,使应用程序事件和状态可搜索但无法在iOS搜索中显示NSUserActivity*userActivity=[[NSUserActivityalloc]initWithActivityType:@"com.mycompany.activity-type"];userActivity.title=@"Helloworldfrominappsearch";userActivity.keywords=[NSSetsetWithArray:@[@"Hello",@"Welcome",@"search"]];userActivity.userInfo=@{@"id":@"c
Createdby大刘liuxing8807@126.com这里是Apple官网的说明。Arepresentationofthestateofyourappatamomentintime.NSUserActivity是Apple在iOS8之后推出来做Handoff的,它主要用于对app记录用户的操作,后来NSUserActivity主要用来支持:HandoffSiriKitSpolightsearchresults创建NSUserActivity的流程:使用app支持的合适的type创建并初始化NSUserActivity对象,类型一般约定为reverse-DNS格式,比如com.myComp
Createdby大刘liuxing8807@126.com这里是Apple官网的说明。Arepresentationofthestateofyourappatamomentintime.NSUserActivity是Apple在iOS8之后推出来做Handoff的,它主要用于对app记录用户的操作,后来NSUserActivity主要用来支持:HandoffSiriKitSpolightsearchresults创建NSUserActivity的流程:使用app支持的合适的type创建并初始化NSUserActivity对象,类型一般约定为reverse-DNS格式,比如com.myComp