This repository was archived by the owner on Apr 16, 2025. It is now read-only.
forked from pschmied/CycleTracks
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathJsonUrlFetcherImpl.m
More file actions
61 lines (44 loc) · 1.73 KB
/
JsonUrlFetcherImpl.m
File metadata and controls
61 lines (44 loc) · 1.73 KB
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
//
// JsonUrlFetcherImpl.m
// OneBusAwaySDK
//
// Created by Aaron Brethorst on 12/16/15.
// Copyright © 2015 One Bus Away. All rights reserved.
//
#import "JsonUrlFetcherImpl.h"
@interface JsonUrlFetcherImpl ()
@property(nonatomic,strong) NSURLSessionDataTask *task;
@end
@implementation JsonUrlFetcherImpl
- (instancetype)initWithCompletionBlock:(OBADataSourceCompletion)completion progressBlock:(OBADataSourceProgress)progress {
self = [super init];
if (self) {
_completionBlock = completion;
_progressBlock = progress;
}
return self;
}
- (void)loadRequest:(NSURLRequest *)request {
self.task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
id responseObject = nil;
NSURL *url = [NSURL URLWithString:@"https://script.google.com/macros/s/AKfycbxpN47XZQGAoh-N5wQtBETp51tznG3JnOrWsAVNy0xGJOkD8ibS/exec"];
NSMutableURLRequest *request2 = [NSMutableURLRequest requestWithURL:url];
NSURLResponse * response2;
data = [NSURLConnection sendSynchronousRequest:request2 returningResponse:&response error:&error];
if (data.length) {
NSError *jsonError = nil;
responseObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&jsonError];
if (!responseObject && jsonError) {
error = jsonError;
}
}
dispatch_async(dispatch_get_main_queue(), ^{
self.completionBlock(responseObject, ((NSHTTPURLResponse*)response).statusCode, error);
});
}];
[self.task resume];
}
- (void)cancel {
[self.task cancel];
}
@end