Manos Chorianopoulos

add initializeWithCallback

No preview for this file type
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
7 <key>Pods-SwiftWarplyFramework.xcscheme_^#shared#^_</key> 7 <key>Pods-SwiftWarplyFramework.xcscheme_^#shared#^_</key>
8 <dict> 8 <dict>
9 <key>orderHint</key> 9 <key>orderHint</key>
10 - <integer>1</integer> 10 + <integer>0</integer>
11 </dict> 11 </dict>
12 </dict> 12 </dict>
13 </dict> 13 </dict>
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
7 <key>SwiftWarplyFramework.xcscheme_^#shared#^_</key> 7 <key>SwiftWarplyFramework.xcscheme_^#shared#^_</key>
8 <dict> 8 <dict>
9 <key>orderHint</key> 9 <key>orderHint</key>
10 - <integer>0</integer> 10 + <integer>1</integer>
11 </dict> 11 </dict>
12 </dict> 12 </dict>
13 </dict> 13 </dict>
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
16 @property (nonatomic, weak) Warply *warply; 16 @property (nonatomic, weak) Warply *warply;
17 17
18 - (void) initialize:(NSDictionary *)launchOptions uuid:(NSString*)uuid merchantId:(NSString*)merchantId lang:(NSString*)lang; 18 - (void) initialize:(NSDictionary *)launchOptions uuid:(NSString*)uuid merchantId:(NSString*)merchantId lang:(NSString*)lang;
19 +- (void) initializeWithCallback:(NSDictionary *)launchOptions uuid:(NSString*)uuid merchantId:(NSString*)merchantId lang:(NSString*)lang successBlock:(void(^)(NSDictionary *successBlock))success failureBlock:(void(^)(NSError *error))failure;
19 - (void) setToStage; 20 - (void) setToStage;
20 - (void) setLang:(NSString*) lang; 21 - (void) setLang:(NSString*) lang;
21 - (void) getSteps:(void(^)(CMPedometerData* stepsData)) completion; 22 - (void) getSteps:(void(^)(CMPedometerData* stepsData)) completion;
......
...@@ -51,6 +51,36 @@ CMPedometer *pedometer; ...@@ -51,6 +51,36 @@ CMPedometer *pedometer;
51 // } 51 // }
52 } 52 }
53 53
54 +- (void)initializeWithCallback:(NSDictionary *)launchOptions uuid:(NSString*)uuid merchantId:(NSString*)merchantId lang:(NSString*)lang successBlock:(void(^)(NSDictionary *successBlock))success failureBlock:(void(^)(NSError *error))failure{
55 + #if (DEBUG == 1)
56 + [Warply launchWithAppUUIDWithCallback:uuid launchOptions:launchOptions andSuccessBlock:success andFailureBlock:failure];
57 + #else
58 + [Warply launchWithAppUUIDWithCallback:uuid launchOptions:launchOptions andSuccessBlock:success andFailureBlock:failure];
59 + #endif
60 + [[Warply sharedService].pushManager registerForRemoteNotifications];
61 + [[Warply sharedService].pushManager resetBadge];
62 + MERCHANT_ID = merchantId;
63 + LANG = lang;
64 + //removal of caching
65 + int cacheSizeMemory = 0; // 0MB
66 + int cacheSizeDisk = 0; // 0MB
67 + NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:cacheSizeMemory diskCapacity:cacheSizeDisk diskPath:@"nsurlcache"];
68 + [NSURLCache setSharedURLCache:sharedCache];
69 +
70 +// date = [NSDate date];
71 +// if ([CMPedometer isStepCountingAvailable]) {
72 +// pedometer = [[CMPedometer alloc] init];
73 +// [NSTimer scheduledTimerWithTimeInterval:0.5f
74 +// target:self
75 +// selector:@selector(recursiveQuery)
76 +// userInfo:nil
77 +// repeats:YES];
78 +// } else {
79 +// NSLog(@"Nothing available");
80 +//
81 +// }
82 +}
83 +
54 - (void)queryDataFrom:(NSDate *)startDate toDate:(NSDate *)endDate { 84 - (void)queryDataFrom:(NSDate *)startDate toDate:(NSDate *)endDate {
55 [pedometer queryPedometerDataFromDate:startDate 85 [pedometer queryPedometerDataFromDate:startDate
56 toDate:endDate 86 toDate:endDate
......
...@@ -145,6 +145,7 @@ typedef enum { ...@@ -145,6 +145,7 @@ typedef enum {
145 empty if application launched by user. 145 empty if application launched by user.
146 */ 146 */
147 + (void)launchWithAppUUID:(NSString *)appUUID launchOptions:(NSDictionary *)launchOptions; 147 + (void)launchWithAppUUID:(NSString *)appUUID launchOptions:(NSDictionary *)launchOptions;
148 ++ (void)launchWithAppUUIDWithCallback:(NSString *)appUUID launchOptions:(NSDictionary *)launchOptions andSuccessBlock:(void (^)(NSDictionary *))success andFailureBlock:(void (^)(NSError *error))failure;
148 149
149 /*! 150 /*!
150 @abstract Launching the Warply service. 151 @abstract Launching the Warply service.
...@@ -157,6 +158,7 @@ typedef enum { ...@@ -157,6 +158,7 @@ typedef enum {
157 @param customPushDelegate an class conforming to the WLCustomPushDelegate protocol responsible for handling the push notification. 158 @param customPushDelegate an class conforming to the WLCustomPushDelegate protocol responsible for handling the push notification.
158 */ 159 */
159 + (void)launchWithAppUUID:(NSString *)appUUID launchOptions:(NSDictionary *)launchOptions customPushHandler:(id <WLCustomPushHandler>)customPushHandler; 160 + (void)launchWithAppUUID:(NSString *)appUUID launchOptions:(NSDictionary *)launchOptions customPushHandler:(id <WLCustomPushHandler>)customPushHandler;
161 ++ (void)launchWithAppUUIDWithCallback:(NSString *)appUUID launchOptions:(NSDictionary *)launchOptions customPushHandler:(id <WLCustomPushHandler>)customPushHandler andSuccessBlock:(void (^)(NSDictionary *))success andFailureBlock:(void (^)(NSError *error))failure;
160 162
161 /*! 163 /*!
162 @abstract Launching the Warply service. 164 @abstract Launching the Warply service.
...@@ -170,6 +172,7 @@ typedef enum { ...@@ -170,6 +172,7 @@ typedef enum {
170 @param baseUrl: A NSString parameter for defining the base url. 172 @param baseUrl: A NSString parameter for defining the base url.
171 */ 173 */
172 + (void)launchWithAppUUID:(NSString *)appUUID launchOptions:(NSDictionary *)launchOptions customPushDelegate:(id <WLCustomPushHandler>)customPushDelegate serverBaseUrl:(NSString *)baseUrl; 174 + (void)launchWithAppUUID:(NSString *)appUUID launchOptions:(NSDictionary *)launchOptions customPushDelegate:(id <WLCustomPushHandler>)customPushDelegate serverBaseUrl:(NSString *)baseUrl;
175 ++ (void)launchWithAppUUIDWithCallback:(NSString *)appUUID launchOptions:(NSDictionary *)launchOptions customPushDelegate:(id <WLCustomPushHandler>)customPushDelegate serverBaseUrl:(NSString *)baseUrl andSuccessBlock:(void (^)(NSDictionary *))success andFailureBlock:(void (^)(NSError *error))failure;
173 176
174 177
175 /*! 178 /*!
......
...@@ -147,17 +147,78 @@ typedef void (^FailureResponse)(NSURLSessionDataTask * _Nullable task, NSError * ...@@ -147,17 +147,78 @@ typedef void (^FailureResponse)(NSURLSessionDataTask * _Nullable task, NSError *
147 } 147 }
148 148
149 /////////////////////////////////////////////////////////////////////////////// 149 ///////////////////////////////////////////////////////////////////////////////
150 ++ (void)launchWithAppUUIDWithCallback:(NSString *)appUUID launchOptions:(NSDictionary *)launchOptions customPushDelegate:(id <WLCustomPushHandler>)customPushDelegate serverBaseUrl:(NSString *)url andSuccessBlock:(void (^)(NSDictionary *))success andFailureBlock:(void (^)(NSError *error))failure
151 +{
152 + //Launch Once
153 + if (_sharedService != nil) {
154 + // TODO: Maybe add failure callback here? - msg: "already launched"
155 + return;
156 + }
157 +
158 + dispatch_once(&pred, ^{
159 + NSString *mode = @"Production";
160 +#if (DEBUG == 1)
161 + mode = @"Debug";
162 +#endif
163 +
164 + //Load Configuration
165 + NSAssert(![appUUID containsString:@"Insert your"] && [appUUID length] > 0, @"WARPLY: You have not inserted your %@ app UUID!", mode);
166 +
167 + _sharedService = [[Warply alloc] initWithApplicationUUID:appUUID];
168 + _sharedService.baseURL = url;
169 +
170 + if ([_sharedService isRegistrationValid] == NO) {
171 + [_sharedService.pendingOperationsQueue setSuspended:YES];
172 + [_sharedService registrationWithSuccessBlock2:success andFailureBlock:failure];
173 +
174 + } else {
175 + NSDictionary* successCallback = [[NSMutableDictionary alloc] init];
176 + [successCallback setValue:@1 forKey:@"status"];
177 + [successCallback setValue:@"success" forKey:@"msg"];
178 + success(successCallback);
179 + }
180 +
181 + if (customPushDelegate != nil) {
182 + _sharedService.pushManager.customPushHanlder = customPushDelegate;
183 + }
184 +
185 + [_sharedService.pushManager didFinishLaunchingWithOptions:launchOptions];
186 +
187 + [_sharedService networkReachabilityReporting];
188 +
189 + [WLAnalyticsManager logAppDidFinishLauchingEvent];
190 +
191 + [WLUserManager sendUUIDS];
192 + [_sharedService sendEventsIfNeeded];
193 +
194 + [_sharedService getLocationPoint];
195 + });
196 +}
197 +
198 +///////////////////////////////////////////////////////////////////////////////
150 + (void)launchWithAppUUID:(NSString *)appUUID launchOptions:(NSDictionary *)launchOptions 199 + (void)launchWithAppUUID:(NSString *)appUUID launchOptions:(NSDictionary *)launchOptions
151 { 200 {
152 [self launchWithAppUUID:appUUID launchOptions:launchOptions customPushHandler:nil]; 201 [self launchWithAppUUID:appUUID launchOptions:launchOptions customPushHandler:nil];
153 } 202 }
154 203
155 /////////////////////////////////////////////////////////////////////////////// 204 ///////////////////////////////////////////////////////////////////////////////
205 ++ (void)launchWithAppUUIDWithCallback:(NSString *)appUUID launchOptions:(NSDictionary *)launchOptions andSuccessBlock:(void (^)(NSDictionary *))success andFailureBlock:(void (^)(NSError *error))failure
206 +{
207 + [self launchWithAppUUIDWithCallback:appUUID launchOptions:launchOptions customPushHandler:nil andSuccessBlock:success andFailureBlock:failure];
208 +}
209 +
210 +///////////////////////////////////////////////////////////////////////////////
156 + (void)launchWithAppUUID:(NSString *)appUUID launchOptions:(NSDictionary *)launchOptions customPushHandler:(id <WLCustomPushHandler>)customPushDelegate 211 + (void)launchWithAppUUID:(NSString *)appUUID launchOptions:(NSDictionary *)launchOptions customPushHandler:(id <WLCustomPushHandler>)customPushDelegate
157 { 212 {
158 [self launchWithAppUUID:appUUID launchOptions:launchOptions customPushDelegate:customPushDelegate serverBaseUrl:WARP_PRODUCTION_BASE_URL]; 213 [self launchWithAppUUID:appUUID launchOptions:launchOptions customPushDelegate:customPushDelegate serverBaseUrl:WARP_PRODUCTION_BASE_URL];
159 } 214 }
160 215
216 +///////////////////////////////////////////////////////////////////////////////
217 ++ (void)launchWithAppUUIDWithCallback:(NSString *)appUUID launchOptions:(NSDictionary *)launchOptions customPushHandler:(id <WLCustomPushHandler>)customPushDelegate andSuccessBlock:(void (^)(NSDictionary *))success andFailureBlock:(void (^)(NSError *error))failure
218 +{
219 + [self launchWithAppUUIDWithCallback:appUUID launchOptions:launchOptions customPushDelegate:customPushDelegate serverBaseUrl:WARP_PRODUCTION_BASE_URL andSuccessBlock:success andFailureBlock:failure];
220 +}
221 +
161 #pragma mark - Initialization 222 #pragma mark - Initialization
162 /////////////////////////////////////////////////////////////////////////////// 223 ///////////////////////////////////////////////////////////////////////////////
163 - (id)initWithApplicationUUID:(NSString *)appUUID 224 - (id)initWithApplicationUUID:(NSString *)appUUID
...@@ -4551,6 +4612,141 @@ CGFloat DistanceBetweenTwoPoints(CGPoint point1,CGPoint point2) ...@@ -4551,6 +4612,141 @@ CGFloat DistanceBetweenTwoPoints(CGPoint point1,CGPoint point2)
4551 // [operation start]; 4612 // [operation start];
4552 } 4613 }
4553 4614
4615 +
4616 +///////////////////////////////////////////////////////////////////////////////
4617 +- (void)registrationWithSuccessBlock2:(void (^)(NSDictionary *successBlock))success
4618 + andFailureBlock:(void (^)(NSError *error))failure
4619 +{
4620 + if (_waitingForRegistration == YES)
4621 + return;
4622 +
4623 + _waitingForRegistration = YES;
4624 +
4625 + NSString *urlString = [NSString stringWithFormat:@"%@/api/mobile/v2/%@/register/", _baseURL, _appUUID];
4626 +// NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:100];
4627 +// [request setAllHTTPHeaderFields:@{@"Accept-Encoding": @"gzip",
4628 +// @"Accept": @"application/json",
4629 +// @"User-Agent": @"gzip",
4630 +// @"loyalty-bundle-id": [NSString stringWithFormat:@"ios:%@", [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleIdentifier"]]}];
4631 +
4632 + [_httpClient.requestSerializer setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];
4633 + [_httpClient.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
4634 + [_httpClient.requestSerializer setValue:@"gzip" forHTTPHeaderField:@"User-Agent"];
4635 + [_httpClient.requestSerializer setValue:[NSString stringWithFormat:@"ios:%@", [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleIdentifier"]] forHTTPHeaderField:@"loyalty-bundle-id"];
4636 +
4637 +
4638 + //HTTP Method
4639 +// [request setHTTPMethod:@"GET"];
4640 +
4641 + /*AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
4642 + success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)*/
4643 + ////////////////////////////!!!!!
4644 +// AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]
4645 +// initWithRequest:request];
4646 +// operation.responseSerializer = [AFJSONResponseSerializer serializer];
4647 +// [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation
4648 +// , id JSON)
4649 + ////////////////////////////
4650 + SuccessResponse successResponse = ^(NSURLSessionDataTask * _Nonnull task, id _Nullable JSON) {
4651 + if ([JSON objectForKey:@"status"]) {
4652 +
4653 + WLLOG(@"************* WARP Registration ********************");
4654 + WLLOG(@"[WARP Registration] URL: %@", urlString);
4655 + NSData *jsonData = [NSJSONSerialization dataWithJSONObject:JSON options:NSJSONWritingPrettyPrinted error:NULL];
4656 + NSString *jsonResponseString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
4657 + WLLOG(@"[WARP Registration] Response:%@", jsonResponseString);
4658 + WLLOG(@"****************************************************");
4659 + _waitingForRegistration = NO;
4660 + int status = [[JSON valueForKey:@"status"] intValue];
4661 + if (status != WLResultCodesSuccess) {
4662 + [_pendingOperationsQueue setSuspended:![self isRegistrationValid]];
4663 + if (failure) {
4664 + NSDictionary *eDict=@{NSLocalizedDescriptionKey: WLResultCodesDescriptions[(status - 1)]};
4665 + NSError *warplyError = [NSError errorWithDomain:WARP_ERROR_DOMAIN code:status userInfo:eDict];
4666 + failure(warplyError);
4667 + }
4668 + return;
4669 + }
4670 +
4671 + NSDictionary *context = [JSON valueForKey:@"context"];
4672 + self.apiKey = [context valueForKey:@"api_key"];
4673 + self.webId = [context valueForKey:@"web_id"];
4674 + [WLKeychain setString:[[[UIDevice currentDevice] identifierForVendor] UUIDString] forKey:@"old_identifier_for_vendor"];
4675 +
4676 + [_pendingOperationsQueue setSuspended:![self isRegistrationValid]];
4677 + if (success) {
4678 + NSDictionary* successCallback = [[NSMutableDictionary alloc] init];
4679 + [successCallback setValue:@1 forKey:@"status"];
4680 + [successCallback setValue:@"success" forKey:@"msg"];
4681 + success(successCallback);
4682 + }
4683 +
4684 +
4685 + } else {
4686 + NSError *error = [NSError errorWithDomain:WARP_ERROR_DOMAIN code:1026 userInfo:@{NSLocalizedDescriptionKey: NSLocalizedString(@"Empry response", @"Warply")}];
4687 + if (failure) {
4688 + failure(error);
4689 + }
4690 +
4691 + }
4692 + };
4693 + //failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON)
4694 + ///////////////!!!!!
4695 +// failure:^(AFHTTPRequestOperation *operation, NSError *error)
4696 + ///////////////
4697 + FailureResponse failureResponse = ^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
4698 + WLLOG(@"************* WARP Registration ********************");
4699 + WLLOG(@"[WARP Registration] Error: %@", [error description]);
4700 + WLLOG(@"****************************************************");
4701 + _waitingForRegistration = NO;
4702 + [_pendingOperationsQueue setSuspended:![self isRegistrationValid]];
4703 + if (failure) {
4704 + failure(error);
4705 + }
4706 + };
4707 +
4708 + _httpClient.responseSerializer = [AFJSONResponseSerializer serializer];
4709 + _httpClient.requestSerializer = [AFJSONRequestSerializer serializer];
4710 +
4711 +
4712 +// NSDictionary *context = [NSDictionary dictionaryWithObject:[[ASIdentifierManager sharedManager].advertisingIdentifier UUIDString] forKey:];
4713 +
4714 +// NSData *parameters = [NSJSONSerialization dataWithJSONObject:context options:0 error:NULL];
4715 +
4716 + NSString *old_identifier_for_vendor;
4717 + if ([WLKeychain getStringForKey:@"old_identifier_for_vendor"]) {
4718 + old_identifier_for_vendor = [WLKeychain getStringForKey:@"old_identifier_for_vendor"];
4719 + } else {
4720 + old_identifier_for_vendor = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
4721 + }
4722 +
4723 + NSDictionary *parameters = @{@"advertising_identifier": [[ASIdentifierManager sharedManager].advertisingIdentifier UUIDString],
4724 + @"old_identifier_for_vendor": old_identifier_for_vendor,
4725 + @"new_identifier_for_vendor": [[[UIDevice currentDevice] identifierForVendor] UUIDString],
4726 + @"unique-device-id": [[[UIDevice currentDevice] identifierForVendor] UUIDString],
4727 + @"vendor": @"apple",
4728 + @"platform": @"ios",
4729 + @"os_version": [[UIDevice currentDevice] systemVersion],
4730 + @"channel": @"mobile"};
4731 +
4732 +
4733 +// NSData *jsonData = [NSJSONSerialization dataWithJSONObject:postDictionary options:0 error:NULL];
4734 +
4735 + [_httpClient POST:urlString parameters:parameters progress:nil success:successResponse failure:failureResponse];
4736 +// [_httpClient GET:urlString parameters:nil progress:nil success:successResponse failure:failureResponse];
4737 +// /*[operation setAuthenticationChallengeBlock:^(NSURLConnection *connection, NSURLAuthenticationChallenge *challenge) {
4738 +// [[challenge sender] continueWithoutCredentialForAuthenticationChallenge:challenge];
4739 +// }];*/
4740 +// ////////////////!!!!!
4741 +// [operation setWillSendRequestForAuthenticationChallengeBlock:^(NSURLConnection *connection, NSURLAuthenticationChallenge *challenge)
4742 +// {
4743 +// [[challenge sender] continueWithoutCredentialForAuthenticationChallenge:challenge];
4744 +// }];
4745 +// ////////////////
4746 +//
4747 +// [operation start];
4748 +}
4749 +
4554 /////////////////////////////////////////////////////////////////////////////// 4750 ///////////////////////////////////////////////////////////////////////////////
4555 - (void)registration 4751 - (void)registration
4556 { 4752 {
......