How to create widget or Today extension in iOS Swift

Solution:

Step 1:  Select your project then goto Capabilities in that below select app groups and On it.`



Step 2: Click the plus icon.It will show one text box.Then enter group.com.companyname.appname.

Step 3:After that select the check box to enable

Step 4:Goto your iOS app developer account and then enable the app groups in your app in the app dependency section.And then regenerate the certificates and download it and add to your keychain access.Also enter the groups name will be like group.com.companyname.appname.

Step 5: Goto editor in the Xcode menu then select Editor->Add Target->search today extension and add it to your project.



Step 6: It will generate a folder like whichever you want.Expand that folder.It will had a Viewcontroller.

Step 7:  In widget class add the extension like below.


class TodayViewController: UIViewController, NCWidgetProviding{

}

Step 8:  Add the two delegate methods like below. widgetActiveDisplayModeDidChange was used for expand the widget. widgetPerformUpdate was used for handle the app updates in the widget


    func widgetActiveDisplayModeDidChange(_ activeDisplayMode: NCWidgetDisplayMode, withMaximumSize maxSize: CGSize) {

}

    func widgetPerformUpdate(completionHandler: (@escaping (NCUpdateResult) -> Void)) {

}

Step 9: When the user clicks on the widget it will want to navigate to the app.So you can pass the data also inside that to app.
            let url = URL(string:"TestWidget://\(str1)")
            self.extensionContext?.open(url!, completionHandler: { (success) in
                if (!success) {
                    print("error: failed to open app from Today Extension")}
            })

Step 10: In your Appdelegate you have to handle the widget clicked data in the below method.

    func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool
    {
        if url.scheme == "TestWidget"
        {

}
}

Step 11: Also handle the persistant container in the app using the class Corestack.You have to create the corestack class and handle it.


    @available(iOS 10.0, *)
    static var persistentContainer: NSPersistentContainer = {
        
        var container = NSPersistentContainer(name: "AppName")
        
        let dir = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.companuyname.appname")
        
        let url = dir?.appendingPathComponent("appname.sqlite")
        
        let persistentStoreDescription = NSPersistentStoreDescription(url: url!)
        
        // Configure Persistent Store Description
        persistentStoreDescription.type = NSSQLiteStoreType
        persistentStoreDescription.shouldMigrateStoreAutomatically = true
        persistentStoreDescription.shouldInferMappingModelAutomatically = true
        
        container.persistentStoreCoordinator.addPersistentStore(with: persistentStoreDescription, completionHandler: { (persistentStoreDescription, error) in
            if let error = error {
                print("Unable to Add Persistent Store")
                print("\(error.localizedDescription)")
            }
        })
        
        
        var failureReason = "There was an error creating or loading the application's saved data."
        return container
    }()
    
    
    static var applicationDocumentsDirectory: URL = {
        let urls = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.companyname.appname")
        return urls!
    }()
    
    static var managedObjectModel: NSManagedObjectModel = {
        // The managed object model for the application. This property is not optional. It is a fatal error for the application not to be able to find and load its model.
        let modelURL = Bundle.main.url(forResource: "appname", withExtension: "momd")!
        return NSManagedObjectModel(contentsOf: modelURL)!
    }()
    
    static var persistentStoreCoordinator: NSPersistentStoreCoordinator = {
        // The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it. This property is optional since there are legitimate error conditions that could cause the creation of the store to fail.
        // Create the coordinator and store
        let coordinator = NSPersistentStoreCoordinator(managedObjectModel: CoreStack.managedObjectModel)
        let url = CoreStack.applicationDocumentsDirectory.appendingPathComponent("appname.sqlite")
        var failureReason = "There was an error creating or loading the application's saved data."
        let storeOptions = [NSPersistentStoreUbiquitousContentNameKey: "appnameStore"]
        do {
            try coordinator.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: url, options: storeOptions)
        } catch {
            // Report any error we got.
            var dict = [String: AnyObject]()
            dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data" as AnyObject?
            dict[NSLocalizedFailureReasonErrorKey] = failureReason as AnyObject?
            
            dict[NSUnderlyingErrorKey] = error as NSError
            let wrappedError = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
            // Replace this with code to handle the error appropriately.
            NSLog("Unresolved error \(wrappedError), \(wrappedError.userInfo)")
            abort()
        }
        
        return coordinator
    }()
    
    static var managedObjectContext: NSManagedObjectContext = {
        let coordinator = CoreStack.persistentStoreCoordinator
        var managedObjectContext = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
        managedObjectContext.persistentStoreCoordinator = coordinator
        return managedObjectContext
    }()
    
    // MARK: - Core Data Saving support
    
    func saveContext () {
        if CoreStack.managedObjectContext.hasChanges {
            do {
                try CoreStack.managedObjectContext.save()
            } catch {
                // Replace this implementation with code to handle the error appropriately.
                // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
                let nserror = error as NSError
                NSLog("Unresolved error \(nserror), \(nserror.userInfo)")
                abort()
            }
        }

    }

Step 11: Your widget is ready go ahead and enjoy...


Comments

Popular posts from this blog

Invalid bundle error while upload the app to the app Store

Saved Image in document directory and save path in coredata not able to fetch the image file iOS swift