-1

I am trying to make an iOS widget which shows the recent news posted on a Microsoft Sharepoint Site. I want to call GraphAPI every few hours, so that I can get new posts and refresh the widget.

However, since GraphAPI requires an access token, I want to know if writing my code within the Timeline Provider is even possible. If it is possible, please could you require me with some sample codes? If not, how can I make the app to work?

user2250152
  • 14,658
  • 4
  • 33
  • 57
Gura
  • 5
  • 2
  • Does [this](https://www.google.com/search?client=safari&rls=en&q=api+call+in+widgets+swift&ie=UTF-8&oe=UTF-8) help? – Timmy Aug 02 '23 at 06:44

1 Answers1

-1

well the thing is that you cannot directly make network requests or API calls from within an iOS widget's Timeline Provider. The Timeline Provider is responsible for providing the data to display in the widget's timeline and is limited in terms of the operations it can perform. It's designed to handle data retrieval and updates, but not for making network requests or API calls.

in your case, if you need to fetch data from the Microsoft Graph API (which requires an access token), you have to typically fetch the data from the API in your main app and then provide that data to the widget using App Groups or other data sharing mechanisms.

Here's an approach you can try:

  1. Main App:
  • Implement the logic to fetch data from the Microsoft Graph API using the the authentication you have to use and get the required data.
  • Store the fetched data in a shared storage location like as UserDefaults or a shared App Group container and sure to store the data in a format that can be easily read and processed by the widget.
  1. Widget Extension:
  • Create a widget extension for your app and configure a Timeline Provider.
  • In the Timeline Provider read the data from the shared storage location (UserDefaults or App Group container) and use that data to populate your widget's timeline.

here are the sample codes that might help you:

  1. Your Main App
// Fetch data from Microsoft Graph API and store it in UserDefaults or App Group container
func fetchAndStoreData() {
    // Fetch data using Microsoft Graph API and obtain the necessary access token
    
    // Store the fetched data in UserDefaults or App Group container
    UserDefaults.standard.set(fetchedData, forKey: "com.yourapp.fetchedData")
}

Your Widget Extension Timeline Provider:

import WidgetKit

struct Provider: TimelineProvider {
    func placeholder(in context: Context) -> SimpleEntry {
        SimpleEntry(date: Date(), newsTitle: "Placeholder Title")
    }

    func getSnapshot(in context: Context, completion: @escaping (SimpleEntry) -> ()) {
        // Retrieve data from UserDefaults or App Group container
        if let fetchedData = UserDefaults.standard.object(forKey: "com.yourapp.fetchedData") as? YourDataModel {
            // Use the data to create a snapshot entry for the widget
            let entry = SimpleEntry(date: Date(), newsTitle: fetchedData.title)
            completion(entry)
        }
    }

    func getTimeline(in context: Context, completion: @escaping (Timeline<SimpleEntry>) -> ()) {
        // Similar to getSnapshot, retrieve data and create timeline entries
        // You can create multiple entries for a timeline if needed
        
        let timeline = Timeline(entries: entries, policy: .atEnd)
        completion(timeline)
    }
}

but keep in mind that this is a simplified example and youll need to change it to your specific use case and data structures

Najdad
  • 1
  • 5