Change Ncwidgetdisplaymode Programmatically in iOS10 Widget

Change NCWidgetDisplayMode programmatically in IOS10 Widget

In iOS 10, Show More/Show Less button is automatically provided in the Today's Extension. So height of the widget is handled automatically through NCWidgetDisplayMode. You don't need to provide any explicit button for handling the widget's height.

override func viewDidLoad() {
super.viewDidLoad()

if #available(iOSApplicationExtension 10.0, *) {
self.extensionContext?.widgetLargestAvailableDisplayMode = .expanded
}
}

Implement NCWidgetProviding protocol's method:

@available(iOSApplicationExtension 10.0, *)
func widgetActiveDisplayModeDidChange(_ activeDisplayMode: NCWidgetDisplayMode, withMaximumSize maxSize: CGSize) {
if activeDisplayMode == .expanded {
preferredContentSize = CGSize(width: maxSize.width, height: 300)
} else {
preferredContentSize = maxSize
}
}

In, iOS 8 and iOS 9, you need to explicitly handle widget's height. In iOS 10, it is not required.

You can refer to https://github.com/pgpt10/Today-Widget on Today's Widget implementation in iOS 8, iOS 9 and iOS 10.

Today Widget Extension Height - iOS10

The height of the widget in iOS 10 is exactly 110 in compact mode. It can be set to whatever height you want in expanded mode, but in compact mode it will always be 110 and that can't be overwritten.

Swift - iOS10 - CoreData - FetchRequest in Today Widget always return 0 elements

You have defined a PersistentContainer subclass to override
defaultDirectoryURL, but your CoreDataStack is still using the build in NSPersistentContainer type for your lazy container.

This should do the trick:

public class CoreDataStack {

public static let shared = CoreDataStack()
public var errorHandler: (Error) -> Void = {_ in }

//#1
public lazy var persistentContainer: PersistentContainer = {
let container = PersistentContainer(name: CoreDataServiceConsts.modelName)
container.loadPersistentStores(completionHandler: { [weak self](storeDescription, error) in
if let error = error {
NSLog("CoreData error \(error), \(error._userInfo)")
self?.errorHandler(error)
}
})
return container
}()

Cheers
Michał

How to create widgets showing on home screen in iOS 10

Swift3 & Xcode8

For above you need to create an extension for your Main App

  • Which extension I need to create to achieve above one?

    Ans:Today Extension

  • How to create Extension?

    Xcode -> File -> New -> Target ->Today Extension

After Creating Extension, If you want to pass data from Main App to Extension then you need know about AppGroups

AppGroups is nothing but to share data between Main App to Extensions

How to achieve?

Just Simple /p>

Go to Xcode -> Capabilities -> AppGroups Enable -> Click + -> Add a New Container with format group.*

for Example: group.com.yourCompany.ProjectName

Go to Main App

Initialise defaults with Suite

var appGroupDefaults = UserDefaults.standard
appGroupDefaults = UserDefaults(suiteName:"group.com.yourCompany.ProjectName")!

After initialising defaults with Suite and Set the ArrayData Or String Data whatever you need to pass to Extension

appGroupDefaults.set(value: arrayDataToPasstoTodayExtension, forKey: "arrayDatatoDisplayInToday")

Then after Retrieve data in Extension /p>

var appGroupDefaults = UserDefaults.standard
appGroupDefaults = UserDefaults(suiteName:"group.com.yourCompany.ProjectName")!
let dataArray = appGroupDefaults.value(forKey: "arrayDatatoDisplayInToday")! as! NSMUtableArray
Print("Hurray i got the data from Main App to Extension")

In Extension

For ShowLess and ShowMore more Options in Widget[Today]

override func viewDidLoad() {
self.preferredContentSize = CGSize(width: 320, height: CGFloat(yourArrayValuesCount.count)*90 )

if #available(iOSApplicationExtension 10.0, *) {
self.extensionContext?.widgetLargestAvailableDisplayMode = .expanded
} else {
// Fallback on earlier versions
}
}
// For iOS 10
@available(iOS 10.0, *)
@available(iOSApplicationExtension 10.0, *)
func widgetActiveDisplayModeDidChange(_ activeDisplayMode: NCWidgetDisplayMode, withMaximumSize maxSize: CGSize) {
self.preferredContentSize = (activeDisplayMode == .expanded) ? CGSize(width: 320, height: CGFloat(yourArrayValuesCount.count)*90 ) : CGSize(width: maxSize.width, height: 90)
}


Related Topics



Leave a reply



Submit