Skip to content

Commit

Permalink
Fix for issue with compiling in Xcode 13
Browse files Browse the repository at this point in the history
When using the framework in iOS, you now need to configure it with the host (shared) UIApplication instance to enable Background Task management.
  • Loading branch information
dennisbirchdev committed Aug 3, 2021
1 parent a903be2 commit f14bd18
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 49 deletions.
28 changes: 19 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ See the SimpleAnalyticsDemo project for a rudimentary example of using it in an
## Installation
SimpleAnalytics is distributed as a Swift package, which you can load into Xcode projects using the available tools built into Xcode 11.0 and higher.

In the Swift Packages section of the Project configuration panel, inlude a dependency for SimpleAnalytics with the URL:
In the Swift Packages section of the Project configuration panel, include a dependency for SimpleAnalytics with the URL:

`https://github.com/dennisbirch/simple-analytics`

Be sure to add SimpleAnalytics to your target's Frameworks list.
Be sure to add SimpleAnalytics to your target's Frameworks list if it isn't added automatically.

![Adding reference to SimpleAnalytics framework.](images/add-framework.png)

Expand Down Expand Up @@ -55,35 +55,45 @@ A companion open-source project, __SimpleAnalytics Reader__ is available to allo
### Configuration
The SimpleAnalytics package requires one configuration step for full implementation, and offers a couple of other configuration options.

#### Endpoint
In order to submit your app's analytics data from users' devices to a web service where you can access it, you'll need to set the endpoint for the web service. The web service can be any web application that can handle a JSON payload.
#### Endpoint (and shared application on iOS)
In order to submit your app's analytics data from users' devices to a web service where you can access it, you'll need to set the endpoint for the web service. The web service can be any web application that can handle a JSON payload.

To set the endpoint, call the `AppAnalytics.setEndpoint(_ :)` method.
New in version 2.0 and higher: On iOS, you also need to provide a reference to the shared application instance. SimpleAnalytics uses this to request and dispose of a background task when submitting data to your web service.

To set the endpoint on __macOS__, call the `AppAnalytics.setEndpoint(_:)` method.

__Parameters:__

_urlString_: String for your web service URL.

To set the endpoint and shared app on __iOS__, call the `AppAnalytics.setEndpoint(_:, sharedApp:)` method.

__Parameters:__

_urlString_: String for your web service URL.

_sharedApp_: Pass the __UIApplication.shared__ property to this argument.

This call should be made as early as possible in your app's lifecycle.

#### Platform name
To help differentiate data entries, SimpleAnalytics includes a field for the platform for every entry. The framework automatically assigns the values *iOS* and *macOS* for those platforms along with the device type for iOS. But if your app is running in a hybrid environment (e.g. iOS app running on Mac), you can override that assignment with the `AppAnalytics.setPlatform(_ :)` method.
To help differentiate data entries, SimpleAnalytics includes a field for the platform for every entry. The framework automatically assigns the values *iOS* and *macOS* for those platforms along with the device type for iOS. But if your app is running in a hybrid environment (e.g. iOS app running on Mac), you can override that assignment with the `AppAnalytics.setPlatform(_:)` method.

__Parameters:__

_platformName_: String with a platform name.

#### <span id=submission>Maximum count</span>

SimpleAnalytics automatically attempts to submit its contents when the total count of its data items reaches the maximum count value. The default maximum value is 100, but you can change that to fit the needs of your application. To change the maximum count value, call the `AppAnalytics.setMaxItemCount(_ :)` method.
SimpleAnalytics automatically attempts to submit its contents when the total count of its data items reaches the maximum count value. The default maximum value is 100, but you can change that to fit the needs of your application. To change the maximum count value, call the `AppAnalytics.setMaxItemCount(_:)` method.

__Parameters:__

_count_: Int defining the base maximum number of items to accumulate before attempting to submit them to your server.

#### Submit failure increment

When a submit attempt fails, SimpleAnalytics restores the items it was attempting to submit for a subsequent resubmit attempt. It also increments the maximum count value to add a delay before subsequent new events trigger another submission attempt. The default value for this failure increment is 20. You can change that by calling the `AppAnalytics.setSubmitFailureIncrement(_ :)` method.
When a submit attempt fails, SimpleAnalytics restores the items it was attempting to submit for a subsequent resubmit attempt. It also increments the maximum count value to add a delay before subsequent new events trigger another submission attempt. The default value for this failure increment is 20. You can change that by calling the `AppAnalytics.setSubmitFailureIncrement(_:)` method.

__Parameters:__

