feat(ios): share extension ile Ratebubble iOS istemcisini ekle ve paylaşım akışını düzelt

This commit is contained in:
2026-03-01 18:07:07 +03:00
parent 8c66fa9b82
commit 5c6a829a4d
20 changed files with 1235 additions and 0 deletions

View File

@@ -0,0 +1,82 @@
import SwiftUI
struct ContentView: View {
@StateObject private var viewModel = MainViewModel()
@Environment(\.scenePhase) private var scenePhase
var body: some View {
NavigationStack {
Form {
Section("Paylaşılan Link") {
TextField("https://www.netflix.com/tr/title/...", text: $viewModel.sharedURL)
.textInputAutocapitalization(.never)
.autocorrectionDisabled(true)
.keyboardType(.URL)
Button("Backend'den Getir") {
Task { await viewModel.fetch() }
}
.disabled(viewModel.isLoading)
}
if viewModel.isLoading {
Section {
HStack {
ProgressView()
Text("Veri alınıyor...")
}
}
}
if let error = viewModel.errorMessage {
Section("Hata") {
Text(error)
.foregroundStyle(.red)
}
}
if let result = viewModel.result {
Section("Sonuç") {
KeyValueRow(key: "Provider", value: result.provider)
KeyValueRow(key: "Title", value: result.title)
KeyValueRow(key: "Year", value: result.year.map(String.init) ?? "-")
KeyValueRow(key: "Type", value: result.type)
KeyValueRow(key: "Age Rating", value: result.ageRating ?? "-")
KeyValueRow(key: "Current Season", value: result.currentSeason.map(String.init) ?? "-")
KeyValueRow(key: "Genres", value: result.genres.joined(separator: ", "))
KeyValueRow(key: "Cast", value: result.cast.joined(separator: ", "))
KeyValueRow(key: "Plot", value: result.plot ?? "-")
}
}
}
.navigationTitle("Ratebubble")
}
.onAppear {
viewModel.consumeSharedURLIfAny()
}
.onOpenURL { _ in
viewModel.consumeSharedURLIfAny()
}
.onChange(of: scenePhase) { phase in
if phase == .active {
viewModel.consumeSharedURLIfAny()
}
}
}
}
private struct KeyValueRow: View {
let key: String
let value: String
var body: some View {
VStack(alignment: .leading, spacing: 4) {
Text(key)
.font(.caption)
.foregroundStyle(.secondary)
Text(value.isEmpty ? "-" : value)
.font(.body)
}
.padding(.vertical, 2)
}
}

View File

@@ -0,0 +1,36 @@
import Foundation
@MainActor
final class MainViewModel: ObservableObject {
@Published var sharedURL: String = ""
@Published var isLoading: Bool = false
@Published var result: GetInfoResponse?
@Published var errorMessage: String?
func consumeSharedURLIfAny() {
guard let incoming = SharedPayloadStore.consumeIncomingURL(), !incoming.isEmpty else {
return
}
sharedURL = incoming
Task { await fetch() }
}
func fetch() async {
guard !sharedURL.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else {
errorMessage = "Paylaşılan URL boş olamaz."
return
}
isLoading = true
errorMessage = nil
result = nil
do {
result = try await APIClient.shared.getInfo(url: sharedURL)
} catch {
errorMessage = error.localizedDescription
}
isLoading = false
}
}

View File

@@ -0,0 +1,10 @@
import SwiftUI
@main
struct RatebubbleApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}

View File

@@ -0,0 +1,5 @@
SLASH = /
API_BASE_URL = http:$(SLASH)$(SLASH)localhost:3000
MOBILE_API_KEY = mobile-dev-key-change-me
APP_GROUP_ID = group.net.wisecolt.ratebubble
APP_URL_SCHEME = ratebubble

View File

