0
# Framework and Dynamic Loading
1
2
Utilities for loading Objective-C frameworks and libraries dynamically, including BridgeSupport file parsing, lazy module loading, and framework introspection capabilities.
3
4
## Capabilities
5
6
### Dynamic Library Loading
7
8
Functions for locating and loading frameworks and dynamic libraries using dyld semantics.
9
10
```python { .api }
11
def dyld_framework(filename, framework_name, version=None):
12
"""
13
Find framework using dyld semantics.
14
15
Args:
16
filename (str): Framework filename
17
framework_name (str): Name of the framework
18
version (str, optional): Specific version to load
19
20
Returns:
21
str: Path to the framework or None if not found
22
"""
23
24
def dyld_library(filename, libname):
25
"""
26
Find dynamic library using dyld semantics.
27
28
Args:
29
filename (str): Library filename
30
libname (str): Library name
31
32
Returns:
33
str: Path to the library or None if not found
34
"""
35
36
def dyld_find(filename):
37
"""
38
Generic dyld locator function for libraries and frameworks.
39
40
Args:
41
filename (str): File to locate using dyld search paths
42
43
Returns:
44
str: Full path to located file or None if not found
45
"""
46
```
47
48
### Framework Information
49
50
Functions for obtaining framework metadata and path information.
51
52
```python { .api }
53
def pathForFramework(path):
54
"""
55
Get the full path for a framework.
56
57
Args:
58
path (str): Framework path or name
59
60
Returns:
61
str: Full framework path
62
"""
63
64
def infoForFramework(filename):
65
"""
66
Get framework metadata information.
67
68
Args:
69
filename (str): Framework filename or path
70
71
Returns:
72
dict: Framework information including version, identifier, etc.
73
"""
74
```
75
76
### BridgeSupport Integration
77
78
Functions for parsing BridgeSupport files and initializing framework wrappers.
79
80
```python { .api }
81
def initFrameworkWrapper(frameworkName, frameworkPath, frameworkIdentifier,
82
isIndirect=False, frameworkDict=None,
83
frameworkGlobals=None, inline_list=None,
84
initialdict=None, metadict=None):
85
"""
86
Initialize a framework wrapper with BridgeSupport data.
87
88
Args:
89
frameworkName (str): Name of the framework
90
frameworkPath (str): Path to framework bundle
91
frameworkIdentifier (str): Framework bundle identifier
92
isIndirect (bool): Whether framework is loaded indirectly
93
frameworkDict (dict, optional): Framework-specific data
94
frameworkGlobals (dict, optional): Global symbols
95
inline_list (list, optional): Inline function definitions
96
initialdict (dict, optional): Initial symbol dictionary
97
metadict (dict, optional): Metadata dictionary
98
99
Returns:
100
Module-like object providing access to framework APIs
101
"""
102
103
def parseBridgeSupport(xmldata, globals, frameworkName, dylib_path=None, inlineTab=None):
104
"""
105
Parse BridgeSupport XML files to extract API metadata.
106
107
Args:
108
xmldata (bytes): BridgeSupport XML data
109
globals (dict): Global namespace to populate
110
frameworkName (str): Name of the framework being parsed
111
dylib_path (str, optional): Path to dynamic library
112
inlineTab (dict, optional): Inline function table
113
114
Populates the globals dictionary with parsed API definitions.
115
"""
116
```
117
118
### Lazy Loading Support
119
120
Classes and functions for implementing lazy framework loading to improve startup performance.
121
122
```python { .api }
123
class ObjCLazyModule:
124
"""
125
Lazy module loading class for frameworks.
126
127
Delays actual framework loading until first access to minimize
128
startup time and memory usage.
129
"""
130
131
def createFrameworkDirAndGetattr():
132
"""
133
Framework directory creation utility for lazy loading.
134
135
Creates framework directory structures and handles attribute
136
access for lazy-loaded framework modules.
137
"""
138
```
139
140
**Usage Examples:**
141
142
```python
143
import objc
144
from objc import dyld_framework, pathForFramework, initFrameworkWrapper
145
146
# Find and load a framework
147
framework_path = dyld_framework("AppKit", "AppKit")
148
if framework_path:
149
print(f"Found AppKit at: {framework_path}")
150
151
# Get framework information
152
info = infoForFramework("Foundation")
153
print(f"Foundation info: {info}")
154
155
# Load a custom framework
156
custom_path = dyld_framework("MyFramework", "MyFramework", version="1.0")
157
158
# Using lazy loading for better performance
159
class LazyFrameworkLoader:
160
def __init__(self, framework_name):
161
self.framework_name = framework_name
162
self._framework = None
163
164
@property
165
def framework(self):
166
if self._framework is None:
167
path = dyld_framework(self.framework_name, self.framework_name)
168
if path:
169
self._framework = initFrameworkWrapper(
170
self.framework_name,
171
path,
172
f"com.apple.{self.framework_name.lower()}"
173
)
174
return self._framework
175
176
# Lazy load Core Graphics framework
177
cg_loader = LazyFrameworkLoader("CoreGraphics")
178
# Framework is only loaded when first accessed
179
cg_framework = cg_loader.framework
180
```
181
182
### Framework Loading Patterns
183
184
Common patterns for framework loading and integration:
185
186
```python
187
# Standard framework loading
188
import objc
189
190
# Load system frameworks (these are typically auto-loaded)
191
# Foundation and AppKit are loaded automatically with PyObjC
192
193
# Manual framework loading for less common frameworks
194
try:
195
# Try to find and load Metal framework
196
metal_path = objc.dyld_framework("Metal", "Metal")
197
if metal_path:
198
# Initialize Metal framework wrapper
199
metal = objc.initFrameworkWrapper(
200
"Metal",
201
metal_path,
202
"com.apple.Metal"
203
)
204
# Now can use Metal APIs
205
print("Metal framework loaded successfully")
206
except Exception as e:
207
print(f"Failed to load Metal framework: {e}")
208
209
# Loading private or third-party frameworks
210
custom_framework_path = "/Library/Frameworks/MyFramework.framework"
211
custom_info = objc.infoForFramework(custom_framework_path)
212
if custom_info:
213
custom_framework = objc.initFrameworkWrapper(
214
"MyFramework",
215
custom_framework_path,
216
custom_info.get("identifier", "com.example.MyFramework")
217
)
218
```
219
220
### Integration with macOS Development
221
222
Framework loading enables access to the complete macOS development ecosystem:
223
224
- **System Frameworks**: Foundation, AppKit, Core Graphics, etc.
225
- **Specialized Frameworks**: Metal, Core ML, Vision, etc.
226
- **Third-party Frameworks**: Custom and vendor frameworks
227
- **Version Management**: Load specific framework versions
228
- **Performance Optimization**: Lazy loading reduces startup time