I've been struggling on an issue for some time now. I'm wondering why this code :
private func generateIdentity (base64p12 : String, password : String?, url : NSURL) {
let p12KeyFileContent = NSData(base64EncodedString: base64p12, options: NSDataBase64DecodingOptions(rawValue: 0))
if (p12KeyFileContent == nil) {
NSLog("Cannot read PKCS12 data")
return
}
let options = [String(kSecImportExportPassphrase):password ?? ""]
var citems: CFArray? = nil
let resultPKCS12Import = withUnsafeMutablePointer(&citems) { citemsPtr in
SecPKCS12Import(p12KeyFileContent!, options, citemsPtr)
}
if (resultPKCS12Import != errSecSuccess) {
print(resultPKCS12Import)
return
}
let items = citems! as NSArray
let myIdentityAndTrust = items.objectAtIndex(0) as! NSDictionary
let identityKey = String(kSecImportItemIdentity)
identity = myIdentityAndTrust[identityKey] as! SecIdentityRef
hasCertificate = true
print("cert cre", identity)
}
compiles, whereas this other one not :
private func generateIdentity (base64p12 : NSData, password : String?) {
let p12KeyFileContent = NSData(data: base64p12)
let options = [String(kSecImportExportPassphrase):password ?? ""]
var citems: CFArray? = nil
let resultPKCS12Import = withUnsafeMutablePointer(&citems) { citemsPtr in // line with the error
SecPKCS12Import(p12KeyFileContent!, options, citemsPtr)
}
if (resultPKCS12Import != errSecSuccess) {
print(resultPKCS12Import)
return
}
let items = citems! as NSArray
let myIdentityAndTrust = items.objectAtIndex(0) as! NSDictionary
let identityKey = String(kSecImportItemIdentity)
identity = myIdentityAndTrust[identityKey] as! SecIdentityRef
hasCertificate = true
print("cert cre", identity)
}
XCode tells me that :
Cannot convert value of type 'inout CFArray?' (aka 'inout Optional') to expected argument type 'inout _'
I really don't see how the 2 codes are different for the citems variable because what I basically just did was to use a NSData argument for the function, thus bypassing base64 string conversion to NSData.
The error message is super-confusing, but the cause of the error resides here:
SecPKCS12Import(p12KeyFileContent!, options, citemsPtr)
In your second example, with declaring let p12KeyFileContent = NSData(data: base64p12), the type of p12KeyFileContent is NSData, not NSData?. So, you cannot use ! for p12KeyFileContent.
Try changing the line as:
SecPKCS12Import(p12KeyFileContent, options, citemsPtr)
(! removed.)
One more.
You usually have no need to use withUnsafeMutablePointer to call SecPKCS12Import.
Try replacing these 3 lines (in your second example):
let resultPKCS12Import = withUnsafeMutablePointer(&citems) { citemsPtr in // line with the error
SecPKCS12Import(p12KeyFileContent!, options, citemsPtr)
}
with this:
let resultPKCS12Import = SecPKCS12Import(p12KeyFileContent, options, &citems)
The first example code can be rewritten as well.
Related
I am trying to get some data back from Core Spotlight which I am storing using a custom attribute key. Tested this on macOS and iOS as well, the result is always the same.
My test class:
import CoreSpotlight
class SpotlightSearch {
let domainId = "com.company.some"
let originalDataKeyName: String
init() {
self.originalDataKeyName = domainId.replacingOccurrences(of: ".", with: "_") + "_originalData"
}
func addToIndex(title: String, content: String) {
guard let originalDataKey = CSCustomAttributeKey(keyName: originalDataKeyName, searchable: false, searchableByDefault: false, unique: false, multiValued: false)
else { return }
let uniqueId = "MyUniqueId" + title
let originalContent = NSString(string: content)
let attributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeText as String)
attributeSet.title = title
attributeSet.setValue(originalContent, forCustomKey: originalDataKey)
let item = CSSearchableItem(uniqueIdentifier: uniqueId, domainIdentifier: domainId, attributeSet: attributeSet)
CSSearchableIndex.default().indexSearchableItems([item]) { error in
if let error = error {
print("Indexing error: \(error.localizedDescription)")
} else {
print("Item '\(title)' successfully indexed!")
}
}
}
var query: CSSearchQuery?
func search(title: String) {
var allItems = [CSSearchableItem]()
let queryString = "title == '\(title)'cd"
let attributes = [ "title", originalDataKeyName ]
let newQuery = CSSearchQuery(queryString: queryString, attributes: attributes)
newQuery.foundItemsHandler = { (items: [CSSearchableItem]) -> Void in
allItems.append(contentsOf: items)
}
newQuery.completionHandler = { [weak self] (error: Error?) -> Void in
guard let originalDataKeyName = self?.originalDataKeyName,
let originalDataKey = CSCustomAttributeKey(keyName: originalDataKeyName)
else { return }
print("Search complete")
for item in allItems {
let attributeSet = item.attributeSet
let customData = attributeSet.value(forCustomKey: originalDataKey)
// Always nil
if customData == nil {
print("\(String(describing: originalDataKeyName)) not found in \(attributeSet.description)")
} else if let originalData = customData as? NSData {
let data = Data(referencing: originalData)
if let originalString = String(data: data, encoding: .utf8) {
print("Found '\(originalString)'")
}
}
}
}
query = newQuery
newQuery.start()
}
}
On app init:
let newSpotlightSearch = SpotlightSearch()
newSpotlightSearch.addToIndex(title: "Banana", content: "🍌")
Later:
spotlightSearch.search(title: "Banana")
It will find the title, but will not give me back the custom attribute value. If I put a breakpoint after "// Always nil" and use po attributeSet I will get
(lldb) po attributeSet
{
"_kMDItemBundleID" = "de.axelspringer.SearchMac";
"_kMDItemDomainIdentifier" = "com.company.some";
"_kMDItemExpirationDate" = "2018-08-26 00:00:00 +0000";
"_kMDItemExternalID" = MyUniqueIdBanana;
"com_company_some_originalData" = "\Ud83c\Udf4c";
kMDItemTitle = Banana;
}
So the value is there, but Spotlight will not return it to me. Already tried to use NSData instead of NSString for the custom attribute, but same result.
Also found this orphaned question in the Apple developer forums:
CSCustomAttributeKey valueForCustomKey not working
I believe it's iOS issue.
While it's not fixed, maybe Apple will allow you to use a private API to do your thing.
So, attributeSet has private Dictionaries attributes and customAttributes. You can try to get those values using Key Value Coding and ObjC:
NSDictionary *attributes = [attributeSet valueForKey:#"attributes"];
id customData = attributes[originalDataKeyName];
OR
NSDictionary *customAttributes = [attributeSet valueForKey:#"customAttributes"];
id customData = customAttributes[originalDataKeyName];
Key type in those dictionaries is either NSString* or CSCustomAttributeKey*, so you can try supplying both originalDataKeyName and originalDataKey.
I have a project built in Swift 1.5. When I converted the code to swift 3.0 it started showing me errors in each 'if' statement in below code:
convenience init?(userInfo: [NSObject: AnyObject]) {
guard let statusString = userInfo[ConnectionMessage.StatusKey] as? String else {
return nil
}
guard let status = ConnectionStatus(string: statusString) else {
return nil
}
guard let connectionId = userInfo[ConnectionMessage.ConnectionIdKey]?.longLongValue else {
return nil
}
var ssid = ""
if let newSsid = userInfo[ConnectionMessage.SSIDKey] as? String {
ssid = newSsid
}
var password = ""
if let pw = userInfo[ConnectionMessage.PasswordKey] as? String {
password = pw
}
let buyerName = userInfo[ConnectionMessage.BuyerNameKey] as? String
self.init(status: status, connectionId: connectionId, ssid: ssid, password: password, buyerName: buyerName)
}
The error is
Ambiguous reference to member subscript
I tried the solutions found on StackOverflow but no luck. Please guide.
Change userInfo from [NSObject : AnyObject] to [String : AnyObject]. This assumes all of your ConnectionMessage.xxxKey values are String.
You also need to ensure that the dictionary you pass into the userInfo parameter is actually a dictionary with keys of type String.
I am trying to detect if the dictionary coming from API is empty or has values but whenever i am trying to do Dict.count it crashes.
if let personalInfo = self.scanResult?.fields { // personalInfo has 0 values but not nil
let image = NSData(base64EncodedString: (personalInfo["Photo"] as? String)!, options: NSDataBase64DecodingOptions(rawValue: 0)) // Crashes
}
I also tried isEmpty and it crashes there as well.
You should unwrap personalInfo["Photo"] before trying to use it. This way you can ensure you're not trying to instantiate NSData without a value for the Photo key being present
if let personalInfo = self.scanResult?.fields{
if let encodedString = personalInfo["Photo"] as? String{
let image = NSData(base64EncodedString: encodedString,options: NSDataBase64DecodingOptions(rawValue:0))
}
}
I think the simplest solution is to check dictionary keys count is greater than 0. For example:
let dictionary: Dictionary<String, AnyObject> = Dictionary()
if dictionary.keys.count > 0 {
// This mean dictionary has values.
}
The problem was that the self.scanResult.fields was coming out as NSNull. so i found these 3 methods which resolves the problem.
func isNotNull(object:AnyObject?) -> Bool {
guard let object = object else {
return false
}
return (isNotNSNull(object) && isNotStringNull(object))
}
func isNotNSNull(object:AnyObject) -> Bool {
return object.classForCoder != NSNull.classForCoder()
}
func isNotStringNull(object:AnyObject) -> Bool {
if let object = object as? String where object.uppercaseString == "NULL" {
return false
}
return true
}
And i just call like this :
if self.isNotNSNull((self.scanResult?.fields)!
{
}
You can use the "count" of dictionary. See the code.
if let personalInfo = self.scanResult?.fields {
if personalInfo.count > 0 {
if let base64ImageString = personalInfo["Photo"] as? String {
if let image = NSData(base64EncodedString: base64ImageString, options: NSDataBase64DecodingOptions(rawValue: 0)) {
// do your stuff with image
}
}
}
}
I am getting a headache due to UnsafePointer in Swift.
This is the method I want to call:
func CFDictionaryGetValue(theDict: CFDictionary!, _ key: UnsafePointer<Void>) -> UnsafePointer<Void>
And this is how I do it.
let ds: SCDynamicStoreRef = SCDynamicStoreCreate(nil, "setNet" as CFString, nil, nil)!
let list = SCDynamicStoreCopyProxies(ds)
print(list!)
print(CFDictionaryGetValue(list, UnsafePointer("HTTPPort")))
This however returns an error. I have no idea how to pass dictionary key to this method. If I remove the UnsafePointer("HTTPPort") and use "HTTPPort" instead I get a runtime error."
How can you access the dictionary values?
The easiest solution is to take advantage of the toll-free bridging
between CFDictionary and NSDictionary, and use the NSDictionary
accessor methods:
let ds = SCDynamicStoreCreate(nil, "setNet", nil, nil)!
if let list = SCDynamicStoreCopyProxies(ds) as NSDictionary? {
if let port = list[kSCPropNetProxiesHTTPPort as NSString] as? Int {
print("HTTPPort:", port)
}
}
But just for the sake of completeness: It can be done with
CFDictionaryGetValue:
if let list = SCDynamicStoreCopyProxies(ds) {
let key = kSCPropNetProxiesHTTPPort
let port = unsafeBitCast(CFDictionaryGetValue(list, unsafeAddressOf(key)), NSObject!.self)
if port != nil {
print("HTTPPort:", port)
}
}
I took a look at the SE question here [Avinash's answer] and the Apple resource here [bottom of page 18 and top of page 19] in my attempt to set an address for an ABRecord. I did my best to translate them from Objective-C, but apparently I made an mistake somewhere since on the line let dict = CFDictionaryCreate(kCFAllocatorDefault, keys, values, 5, &kCFCopyStringDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks) I get the error Cannot assign to immutable value of type 'CFDictionaryValueCallBacks'.
Here's my code:
let information: ABRecord = ABPersonCreate().takeRetainedValue()
let address: ABMutableMultiValueRef = ABMultiValueCreateMutable(ABPropertyType(kABMultiDictionaryPropertyType)).takeUnretainedValue()
var keys = [CFStringRef]()
var values = [CFStringRef]()
keys.append(kABPersonAddressStreetKey)
keys.append(kABPersonAddressCityKey)
keys.append(kABPersonAddressStateKey)
keys.append(kABPersonAddressZIPKey)
keys.append(kABPersonAddressCountryKey)
keys.append(kABPersonAddressCountryCodeKey)
Note: country code left out
values.append(s["kABPersonAddressStreetKey"]!! as NSString)
values.append(s["kABPersonAddressCityKey"]!! as NSString)
values.append(s["kABPersonAddressStateKey"]!! as NSString)
values.append(s["kABPersonAddressZIPKey"]!! as NSString)
values.append(s["kABPersonAddressCountryKey"]!! as NSString)
values.append(s["kABPersonAddressCountryCodeKey"]!! as NSString)
let dict = CFDictionaryCreate(kCFAllocatorDefault, keys, values, 5, &kCFCopyStringDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks)
let scanned = ABUnknownPersonViewController()
let identifier = ABMultiValueIdentifier()
ABMultiValueAddValueAndLabel(address, dict, kABHomeLabel, &identifier)
ABRecordSetValue(information, kABPersonAddressProperty, address, nil)
I'm sure there's a much more concise way to do this, but here's the solution I've come up with.
var addressComponents = [String : String]()
if let value = s["kABPersonAddressStreetKey"] {
addressComponents[kABPersonAddressStreetKey as String] = value
}
if let value = s["kABPersonAddressCityKey"] {
addressComponents[kABPersonAddressCityKey as String] = value
}
if let value = s["kABPersonAddressStateKey"] {
addressComponents[kABPersonAddressStateKey as String] = value
}
if let value = s["kABPersonAddressZIPKey"] {
addressComponents[kABPersonAddressZIPKey as String] = value
}
if let value = s["kABPersonAddressCountryKey"] {
addressComponents[kABPersonAddressCountryKey as String] = value
}
if let value = s["kABPersonAddressCountryCodeKey"] {
addressComponents[kABPersonAddressCountryCodeKey as String] = value
}
let address: ABMutableMultiValue = ABMultiValueCreateMutable(ABPropertyType(kABMultiStringPropertyType)).takeRetainedValue()
ABMultiValueAddValueAndLabel(address, addressComponents, kABHomeLabel, nil)
ABRecordSetValue(information, kABPersonAddressProperty, address, nil)