Apple提供了用Swift编写的Generic Keychain示例,我想继续使用Objective-C。
我已在两个应用程序和canOpenUrl中启用了钥匙串共享我可以从A调用应用程序B,现在我想要从应用程序A到应用程序B共享用户名和密码。应用程序ID对于这两个应用程序都是相同的。< / p>
我看过各种教程也不想使用任何第三方项目。 无法知道如何将参数从app A传递给app B.
答案 0 :(得分:2)
启用钥匙串共享:
访问钥匙串(检索共享项目):
let itemKey = "Item Key"
let keychainAccessGroupName = "AB123CDE45.testKeychainG1"
let query:[String:Any] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrAccount as String: itemKey,
kSecReturnData as String: kCFBooleanTrue,
kSecMatchLimit as String: kSecMatchLimitOne,
kSecAttrAccessGroup as String: keychainAccessGroupName
]
var result: AnyObject?
let resultCodeLoad = withUnsafeMutablePointer(to: &result) {
SecItemCopyMatching(query as CFDictionary, UnsafeMutablePointer($0))
}
if resultCodeLoad == noErr {
if let result = result as? Data,
let keyValue = NSString(data: result,
encoding: String.Encoding.utf8.rawValue) as? String {
// Found successfully
print(keyValue)
}
} else {
print("Error: \(resultCodeLoad)")
}
答案 1 :(得分:-1)
第1步:
设置网址架构并将 AppA 的网址添加到 AppB 的info.plist
之类这样:<key>LSApplicationQueriesSchemes</key>
<array>
<string>Aapp_Scheme</string>
</array>
第2步:
在应用程序A中:
let url = URL.init(string: "B_Scheme://name=Obama&password=Trump");
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: ["":""], completionHandler: nil);
}
第3步:
在应用B的 AppDelegate.swift 中添加代码:
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
var name = "", password = "";
let param = url.absoluteString.replacingOccurrences(of: "B_Scheme://", with: "");
let paramArray = param.components(separatedBy: "&");
for temp in paramArray {
if (temp.range(of: "name=") != nil){
name = temp.replacingOccurrences(of: "name=", with: "");
}
else if (temp.range(of: "password=") != nil){
password = temp.replacingOccurrences(of: "password=", with: "");
}
}
if name == "Obama" && password == "Trump" {
print("get param success!");
}
return true;
}