草庐IT

NSUserActivity

大刘 2023-03-28 原文

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主要用来支持:

  • Handoff
  • SiriKit
  • Spolight search results

创建NSUserActivity的流程:

  1. 使用app支持的合适的type创建并初始化NSUserActivity对象, 类型一般约定为reverse-DNS格式,比如com.myCompany.myApp, type需要在Info.plist中使用key为NSUserActivityTypes配置
  2. 设置title
  3. 为对象设置合适的属性以让它支持不同的业务 isEligibleForHandoffisEligibleForSearchisEligibleForPublicIndexing
  4. 配置其他属性
  5. 对于配置了search或public indexing的对象,配置 contentAttributeSet, keywords, or webpageURL
  6. 调用becomeCurrent()向系统注册

支持Handoff
设置isEligibleForHandoff属性为true

支持SiriKit
当SiriKit需要开启app的时候,它会创建一个user activity对象,并且设置userActivityObj.interaction = INInteraction对象,具体可以参见SiriKit Programming Guide.

支持Search Results
在未打开任何app的时候,由上往下拖会启用Spotlight:

11.jpg

需要深一步了解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.

有关NSUserActivity的更多相关文章

  1. ios - 指示无效的 NSUserActivity - 2

    我在使用我的应用程序中的mapItem实现NSUserActivity时遇到了一些问题。我想要的只是能够在应用程序切换器中使用“获取路线”建议,就像Foursquare所做的那样:我在我的viewDidLoad方法中使用以下代码,在显示我要注册的位置(酒吧)的ViewController中。NSUserActivity*activity=[[NSUserActivityalloc]initWithActivityType:@"co.pubmapper.ViewPub"];CLLocationCoordinate2Dcoords=CLLocationCoordinate2DMake(se

  2. ios Handoff 在 launchOptions 中缺少 NSUserActivity? - 2

    我已经在我们的应用程序中实现了Handoff,当应用程序在前台或后台运行时,它可以很好地用于Web到应用程序的切换,反之亦然。但是,如果应用未运行,那么如果用户从Web到应用切换启动应用,在launchOptions字典中,我得到UIApplicationLaunchOptionsUserActivityDictionaryKey,但缺少对事件的引用.看截图:如您所见,我只获得了NSUserActivity的ID。这是iOS9中的错误吗?有没有办法通过使用id来获取对事件的引用?编辑,这是代码,虽然我认为这不相关-(BOOL)application:(UIApplication*)ap

  3. iOS 9 - NSUserActivity 用户信息属性显示为空 - 2

    我有一个关于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

  4. iOS 9 - NSUserActivity 用户信息属性显示为空 - 2

    我有一个关于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

  5. ios - NSUserActivity 没有在 osx 上建立索引 - 2

    我正在尝试将我自己的事件添加到osx10.11上的Spotlight搜索结果中。但经过多次尝试,我无法通过关键字搜索或标题搜索进入Spotlight搜索结果。{self.userActivity=[[NSUserActivityalloc]initWithActivityType:@"ReverseDNSkeyword"];self.userActivity.title=@"Sometitle";self.userActivity.keywords=[NSSetsetWithArray:@[@"Somekeywords"]];self.userActivity.eligibleForS

  6. ios - NSUserActivity 没有在 osx 上建立索引 - 2

    我正在尝试将我自己的事件添加到osx10.11上的Spotlight搜索结果中。但经过多次尝试,我无法通过关键字搜索或标题搜索进入Spotlight搜索结果。{self.userActivity=[[NSUserActivityalloc]initWithActivityType:@"ReverseDNSkeyword"];self.userActivity.title=@"Sometitle";self.userActivity.keywords=[NSSetsetWithArray:@[@"Somekeywords"]];self.userActivity.eligibleForS

  7. ios - 使用 NSUserActivity 使应用事件和状态可搜索 - 2

    以下是我试图实现的代码,使应用程序事件和状态可搜索但无法在iOS搜索中显示NSUserActivity*userActivity=[[NSUserActivityalloc]initWithActivityType:@"com.mycompany.activity-type"];userActivity.title=@"Helloworldfrominappsearch";userActivity.keywords=[NSSetsetWithArray:@[@"Hello",@"Welcome",@"search"]];userActivity.userInfo=@{@"id":@"c

  8. ios - 使用 NSUserActivity 使应用事件和状态可搜索 - 2

    以下是我试图实现的代码,使应用程序事件和状态可搜索但无法在iOS搜索中显示NSUserActivity*userActivity=[[NSUserActivityalloc]initWithActivityType:@"com.mycompany.activity-type"];userActivity.title=@"Helloworldfrominappsearch";userActivity.keywords=[NSSetsetWithArray:@[@"Hello",@"Welcome",@"search"]];userActivity.userInfo=@{@"id":@"c

  9. NSUserActivity - 2

    Createdby大刘liuxing8807@126.com这里是Apple官网的说明。Arepresentationofthestateofyourappatamomentintime.NSUserActivity是Apple在iOS8之后推出来做Handoff的,它主要用于对app记录用户的操作,后来NSUserActivity主要用来支持:HandoffSiriKitSpolightsearchresults创建NSUserActivity的流程:使用app支持的合适的type创建并初始化NSUserActivity对象,类型一般约定为reverse-DNS格式,比如com.myComp

  10. NSUserActivity - 2

    Createdby大刘liuxing8807@126.com这里是Apple官网的说明。Arepresentationofthestateofyourappatamomentintime.NSUserActivity是Apple在iOS8之后推出来做Handoff的,它主要用于对app记录用户的操作,后来NSUserActivity主要用来支持:HandoffSiriKitSpolightsearchresults创建NSUserActivity的流程:使用app支持的合适的type创建并初始化NSUserActivity对象,类型一般约定为reverse-DNS格式,比如com.myComp

随机推荐