Quantcast
Channel: Yudiz Solutions Ltd.
Viewing all articles
Browse latest Browse all 595

iOS Proactive Suggestions

$
0
0

Overview

Introduce new ways to increase user engagement with your app.System suggest your app to users at appropriate times.

You can provide information about what users do in your app, which helps the system promote your app in additional places, such as

  • Keyboard with QuickType suggestions,
  • Maps and CarPlay,
  • The app switcher,
  • Siri interactions,
  • The lock screen

There are some ways to increase user engagement, such as:

NSUserActivity

NSUserActivity is eyes of operating system, an NSUserActivity object provides a lightweight way to capture the state of your app and put it to use later.

iOS 10 and later in NSUserActivity class added mapItem attribute, that provide location information that can be used in other contexts.

You can use the new text-based address component properties in CSSearchableItemAttributeSet, such as thoroughfare and postalCode, to fully specify locations to which the user may want to go.

App Switcher Proactive Suggestion using handoff

You must need to use same icloud account in your devices and bluetooth of your device need to on

var continuedActivity: NSUserActivity?

// call this method when you want to suggest your app to another device 
   func createUserActivity(company:Company,index:Int) {
        userActivity = NSUserActivity(activityType: "Yudiz.ProactiveSuggetions.viewDetails")
        userActivity?.userInfo = ["name":company.name, "address":company.address,"link":company.url]
        userActivity?.title = "\(index)"
        userActivity?.needsSave = true
        userActivity?.becomeCurrent()
    }

// this method is called when another device opens app from app switcher suggestion 
    override func restoreUserActivityState(_ activity: NSUserActivity) {
        continuedActivity = activity
        super.restoreUserActivityState(activity)
    }

// this method is called when app is successfully updated useractivity 
    override func updateUserActivityState(_ activity: NSUserActivity) {

        super.updateUserActivityState(activity)
    }

// this method is called when app is opened from proactive suggestion user get suggestion when user unlock device.(left bottom of device)
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
        NSLog("Received a payload via handoff : %@",userActivity.title ?? "nil")
                return true
    }

Here Info.plist file add key for activityType

Location Suggestions

Provide map item to be promoted throughout the system, This will automatically populate the contentAttributeSet property with all available location information, including geo coordinates, text address components, phone numbers, etc.
Provide just enough information in the userInfo dictionary to be able to restore state

a. Stoplight Search

var arrCompany = [Company]()
    override func viewDidLoad() {
        super.viewDidLoad()

        arrCompany.append(
            Company(name:"Apple",
                    address:"Cupertino, California, U.S.",
                    url:"https://en.wikipedia.org/wiki/Apple_Inc",
                    logo:#imageLiteral(resourceName: "apple"))
        )

        arrCompany.append(
            Company(name:"Google",
                    address:"Googleplex, Mountain View, California, U.S",
                    url:"https://en.wikipedia.org/wiki/Google",
                    logo:#imageLiteral(resourceName: "google"))
        )

        arrCompany.append(
            Company(name:"Microsoft",
                    address:"Albuquerque, New Mexico, United States",
                    url:"https://en.wikipedia.org/wiki/Microsoft",
                    logo:#imageLiteral(resourceName: "microsoft"))
        )

        arrCompany.append(
            Company(name:"Samsung",
                    address:"Seocho District, Seoul, South Korea",
                    url:"https://en.wikipedia.org/wiki/Samsung",
                    logo:#imageLiteral(resourceName: "samsung"))
        )

        arrCompany.append(
            Company(name:"Amazon",
                    address:"Seattle, Washington, U.S",
                    url:"https://en.wikipedia.org/wiki/Amazon.com",
                    logo:#imageLiteral(resourceName: "amazon"))
        )

        arrCompany.append(
            Company(name:"Facebook",
                    address:"Menlo Park, California, U.S.",
                    url:"https://en.wikipedia.org/wiki/Facebook",
                    logo:#imageLiteral(resourceName: "facebook"))
        )

        arrCompany.append(
            Company(name:"Flipkart",
                    address:"Bangalore, Karnataka, India",
                    url:"https://en.wikipedia.org/wiki/Flipkart",
                    logo:#imageLiteral(resourceName: "flipkart"))
        )

        arrCompany.append(
            Company(name:"Yahoo",
                    address:"Sunnyvale, California, U.S.",
                    url:"https://en.wikipedia.org/wiki/Yahoo!",
                    logo:#imageLiteral(resourceName: "yahoo"))
        )

setupSearchableContent()

    }

func setupSearchableContent() {
        var searchableItems = [CSSearchableItem]()

        for record in arrCompany {

            let searchableItemAttributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeText as String)
            searchableItemAttributeSet.title = record.name
            searchableItemAttributeSet.thumbnailData = UIImagePNGRepresentation(record.logo)
            searchableItemAttributeSet.contentDescription = record.address
            searchableItemAttributeSet.keywords = [record.name,record.address]
            let searchableItem = CSSearchableItem(uniqueIdentifier: "com.yudiz.SpotIt.\(record.name)", domainIdentifier: "company", attributeSet: searchableItemAttributeSet)
            searchableItems.append(searchableItem)

        }

        // this method index your data to eligible for spotlight suggestion  
        CSSearchableIndex.default().indexSearchableItems(searchableItems) { (error) -> Void in
            if error != nil {
                print(error?.localizedDescription ?? "nil")
            }
        }

    }

Address component properties in CSSearchableItemAttributeSet. It’s also recommended that you supply a value for the namedLocation property, so that users can view the name of the location, and the phoneNumbers property, so that users can use Siri to initiate a call to the location

b. Location Suggestions and Keyboard with QuickType suggestions

func getLocation(company:Company) {
        let request = MKLocalSearchRequest()

        request.naturalLanguageQuery = company.name // Location name you want to search for given lat,long 
        request.region = MKCoordinateRegionMakeWithDistance(CLLocationCoordinate2DMake(23.0225, 72.5714), 1600, 1600)

        MKLocalSearch(request: request).start { (response, error) in
            guard error == nil else { return }
            guard let response = response else { return }
            guard let exampleMapItem = response.mapItems.first else { return }

            let activity = NSUserActivity(activityType: "Yudiz.ProactiveSuggetions.viewDetails")
            activity.title = exampleMapItem.name
            activity.mapItem = exampleMapItem
            self.userActivity = activity

            activity.isEligibleForHandoff = true
            activity.isEligibleForSearch = true
            activity.isEligibleForPublicIndexing = true

            // Set delegate
            activity.delegate = self
            self.userActivity?.becomeCurrent()

        }
    }

override func updateUserActivityState(_ activity: NSUserActivity) {
              var userInfo = [String: AnyObject]()
        userInfo["placemark"] = NSKeyedArchiver.archivedData(withRootObject: (activity.mapItem.placemark)) as AnyObject?
        if let url = activity.mapItem.url {
            userInfo["url"] = url as AnyObject?
        }
        if let phoneNumber = activity.mapItem.phoneNumber {
            userInfo["phoneNumber"] = phoneNumber as AnyObject? 
        }

        activity.userInfo = userInfo

        // Provide additional searchable attributes.
        activity.contentAttributeSet?.supportsNavigation = true
        activity.contentAttributeSet?.supportsPhoneCall = true
    }

If users view a location described on your app, the system can suggest the same location when users switch to Maps Application


Viewing all articles
Browse latest Browse all 595

Trending Articles