Closed huytower closed 3 years ago
You need to call:
HomeWidget.updateWidget
with
iOSName: 'nct_home_widget'
As described in the README the Name for the iOS Widget needs to be equal to that specified in kind in the Widget implementation on iOS
I already changed as you details, but it does not work (already uninstalled old app).
What I have missed?
Future<void> updateWidget() async {
print('updateWidget() ');
try {
return HomeWidget.updateWidget(
name: 'HomeWidgetExampleProvider',
androidName: 'HomeWidgetExampleProvider',
iOSName: 'nct_home_widget'
);
// iOSName: 'HomeWidgetExample');
} on PlatformException catch (exception) {
debugPrint('Error Updating Widget. $exception');
}
}
This part in the swift file: private let widgetGroupId = "nct_home_widget"
Should be your appgroup and not the string that you provide in the updateWidget. So it must starts with: group.something People usally make something like: group.com.inovatso.picPics.Widget (group + your reverse bundle id + something)
Also did you add the app group to the main target as well as to the plugin?
Hello,
I tried as your recommend, Rebuilt but it did not work.
Please see these images to correct me.
Thanks,
Code
*.entitlements
App Group
Could you try to solve the issues regarding the provisioning profile pointed out by Xcode?
Facing the same issue.
Double checked all the setup details and done everything as per docs.
here is the code sample
1. Future<void> _saveHomeWidget(title01, mesaage01) async {
2. try {
3. print('Save value Started');
4. return Future.wait([
5. HomeWidget.saveWidgetData<String>('title', title01),
6. HomeWidget.saveWidgetData<String>('message', mesaage01),
7. ]).then((value) => {
8. print('SAVED Value'),
9. print(value)
10. });
11. } on PlatformException catch (exception) {
12. debugPrint('Error Sending Data. $exception');
13. }
14. }
15.
16. Future<void> _updateWidget() async {
17. try {
18. print('update value Started');
19. return HomeWidget.updateWidget(
20. name: 'HomeWidgetForSys',
21. androidName: 'HomeWidgetForSysAndroid',
22. iOSName: 'HomeWidgetForSys')
23. .catchError((error) {
24. print('error');
25. }).then((value) => {
26. print('UPDATED Value'),
27. print(value)
28. });
29. } on PlatformException catch (exception) {
30. debugPrint('Error Updating Widget. $exception');
31. }
32. }
33.
34. Future<void> _retrieveData() async {
35. try {
36. print('retrieve value Strted');
37. return HomeWidget.getWidgetData<String>('message', defaultValue: 'data')
38. .then((value01) => {
39. print('RETRIEVED Value'),
40. print(value01)
41. });
42. } on PlatformException catch (exception) {
43. debugPrint('Error Retrieving Widget. $exception');
44. }
45. }
46.
47. Future<void> _sendAndUpdate(title01, mesaage01) async {
48. await _saveHomeWidget(title01, mesaage01);
49. await _retrieveData();
50. await _updateWidget();
51. }
Here the snapshot of print statement
Note that values are saving and even the retrieving(getWidgetData<>) is also working, but the widget is not updating.
Few more details Flutter version - 2.0.4 - stable XCode - 12.4 home_widget - latest
Any other details, if needed will be provided.
Could you also provide the code of your iOS Widget? Also a Screenshot of the signing portion in Xcode to make sure the app group is setup correctly?
Hi,
find the attached
iOS Widget code // used home-widget example code
import WidgetKit
import SwiftUI
private let widgetGroupId = "group.homewidget.sernal"
struct Provider: TimelineProvider {
func placeholder(in context: Context) -> SimpleEntry {
SimpleEntry(date: Date(), title: "My Goal",
message: "Add your goal from Sernal main Screen")
}
func getSnapshot(in context: Context, completion: @escaping (SimpleEntry) -> ()) {
let data = UserDefaults.init(suiteName:widgetGroupId)
print("getSnapshot")
let entry = SimpleEntry(date: Date(), title: "My Goal",
message: data?.string(forKey: "message")
?? "Add your goal from Sernal ")
completion(entry)
}
func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
//To check here to update widget data
getSnapshot(in: context) { (entry) in
let timeline = Timeline(entries: [entry], policy: .atEnd)
completion(timeline)
}
}
}
struct SimpleEntry: TimelineEntry {
let date: Date
let title: String
let message: String
}
struct HomeWidgetForSernalEntryView : View {
var entry: Provider.Entry
let data = UserDefaults.init(suiteName:widgetGroupId)
var body: some View {
VStack.init(alignment: .leading, spacing: nil,
content:{
Text(entry.title).font(.body)
Text(entry.message).bold().font(.title)
.widgetURL(URL(string: "HomeWidgetForSernal://openmainscreen"))
})
}
}
@main
struct HomeWidgetForSernal: Widget {
let kind: String = "HomeWidgetForSernal"
var body: some WidgetConfiguration {
StaticConfiguration(
kind: kind,
provider: Provider()) { entry in
HomeWidgetForSernalEntryView(entry: entry)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.gray)
}
.configurationDisplayName("My Goal")
.description("Your added goals are displayed like this.").supportedFamilies([.systemMedium, .systemLarge])
}
}
struct HomeWidgetForSernal_Previews: PreviewProvider {
static var previews: some View {
HomeWidgetForSernalEntryView(entry: SimpleEntry(date: Date(), title: "My Goal",
message: "My added Goals."))
.previewContext(WidgetPreviewContext(family: .systemMedium))
}
}
I think you are missing the App Group Capability in your App.
Go to: Signing & Capabilities > +Capabilities > App Group
App group is in both runner & widget, please find the attached. Edit:- YES, it was missing before, later on I added & checked with rebuild, Clean, still the same problem.
The Widget seems to only declare the App Group for Profile Builds, not sure if that might be it? Also just to make sure: You did add the App Group on the Apple Developer Page?
Thanks for your time & patience.
Now it is working.
@huytower Does this help you as well?
For those how might encounter this problem as @ABausG said, make sure to add the app groups to debug, profile, and release.
Make sure you select all:
See here its not added to the debug:
When add to all it will look like this:
@ABausG Thanks man!
let me check in next week.
Hello bro,
I can reload home widget for platform Android, while platform iOs is not.
I can replace content "..." by other content in platform Android, while can not in platform iOs.
Please tell me what I need to do more.
Code sample :
I used this in main widget as guide told me.
I'm using these code for update data (it works for platform Android)
iOs files structure: (HomeWidgetExample created as Widget Extension in Xcode)
*.entitlements
HomeWidgetExample.swift