Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switched NSURLConnection to NSURLSession to be iOS 9 compliant. #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion NightscoutWatch WatchKit App/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>com.dc.NightscoutWatch.watchkitapp</string>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
Expand Down
105 changes: 51 additions & 54 deletions NightscoutWatch WatchKit App/TableInterfaceController.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,65 +52,62 @@ -(void) setup

NSURL *nightScoutUrl = [NSURL URLWithString:thisUrl];



NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:nightScoutUrl
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:60];
[request setHTTPMethod:@"GET"];

NSURLResponse * response = nil;
NSError * error = nil;
NSData * data = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:&error];



if (error == nil)
{
NSArray *sgvArray = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];

for (int i = 0; i < (int)([sgvArray count]-1); i++)
{
NSDictionary *jsonDict = [sgvArray objectAtIndex:i];
NSDictionary *nextDict = [sgvArray objectAtIndex:i+1];

id temp = [jsonDict objectForKey:@"sgv"];
NSString *sgv;
if ([temp isKindOfClass:[NSString class]])
{
sgv = temp;
}
else
{
sgv = [temp stringValue];
}

NSString *nextSgv = [nextDict objectForKey:@"sgv"];

int sgvInt = [sgv intValue];
int nextSgvInt = [nextSgv intValue];

int delta = sgvInt - nextSgvInt;

[deltaListArray addObject:[NSString stringWithFormat:@"%d", delta]];
[sgvListArray addObject:sgv];

NSString *datetime = [jsonDict objectForKey:@"date"];

long dateInt = [datetime longLongValue];

NSDate *date = [NSDate dateWithTimeIntervalSince1970:dateInt/1000];
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:@"h:mm a"];
NSString *timeString = [dateFormatter1 stringFromDate:date];

[dateListArray addObject:timeString];

}


[self performSelectorOnMainThread:@selector(configureTableWithData:) withObject:nil waitUntilDone:false];
}
[[NSURLSession sharedSession] dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error){
if (error == nil)
{
NSArray *sgvArray = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];

for (int i = 0; i < (int)([sgvArray count]-1); i++)
{
NSDictionary *jsonDict = [sgvArray objectAtIndex:i];
NSDictionary *nextDict = [sgvArray objectAtIndex:i+1];

id temp = [jsonDict objectForKey:@"sgv"];
NSString *sgv;
if ([temp isKindOfClass:[NSString class]])
{
sgv = temp;
}
else
{
sgv = [temp stringValue];
}

NSString *nextSgv = [nextDict objectForKey:@"sgv"];

int sgvInt = [sgv intValue];
int nextSgvInt = [nextSgv intValue];

int delta = sgvInt - nextSgvInt;

[deltaListArray addObject:[NSString stringWithFormat:@"%d", delta]];
[sgvListArray addObject:sgv];

NSString *datetime = [jsonDict objectForKey:@"date"];

long dateInt = [datetime longLongValue];

NSDate *date = [NSDate dateWithTimeIntervalSince1970:dateInt/1000];
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:@"h:mm a"];
NSString *timeString = [dateFormatter1 stringFromDate:date];

[dateListArray addObject:timeString];

}


[self performSelectorOnMainThread:@selector(configureTableWithData:) withObject:nil waitUntilDone:false];
}
}];
}

- (void)configureTableWithData:(NSArray*)dataObjects {
Expand Down
28 changes: 11 additions & 17 deletions NightscoutWatch WatchKit Extension/GlanceController.m
Original file line number Diff line number Diff line change
Expand Up @@ -451,23 +451,17 @@ -(void) setup
timeoutInterval:60];
[request setHTTPMethod:@"GET"];

NSURLResponse * response = nil;
NSError * error = nil;
NSData * data = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:&error];



if (error == nil)
{

NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];

[self performSelectorOnMainThread:@selector(setupDisplay:) withObject:jsonDict waitUntilDone:false];

}

[[NSURLSession sharedSession] dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error){
if (error == nil)
{

NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];

[self performSelectorOnMainThread:@selector(setupDisplay:) withObject:jsonDict waitUntilDone:false];

}
}];
}

- (void)didDeactivate {
Expand Down
2 changes: 1 addition & 1 deletion NightscoutWatch WatchKit Extension/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>com.dc.NightscoutWatch.watchkitextension</string>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
Expand Down
26 changes: 10 additions & 16 deletions NightscoutWatch WatchKit Extension/InterfaceController.m
Original file line number Diff line number Diff line change
Expand Up @@ -452,22 +452,16 @@ -(void) setup
timeoutInterval:60];
[request setHTTPMethod:@"GET"];

NSURLResponse * response = nil;
NSError * error = nil;
NSData * data = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:&error];



if (error == nil)
{
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];

[self performSelectorOnMainThread:@selector(setupDisplay:) withObject:jsonDict waitUntilDone:false];

}

[[NSURLSession sharedSession] dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error){
if (error == nil)
{
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];

[self performSelectorOnMainThread:@selector(setupDisplay:) withObject:jsonDict waitUntilDone:false];

}
}];
}

- (void)didDeactivate {
Expand Down
Loading