iphone - How to create widgets showing on home screen in iOS 10 -
i need create custom widgets on home screen in iphone. have attach sample image. please give me idea extension helpful in creating this.
swift3 & xcode8
for above need create extension main app
which extension need create achieve above one?
ans:today extension
how create extension?
xcode -> file -> new -> target ->today extension
after creating extension, if want pass data main app extension need know appgroups
appgroups nothing share data between main app extensions
how achieve?
just simple 👇
go xcode -> capabilities -> appgroups enable -> click + -> add new container format group.*
for example: group.com.yourcompany.projectname
go main app
initialise defaults suite
var appgroupdefaults = userdefaults.standard appgroupdefaults = userdefaults(suitename:"group.com.yourcompany.projectname")!
after initialising defaults suite , set arraydata or string data whatever need pass extension
appgroupdefaults.set(value: arraydatatopasstotodayextension, forkey: "arraydatatodisplayintoday")
then after retrieve data in extension 👇
var appgroupdefaults = userdefaults.standard actiongroupdefaults = userdefaults(suitename:"group.com.yourcompany.projectname")! let dataarray = appgroupdefaults.value(forkey: "arraydatatodisplayintoday")! as! nsmutablearray print("hurray got data main app extension")
in extension
for showless , 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 } } // 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) }
Comments
Post a Comment