Post-Request nun asynchron
This commit is contained in:
parent
f272285193
commit
72f7eccea3
Binary file not shown.
@ -13,6 +13,7 @@
|
|||||||
NSTimer *updateTimer;
|
NSTimer *updateTimer;
|
||||||
NSString *usersIPv4;
|
NSString *usersIPv4;
|
||||||
BOOL isNetworkAvailable;
|
BOOL isNetworkAvailable;
|
||||||
|
BOOL isRequestRunning;
|
||||||
}
|
}
|
||||||
|
|
||||||
-(BOOL)updateDomain;
|
-(BOOL)updateDomain;
|
||||||
|
|||||||
@ -19,13 +19,24 @@
|
|||||||
userInfo: nil
|
userInfo: nil
|
||||||
repeats: YES];
|
repeats: YES];
|
||||||
|
|
||||||
|
isRequestRunning = NO;
|
||||||
|
|
||||||
isNetworkAvailable = NO;
|
isNetworkAvailable = NO;
|
||||||
[self checkNetworkAvailability];
|
[self checkNetworkAvailability];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)tick:(NSTimer *)theTimer {
|
- (void)tick:(NSTimer *)theTimer {
|
||||||
if(isNetworkAvailable) {
|
if(isNetworkAvailable && !isRequestRunning) {
|
||||||
[self updateDomain];
|
[self updateDomain];
|
||||||
|
} else {
|
||||||
|
// else for logging only
|
||||||
|
|
||||||
|
if(!isNetworkAvailable) {
|
||||||
|
NSLog(@"no ip update, 'cause network unavailable");
|
||||||
|
}
|
||||||
|
if(isRequestRunning) {
|
||||||
|
NSLog(@"no ip update, 'cause of active request");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -82,16 +93,14 @@
|
|||||||
} else {
|
} else {
|
||||||
NSLog(@"compare stored %@ with %@", storedIPv4, fetchedIPv4);
|
NSLog(@"compare stored %@ with %@", storedIPv4, fetchedIPv4);
|
||||||
BOOL changed = [storedIPv4 isEqualToString:fetchedIPv4];
|
BOOL changed = [storedIPv4 isEqualToString:fetchedIPv4];
|
||||||
|
|
||||||
if(changed) {
|
|
||||||
[[NSUserDefaults standardUserDefaults] setObject:fetchedIPv4 forKey:@"value.ip.ipv4"];
|
|
||||||
}
|
|
||||||
|
|
||||||
return changed;
|
return changed;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
-(BOOL)doWebServiceUpdate {
|
-(BOOL)doWebServiceUpdate {
|
||||||
|
NSLog(@"update ip to %@", usersIPv4);
|
||||||
|
isRequestRunning = YES;
|
||||||
|
|
||||||
NSString *settingTempName = [[NSUserDefaults standardUserDefaults] stringForKey:@"setting.name"];
|
NSString *settingTempName = [[NSUserDefaults standardUserDefaults] stringForKey:@"setting.name"];
|
||||||
NSString *settingTempToken = [[NSUserDefaults standardUserDefaults] stringForKey:@"setting.token"];
|
NSString *settingTempToken = [[NSUserDefaults standardUserDefaults] stringForKey:@"setting.token"];
|
||||||
|
|
||||||
@ -108,25 +117,21 @@
|
|||||||
NSData *postData = [NSJSONSerialization dataWithJSONObject:tmp options:0 error:&error];
|
NSData *postData = [NSJSONSerialization dataWithJSONObject:tmp options:0 error:&error];
|
||||||
[request setHTTPBody:postData];
|
[request setHTTPBody:postData];
|
||||||
|
|
||||||
NSLog(@"update ip to %@", usersIPv4);
|
/*NSData *resultData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
|
||||||
//NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
|
NSString *strData = [[NSString alloc]initWithData:resultData encoding:NSUTF8StringEncoding];
|
||||||
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
|
NSLog(@"resultData: %@", strData);*/
|
||||||
|
|
||||||
|
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *resultData, NSError *error) {
|
||||||
|
NSString *strData = [[NSString alloc]initWithData:resultData encoding:NSUTF8StringEncoding];
|
||||||
|
NSLog(@"resultData: %@", strData);
|
||||||
|
|
||||||
|
// locally save posted ip
|
||||||
|
[[NSUserDefaults standardUserDefaults] setObject:usersIPv4 forKey:@"value.ip.ipv4"];
|
||||||
|
|
||||||
|
isRequestRunning = NO;
|
||||||
|
}];
|
||||||
|
|
||||||
NSString *strData = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
|
|
||||||
NSLog(@"data: %@", strData);
|
|
||||||
return YES;
|
return YES;
|
||||||
|
|
||||||
/*NSString *post = [NSString stringWithFormat:@"name=%@&token=%@", settingTempName, settingTempToken];
|
|
||||||
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
|
|
||||||
|
|
||||||
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
|
|
||||||
|
|
||||||
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
|
|
||||||
[request setURL:[NSURL URLWithString:WS_RESOURCE]];
|
|
||||||
[request setHTTPMethod:@"POST"];
|
|
||||||
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
|
|
||||||
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
|
|
||||||
[request setHTTPBody:postData];*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
-(NSString *)fetchIPv4 {
|
-(NSString *)fetchIPv4 {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user