Skip to content

Commit 4831d42

Browse files
authored
consume security scoped bookmarks to read urls (#314)
* consume security scoped bookmarks to read urls * move entitlements to correct directory
1 parent d9552e8 commit 4831d42

File tree

4 files changed

+22
-7
lines changed

4 files changed

+22
-7
lines changed

MLXChatExample.entitlements renamed to Applications/MLXChatExample/MLXChatExample.entitlements

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
<true/>
99
<key>com.apple.security.files.downloads.read-write</key>
1010
<true/>
11+
<key>com.apple.security.files.user-selected.read-only</key>
12+
<true/>
1113
<key>com.apple.security.network.client</key>
1214
<true/>
1315
</dict>

Applications/MLXChatExample/Services/MLXService.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ class MLXService {
107107
}
108108

109109
// Prepare input for model processing
110-
let userInput = UserInput(chat: chat)
110+
let userInput = UserInput(
111+
chat: chat, processing: .init(resize: .init(width: 1024, height: 1024)))
111112

112113
// Generate response using the model
113114
return try await modelContainer.perform { (context: ModelContext) in

Applications/MLXChatExample/ViewModels/ChatViewModel.swift

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,15 +160,29 @@ class MediaSelection {
160160
var isShowing = false
161161

162162
/// Currently selected image URLs
163-
var images: [URL] = []
163+
var images: [URL] = [] {
164+
didSet {
165+
didSetURLs(oldValue, images)
166+
}
167+
}
164168

165169
/// Currently selected video URLs
166-
var videos: [URL] = []
170+
var videos: [URL] = [] {
171+
didSet {
172+
didSetURLs(oldValue, videos)
173+
}
174+
}
167175

168176
/// Whether any media is currently selected
169177
var isEmpty: Bool {
170178
images.isEmpty && videos.isEmpty
171179
}
180+
181+
private func didSetURLs(_ old: [URL], _ new: [URL]) {
182+
// the urls we get from fileImporter require SSB calls to access
183+
new.filter { !old.contains($0) }.forEach { _ = $0.startAccessingSecurityScopedResource() }
184+
old.filter { !new.contains($0) }.forEach { $0.stopAccessingSecurityScopedResource() }
185+
}
172186
}
173187

174188
/// Options for clearing different aspects of the chat state

mlx-swift-examples.xcodeproj/project.pbxproj

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,6 @@
191191
/* End PBXCopyFilesBuildPhase section */
192192

193193
/* Begin PBXFileReference section */
194-
0A7C100B2DC37CEA00FD5155 /* MLXChatExample.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = MLXChatExample.entitlements; sourceTree = "<group>"; };
195194
0AC74EBB2D136221003C90A7 /* VLMEval.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = VLMEval.app; sourceTree = BUILT_PRODUCTS_DIR; };
196195
0AC74EC92D13622A003C90A7 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = "<group>"; };
197196
0AC74ECB2D13622A003C90A7 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
@@ -528,7 +527,6 @@
528527
C39273672B60697700368D5D = {
529528
isa = PBXGroup;
530529
children = (
531-
0A7C100B2DC37CEA00FD5155 /* MLXChatExample.entitlements */,
532530
C325DE3F2B648CDB00628871 /* README.md */,
533531
F8D7023A2BB4E223003D7CF5 /* Package.swift */,
534532
C3C36A6C2CA714600099FFA4 /* Configuration */,
@@ -1319,7 +1317,7 @@
13191317
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
13201318
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
13211319
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
1322-
CODE_SIGN_ENTITLEMENTS = MLXChatExample.entitlements;
1320+
CODE_SIGN_ENTITLEMENTS = Applications/MLXChatExample/MLXChatExample.entitlements;
13231321
CODE_SIGN_STYLE = Automatic;
13241322
COPY_PHASE_STRIP = NO;
13251323
CURRENT_PROJECT_VERSION = 1;
@@ -1385,7 +1383,7 @@
13851383
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
13861384
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
13871385
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
1388-
CODE_SIGN_ENTITLEMENTS = MLXChatExample.entitlements;
1386+
CODE_SIGN_ENTITLEMENTS = Applications/MLXChatExample/MLXChatExample.entitlements;
13891387
CODE_SIGN_STYLE = Automatic;
13901388
COPY_PHASE_STRIP = NO;
13911389
CURRENT_PROJECT_VERSION = 1;

0 commit comments

Comments
 (0)