Skip to content

Commit 755b480

Browse files
committed
add SPM support for iOS plugin
1 parent cf7435d commit 755b480

26 files changed

+1821
-7
lines changed

.gitignore

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,4 @@ dlcov.log
2525
.idea/
2626

2727
# Swift Package Manager
28-
**/.build/
29-
**/.swiftpm/
30-
**/Package.resolved
31-
**/.lock
32-
**/workspace-state.json
28+
!.gitkeep

.pubignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,14 @@ dlcov.log
3131
pubspec.lock
3232
.flutter-plugins-dependencies
3333
flutter_export_environment.sh
34+
analysis_options.yaml
35+
36+
# Misc
37+
CODEOWNERS
38+
CONTRIBUTING.md
39+
MIGRATION_GUIDE.md
40+
41+
# Examples
42+
example
43+
example_pod
44+
example_spm

analysis_options.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
analyzer:
22
exclude:
33
- test/dlcov_references_test.dart
4+
- example/**
5+
- example_pod/**
6+
- example_spm/**

ios/Assets/.gitkeep

Whitespace-only changes.

ios/onesignal_flutter.podspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ Pod::Spec.new do |s|
1010
s.license = { :file => '../LICENSE' }
1111
s.author = { 'Brad Hesse' => '[email protected]', 'Josh Kasten' => '[email protected]' }
1212
s.source = { :path => '.' }
13-
s.source_files = 'Classes/**/*'
14-
s.public_header_files = 'Classes/**/*.h'
13+
s.source_files = 'onesignal_flutter/Sources/onesignal_flutter/**/*.{h,m}'
14+
s.public_header_files = 'onesignal_flutter/Sources/onesignal_flutter/include/**/*.h'
1515
s.dependency 'Flutter'
1616
s.dependency 'OneSignalXCFramework', '5.2.15'
1717
s.ios.deployment_target = '11.0'
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// swift-tools-version: 5.9
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "onesignal_flutter",
8+
platforms: [
9+
.iOS("12.0")
10+
],
11+
products: [
12+
.library(name: "onesignal-flutter", targets: ["onesignal_flutter"])
13+
],
14+
dependencies: [
15+
.package(url: "https://github.com/OneSignal/OneSignal-XCFramework", exact: "5.2.15")
16+
],
17+
targets: [
18+
.target(
19+
name: "onesignal_flutter",
20+
dependencies: [
21+
.product(name: "OneSignalFramework", package: "OneSignal-XCFramework"),
22+
.product(name: "OneSignalInAppMessages", package: "OneSignal-XCFramework"),
23+
.product(name: "OneSignalLocation", package: "OneSignal-XCFramework")
24+
],
25+
path: "Sources/onesignal_flutter"
26+
)
27+
]
28+
)
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
/**
2+
* Modified MIT License
3+
*
4+
* Copyright 2017 OneSignal
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* 1. The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* 2. All copies of substantial portions of the Software may only be used in
17+
* connection with services provided by OneSignal.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
#import "./include/onesignal_flutter/OSFlutterCategories.h"
29+
30+
/*
31+
The OneSignal iOS SDK implements similar methods (`toDictionary`)
32+
However we decided to implement custom `toJson` methods for several
33+
of these objects to add more properties.
34+
35+
TODO: Update the native iOS SDK to add these details
36+
(ie. `templateId` is missing from OSNotificationPayload's `toDictionary`
37+
method in the native SDK) and remove them from here.
38+
*/
39+
40+
@implementation OSNotification (Flutter)
41+
- (NSDictionary *)toJson {
42+
NSMutableDictionary *json = [NSMutableDictionary new];
43+
44+
json[@"contentAvailable"] = @(self.contentAvailable);
45+
json[@"mutableContent"] = @(self.mutableContent);
46+
47+
if (self.rawPayload) {
48+
NSError *jsonError;
49+
NSData *data =
50+
[NSJSONSerialization dataWithJSONObject:self.rawPayload
51+
options:NSJSONWritingPrettyPrinted
52+
error:&jsonError];
53+
54+
if (!jsonError) {
55+
NSString *rawPayloadString =
56+
[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
57+
json[@"rawPayload"] = rawPayloadString;
58+
}
59+
}
60+
61+
if (self.notificationId)
62+
json[@"notificationId"] = self.notificationId;
63+
if (self.templateName)
64+
json[@"templateName"] = self.templateName;
65+
if (self.templateId)
66+
json[@"templateId"] = self.templateId;
67+
if (self.badge)
68+
json[@"badge"] = @(self.badge);
69+
if (self.badgeIncrement)
70+
json[@"badgeIncrement"] = @(self.badgeIncrement);
71+
if (self.sound)
72+
json[@"sound"] = self.sound;
73+
if (self.title)
74+
json[@"title"] = self.title;
75+
if (self.subtitle)
76+
json[@"subtitle"] = self.subtitle;
77+
if (self.body)
78+
json[@"body"] = self.body;
79+
if (self.launchURL)
80+
json[@"launchUrl"] = self.launchURL;
81+
if (self.additionalData)
82+
json[@"additionalData"] = self.additionalData;
83+
if (self.attachments)
84+
json[@"attachments"] = self.attachments;
85+
if (self.actionButtons)
86+
json[@"buttons"] = self.actionButtons;
87+
if (self.category)
88+
json[@"category"] = self.category;
89+
90+
return json;
91+
}
92+
@end
93+
94+
@implementation OSNotificationClickEvent (Flutter)
95+
- (NSDictionary *)toJson {
96+
NSMutableDictionary *json = [NSMutableDictionary new];
97+
98+
json[@"notification"] = self.notification.toJson;
99+
json[@"result"] = self.result.toJson;
100+
101+
return json;
102+
}
103+
@end
104+
105+
@implementation OSNotificationClickResult (Flutter)
106+
- (NSDictionary *)toJson {
107+
NSMutableDictionary *json = [NSMutableDictionary new];
108+
109+
json[@"action_id"] = self.actionId;
110+
json[@"url"] = self.url;
111+
112+
return json;
113+
}
114+
@end
115+
116+
@implementation OSNotificationWillDisplayEvent (Flutter)
117+
- (NSDictionary *)toJson {
118+
NSMutableDictionary *json = [NSMutableDictionary new];
119+
120+
json[@"notification"] = self.notification.toJson;
121+
122+
return json;
123+
}
124+
@end
125+
126+
@implementation OSInAppMessageWillDisplayEvent (Flutter)
127+
- (NSDictionary *)toJson {
128+
NSMutableDictionary *json = [NSMutableDictionary new];
129+
130+
json[@"message"] = self.message.toJson;
131+
132+
return json;
133+
}
134+
@end
135+
136+
@implementation OSInAppMessageDidDisplayEvent (Flutter)
137+
- (NSDictionary *)toJson {
138+
NSMutableDictionary *json = [NSMutableDictionary new];
139+
140+
json[@"message"] = self.message.toJson;
141+
142+
return json;
143+
}
144+
@end
145+
146+
@implementation OSInAppMessageWillDismissEvent (Flutter)
147+
- (NSDictionary *)toJson {
148+
NSMutableDictionary *json = [NSMutableDictionary new];
149+
150+
json[@"message"] = self.message.toJson;
151+
152+
return json;
153+
}
154+
@end
155+
156+
@implementation OSInAppMessageDidDismissEvent (Flutter)
157+
- (NSDictionary *)toJson {
158+
NSMutableDictionary *json = [NSMutableDictionary new];
159+
160+
json[@"message"] = self.message.toJson;
161+
162+
return json;
163+
}
164+
@end
165+
166+
@implementation OSInAppMessageClickEvent (Flutter)
167+
- (NSDictionary *)toJson {
168+
NSMutableDictionary *json = [NSMutableDictionary new];
169+
170+
json[@"message"] = self.message.toJson;
171+
json[@"result"] = self.result.toJson;
172+
173+
return json;
174+
}
175+
@end
176+
177+
@implementation OSInAppMessageClickResult (Flutter)
178+
- (NSDictionary *)toJson {
179+
NSMutableDictionary *json = [NSMutableDictionary new];
180+
181+
json[@"action_id"] = self.actionId;
182+
json[@"url"] = self.url;
183+
json[@"closing_message"] = @(self.closingMessage);
184+
185+
return json;
186+
}
187+
@end
188+
189+
@implementation OSInAppMessage (Flutter)
190+
- (NSDictionary *)toJson {
191+
NSMutableDictionary *json = [NSMutableDictionary new];
192+
193+
json[@"message_id"] = self.messageId;
194+
195+
return json;
196+
}
197+
@end
198+
199+
@implementation NSError (Flutter)
200+
- (FlutterError *)flutterError {
201+
return [FlutterError
202+
errorWithCode:[NSString stringWithFormat:@"%i", (int)self.code]
203+
message:self.localizedDescription
204+
details:nil];
205+
}
206+
@end
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* Modified MIT License
3+
*
4+
* Copyright 2023 OneSignal
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* 1. The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* 2. All copies of substantial portions of the Software may only be used in
17+
* connection with services provided by OneSignal.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
#import "./include/onesignal_flutter/OSFlutterDebug.h"
29+
#import "./include/onesignal_flutter/OSFlutterCategories.h"
30+
#import <OneSignalFramework/OneSignalFramework.h>
31+
32+
@implementation OSFlutterDebug
33+
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
34+
OSFlutterDebug *instance = [OSFlutterDebug new];
35+
36+
instance.channel =
37+
[FlutterMethodChannel methodChannelWithName:@"OneSignal#debug"
38+
binaryMessenger:[registrar messenger]];
39+
40+
[registrar addMethodCallDelegate:instance channel:instance.channel];
41+
}
42+
43+
- (void)handleMethodCall:(FlutterMethodCall *)call
44+
result:(FlutterResult)result {
45+
if ([@"OneSignal#setLogLevel" isEqualToString:call.method])
46+
[self setLogLevel:call withResult:result];
47+
else if ([@"OneSignal#setAlertLevel" isEqualToString:call.method])
48+
[self setAlertLevel:call withResult:result];
49+
else
50+
result(FlutterMethodNotImplemented);
51+
}
52+
53+
- (void)setLogLevel:(FlutterMethodCall *)call withResult:(FlutterResult)result {
54+
ONE_S_LOG_LEVEL logLevel =
55+
(ONE_S_LOG_LEVEL)[call.arguments[@"logLevel"] intValue];
56+
[OneSignal.Debug setLogLevel:logLevel];
57+
result(nil);
58+
}
59+
60+
- (void)setAlertLevel:(FlutterMethodCall *)call
61+
withResult:(FlutterResult)result {
62+
ONE_S_LOG_LEVEL visualLogLevel =
63+
(ONE_S_LOG_LEVEL)[call.arguments[@"visualLevel"] intValue];
64+
[OneSignal.Debug setAlertLevel:visualLogLevel];
65+
result(nil);
66+
}
67+
68+
@end

0 commit comments

Comments
 (0)