请帮助我解决在 OS X 中枚举钥匙串项的问题。我阅读了有关在我的 iOS 应用程序中枚举所有钥匙串项的主题,但想了解如何编写相同的代码,该代码在主题中使用 Core Foundation 函数编写???我尝试这样做:
CFDictionaryRef query = CFDictionaryCreate ( kCFAllocatorDefault, NULL, NULL, 0, NULL, NULL);
CFTypeRef *result = NULL;
status = SecItemCopyMatching(query, result );
if (status != errSecSuccess)
{
GetLastError(status);
}
Run Code Online (Sandbox Code Playgroud)
但这段代码不起作用!你能帮助我理解我错在哪里吗?函数“SecItemCopyMatching”返回错误:传递给函数的一个或多个参数无效。错误代码:-50。
编辑: 如果更具可读性,这里是以下代码的 Github 要点。
我正在编写一种使用 Core Text 在 PDF 中的矩形内绘制文本的方法。我所写的内容完成了我需要的一切,除了垂直对齐(顶部、中心、底部)。当前绘制文本的标准方式是顶部对齐,我特别需要底部对齐。
- (void)drawText:(NSString *)textToDraw inFrame:(CGRect)frameRect withFont:(UIFont *)originalFont textColor:(UIColor *)textColor alignment:(PDFTextAlignment)alignment verticalAlignment:(PDFTextVerticalAlignment)verticalAlignment {
if (!textToDraw) {
// If nil, give it an empty value to draw
textToDraw = @"";
}
// Prepare font
CTFontRef font = [self ctFontRefFromUIFont:originalFont];
CGColorRef color = textColor.CGColor;
// Paragraph
CTTextAlignment ctAlignment;
switch (alignment) {
case PDFTextAlignmentLeft:
ctAlignment = kCTTextAlignmentLeft;
break;
case PDFTextAlignmentCenter:
ctAlignment = kCTTextAlignmentCenter;
break;
case PDFTextAlignmentRight:
ctAlignment = kCTTextAlignmentRight;
break;
case PDFTextAlignmentJustified:
ctAlignment = …Run Code Online (Sandbox Code Playgroud) 我想用 Swift 创建一些 PDF 页面。
这是我创建 PDF 页面的代码
// Initialize context and other stuff
CGPDFContextBeginPage(pdfContext, aDictionary);
CGContextDrawPDFPage(pdfContext, pdfPageRef)
CGPDFContextEndPage(pdfContext)
Run Code Online (Sandbox Code Playgroud)
我对“aDictionary”变量有疑问。我想在 kCGPDFContextMediaBox 键下添加媒体框(CGRect 类型变量)值。该文档告诉我以下内容:
The media box for the document or for a given page. This key is optional. If present, the value of this key must be a CFData object that contains a CGRect (stored by value, not by reference).
在 Objective-C 中,从 CGRect 创建 CFData 相当容易,但在 Swift 中我不知道如何创建它。这是我的 Objective-C 代码:
CGRect mediaBox = CGRectZero;
CFDataCreate(kCFAllocatorDefault, (const UInt8 *)&mediaBox, sizeof(mediaBox)); …Run Code Online (Sandbox Code Playgroud) Apple 似乎改变了通过 URL 方案拨打电话时的行为。我们目前使用此代码来发起电话呼叫:
let url = NSURL(string: "tel://011111111111")
UIApplication.sharedApplication().openURL()
Run Code Online (Sandbox Code Playgroud)
在 iOS 10.2 之前,这会立即启动拨号器并拨打电话。现在情况似乎已经改变,用户会收到确认拨打电话的提示。看来 tel:// 的行为更像 telprompt://。尽管官方 Apple URL 方案文档(最后更新于 2015 年)提到,如果从本机应用程序打开 tel:// URL,则不需要用户交互。
有谁知道(a)这是否是新的、预期的行为和/或(b)是否有另一种方法可以解决这个问题,可以在不提示用户的情况下发起电话呼叫?
我正在尝试复制 uiimage 并更改元数据而不重新编码图像,以免导致 jpg 伪影(我知道下面的代码重新编码,但这只是为了可以运行以进行测试)。CGImageDestinationCopyImageSource 应该在不重新编码的情况下复制图像的源,但是当我使用该函数时,它在 CGImageDestinationFinalize 失败。我正在尝试遵循此技术说明https://developer.apple.com/library/content/qa/qa1895/_index.html 知道为什么 CGImageDestinationFinalize 会因以下代码而失败吗?
-(NSData *)copyImageAndChangeMetaData:(UIImage *)image {
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)imageData, NULL);
NSLog(@" source: 0x%x", source);
if (source == NULL) {
NSLog(@" source is NULL");
}
CFStringRef UTI = CGImageSourceGetType(source);
NSLog(@" UTI: %@", (__bridge NSString *)UTI);
if (UTI == NULL) {
NSLog(@" UTI is NULL");
}
NSMutableData *dest_data = [NSMutableData data];
CGImageDestinationRef destination = CGImageDestinationCreateWithData((__bridge CFMutableDataRef)dest_data, UTI, 1, NULL);
if (!destination)
{
NSLog(@"Image Data Error: …Run Code Online (Sandbox Code Playgroud) 我正在尝试为CFNotificationCenter.
该文件指出:
姓名
要观察的通知的名称。如果为 NULL,则为对象发布的任何通知调用回调。如果 center 是 Darwin 通知中心,则此值不能为 NULL。
据我所知NULL,Swift 中不存在。那么这个论点应该是什么呢?
以下编译正常,但不是空字符串NULL:
CFNotificationCenterAddObserver(
CFNotificationCenterGetDistributedCenter(),
UnsafeMutableRawPointer?.none,
receiveNotification,
"" as CFString,
UnsafeRawPointer?.none,
CFNotificationSuspensionBehavior.deliverImmediately
)
Run Code Online (Sandbox Code Playgroud)
我试过CFString?,NSNull并且NSNull as String但是编译器会抱怨。
我误解了文档吗?如何为名称参数提供 NULL 值?
我正在新的 macOS 12 Monterey beta 上运行使用 C++ 构建的应用程序。该应用程序是使用 macOS SDK 10.9 构建的,该版本相当旧。
问题:
有一段代码用于获取平台版本,我在其中使用 Core-Foundation API 解析文件内容System/Library/CoreServices/SystemVersion.plist。应用程序不会将 macOS 版本返回为 12.0,而是将其读取为 10.16。代码中没有缺陷,因为相同的代码已用于识别许多较旧的 macOS 版本。
可能的原因: macOS 11.0 bigSur 版本期间发生了一些变化,其中有另一个文件名 SystemVersionCompat.plist 与 SystemVersion.plist 位于同一位置。我的应用程序正在读取前者而不是后者的 plist,并且在一些网络搜索中由于使用旧的 SDK 而了解了它。
内容 System/Library/CoreServices/SystemVersion.plist:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>ProductBuildVersion</key>
<string>21A5304g</string>
<key>ProductCopyright</key>
<string>1983-2021 Apple Inc.</string>
<key>ProductName</key>
<string>macOS</string>
<key>ProductUserVisibleVersion</key>
<string>12.0</string>
<key>ProductVersion</key>
<string>12.0</string>
<key>iOSSupportVersion</key>
<string>15.0</string>
</dict>
</plist>
Run Code Online (Sandbox Code Playgroud)
内容 System/Library/CoreServices/SystemVersionCompat.plist:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST …Run Code Online (Sandbox Code Playgroud) 我目前正在查看NSMutableSet由该函数创建的CFSetCreateMutable().文档说明返回值CFSetCreateMutable()是免费的桥接,这意味着我可以简单地将其转换为NSMutableSet.这是否意味着向其发送release消息是完全有效的?我总是可以安全地假设我可以像对待allocNS级一样对待这些对象吗?
我正在使用IOKit并拥有以下代码,一般的想法是将platformExpert密钥传递给这个小的核心基础命令行应用程序并让它打印解码后的字符串.测试用例是"序列号".运行时的代码如下:
./编译的序列号
几乎可以工作,但在字符串的开头返回序列号的最后4个字符,即对于C12D2JMPDDQX等示例序列,它将返回
DDQXC12D2JMPDDQX
有任何想法吗?
#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/IOKitLib.h>
int main (int argc, const char * argv[]) {
CFStringRef parameter = CFSTR("serial-number");
if (argv[1]) {
parameter = CFStringCreateWithCString(
NULL,
argv[1],
kCFStringEncodingUTF8);
}
CFDataRef data;
io_service_t platformExpert = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("IOPlatformExpertDevice"));
if (platformExpert)
{
data = IORegistryEntryCreateCFProperty(platformExpert,
parameter,
kCFAllocatorDefault, 0);
}
IOObjectRelease(platformExpert);
CFIndex bufferLength = CFDataGetLength(data);
UInt8 *buffer = malloc(bufferLength);
CFDataGetBytes(data, CFRangeMake(0,bufferLength), (UInt8*) buffer);
CFStringRef string = CFStringCreateWithBytes(kCFAllocatorDefault,
buffer,
bufferLength,
kCFStringEncodingUTF8,
TRUE);
CFShow(string);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我有当前的代码,它似乎工作,除了事实CFShow不会将\ u00e9的unicode UTF8编码转换为é
#include <CoreFoundation/CoreFoundation.h>
int main()
{
char *s = "This is a test of unicode support: fiancée\n";
CFTypeRef cfs = CFStringCreateWithCString(NULL, s, kCFStringEncodingUTF8);
CFShow(cfs);
}
Run Code Online (Sandbox Code Playgroud)
输出是
This is a test of unicode support: fianc\u00e9e
|____|
> é doesn't output properly.
Run Code Online (Sandbox Code Playgroud)
如何指示CFShow它是unicode?printf是ac字符串时处理得很好.
core-foundation ×10
objective-c ×5
ios ×4
macos ×3
swift ×3
c++ ×2
c ×1
cfdata ×1
cfdictionary ×1
core-text ×1
iokit ×1
keychain ×1
nsmutableset ×1
security ×1
swift5.1 ×1