Core Spotlight , NSUserActivity with supports Call

To add a NSUseActivity with a call contact feature , is quite useful to have a better user experience for your app. even when it's not active.

In case you need to refer to some basic's please refer to my previous page.

With this blog today we will see how to use , NSUserActivity a supportCall feature.

For example in your app. you have ability to add local mechanic where you had performed  last service , next appointment so on and so forth.

The code is quite simple and straight forward nothing much fancy here.

  func addLocalMechanicContactActivity(){  
     let localContact = NSUserActivity(activityType: "yourDomain.contact")  
     localContact.title = "Local Mechanic Contact"  
     localContact.userInfo = ["groupId":"20"]  
     localContact.isEligibleForSearch = true  
     localContact.becomeCurrent()  
     localContact.contentAttributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeContact as String)  
     localContact.contentAttributeSet?.contactKeywords = ["Mechanic","Local mechanic"]  
     localContact.contentAttributeSet?.phoneNumbers = ["999-999-9999"]  
     localContact.contentAttributeSet?.supportsPhoneCall = NSNumber(integerLiteral: 1)  
     localContact.contentAttributeSet?.authorNames = ["Ron"]  
     self.localContactUserContact = localContact  
   }  

1) Create a NSUserActivity with type , for example

     let localContact = NSUserActivity(activityType: "yourDomain.contact")  

2) Assign some required attributes.
Assign some title as it is recommended for all user activity object.
isEligibileForSearch is required to enable it for on-device indexing.
becomeCurrent is required for marking the activity as currently in use by current user.

localContact.title = "Local Mechanic Contact"  
     localContact.userInfo = ["groupId":"20"]  
     localContact.isEligibleForSearch = true  
     localContact.becomeCurrent()  

3) Create a Searchable Item of Type Contact.
Note: Here we will need to import CoreServices framework to use KUTTTypeContact.

     localContact.contentAttributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeContact as String)  
     localContact.contentAttributeSet?.contactKeywords = ["Mechanic","Local mechanic"]  
     localContact.contentAttributeSet?.phoneNumbers = ["999-999-9999"]  
     localContact.contentAttributeSet?.supportsPhoneCall = NSNumber(integerLiteral: 1)  
     localContact.contentAttributeSet?.authorNames = ["Ron"]  

contactKeyWords is optional but it's better to have to increasing the relevancy of the data.
phoneNumbers are required as we are enabling the activity which supports phone call.
supportPhoneCall , set it to 1, to enable the feature.

The final thing is make your contactActivity object strong by creating a local strong reference , declare just below in your UIViewController.


 var localContactUserContact:NSUserActivity?  

Now, the question is it will work ?
Yes , it will on device it should work as it Call feature is only supported on device.
On simulator you might not be able to see it just like below.



Comments

Popular posts from this blog

hitTest on iOS to identify the view being hit

CMTimeMakeWithSeconds explained

Custom Editing Control for UITableViewCell , like iPhone Mail Appliation