如何用python编写Objectivec代码

2024-09-30 20:32:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用PyObjC编写以下Objective-C代码的等效Python代码。不知道怎么办?如果能在ow上帮助将Objective-C代码连接到Python中,我们将不胜感激。在

#import <IOKit/pwr_mgt/IOPMLib.h>

...
// kIOPMAssertionTypeNoDisplaySleep prevents display sleep,
// kIOPMAssertionTypeNoIdleSleep prevents idle sleep

//reasonForActivity is a descriptive string used by the system whenever it needs 
//  to tell the user why the system is not sleeping. For example, 
//  "Mail Compacting Mailboxes" would be a useful string.

//  NOTE: IOPMAssertionCreateWithName limits the string to 128 characters. 
CFStringRef* reasonForActivity= CFSTR("Describe Activity Type");

IOPMAssertionID assertionID;
IOReturn success = IOPMAssertionCreateWithName(kIOPMAssertionTypeNoDisplaySleep, 
                                    kIOPMAssertionLevelOn, reasonForActivity, &assertionID); 
if (success == kIOReturnSuccess)
{

    //Add the work you need to do without 
    //  the system sleeping here.

    success = IOPMAssertionRelease(assertionID);
    //The system will be able to sleep again. 
}
...

Tags: theto代码stringissleepbesystem
1条回答
网友
1楼 · 发布于 2024-09-30 20:32:52

您需要首先使用gen_bridge_metadata命令为IOKit.framework生成一个网桥文件。在

如果需要,可以将文件的内容硬编码为Python变量。在

然后使用objc.parseBridgeSupport()将桥加载到PyObjC中

objc.parseBridgeSupport(BRIDGE_FILE_STRING, globals(), objc.pathForFramework("/System/Library/Frameworks/IOKit.framework"))`

示例here和{a2}

Here is an example这几乎完全符合你的要求。在

相关问题 更多 >