Expand Down Expand Up @@ -146,7 +156,7 @@ AppAnalytics submits its data in a JSON payload with the following format:
| | _app_version_: A string with the application's version number, as defined in its info.plist file |
| | _platform_: A string with the name of the platform in which the app was running (iOS or macOS), and the device type (iPhone or iPad) for iOS |
| | _system_version_: A string with the operating system version the user is running |
| | _timestamp_ : A string with the date and time that the item was generated, in ISO8661 format for the user's timezone |
| | _timestamp_: A string with the date and time that the item was generated, in ISO8661 format for the user's timezone |
|__counters__ | An array of 'counters', each of which includes: |
| | _name_: The name of the action being counted, as defined by the call to AppAnalytics.countItem(_:) |
| | _count_: The number of times the event was counted during the current analytics collection session |
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="18122" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="B8D-0N-5wS">
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="19150" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="B8D-0N-5wS">
<dependencies>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="18122"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="19150"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
Expand Down Expand Up @@ -857,8 +857,8 @@
</connections>
</button>
<button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="qT0-Zf-Iuz">
<rect key="frame" x="24" y="42" width="106" height="32"/>
<buttonCell key="cell" type="push" title="Repeat Last" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="M8B-Da-GJV">
<rect key="frame" x="7" y="42" width="140" height="32"/>
<buttonCell key="cell" type="push" title="Count Something" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="M8B-Da-GJV">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
Expand All @@ -867,8 +867,8 @@
</connections>
</button>
<button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="jTv-Wd-aRk">
<rect key="frame" x="0.0" y="-7" width="154" height="32"/>
<buttonCell key="cell" type="push" title="Repeat Last (Other)" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="f0H-J3-r2Z">
<rect key="frame" x="-7" y="-7" width="168" height="32"/>
<buttonCell key="cell" type="push" title="Count Something Else" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="f0H-J3-r2Z">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ViewController: NSViewController {
@IBAction func handleButtonPress(_ sender: NSButton) {
let name = sender.title

if name.lowercased().contains("repeat") {
if name.lowercased().contains("count something") {
DemoAnalytics.countItem(name)
} else {
DemoAnalytics.addAnalyticsItem(name, params: ["Demo detail" : randomString()])
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="18122" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="B8D-0N-5wS">
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="19150" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="B8D-0N-5wS">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="18122"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="19150"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
Expand Down Expand Up @@ -859,8 +859,8 @@
</connections>
</button>
<button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="qT0-Zf-Iuz">
<rect key="frame" x="24" y="42" width="106" height="32"/>
<buttonCell key="cell" type="push" title="Repeat Last" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="M8B-Da-GJV">
<rect key="frame" x="7" y="42" width="140" height="32"/>
<buttonCell key="cell" type="push" title="Count Something" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="M8B-Da-GJV">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
Expand All @@ -869,8 +869,8 @@
</connections>
</button>
<button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="jTv-Wd-aRk">
<rect key="frame" x="0.0" y="-7" width="154" height="32"/>
<buttonCell key="cell" type="push" title="Repeat Last (Other)" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="f0H-J3-r2Z">
<rect key="frame" x="-7" y="-7" width="168" height="32"/>
<buttonCell key="cell" type="push" title="Count Something Else" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="f0H-J3-r2Z">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ - (IBAction)handleSoundButton:(NSButton *)sender {
}

- (IBAction)repeatLast:(NSButton *)sender {
[AnalyticsManager addCounter:@"Repeat last"];
[AnalyticsManager addCounter:@"Count Something"];
}

- (IBAction)repeatLastOther:(NSButton *)sender {
[AnalyticsManager addCounter:@"Repeat last (other)"];
[AnalyticsManager addCounter:@"Count Something Else"];
}

- (void)playSound:(NSString *)title {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ButtonTableViewCell: UITableViewCell {
return
}

if name.lowercased().contains("repeat") {
if name.lowercased().contains("count something") {
DemoAnalytics.countItem(name)
} else {
DemoAnalytics.addAnalyticsItem(name, params: ["Demo detail" : randomString()])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import UIKit

class ViewController: UIViewController, UITableViewDataSource {
@IBOutlet private weak var tableView: UITableView!
private var titles = ["Ring Bell", "Horn Blaring", "Foghorn Warning", "Car Revving", "Dog Barking", "Fire Siren", "Oven Timer", "Car Door Slam", "Heavy Breathing", "Soft Sigh", "Thunderclap", "Ocean Wave Crashing", "Repeat Last", "Repeat Last (Other)"]
private var titles = ["Ring Bell", "Horn Blaring", "Foghorn Warning", "Car Revving", "Dog Barking", "Fire Siren", "Oven Timer", "Car Door Slam", "Heavy Breathing", "Soft Sigh", "Thunderclap", "Ocean Wave Crashing", "Count Something", "Count Something Else"]

override func viewDidLoad() {
super.viewDidLoad()
Expand Down
7 changes: 7 additions & 0 deletions SimpleAnalyticsDemo/Shared/AnalyticsManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

import Foundation
import SimpleAnalytics
#if os(iOS)
import UIKit
#endif

/*
This is an app-level manager for SimpleAnalytics to allow avoid calls directly to SimpleAnalytics throughout the rest of the app. If we needed to change to a different Analytics solution, we would then only need to rewrite this file after installing the other analytics solution's dependencies.
Expand All @@ -15,7 +18,11 @@ import SimpleAnalytics

struct DemoAnalytics {
static func initializeEndpoint() {
#if os(macOS)
AppAnalytics.setEndpoint("URL FOR YOUR WEB SERVICE")
#elseif os(iOS)
AppAnalytics.setEndpoint("URL FOR YOUR WEB SERVICE", sharedApp: UIApplication.shared)
#endif
}

static func addAnalyticsItem(_ item: String, params: [String : String]? = nil) {
Expand Down
4 changes: 2 additions & 2 deletions SimpleAnalyticsDemo/Shared/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ struct SecondButtonGroup: View {
PlaySoundButton(title: "Soft Sigh")
PlaySoundButton(title: "Thunderclap")
PlaySoundButton(title: "Ocean Wave Crashing")
CountActionButton(title: "Repeat Last")
CountActionButton(title: "Repeat Last (Other)")
CountActionButton(title: "Count Something")
CountActionButton(title: "Count Something Else")
}
}
}
Expand Down
Loading

0 comments on commit f14bd18

Please sign in to comment.