4

I have added WidgetKit to my app but on iOS 13 devices it crashes when the app starts with this error: dyld`__abort_with_payload

The part of the code that causes the crash is this:

static func reloadTimelines () {  
 
    if #available(iOS 14, *) {

      WidgetCenter.shared.reloadTimelines(ofKind: "com.myDomain.myApp.ProgressWidget")
   }
}

I have tried other unsuccessful variations like this:

@available(iOS 14, *)
static func reloadTimelines () {

      WidgetCenter.shared.reloadTimelines(ofKind: "com.myDomain.myApp.ProgressWidget")
}

The project is in objective-c and I import that class in swift.

But without adding #import "myApp-Swift-h" anywhere, or using that class it keeps crashing.

Can someone tell me what I am doing wrong or what is happening?

Thanks in advance.

Edit: I try this code on Xcode 11 and work!

#if canImport(WidgetKit)
   WidgetCenter.shared.reloadTimelines(ofKind: "com.literautas.StoryPlanner.ProgressWidget")
#endif

But it crashes on Xcode 12 beta 4

Edit 2: It also crashes on Xcode 12 beta 6

Tomeu Mascó
  • 319
  • 2
  • 11
  • Please see my answer here to easily solve this problem, https://stackoverflow.com/a/64809435/1002338 – Mona Nov 14 '20 at 04:17
  • Please see my answer here to easily solve this problem, https://stackoverflow.com/a/64809435/1002338 – Mona Nov 14 '20 at 04:18
  • @Mona This solution is not possible because users with older ios versions have to be supported – Tomeu Mascó Nov 15 '20 at 14:01
  • @ Tomeu Mascó yes it is. The target iOS version of the app can remain at whatever it is. Even the target iOS for the old today widget can remain whatever it was. This is just setting the iOS version for the new widget target that you add to your project to 14. – Mona Nov 16 '20 at 21:45
  • The target iOS for the main app and the widget can be different. – Mona Nov 16 '20 at 21:46
  • @Mona Oh! Sorry but the widget target is already iOS 14 – Tomeu Mascó Nov 17 '20 at 09:38

3 Answers3

8

Another person (thank you, Mark) gave me the solution. Marking the WidgetKit.framework as "optional" did the trick!

Tomeu Mascó
  • 319
  • 2
  • 11
  • 1
    Thanks! That did the trick for me in addition to using canImport. For those wondering: go to the frameworks folder, add WidgetKit.framework to your main target and then set it as optional. – Jose Santos Sep 09 '20 at 19:17
3

For those who are still having issues. Try these changes.

Make Optional Import

#if canImport(WidgetKit)
import WidgetKit
#endif

Check for iOS Version

if #available(iOS 14, *) {
     WidgetCenter.shared.reloadAllTimelines()
 }

Last Step - Most Imp

Add WidgetKit to the Build Phase -> Link Libraries and make it optional

I was missing doing the last step and then the code started working in iOS 13 as well.

Ankit Saxena
  • 809
  • 7
  • 10
0

We had

  • #if canImport(WidgetKit)
  • @available(iOS 14.0, *)
  • and the WidgetKit as optional in our main target

And the crash was still happening, apparently even on iOS 14.

We moved all WidgetCenter calls to a single file with all the measures and an additional one:

  • NSClassFromString("WidgetKit.WidgetCenter")

For some reason, we can't explain yet, this is actually returning nil for some cases. We prevented the crashes but in those cases, it means we can't really use the WidgetCenter API.

Daniel Carlos
  • 268
  • 4
  • 6