forked from aws-amplify/aws-sdk-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AWSPinpointConfiguration.m
167 lines (147 loc) · 7.78 KB
/
AWSPinpointConfiguration.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//
// Copyright 2010-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License").
// You may not use this file except in compliance with the License.
// A copy of the License is located at
//
// http://aws.amazon.com/apache2.0
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.
//
#import "AWSPinpointConfiguration.h"
#import <AWSCore/AWSService.h>
#import "AWSPinpointService.h"
int const MAX_STORAGE_SIZE = 1024 * 1024 * 5; // 5 MB
int const SESSION_TIMEOUT = 5000;
static NSString *const AWSInfoPinpointAnalytics = @"PinpointAnalytics";
static NSString *const AWSInfoPinpointTargeting = @"PinpointTargeting";
static NSString *const AWSInfoAppId = @"AppId";
@implementation AWSPinpointEnvironment
static NSString* const UNKNOWN = @"Unknown";
-(instancetype) init {
if(self = [super init]) {
//App Details
NSString *shortVersionString = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
NSString *bundleVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
NSString *bundleIdentifier = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"];
NSString *bundleDisplayName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
_appVersion = shortVersionString ?: UNKNOWN;
_appBuild = bundleVersion ?: UNKNOWN;
_appPackageName = bundleIdentifier ?: UNKNOWN;
_appName = bundleDisplayName ?: UNKNOWN;
AWSDDLogVerbose(@"App Version: [%@]; App Build: [%@]; App Package Name: [%@]; App Name: [%@]",
_appVersion, _appBuild, _appPackageName, _appName);
}
return self;
}
@end
@interface AWSPinpointConfiguration()
@property (nonatomic, strong) NSUserDefaults *userDefaults;
@end
@implementation AWSPinpointConfiguration
#pragma mark - Static Helpers -
+(AWSServiceConfiguration*) analyticsServiceConfiguration {
AWSServiceConfiguration *serviceConfiguration = nil;
AWSServiceInfo *serviceInfo = [[AWSInfo defaultAWSInfo] defaultServiceInfo:AWSInfoPinpointAnalytics];
if (serviceInfo) {
serviceConfiguration = [[AWSServiceConfiguration alloc] initWithRegion:serviceInfo.region
credentialsProvider:serviceInfo.cognitoCredentialsProvider];
}
if (!serviceConfiguration) {
serviceConfiguration = [AWSServiceManager defaultServiceManager].defaultServiceConfiguration;
}
if (!serviceConfiguration) {
@throw [NSException exceptionWithName:NSInternalInconsistencyException
reason:@"The Pinpoint Analytics service configuration is `nil`. You need to configure `awsconfiguration.json` or `Info.plist` or set `defaultServiceConfiguration` before using this method."
userInfo:nil];
}
return serviceConfiguration;
}
+(AWSServiceConfiguration*) targetingServiceConfiguration {
AWSServiceConfiguration *serviceConfiguration = nil;
AWSServiceInfo *serviceInfo = [[AWSInfo defaultAWSInfo] defaultServiceInfo:AWSInfoPinpointTargeting];
if (serviceInfo) {
serviceConfiguration = [[AWSServiceConfiguration alloc] initWithRegion:serviceInfo.region
credentialsProvider:serviceInfo.cognitoCredentialsProvider];
}
if (!serviceConfiguration) {
serviceConfiguration = [AWSServiceManager defaultServiceManager].defaultServiceConfiguration;
}
if (!serviceConfiguration) {
@throw [NSException exceptionWithName:NSInternalInconsistencyException
reason:@"The Pinpoint Targeting service configuration is `nil`. You need to configure `awsconfiguration.json` or `Info.plist` or set `defaultServiceConfiguration` before using this method."
userInfo:nil];
}
return serviceConfiguration;
}
+ (NSString*) appId {
AWSServiceInfo *serviceInfo = [[AWSInfo defaultAWSInfo] defaultServiceInfo:AWSInfoPinpointAnalytics];
NSString *appId = [serviceInfo.infoDictionary objectForKey:AWSInfoAppId];
if (!appId) {
@throw [NSException exceptionWithName:NSInternalInconsistencyException
reason:@"The Pinpoint AppId is `nil`. You need to configure it in `awsconfiguration.json` or `Info.plist` before using this method."
userInfo:nil];
}
return appId;
}
+ (instancetype) defaultPinpointConfigurationWithLaunchOptions:(nullable NSDictionary*) launchOptions {
return [[AWSPinpointConfiguration alloc] initWithAppId:[AWSPinpointConfiguration appId]
launchOptions:launchOptions
maxStorageSize:MAX_STORAGE_SIZE
sessionTimeout:SESSION_TIMEOUT
serviceConfiguration:[AWSPinpointConfiguration analyticsServiceConfiguration]
targetingServiceConfiguration:[AWSPinpointConfiguration targetingServiceConfiguration]];
}
- (instancetype) init {
@throw [NSException exceptionWithName:NSInternalInconsistencyException
reason:@"You must not use init. Please use the public constructors AWSPinpointConfiguration."
userInfo:nil];
}
- (instancetype) initWithAppId:(NSString*) appId
launchOptions:(NSDictionary*) launchOptions {
return [self initWithAppId:appId
launchOptions:launchOptions
maxStorageSize:MAX_STORAGE_SIZE
sessionTimeout:SESSION_TIMEOUT
serviceConfiguration:[[AWSServiceManager defaultServiceManager].defaultServiceConfiguration copy]
targetingServiceConfiguration:[[AWSServiceManager defaultServiceManager].defaultServiceConfiguration copy]];
}
- (instancetype) initWithAppId:(NSString*) appId
launchOptions:(NSDictionary*) launchOptions
maxStorageSize:(int) maxStorageSize
sessionTimeout:(int) sessionTimeout{
return [self initWithAppId:appId
launchOptions:launchOptions
maxStorageSize:maxStorageSize
sessionTimeout:sessionTimeout
serviceConfiguration:[[AWSServiceManager defaultServiceManager].defaultServiceConfiguration copy]
targetingServiceConfiguration:[[AWSServiceManager defaultServiceManager].defaultServiceConfiguration copy]];
}
- (instancetype) initWithAppId:(NSString*) appId
launchOptions:(NSDictionary*) launchOptions
maxStorageSize:(int) maxStorageSize
sessionTimeout:(int) sessionTimeout
serviceConfiguration:(AWSServiceConfiguration*) analyticsServiceConfiguration
targetingServiceConfiguration:(AWSServiceConfiguration*) targetingServiceConfiguration {
if (self = [super init]) {
_userDefaults = [NSUserDefaults standardUserDefaults];
_debug = NO;
_appId = (appId)? appId : [AWSPinpointConfiguration appId];
_launchOptions = launchOptions;
_attributes = [NSDictionary dictionary];
_environment = [AWSPinpointEnvironment new];
_enableEvents = YES;
_enableTargeting = YES;
_enableAutoSessionRecording = YES;
_serviceConfiguration = analyticsServiceConfiguration;
_targetingServiceConfiguration = targetingServiceConfiguration;
_maxStorageSize = maxStorageSize;
_sessionTimeout = sessionTimeout;
}
return self;
}
@end