@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLSchemes</key>
<array>
<string>$(APP_URL_SCHEME)</string>
</array>
</dict>
</array>
<key>CFBundleVersion</key>
<string>1</string>
<key>API_BASE_URL</key>
<string>$(API_BASE_URL)</string>
<key>MOBILE_API_KEY</key>
<string>$(MOBILE_API_KEY)</string>
<key>UILaunchScreen</key>
<dict/>
</dict>
</plist>

View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.application-groups</key>
<array>
<string>$(APP_GROUP_ID)</string>
</array>
</dict>
</plist>

View File

@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundleDisplayName</key>
<string>Ratebubble Share</string>
<key>CFBundlePackageType</key>
<string>XPC!</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>NSExtension</key>
<dict>
<key>NSExtensionAttributes</key>
<dict>
<key>NSExtensionActivationRule</key>
<string>TRUEPREDICATE</string>
</dict>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.share-services</string>
<key>NSExtensionPrincipalClass</key>
<string>$(PRODUCT_MODULE_NAME).ShareViewController</string>
</dict>
</dict>
</plist>

View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.application-groups</key>
<array>
<string>$(APP_GROUP_ID)</string>
</array>
</dict>
</plist>

View File

@@ -0,0 +1,107 @@
import UIKit
import UniformTypeIdentifiers
final class ShareViewController: UIViewController {
private let statusLabel = UILabel()
override func viewDidLoad() {
super.viewDidLoad()
setupUI()
Task { await handleIncomingShare() }
}
private func setupUI() {
view.backgroundColor = .systemBackground
statusLabel.translatesAutoresizingMaskIntoConstraints = false
statusLabel.textAlignment = .center
statusLabel.numberOfLines = 0
statusLabel.text = "Paylaşılan bağlantı alınıyor..."
view.addSubview(statusLabel)
NSLayoutConstraint.activate([
statusLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
statusLabel.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20),
statusLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor)
])
}
@MainActor
private func updateStatus(_ text: String) {
statusLabel.text = text
}
private func handleIncomingShare() async {
guard let item = extensionContext?.inputItems.first as? NSExtensionItem,
let providers = item.attachments else {
updateStatus("Paylaşılan içerik okunamadı.")
return
}
for provider in providers {
if let extracted = await extractURL(from: provider), isSupportedStreamingURL(extracted) {
SharedPayloadStore.saveIncomingURL(extracted.absoluteString)
updateStatus("Bağlantı alındı, uygulama açılıyor...")
openHostApp()
return
}
}
updateStatus("Geçerli bir Netflix/Prime Video linki bulunamadı.")
}
private func extractURL(from provider: NSItemProvider) async -> URL? {
if provider.hasItemConformingToTypeIdentifier(UTType.url.identifier) {
return await withCheckedContinuation { continuation in
provider.loadItem(forTypeIdentifier: UTType.url.identifier, options: nil) { item, _ in
continuation.resume(returning: item as? URL)
}
}
}
if provider.hasItemConformingToTypeIdentifier(UTType.text.identifier) {
return await withCheckedContinuation { continuation in
provider.loadItem(forTypeIdentifier: UTType.text.identifier, options: nil) { item, _ in
if let raw = item as? String, let url = URL(string: raw.trimmingCharacters(in: .whitespacesAndNewlines)) {
continuation.resume(returning: url)
return
}
continuation.resume(returning: nil)
}
}
}
return nil
}
private func isSupportedStreamingURL(_ url: URL) -> Bool {
let host = url.host?.lowercased() ?? ""
let netflixHosts = ["www.netflix.com", "netflix.com", "www.netflix.com.tr", "netflix.com.tr"]
let primeHosts = ["www.primevideo.com", "primevideo.com", "www.amazon.com", "amazon.com"]
let isNetflix = netflixHosts.contains(host)
let isPrime = primeHosts.contains(host)
guard isNetflix || isPrime else { return false }
let path = url.path.lowercased()
if path.contains("/title/") || path.contains("/watch/") || path.contains("/detail/") {
return true
}
// Some share links can be shortened/redirect style without a canonical path.
return !path.isEmpty && path != "/"
}
private func openHostApp() {
guard let url = URL(string: "\(SharedConfig.appURLScheme)://ingest") else {
extensionContext?.completeRequest(returningItems: nil)
return
}
extensionContext?.open(url) { success in
// If opening succeeded, the system should transition to the host app.
// Completing the extension request immediately can bounce back to the source app.
guard !success else { return }
self.extensionContext?.completeRequest(returningItems: nil)
}
}
}

