如何在python中编写Objective-c代码

时间:2013-11-27 19:11:09

标签: python objective-c python-2.7

我想使用PyObjC编写以下Objective-C代码的等效Python代码。不知道该怎么办?任何帮助都会受到高度赞赏,因为它将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. 
}
...

1 个答案:

答案 0 :(得分:2)

首先需要使用IOKit.framework命令为gen_bridge_metadata生成桥文件。

如果需要,可以将文件内容硬编码到Python变量中。

然后使用objc.parseBridgeSupport()

将桥加载到PyObjC中
objc.parseBridgeSupport(BRIDGE_FILE_STRING, globals(), objc.pathForFramework("/System/Library/Frameworks/IOKit.framework"))`

示例herehere

Here is an example几乎完全符合您的要求。

相关问题