Categories
Uncategorized

SwiftUI Label with icon trailing

A quick snippet of code to show you how to create a SwiftUI Label() that has the icon at the end instead of in front.

// Make a struct that conforms to the LabelStyle protocol,
//and return a view that has the title and icon switched in a HStack
struct TrailingIconLabelStyle: LabelStyle {
    func makeBody(configuration: Configuration) -> some View {
        HStack {
            configuration.title
            configuration.icon
        }
    }
}

//Usage
Label("Lightning", systemImage: "bolt.fill").labelStyle(TrailingIconLabelStyle())

Categories
Uncategorized

navigationBarTitle Display Inline when sharing code between iOS and watchOS

I came across an issue when building a SwiftUI cross platform app recently. I did not want a large navigationBarTitle on iOS, I wanted it in the toolbar, but if I set displayMode to inline the watchOS version will not compile as it is not supported there. Basically the following code won’t work on watchOS.

NavigationView {
.navigationBarTitle("Title", displayMode: .inline)
}

I did not want to have to have a lot of platform specific code so a workaround I found is to declare navigationBarTitle without the displayMode set to inline and then add iOS #if code to add to the toolbar, then extend the View object and navigationBarTitle on iOS which will hide navigationBarTitle and hide the padding it creates at the top. Like below.

#if os(iOS)
 extension View {
     func navigationBarTitle(_ title: String) -> some View {
         self.navigationBarTitleDisplayMode(.inline)
     }
 }
 #endif 

// Then you can just do this anywhere for iOS and watchOS.
.navigationBarTitle("Title")

//and then in your view, to show a title in the toolbar for iOS  

#if os(iOS)
ToolbarItem(placement: .principal) {
    Text("Title")
}
#endif 
Categories
Uncategorized

How to get data from Woolworths Online

I get a lot of questions about how I built discountkit.com.au from people looking to build apps to use data from Woolworths Online. I don’t have any official API access to the Woolworths Online API, I basically just consume the endpoints that their public facing website does.

The Woolworths Online website is AngularJS based and has public facing json data that you can find the endpoints to using Chrome DevTools.

Basically head to a product or page you want to get data from such as Almond Kernals. Open DevTools, go to the ‘Network’ tab and click ‘XHR’ as shown in the image below. Then refresh the page.

If you then hover over the items in the Name column, you will see a URL for the endpoint of that data. Right click > Copy > Copy Link Address.

In this case right clicking ‘176111’ the product id gives me the following url: https://www.woolworths.com.au/api/v3/ui/schemaorg/product/176411

If you head to that URL now you can see JSON data which you can then use to easily get what data you need.

Hopefully this helps!