我尝试访问通过应用程序组在使用UIKit创建的swift文件中从使用SwiftUI创建的swift文件中设置的变量。但这是行不通的。我做过与in this youtube video.
类似的事情我的第一部分代码如下:
import UIKit
let sharedUserDefaults = UserDefaults(suiteName: SharedUserDefaults.suiteName)
class StartBildschirm: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let username = "Alex"
sharedUserDefaults?.set(username, forKey: SharedUserDefaults.Keys.username)
guard let sharedUsername = sharedUserDefaults?.string(forKey: SharedUserDefaults.Keys.username) else {return}
print("Shared username: \(sharedUsername)")
}
我的结构如下:
import Foundation
struct SharedUserDefaults {
static let suiteName = "group.co.xxxxxx"
struct Keys {
static let username = "username"
}
}
还有我的Swift文件的扩展名,如下所示:
import WidgetKit
import SwiftUI
import Intents
let sharedUserDefaults = UserDefaults(suiteName: SharedUserDefaults.suiteName)
let sharedUsername = sharedUserDefaults?.string(forKey: SharedUserDefaults.Keys.username)
struct Provider: IntentTimelineProvider {
func placeholder(in context: Context) -> SimpleEntry {
SimpleEntry(date: Date(), configuration: ConfigurationIntent())
}
deleted the code that is unnecessarily
struct myTeamEntryView : View {
var entry: Provider.Entry
var body: some View {
Text(sharedUsername ?? "no")
}
}
我的窗口小部件扩展名应显示sharedUsername,但不显示。