From 4436b56cc752455364c4502f8560ca2136d4d651 Mon Sep 17 00:00:00 2001 From: Hongyan Jiang Date: Tue, 29 Aug 2023 09:26:20 -0700 Subject: [PATCH] fix InstanaExample issues on iOS 12 --- .../project.pbxproj | 17 +++- .../EventListViewController.swift | 89 ++++++++++++++----- .../ImageViewController.swift | 27 ++++-- .../JSONViewController.swift | 36 ++++++-- .../WebViewController.swift | 2 +- .../InstanaPersistableQueueTests.swift | 1 - 6 files changed, 130 insertions(+), 42 deletions(-) diff --git a/Dev/InstanaAgentExample.xcodeproj/project.pbxproj b/Dev/InstanaAgentExample.xcodeproj/project.pbxproj index f2f1d6c..26b6216 100644 --- a/Dev/InstanaAgentExample.xcodeproj/project.pbxproj +++ b/Dev/InstanaAgentExample.xcodeproj/project.pbxproj @@ -3,7 +3,7 @@ archiveVersion = 1; classes = { }; - objectVersion = 52; + objectVersion = 54; objects = { /* Begin PBXBuildFile section */ @@ -29,6 +29,7 @@ 72976A9A2681C45800A284F6 /* Webserver.swift in Sources */ = {isa = PBXBuildFile; fileRef = 72976A992681C45800A284F6 /* Webserver.swift */; }; 729C88B5270DE98200492486 /* AFNetworking in Frameworks */ = {isa = PBXBuildFile; productRef = 729C88B4270DE98200492486 /* AFNetworking */; }; 729C88B8270DF13700492486 /* Alamofire in Frameworks */ = {isa = PBXBuildFile; productRef = 729C88B7270DF13700492486 /* Alamofire */; }; + A4EFD6A12A9983FB00B9A9A3 /* WebKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A4EFD6A02A9983FB00B9A9A3 /* WebKit.framework */; }; CE322E492A6E3CEF00414F37 /* TopRatedViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE322E482A6E3CEF00414F37 /* TopRatedViewController.swift */; }; /* End PBXBuildFile section */ @@ -69,6 +70,7 @@ 727AF73A263AA0FA00B9C59B /* EventListViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EventListViewController.swift; sourceTree = ""; }; 72976A992681C45800A284F6 /* Webserver.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Webserver.swift; path = "../../Tests/InstanaAgentTests/Test helpers/Webserver.swift"; sourceTree = ""; }; 72FBE8A7263AACDB00CF2CDE /* InstanaAgentExample.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = InstanaAgentExample.entitlements; sourceTree = ""; }; + A4EFD6A02A9983FB00B9A9A3 /* WebKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = WebKit.framework; path = Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/System/iOSSupport/System/Library/PrivateFrameworks/WebKit.framework; sourceTree = DEVELOPER_DIR; }; CE322E482A6E3CEF00414F37 /* TopRatedViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TopRatedViewController.swift; sourceTree = ""; }; /* End PBXFileReference section */ @@ -85,6 +87,7 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( + A4EFD6A12A9983FB00B9A9A3 /* WebKit.framework in Frameworks */, 729C88B8270DF13700492486 /* Alamofire in Frameworks */, 727AF748263AA16A00B9C59B /* InstanaAgent in Frameworks */, 729C88B5270DE98200492486 /* AFNetworking in Frameworks */, @@ -160,6 +163,7 @@ 727AF710263A9FC800B9C59B /* Frameworks */ = { isa = PBXGroup; children = ( + A4EFD6A02A9983FB00B9A9A3 /* WebKit.framework */, ); name = Frameworks; sourceTree = ""; @@ -355,6 +359,7 @@ }; 72FBE8AC263AAD0900CF2CDE /* Import secure variables */ = { isa = PBXShellScriptBuildPhase; + alwaysOutOfDate = 1; buildActionMask = 2147483647; files = ( ); @@ -373,6 +378,7 @@ }; 72FBE8AD263AAD2000CF2CDE /* SwiftLint & SwiftFormat */ = { isa = PBXShellScriptBuildPhase; + alwaysOutOfDate = 1; buildActionMask = 2147483647; files = ( ); @@ -391,6 +397,7 @@ }; 72FBE8AE263AAD4C00CF2CDE /* Reset */ = { isa = PBXShellScriptBuildPhase; + alwaysOutOfDate = 1; buildActionMask = 2147483647; files = ( ); @@ -650,8 +657,9 @@ ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; CODE_SIGN_ENTITLEMENTS = InstanaAgentExample/InstanaAgentExample.entitlements; CODE_SIGN_STYLE = Automatic; - DEVELOPMENT_TEAM = D5PCJAP39R; + DEVELOPMENT_TEAM = 6K4C9F3Z9X; INFOPLIST_FILE = InstanaAgentExample/Info.plist; + IPHONEOS_DEPLOYMENT_TARGET = 12.4; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/Frameworks", @@ -671,8 +679,9 @@ ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; CODE_SIGN_ENTITLEMENTS = InstanaAgentExample/InstanaAgentExample.entitlements; CODE_SIGN_STYLE = Automatic; - DEVELOPMENT_TEAM = D5PCJAP39R; + DEVELOPMENT_TEAM = 6K4C9F3Z9X; INFOPLIST_FILE = InstanaAgentExample/Info.plist; + IPHONEOS_DEPLOYMENT_TARGET = 12.4; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/Frameworks", @@ -690,6 +699,7 @@ buildSettings = { CODE_SIGN_STYLE = Automatic; INFOPLIST_FILE = iOSAgentUITests/Info.plist; + IPHONEOS_DEPLOYMENT_TARGET = 12.4; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/Frameworks", @@ -708,6 +718,7 @@ buildSettings = { CODE_SIGN_STYLE = Automatic; INFOPLIST_FILE = iOSAgentUITests/Info.plist; + IPHONEOS_DEPLOYMENT_TARGET = 12.4; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/Frameworks", diff --git a/Dev/InstanaAgentExample/EventListViewController.swift b/Dev/InstanaAgentExample/EventListViewController.swift index a96213c..58a6491 100644 --- a/Dev/InstanaAgentExample/EventListViewController.swift +++ b/Dev/InstanaAgentExample/EventListViewController.swift @@ -28,9 +28,10 @@ class EventListViewController: UITableViewController { override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) - - rateButton.setImage(UIImage(systemName: "star"), for: .normal) - rateButton.setImage(UIImage(systemName: "star.fill"), for: .selected) + if #available(iOS 13.0, *) { + rateButton.setImage(UIImage(systemName: "star"), for: .normal) + rateButton.setImage(UIImage(systemName: "star.fill"), for: .selected) + } accessoryView = rateButton } @@ -47,7 +48,9 @@ class EventListViewController: UITableViewController { + @available(iOS 13.0, *) private var publisher: AnyCancellable? + lazy var session = { URLSession(configuration: URLSessionConfiguration.default) }() override func viewDidLoad() { @@ -70,16 +73,36 @@ class EventListViewController: UITableViewController { func loadJSON() { let url = URL(string: "https://api.mygigs.tapwork.de/eventsnearby?radius=80.0&latitude=52.52&longitude=13.410&apitoken=xQ3vdfKVIF")! - publisher = session.dataTaskPublisher(for: url) - .receive(on: RunLoop.main) - .map { $0.data } - .decode(type: DataSource.self, decoder: JSONDecoder()) - .sink(receiveCompletion: { _ in }, receiveValue: {[weak self] model in - guard let self = self else { return } - dataSource = model - self.tableView.reloadData() - self.refreshControl?.endRefreshing() - }) + if #available(iOS 13.0, *) { + publisher = session.dataTaskPublisher(for: url) + .receive(on: RunLoop.main) + .map { $0.data } + .decode(type: DataSource.self, decoder: JSONDecoder()) + .sink(receiveCompletion: { _ in }, receiveValue: {[weak self] model in + guard let self = self else { return } + dataSource = model + self.tableView.reloadData() + self.refreshControl?.endRefreshing() + }) + } else { + let task = session.dataTask(with: url) { data, response, error in + guard let data = data else { + print("Empty data. error: ", error ?? "nil") + return + } + do { + let model: DataSource? = try JSONDecoder().decode(DataSource.self, from: data) + DispatchQueue.main.async { + dataSource = model + self.tableView.reloadData() + self.refreshControl?.endRefreshing() + } + } catch { + print("Error ", error) + } + } + task.resume() + } } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { @@ -128,6 +151,8 @@ class DetailViewController: UIViewController { let label = UILabel() let imageView = UIImageView() let event: DataSource.Event + + @available(iOS 13.0, *) private var publisher: AnyCancellable? init(event: DataSource.Event) { @@ -142,17 +167,21 @@ class DetailViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() - view.backgroundColor = UIColor.systemBackground + if #available(iOS 13.0, *) { + view.backgroundColor = UIColor.systemBackground + } view.addSubview(imageView) imageView.translatesAutoresizingMaskIntoConstraints = false imageView.contentMode = .scaleAspectFill imageView.addSubview(label) - label.backgroundColor = UIColor.systemBackground + if #available(iOS 13.0, *) { + label.backgroundColor = UIColor.systemBackground + label.textColor = UIColor.secondaryLabel + } label.text = event.name label.numberOfLines = 0 - label.textColor = UIColor.secondaryLabel label.textAlignment = .center label.translatesAutoresizingMaskIntoConstraints = false @@ -178,12 +207,26 @@ class DetailViewController: UIViewController { guard let url = URL(string: event.image_url ?? "") else { return } - publisher = URLSession.shared.dataTaskPublisher(for: url) - .receive(on: RunLoop.main) - .map { UIImage(data: $0.data) } - .sink(receiveCompletion: { _ in }, receiveValue: {[weak self] image in - guard let self = self else { return } - self.imageView.image = image - }) + if #available(iOS 13.0, *) { + publisher = URLSession.shared.dataTaskPublisher(for: url) + .receive(on: RunLoop.main) + .map { UIImage(data: $0.data) } + .sink(receiveCompletion: { _ in }, receiveValue: {[weak self] image in + guard let self = self else { return } + self.imageView.image = image + }) + } else { + let task = URLSession.shared.dataTask(with: url) { data, response, error in + guard let data = data else { + print("Empty data. error: ", error ?? "nil") + return + } + let image = UIImage(data: data) + DispatchQueue.main.async { + self.imageView.image = image + } + } + task.resume() + } } } diff --git a/Dev/InstanaAgentExample/ImageViewController.swift b/Dev/InstanaAgentExample/ImageViewController.swift index 3bda59c..a4847e5 100644 --- a/Dev/InstanaAgentExample/ImageViewController.swift +++ b/Dev/InstanaAgentExample/ImageViewController.swift @@ -15,7 +15,10 @@ class ImageViewViewController: UIViewController { lazy var imageView = UIImageView() lazy var session = { URLSession(configuration: URLSessionConfiguration.default) }() + + @available(iOS 13.0, *) private var publisher: AnyCancellable? + private var timer: Timer? override func viewDidLoad() { @@ -55,11 +58,25 @@ class ImageViewViewController: UIViewController { } func downloadViaCombine(_ url: URL) { - publisher = session.dataTaskPublisher(for: url) - .receive(on: RunLoop.main) - .map { UIImage(data: $0.data) } - .replaceError(with: UIImage()) - .assign(to: \.image, on: self.imageView) + if #available(iOS 13.0, *) { + publisher = session.dataTaskPublisher(for: url) + .receive(on: RunLoop.main) + .map { UIImage(data: $0.data) } + .replaceError(with: UIImage()) + .assign(to: \.image, on: self.imageView) + } else { + let task = session.dataTask(with: url) { data, response, error in + guard let data = data else { + print("Empty data. error: ", error ?? "nil") + return + } + let image = UIImage(data: data) + DispatchQueue.main.async { + self.imageView.image = image + } + } + task.resume() + } } func downloadWViaAFN(_ url: URL) { diff --git a/Dev/InstanaAgentExample/JSONViewController.swift b/Dev/InstanaAgentExample/JSONViewController.swift index a8a6274..7f6d740 100644 --- a/Dev/InstanaAgentExample/JSONViewController.swift +++ b/Dev/InstanaAgentExample/JSONViewController.swift @@ -13,12 +13,16 @@ class JSONViewController: UIViewController { @IBOutlet var searchTextField: UITextField! @IBOutlet var textView: UITextView! + + @available(iOS 13.0, *) private var publisher: AnyCancellable? + lazy var session = { URLSession(configuration: URLSessionConfiguration.default) }() override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) Instana.setView(name: "JSONView") + searchTextField.text = "https://www.ibm.com/de-de" } @IBAction func loadJSON() { @@ -27,16 +31,30 @@ class JSONViewController: UIViewController { showAlert(message: "URL is invalid") return } - publisher = session.dataTaskPublisher(for: url) - .receive(on: RunLoop.main) - .map { String(data: $0.data, encoding: .ascii) } - .sink(receiveCompletion: { complete in - if let errorMessage = complete.localizedError { - self.showAlert(message: errorMessage) + if #available(iOS 13.0, *) { + publisher = session.dataTaskPublisher(for: url) + .receive(on: RunLoop.main) + .map { String(data: $0.data, encoding: .ascii) } + .sink(receiveCompletion: { complete in + if let errorMessage = complete.localizedError { + self.showAlert(message: errorMessage) + } + }, receiveValue: {[weak self] result in + self?.textView.text = result + }) + } else { + let task = session.dataTask(with: url) { data, response, error in + guard let data = data else { + print("Empty data. error: ", error ?? "nil") + return } - }, receiveValue: {[weak self] result in - self?.textView.text = result - }) + let str = String(data: data, encoding: .utf8) + DispatchQueue.main.async { + self.textView.text = str + } + } + task.resume() + } } func showAlert(message: String) { diff --git a/Dev/InstanaAgentExample/WebViewController.swift b/Dev/InstanaAgentExample/WebViewController.swift index a5ea2f2..10e5ff7 100644 --- a/Dev/InstanaAgentExample/WebViewController.swift +++ b/Dev/InstanaAgentExample/WebViewController.swift @@ -16,7 +16,7 @@ class WebViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() webView.navigationDelegate = self - load(url: URL(string: "https://www.instana.com")!) + load(url: URL(string: "https://www.ibm.com/cn-zh")!) } func load(url: URL) { diff --git a/Tests/InstanaAgentTests/Utils_Tests/InstanaPersistableQueueTests.swift b/Tests/InstanaAgentTests/Utils_Tests/InstanaPersistableQueueTests.swift index 7e1371a..fef3352 100644 --- a/Tests/InstanaAgentTests/Utils_Tests/InstanaPersistableQueueTests.swift +++ b/Tests/InstanaAgentTests/Utils_Tests/InstanaPersistableQueueTests.swift @@ -48,7 +48,6 @@ class InstanaPersistableQueueTests: InstanaTestCase { // Given let sessionID = UUID() let id = UUID() - let userSessionID = UUID() let beacon1 = CoreBeacon.createDefault(viewName: "View_1", key: "Key_1", timestamp: 0, sid: sessionID, usi: session.usi, id: id, mobileFeatures: "c") let beacon2 = CoreBeacon.createDefault(viewName: "View_2", key: "Key_2", timestamp: 0,