Open spencer0124 opened 1 year ago
I think it is weird because lock screen widget and home widget (generating barcode) works well, but whenever i click the widget to open the app directly, it always crashes. below is my flutter code and swift code.
==== 1. flutter code ==== WidgetKit.setItem( 'widgetData', jsonEncode(WidgetData( '${passwordController.text}10')), 'group.flutterioswidget1'); WidgetKit.reloadAllTimelines();
==== 2. swift code ==== // // skkupass_widget.swift // skkupass widget // // Created by --- on 2023/04/22. //
import WidgetKit import SwiftUI import Intents import CoreImage
struct FlutterData: Decodable, Hashable { let text: String }
struct SimpleEntry: TimelineEntry { let date: Date var flutterData: FlutterData? }
// ์ธ์ ๊ฐฑ์ ํ ๊ฒ์ธ๊ฐ? struct Provider: TimelineProvider {
// data๊ฐ ์์๋ ๋ณด์ฌ์ค ์์ (dummy data)
func placeholder(in context: Context) -> SimpleEntry {
SimpleEntry(date: Date(), flutterData: FlutterData(text: "๊ฐ์ธ์ฉ ๋ฐ์ฝ๋๊ฐ ์ฌ๊ธฐ ์์ฑ๋ฉ๋๋ค"))
}
// ์ค์ ๊ด๋ จ data
func getSnapshot(in context: Context, completion: @escaping (SimpleEntry) -> ()) {
let entry = SimpleEntry(date: Date(), flutterData: FlutterData(text: "๊ฐ์ธ์ฉ ๋ฐ์ฝ๋๊ฐ ์ฌ๊ธฐ ์์ฑ๋ฉ๋๋ค"))
completion(entry)
}
// ์ค์ ๋ก timeline ๊ด๋ จ๋ ๋ถ๋ถ
func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
//var entries: [SimpleEntry] = []
let sharedDefaults = UserDefaults.init(suiteName: "group.flutterioswidget1")
var flutterData: FlutterData? = nil
if(sharedDefaults != nil) {
do {
let shared = sharedDefaults?.string(forKey: "widgetData")
if(shared != nil){
let decoder = JSONDecoder()
flutterData = try decoder.decode(FlutterData.self, from: shared!.data(using: .utf8)!)
}
} catch {
print(error)
}
}
let entryDate = Calendar.current.date(byAdding: .hour, value:24, to:Date())!
let entry = SimpleEntry(date: entryDate, flutterData: flutterData)
let timeline = Timeline(entries: [entry], policy: .never)
completion(timeline)
}
}
func generateBarcode(from string: String) -> UIImage? { // Convert the string to data let data = string.data(using: .ascii)
// Create a CIFilter for Code 128 barcode generation
guard let filter = CIFilter(name: "CICode128BarcodeGenerator") else { return nil }
// Set the input message for the filter
filter.setValue(data, forKey: "inputMessage")
// Get the output image from the filter
guard let outputImage = filter.outputImage else { return nil }
// Create a UIImage from the output image
let context = CIContext()
guard let cgImage = context.createCGImage(outputImage, from: outputImage.extent) else { return nil }
return UIImage(cgImage: cgImage)
}
struct skkupass_widget: Widget { let kind: String = "skkupass_widget"
var body: some WidgetConfiguration {
StaticConfiguration(kind: kind, provider: Provider()) { entry in
skkupasswidgetView(entry: entry)
}
.supportedFamilies([.systemMedium, .accessoryCircular, .accessoryInline, .accessoryRectangular])
.configurationDisplayName("์ค๊พธํจ์ค ์์ ฏ")
.description("ํ์์ฆ ๋ฐ์ฝ๋๋ฅผ ๋ณด์ฌ์ฃผ๋ฉฐ, ํด๋ฆญ์ ์ฑ์ ์คํํฉ๋๋ค")
}
}
struct skkupasswidgetView: View { var entry: Provider.Entry
@Environment(\.widgetFamily) var family
@ViewBuilder
var body: some View {
switch family {
case .accessoryCircular:
ZStack {
Circle()
.fill(Color.black)
Image("skkuinvert")
.resizable()
.frame(width: 57, height: 57)
}
case .accessoryRectangular:
ZStack {
HStack {
Text("SKKU PASS")
.font(.headline)
.foregroundColor(.white)
}
}
case .accessoryInline:
Text("๐ฑ")
case .systemMedium:
ZStack {
Color.white // set background color
VStack {
if let barcodeImage = generateBarcode(from: entry.flutterData?.text ?? "") {
Image(uiImage: barcodeImage)
.resizable()
.aspectRatio(contentMode: .fit)
.padding(0)
}
}
}
default:
Text("error")
}
}
}
It works well, but when i click to widget to launch the app, it always crashes Any idea how to solve error about NSInvalidArgumentException, nil error?
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: ' -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[0]' *** First throw call stack: (0x1b7f04e38 0x1b108f8d8 0x1b80aa078 0x1b80b53ac 0x1b7f202b8 0x1b7f20158 0x100d9eae8 0x100d9efe8 0x103d679f0 0x103d489fc 0x100fb2164 0x1bae0cadc 0x1bb046798 0x1bb047338 0x1ba8cfb1c 0x1ba8cfe3c 0x1bae0c7b4 0x1bb046734 0x1bb0470e8 0x1bf51b460 0x1bf51cf88 0x1bf52b7f4 0x1bf52b444 0x1b7f956c8 0x1b7f7702c 0x1b7f7beb0 0x1f2171368 0x1ba471668 0x1ba4712cc 0x1007c2448 0x1d6874960) libc++abi: terminating with uncaught exception of type NSException
__pthread_kill + 8 libsystem_kernel.dylib
: -> 0x1f5a1f674 <+8>: b.lo 0x1f5a1f694 ; <+40> 0x1f5a1f678 <+12>: pacibsp 0x1f5a1f67c <+16>: stp x29, x30, [sp, #-0x10]! 0x1f5a1f680 <+20>: mov x29, sp Target 0: (Runner) stopped. Lost connection to device.