Skip to content

Commit 943676a

Browse files
authored
Fix watchOS can't open link issue (#80)
* Fix watchOS Link issue * Add LSApplicationCategoryType to Info.plist * Add OpenURL+watchOS to add open link support
1 parent 4ef0909 commit 943676a

File tree

8 files changed

+64
-18
lines changed

8 files changed

+64
-18
lines changed

Forumate.xcodeproj/project.pbxproj

+24
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@
208208
275482A12AAD8DA00050E65D /* SDWebImageSwiftUI in Frameworks */ = {isa = PBXBuildFile; productRef = 275482A02AAD8DA00050E65D /* SDWebImageSwiftUI */; };
209209
275482A32AAD918D0050E65D /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 275482A22AAD918D0050E65D /* AppDelegate.swift */; };
210210
275482A42AAD91940050E65D /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 275482A22AAD918D0050E65D /* AppDelegate.swift */; };
211+
275727592B16275500CAAE84 /* OpenURL+watchOS.swift in Sources */ = {isa = PBXBuildFile; fileRef = 275727582B16275500CAAE84 /* OpenURL+watchOS.swift */; };
211212
275E04102A2CE75900A91F5B /* ViewByMenuButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 273AFBCE2A1A73F600DD08F5 /* ViewByMenuButton.swift */; };
212213
276F9F422A2A348100020B1B /* PreviewChecker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 276F9F412A2A348100020B1B /* PreviewChecker.swift */; };
213214
276F9F432A2A348100020B1B /* PreviewChecker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 276F9F412A2A348100020B1B /* PreviewChecker.swift */; };
@@ -549,6 +550,7 @@
549550
274C4CA12A3F84F10018BFA4 /* Forumate.storekit */ = {isa = PBXFileReference; lastKnownFileType = text; path = Forumate.storekit; sourceTree = "<group>"; };
550551
275482982AAD77FB0050E65D /* RecommendCommunity.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RecommendCommunity.swift; sourceTree = "<group>"; };
551552
275482A22AAD918D0050E65D /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
553+
275727582B16275500CAAE84 /* OpenURL+watchOS.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "OpenURL+watchOS.swift"; sourceTree = "<group>"; };
552554
276F9F412A2A348100020B1B /* PreviewChecker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreviewChecker.swift; sourceTree = "<group>"; };
553555
276F9F462A2A606600020B1B /* PostView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostView.swift; sourceTree = "<group>"; };
554556
277546D72A19DE3F000FDAA0 /* AppInfo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppInfo.swift; sourceTree = "<group>"; };
@@ -872,6 +874,8 @@
872874
children = (
873875
2722B94E2B0C7C1300D24617 /* ForumateApp.swift */,
874876
2722B9912B0C7D4200D24617 /* ContentView.swift */,
877+
275727582B16275500CAAE84 /* OpenURL+watchOS.swift */,
878+
2754F6412B1359B300F4BF2E /* View */,
875879
279283BD2B0D287800094683 /* Assets.xcassets */,
876880
279284852B0D2DEB00094683 /* InfoPlist.xcstrings */,
877881
);
@@ -921,6 +925,21 @@
921925
path = View;
922926
sourceTree = "<group>";
923927
};
928+
2754F6412B1359B300F4BF2E /* View */ = {
929+
isa = PBXGroup;
930+
children = (
931+
2754F6422B1359BE00F4BF2E /* Tab */,
932+
);
933+
path = View;
934+
sourceTree = "<group>";
935+
};
936+
2754F6422B1359BE00F4BF2E /* Tab */ = {
937+
isa = PBXGroup;
938+
children = (
939+
);
940+
path = Tab;
941+
sourceTree = "<group>";
942+
};
924943
276F9F452A2A604D00020B1B /* Post */ = {
925944
isa = PBXGroup;
926945
children = (
@@ -1848,6 +1867,7 @@
18481867
273AFC212A1A967D00DD08F5 /* CategoryText.swift in Sources */,
18491868
273AFC1C2A1A967D00DD08F5 /* CommunityList.swift in Sources */,
18501869
273AFC132A1A967300DD08F5 /* Community.swift in Sources */,
1870+
275727592B16275500CAAE84 /* OpenURL+watchOS.swift in Sources */,
18511871
276F9F482A2A606600020B1B /* PostView.swift in Sources */,
18521872
2722B9932B0C7DEF00D24617 /* ContentView.swift in Sources */,
18531873
2779C07B2A34978700CE4DF1 /* PreviewContainer.swift in Sources */,
@@ -2067,6 +2087,7 @@
20672087
GCC_C_LANGUAGE_STANDARD = gnu17;
20682088
GENERATE_INFOPLIST_FILE = YES;
20692089
INFOPLIST_FILE = Shared/Other/Info.plist;
2090+
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.social-networking";
20702091
INFOPLIST_KEY_UILaunchScreen_Generation = YES;
20712092
INFOPLIST_KEY_UIUserInterfaceStyle = Automatic;
20722093
LD_RUNPATH_SEARCH_PATHS = (
@@ -2095,6 +2116,7 @@
20952116
GCC_C_LANGUAGE_STANDARD = gnu17;
20962117
GENERATE_INFOPLIST_FILE = YES;
20972118
INFOPLIST_FILE = Shared/Other/Info.plist;
2119+
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.social-networking";
20982120
INFOPLIST_KEY_UILaunchScreen_Generation = YES;
20992121
INFOPLIST_KEY_UIUserInterfaceStyle = Automatic;
21002122
LD_RUNPATH_SEARCH_PATHS = (
@@ -2126,6 +2148,7 @@
21262148
GCC_C_LANGUAGE_STANDARD = gnu17;
21272149
GENERATE_INFOPLIST_FILE = YES;
21282150
INFOPLIST_FILE = Shared/Other/Info.plist;
2151+
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.social-networking";
21292152
INFOPLIST_KEY_NSHumanReadableCopyright = "Copyright ©️ 2023 YEXULEI. All rights reserved";
21302153
LD_RUNPATH_SEARCH_PATHS = (
21312154
"$(inherited)",
@@ -2154,6 +2177,7 @@
21542177
GCC_C_LANGUAGE_STANDARD = gnu17;
21552178
GENERATE_INFOPLIST_FILE = YES;
21562179
INFOPLIST_FILE = Shared/Other/Info.plist;
2180+
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.social-networking";
21572181
INFOPLIST_KEY_NSHumanReadableCopyright = "Copyright ©️ 2023 YEXULEI. All rights reserved";
21582182
LD_RUNPATH_SEARCH_PATHS = (
21592183
"$(inherited)",

Forumate/ForumateWatch/ForumateApp.swift

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ struct ForumateApp: App {
3131
.environmentObject(appState)
3232
.environment(plusManager)
3333
.modelContainer(container)
34+
.environment(\.openURL, .authenticationSessionAction)
3435
}
3536
}
3637
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// OpenURL+watchOS.swift
3+
// ForumateWatch
4+
//
5+
// Created by Kyle on 2023/11/28.
6+
//
7+
8+
import AuthenticationServices
9+
import SwiftUI
10+
11+
extension OpenURLAction {
12+
static let authenticationSessionAction = OpenURLAction {
13+
let session = ASWebAuthenticationSession(
14+
url: $0,
15+
callbackURLScheme: nil
16+
) { _, _ in
17+
}
18+
session.prefersEphemeralWebBrowserSession = true
19+
session.start()
20+
return .handled
21+
}
22+
}

Shared/View/Tab/Topics/Community/View/CommunityDetail/CommunityDetail.swift

+5-7
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,10 @@ struct CommunityDetail: View {
5050
}
5151
}
5252

53-
struct CommunityView_Previews: PreviewProvider {
54-
static var previews: some View {
55-
NavigationStack {
56-
CommunityDetail(community: .swift)
57-
}
58-
.environmentObject(AppState())
59-
.environmentObject(TopicsTabState())
53+
#Preview {
54+
NavigationStack {
55+
CommunityDetail(community: .swift)
6056
}
57+
.environmentObject(AppState())
58+
.environmentObject(TopicsTabState())
6159
}

Shared/View/Tab/Topics/Community/View/CommunityDetail/MenuButton/AccountMenuButton.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ struct AccountMenuButton: View {
1818
@Environment(\.openURL) private var openURL
1919
@EnvironmentObject private var state: CommunityDetailState
2020

21-
@StateObject var viewModel = SignInViewModel()
21+
@State private var viewModel = SignInViewModel()
2222

2323
@AppStorage("forumate_client_id")
2424
private var clientID = UUID().uuidString

Shared/View/Tab/Topics/Community/View/CommunityDetail/MenuButton/ViewByMenuButton.swift

+5-6
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,8 @@ struct ViewByMenuButton: View {
4343
}
4444
}
4545

46-
// Compiler error for Community swift undefined in Xcdoe 15 beta 2
47-
//#Preview {
48-
// ViewByMenuButton()
49-
// .buttonStyle(.borderedProminent)
50-
// .environmentObject(CommunityDetailState(community: .swift))
51-
//}
46+
#Preview {
47+
ViewByMenuButton()
48+
.buttonStyle(.borderedProminent)
49+
.environmentObject(CommunityDetailState(community: .swift))
50+
}

Shared/View/Tab/Topics/Community/View/CommunityDetail/ViewModel/SignInViewModel.swift

+5-3
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@ import Combine
1010
import DiscourseKit
1111
import os.log
1212

13-
class SignInViewModel: NSObject, ObservableObject {
13+
@Observable
14+
class SignInViewModel: NSObject {
1415
private static let logger = Logger(subsystem: Logger.subsystem, category: "Account")
1516

1617
// TODO: Support iOS, visionOS and macOS direct sign-in first. watchOS and tvOS support should via nearby iOS device.
18+
// watchOS: WatchConnectivity
1719
func signIn(request url: URL) {
1820
// https://meta.discourse.org/t/user-api-keys-specification/48536
1921
// Use https://sitename.com/user-api-key/new here
20-
#if os(iOS) || os(macOS) || os(visionOS)
22+
#if os(iOS) || os(macOS) || os(visionOS) || os(watchOS)
2123
let authSession = ASWebAuthenticationSession(
2224
url: url,
2325
callbackURLScheme: APIKeyManager.callbackSchema
@@ -35,7 +37,7 @@ class SignInViewModel: NSObject, ObservableObject {
3537
_ = try? processResponseURL(url: url)
3638
}
3739
}
38-
authSession.presentationContextProvider = self
40+
// authSession.presentationContextProvider = self
3941
// TODO: Add a setting item to decide this
4042
// authSession.prefersEphemeralWebBrowserSession
4143
authSession.start()

Shared/ViewModel/PlusManager.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ enum PlusError: LocalizedError {
9595
var errorDescription: String? {
9696
switch self {
9797
case .plusOnlyFeature:
98-
NSLocalizedString("You need an active Forumate+ membership to unlock this feature.", comment: "Plus only feature Description")
98+
String(localized: "You need an active Forumate+ membership to unlock this feature.", comment: "Plus only feature Description")
9999
}
100100
}
101101
}

0 commit comments

Comments
 (0)