0

I need to create custom widgets on home screen in iPhone. I have attach the sample image. Please give me any idea which extension will be helpful in creating this.enter image description here

Kashif Jilani
  • 1,207
  • 3
  • 22
  • 49

3 Answers3

3

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

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

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)
}
Anjali jariwala
  • 410
  • 5
  • 15
Sanju
  • 1,148
  • 11
  • 26
0

Today Extension. Apple programming guide

0

Yes you can use the Today extension to show the Widget. From iOS 10 If you enable 3D shortcut items then Today extension (Widget) will show under the 3D shortcut items. So you can see Widget in 2 places. 1. Widget screen 2. Under 3D shortcut items. (If you enable 3D shortcut items)

Yogesh Mv
  • 1,041
  • 1
  • 6
  • 12