View File

@@ -0,0 +1,65 @@
import Foundation
enum APIClientError: LocalizedError {
case invalidBaseURL
case invalidResponse
case server(String)
var errorDescription: String? {
switch self {
case .invalidBaseURL:
return "API_BASE_URL geçersiz."
case .invalidResponse:
return "Sunucudan geçerli yanıt alınamadı."
case .server(let message):
return message
}
}
}
final class APIClient {
static let shared = APIClient()
private init() {}
private var baseURL: URL? {
guard let raw = Bundle.main.object(forInfoDictionaryKey: "API_BASE_URL") as? String else {
return nil
}
return URL(string: raw)
}
private var mobileAPIKey: String {
Bundle.main.object(forInfoDictionaryKey: "MOBILE_API_KEY") as? String
?? "mobile-dev-key-change-me"
}
func getInfo(url: String) async throws -> GetInfoResponse {
guard let baseURL else { throw APIClientError.invalidBaseURL }
var request = URLRequest(url: baseURL.appending(path: "/api/getinfo"))
request.httpMethod = "POST"
request.timeoutInterval = 20
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue(mobileAPIKey, forHTTPHeaderField: "X-API-Key")
request.httpBody = try JSONEncoder().encode(GetInfoRequest(url: url))
let (data, response) = try await URLSession.shared.data(for: request)
guard let http = response as? HTTPURLResponse else {
throw APIClientError.invalidResponse
}
let decoder = JSONDecoder()
let envelope = try decoder.decode(APIEnvelope<GetInfoResponse>.self, from: data)
if (200..<300).contains(http.statusCode), envelope.success, let payload = envelope.data {
return payload
}
if let errorMessage = envelope.error?.message {
throw APIClientError.server(errorMessage)
}
throw APIClientError.server("İstek başarısız oldu (\(http.statusCode)).")
}
}

View File

@@ -0,0 +1,29 @@
import Foundation
struct GetInfoRequest: Encodable {
let url: String
}
struct APIErrorPayload: Decodable, Error {
let code: String
let message: String
}
struct APIEnvelope<T: Decodable>: Decodable {
let success: Bool
let data: T?
let error: APIErrorPayload?
}
struct GetInfoResponse: Decodable {
let provider: String
let title: String
let year: Int?
let plot: String?
let ageRating: String?
let type: String
let genres: [String]
let cast: [String]
let backdrop: String?
let currentSeason: Int?
}

View File

@@ -0,0 +1,31 @@
import Foundation
enum SharedConfig {
static var appGroupID: String {
Bundle.main.object(forInfoDictionaryKey: "APP_GROUP_ID") as? String
?? "group.net.wisecolt.ratebubble"
}
static var appURLScheme: String {
Bundle.main.object(forInfoDictionaryKey: "APP_URL_SCHEME") as? String
?? "ratebubble"
}
}
enum SharedKeys {
static let incomingURL = "incoming_shared_url"
}
enum SharedPayloadStore {
static func saveIncomingURL(_ url: String) {
guard let defaults = UserDefaults(suiteName: SharedConfig.appGroupID) else { return }
defaults.set(url, forKey: SharedKeys.incomingURL)
defaults.synchronize()
}
static func consumeIncomingURL() -> String? {
guard let defaults = UserDefaults(suiteName: SharedConfig.appGroupID) else { return nil }
defer { defaults.removeObject(forKey: SharedKeys.incomingURL) }
return defaults.string(forKey: SharedKeys.incomingURL)
}
}