{"id":18839769,"url":"https://github.com/maximbilan/ios-today-extension-simple-tutorial","last_synced_at":"2025-04-14T07:01:00.681Z","repository":{"id":76126052,"uuid":"44326863","full_name":"maximbilan/iOS-Today-Extension-Simple-Tutorial","owner":"maximbilan","description":"iOS Today Extension Simple Tutorial","archived":false,"fork":false,"pushed_at":"2018-09-22T08:53:36.000Z","size":583,"stargazers_count":25,"open_issues_count":1,"forks_count":8,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-27T20:44:25.176Z","etag":null,"topics":["ios","swift","today-extension","today-widget","tutorial","widget"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/maximbilan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-10-15T15:26:19.000Z","updated_at":"2023-11-27T09:10:30.000Z","dependencies_parsed_at":"2023-02-25T05:30:42.276Z","dependency_job_id":null,"html_url":"https://github.com/maximbilan/iOS-Today-Extension-Simple-Tutorial","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maximbilan%2FiOS-Today-Extension-Simple-Tutorial","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maximbilan%2FiOS-Today-Extension-Simple-Tutorial/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maximbilan%2FiOS-Today-Extension-Simple-Tutorial/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maximbilan%2FiOS-Today-Extension-Simple-Tutorial/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/maximbilan","download_url":"https://codeload.github.com/maximbilan/iOS-Today-Extension-Simple-Tutorial/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248837285,"owners_count":21169374,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ios","swift","today-extension","today-widget","tutorial","widget"],"created_at":"2024-11-08T02:44:00.750Z","updated_at":"2025-04-14T07:01:00.606Z","avatar_url":"https://github.com/maximbilan.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# iOS Today Extension Simple Tutorial\n\nI would like to tell a little bit about how to create a simple Today Widget for iOS 8 and higher. We can find a lot of application in the store with widgets, really different widgets, such as weather, currencies, quick buttons for tracking something, location, etc. Example from my application, which displays currencies:\n\n![alt tag](https://raw.github.com/maximbilan/iOS-Today-Extension-Simple-Tutorial/master/screenshots/1.png)\n\nIt’s not separated application, which you can submit to the store, it’s extension of your application, and you can’t submit only the widget.\n\nLet’s start. First of all, add a new target to your project:\n\n![alt tag](https://raw.github.com/maximbilan/iOS-Today-Extension-Simple-Tutorial/master/screenshots/2.png)\n\nChoose options:\n\n![alt tag](https://raw.github.com/maximbilan/iOS-Today-Extension-Simple-Tutorial/master/screenshots/3.png)\n\nAnd create a scheme:\n\n![alt tag](https://raw.github.com/maximbilan/iOS-Today-Extension-Simple-Tutorial/master/screenshots/4.png)\n\nNow you have _MainInterface.storyboard_ and _TodayViewController.swift_. The main controller implements _NCWidgetProviding_ protocol. In this protocol we have two methods:\n\n```swift\n// If implemented, the system will call at opportune times for the widget to update its state, both when the Notification Center is visible as well as in the background.\n// An implementation is required to enable background updates.\n// It’s expected that the widget will perform the work to update asynchronously and off the main thread as much as possible.\n// Widgets should call the argument block when the work is complete, passing the appropriate ‘NCUpdateResult’.\n// Widgets should NOT block returning from ‘viewWillAppear:’ on the results of this operation.\n// Instead, widgets should load cached state in ‘viewWillAppear:’ in order to match the state of the view from the last ‘viewWillDisappear:’, then transition smoothly to the new data when it arrives.\n@available(iOS 8.0, *)\noptional public func widgetPerformUpdateWithCompletionHandler(completionHandler: (NCUpdateResult) -\u003e Void)\n\n// Widgets wishing to customize the default margin insets can return their preferred values.\n// Widgets that choose not to implement this method will receive the default margin insets.\noptional public func widgetMarginInsetsForProposedMarginInsets(defaultMarginInsets: UIEdgeInsets) -\u003e UIEdgeInsets\n```\n\nIn the first method you need to fetch a new data if you have, and you need to call a completion handler with three possible values: _NewData_, _NoData_, _Failed_. And widget will be know, will update UI or use old snapshot.\n\nIn the second method Apple gives a chance to change margins. By default Today Widget has left margin, you can see in default Apple applications, such as Calendar, Stocks.\n\n![alt tag](https://raw.github.com/maximbilan/iOS-Today-Extension-Simple-Tutorial/master/screenshots/5.png)\n\nFor example:\n\n```swift\nfunc widgetMarginInsetsForProposedMarginInsets(defaultMarginInsets: UIEdgeInsets) -\u003e (UIEdgeInsets) {\n    return UIEdgeInsetsZero\n}\n```\n\nLet’s create a simple extension. Please, add the _TableView_ to interface and set up an outlet.\n\n![alt tag](https://raw.github.com/maximbilan/iOS-Today-Extension-Simple-Tutorial/master/screenshots/6.png)\n\nFor the static height of the view you need to set up _preferredContentSize_:\n\n```swift\nself.preferredContentSize.height = 200\n```\n\nImplementation of loading data, for example from _plist_:\n\n```swift\nfunc loadData() {\n    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {\n        \n        self.data.removeAll()\n        \n        if let path = NSBundle.mainBundle().pathForResource(“Data”, ofType: “plist”) {\n            if let array = NSArray(contentsOfFile: path) {\n                for item in array {\n                    self.data.append(item as! NSDictionary)\n                }\n            }\n        }\n\n        dispatch_async(dispatch_get_main_queue()) {\n            self.tableView.reloadData()\n        }\n    }\n}\n```\n\nAnd of course the simple implementation of _UITableView_:\n\n```swift\nfunc tableView(tableView: UITableView, numberOfRowsInSection section: Int) -\u003e Int {\n    return data.count\n}\n\nfunc tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -\u003e UITableViewCell {\n    let cell = tableView.dequeueReusableCellWithIdentifier(\"tableViewCellIdentifier\", forIndexPath: indexPath)\n\n    let item = data[indexPath.row]\n    cell.textLabel?.text = item[\"title\"] as? String\n    cell.textLabel?.textColor = UIColor.whiteColor()\n\n    return cell\n}\n```\n\nAnd finally we get the result:\n\n![alt tag](https://raw.github.com/maximbilan/iOS-Today-Extension-Simple-Tutorial/master/screenshots/7.png)\n\nFull code you can find in this repository. Happy coding!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaximbilan%2Fios-today-extension-simple-tutorial","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmaximbilan%2Fios-today-extension-simple-tutorial","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaximbilan%2Fios-today-extension-simple-tutorial/lists"}