我最近一直在尝试制作一个小部件,并想在我的小部件和我的应用程序之间共享一段数据。
static var sharedDataFileURL: URL {
let appGroupIdentifier = "group.com.unknownstudios.yk"
guard let url = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroupIdentifier)
else { preconditionFailure("Expected a valid app group container") }
return url.appendingPathComponent("userID.plist")
}
Run Code Online (Sandbox Code Playgroud)
上面的代码在我的应用程序上运行良好,但是当我在我的小部件上运行它时,它会给出以下输出。我已检查应用程序组是否正确,并且在 iOS 应用程序和小部件上均处于活动状态。
[unspecified] container_create_or_lookup_path_for_platform: client is not entitled
[unspecified] container_create_or_lookup_app_group_path_by_app_group_identifier: client is not entitled
Fatal error: Expected a valid app group container: file WidgetExtension/Library.swift, line 143
Run Code Online (Sandbox Code Playgroud)
编辑:
我也尝试过使用 UserDefaults,但它们也不起作用。
以下是我使用FileManager和UserDefaults的方式
UserDefaults(suiteName: "group.com.unknownstudios.yk")!.set("**USERID**", forKey: "userID")
let data = Data("**USERID**".utf8)
do {
try data.write(to: URL.sharedDataFileURL, options: .atomic)
} catch {
print(error.localizedDescription)
}
Run Code Online (Sandbox Code Playgroud)
以下是我尝试从小部件读取数据的方法: …
我们有一个已发布的iOS应用程序.IDE是XCode6.我想添加钥匙串共享以从iOS 8共享扩展访问应用程序中存在的sessionID.
问题是,只要打开钥匙串共享,就无法再访问已存在的sessionID.
只要关闭钥匙串共享,就可以访问它.
该字典被传递到SecItemCopyMatching,无论"Keychain Groups:"是什么,只要启用了keychain共享,它总是返回-25300(未找到).
[0] (null) @"svce" : @"SESSION_ID_KEY"
[1] (null) @"r_Data" : @"1"
[2] (null) @"m_Limit" : @"m_LimitOne"
[3] (null) @"class" : @"genp"
[4] (null) @"acct" : @"SESSION_ID_KEY"
[5] (null) @"pdmn" : @"ck"
Run Code Online (Sandbox Code Playgroud)
知道为什么访问密钥可能不起作用?我尝试使用捆绑包前缀和名称设置kSecAttrAccessGroup,但它仍然无法在模拟器上运行.
我在Apple商店上传了应用程序.现在我正在开发更新版本.但是,在处理现有项目时,我创建了具有不同名称的新项目.当我完成后,我更改了名称和包标识符以匹配现有的应用程序.
我尝试上传应用程序以测试航班,但我收到了错误
"Invalid IPA: The keychain-access-group in the embedded.mobileprovision and your binary don't match."
Run Code Online (Sandbox Code Playgroud)
所以我开始浏览网络以获得答案......我所提出的是启用权利.我做了,但无济于事......然后我编辑了权利,改变了这个:
$(AppIdentifierPrefix)com.xxxx
Run Code Online (Sandbox Code Playgroud)
对此:
12HJ2312.com.xxxx (the number is number of prefix from developer portal)
Run Code Online (Sandbox Code Playgroud)
它工作,我可以上传.世界再次变得美好.或者是吗?我仍然不明白为什么我要改变它.从哪里来
$(AppIdentifierPrefix)
Run Code Online (Sandbox Code Playgroud)
是红色的?我认为钥匙串是红色的,但似乎没有,因为钥匙串是我输入的相同值.那么我怎么能看到这个价值,它在哪里画出它的内容呢?
有没有办法以编程方式在iPhone应用程序中找到应用程序标识符前缀?或者甚至只是获取整个应用程序标识符字符串?
我看到你可以通过窥视"embedded.mobileprovision"文件找到它,但有更简单的方法吗?(如果你在模拟器中运行,我认为这不起作用)
编辑:对不起,我的意思是标识符PREFIX(10个字符).我已经意识到我实际上并不需要这个,因为无论如何,ID的其余部分都保证是唯一的.
在macOS应用程序中,我使用此代码在Application Support文件夹中创建目录.
let directoryURL = appSupportURL.appendingPathComponent("com.myCompany.myApp").appendingPathComponent("Documents")
Run Code Online (Sandbox Code Playgroud)
如何在Swift中以编程方式获得字符串com.myCompany.myApp?
我看到了这个问题,但我不确定如何在我的macOS Swift应用程序中使用它:以编程方式访问应用程序标识符前缀