Skip to content

Commit

Permalink
Add OpenSensitiveURLActionKey for iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle-Ye committed Nov 27, 2023
1 parent c1ee043 commit 7466ac4
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 22 deletions.
16 changes: 15 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,21 @@ let openSwiftUITarget = Target.target(
name: "OpenSwiftUI",
dependencies: [
"OpenSwiftUIShims",
"CoreServices",
"UIKitCore",
],
swiftSettings: [
.enableExperimentalFeature("AccessLevelOnImport"),
],
linkerSettings: [
.unsafeFlags(
[systemFrameworkSearchFlag, "/System/Library/PrivateFrameworks/"],
.when(platforms: [.iOS], configuration: .debug)
),
.linkedFramework(
"CoreServices",
.when(platforms: [.iOS], configuration: .debug)
),
]
)
let openSwiftUITestTarget = Target.testTarget(
Expand All @@ -35,7 +47,7 @@ let openSwiftUICompatibilityTestTarget = Target.testTarget(

let package = Package(
name: "OpenSwiftUI",
platforms: [.iOS(.v13), .macOS(.v10_15), .tvOS(.v13), .watchOS(.v6), .visionOS(.v1)],
platforms: [.iOS(.v13), .macOS(.v10_15), .macCatalyst(.v13), .tvOS(.v13), .watchOS(.v6), .visionOS(.v1)],
products: [
.library(name: "OpenSwiftUI", targets: ["OpenSwiftUI"]),
],
Expand All @@ -57,6 +69,8 @@ let package = Package(
name: "OpenSwiftUIShims",
dependencies: [.product(name: "OpenFoundation", package: "OpenFoundation")]
),
.target(name: "CoreServices", path: "PrivateFrameworks/CoreServices"),
.target(name: "UIKitCore", path: "PrivateFrameworks/UIKitCore"),
openSwiftUITarget,
openSwiftUITestTarget,
openSwiftUICompatibilityTestTarget,
Expand Down
1 change: 1 addition & 0 deletions PrivateFrameworks/CoreServices/Dummy.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Empty file
16 changes: 16 additions & 0 deletions PrivateFrameworks/CoreServices/include/LSApplicationWorkspace.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* This header is generated by classdump-dyld 1.0
* on Friday, January 21, 2022 at 6:51:04 AM Pacific Standard Time
* Operating System: Version 15.2.1 (Build 19C63)
* Image Source: /System/Library/Frameworks/CoreServices.framework/CoreServices
* classdump-dyld is licensed under GPLv3, Copyright © 2013-2016 by Elias Limneos.
*/

#import <Foundation/Foundation.h>
#import "_LSOpenConfiguration.h"

@interface LSApplicationWorkspace : NSObject
+(nullable instancetype)defaultWorkspace;
-(void)openURL:(nonnull NSURL *)url configuration:(nonnull _LSOpenConfiguration *)config completionHandler:(void (^ _Nonnull)(BOOL))completion;

@end
19 changes: 19 additions & 0 deletions PrivateFrameworks/CoreServices/include/_LSOpenConfiguration.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* This header is generated by classdump-dyld 1.0
* on Friday, January 21, 2022 at 6:51:04 AM Pacific Standard Time
* Operating System: Version 15.2.1 (Build 19C63)
* Image Source: /System/Library/Frameworks/CoreServices.framework/CoreServices
* classdump-dyld is licensed under GPLv3, Copyright © 2013-2016 by Elias Limneos.
*/

#import <Foundation/Foundation.h>

@interface _LSOpenConfiguration : NSObject
@property(assign, getter=isSensitive,nonatomic) BOOL sensitive;
@property(nonatomic, retain, nullable) id targetConnectionEndpoint;
-(nonnull instancetype)init;
-(BOOL)isSensitive;
-(void)setSensitive:(BOOL)sensitive;
-(void)setTargetConnectionEndpoint:(id)endpoint ;
-(id)targetConnectionEndpoint;
@end
1 change: 1 addition & 0 deletions PrivateFrameworks/UIKitCore/Dummy.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Empty file
13 changes: 13 additions & 0 deletions PrivateFrameworks/UIKitCore/include/UIScene.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* This header is generated by classdump-dyld 1.0
* on Friday, January 21, 2022 at 8:58:16 AM Pacific Standard Time
* Operating System: Version 15.2.1 (Build 19C63)
* Image Source: /System/Library/PrivateFrameworks/UIKitCore.framework/UIKitCore
* classdump-dyld is licensed under GPLv3, Copyright © 2013-2016 by Elias Limneos.
*/

#import <UIKit/UIKit.h>

@interface UIScene (OpenSwiftUI)
-(nullable id)_currentOpenApplicationEndpoint;
@end
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,17 @@
//
// Created by Kyle on 2023/11/26.
// Lastest Version: iOS 15.5
// Status: Complete
// Status: WIP

#if os(iOS)
#if canImport(Darwin)
#if os(iOS) || os(tvOS)
import UIKit
#if DEBUG
private import UIKitCore
private import CoreServices
#endif
#endif
#endif

struct OpenURLActionKey: EnvironmentKey {
static let defaultValue = OpenURLAction(
handler: .system { url, completion in
#if os(iOS)
UIApplication.shared.open(url, options: [:], completionHandler: completion)
#else
fatalError("Unimplemented")
#endif
},
isDefault: false
)
}

extension EnvironmentValues {
/// An action that opens a URL.
Expand Down Expand Up @@ -83,12 +76,62 @@ extension EnvironmentValues {
/// For example, a view that uses the action declared above
/// receives `true` when calling the action, because the
/// handler always returns ``OpenURLAction/Result/handled``.
// MARK: - TODO
public var openURL: OpenURLAction {
get {
self[OpenURLActionKey.self]
}
set {
self[OpenURLActionKey.self] = newValue
}
get { _openURL }
set { _openURL = newValue }
}

var _openURL: OpenURLAction {
get { self[OpenURLActionKey.self] }
set { self[OpenURLActionKey.self] = newValue }
}
// MARK: TODO -

var _openSensitiveURL: OpenURLAction {
get { self[OpenSensitiveURLActionKey.self] }
set { self[OpenSensitiveURLActionKey.self] = newValue }
}
}

struct OpenURLActionKey: EnvironmentKey {
static let defaultValue = OpenURLAction(
handler: .system { url, completion in
#if os(iOS) || os(tvOS) || os(visionOS)
UIApplication.shared.open(url, options: [:], completionHandler: completion)
#elseif os(macOS)
fatalError("Unimplemented")
#else
fatalError("Unimplemented")
#endif
},
isDefault: false
)
}

struct OpenSensitiveURLActionKey: EnvironmentKey {
static let defaultValue = OpenURLAction(
handler: .system { url, completion in
#if DEBUG && os(iOS)
let config = _LSOpenConfiguration()
config.isSensitive = true
let scene = UIApplication.shared.connectedScenes.first
config.targetConnectionEndpoint = scene?._currentOpenApplicationEndpoint
guard let workspace = LSApplicationWorkspace.default() else {
return
}
workspace.open(url, configuration: config, completionHandler: completion)
#else
fatalError("Unimplemented")
#endif
},
isDefault: false
)
}

struct HostingViewOpenURLActionKey: EnvironmentKey {
static let defaultValue = OpenURLAction(
handler: .custom({ .systemAction($0)}, fallback: nil),
isDefault: true
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// EnvironmentValuesOpenURLTests.swift
//
//
// Created by Kyle on 2023/11/28.
//

import XCTest
@testable import OpenSwiftUI

final class EnvironmentValuesOpenURLTests: XCTestCase {
#if DEBUG
func testOpenSensitiveURLActionKey() throws {
let value = OpenSensitiveURLActionKey.defaultValue
value.callAsFunction(URL(string: "https://example.com")!)
}
#endif
}

0 comments on commit 7466ac4

Please sign in to comment.