Periodically ip update
This commit is contained in:
@@ -721,7 +721,7 @@
|
||||
<textField verticalHuggingPriority="750" horizontalCompressionResistancePriority="250" fixedFrame="YES" setsMaxLayoutWidthAtFirstLayout="YES" translatesAutoresizingMaskIntoConstraints="NO" id="hXp-Wv-qfT">
|
||||
<rect key="frame" x="131" y="49" width="332" height="147"/>
|
||||
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
|
||||
<textFieldCell key="cell" sendsActionOnEndEditing="YES" title="You need an ip-breadcrump.orc account to use this software." id="PYW-tK-cnY">
|
||||
<textFieldCell key="cell" sendsActionOnEndEditing="YES" title="You need an ip-breadcrump.org account to use this software." id="PYW-tK-cnY">
|
||||
<font key="font" metaFont="system"/>
|
||||
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
|
||||
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
|
||||
@@ -755,5 +755,6 @@ Gw
|
||||
</subviews>
|
||||
</view>
|
||||
</window>
|
||||
<customObject id="QYg-I7-rYj" customClass="WebserviceClient"/>
|
||||
</objects>
|
||||
</document>
|
||||
@@ -0,0 +1,16 @@
|
||||
//
|
||||
// WebServiceConstants.h
|
||||
// ipbc-Client
|
||||
//
|
||||
// Created by Gerrit Linnemann on 28.10.13.
|
||||
// Copyright (c) 2013 Adawim UG (haftungsbeschränkt). All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
@interface WebServiceConstants : NSObject
|
||||
|
||||
extern NSString * const WS_RESOURCE;
|
||||
extern NSString * const WS_GET_USERS_IP_SERVICE;
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,16 @@
|
||||
//
|
||||
// WebServiceConstants.m
|
||||
// ipbc-Client
|
||||
//
|
||||
// Created by Gerrit Linnemann on 28.10.13.
|
||||
// Copyright (c) 2013 Adawim UG (haftungsbeschränkt). All rights reserved.
|
||||
//
|
||||
|
||||
#import "WebServiceConstants.h"
|
||||
|
||||
@implementation WebServiceConstants
|
||||
|
||||
NSString * const WS_RESOURCE = @"http://www.ip-bc.org/ws/update/ip";
|
||||
NSString * const WS_GET_USERS_IP_SERVICE = @"http://www.ip-bc.org/ws/info/ip";
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,19 @@
|
||||
//
|
||||
// WebserviceClient.h
|
||||
// ipbc-Client
|
||||
//
|
||||
// Created by Gerrit Linnemann on 28.10.13.
|
||||
// Copyright (c) 2013 Adawim UG (haftungsbeschränkt). All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
@interface WebserviceClient : NSObject {
|
||||
|
||||
NSTimer *updateTimer;
|
||||
NSString *usersIPv4;
|
||||
}
|
||||
|
||||
-(BOOL)updateDomain;
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,114 @@
|
||||
//
|
||||
// WebserviceClient.m
|
||||
// ipbc-Client
|
||||
//
|
||||
// Created by Gerrit Linnemann on 28.10.13.
|
||||
// Copyright (c) 2013 Adawim UG (haftungsbeschränkt). All rights reserved.
|
||||
//
|
||||
|
||||
#import "WebserviceClient.h"
|
||||
#import "WebServiceConstants.h"
|
||||
|
||||
@implementation WebserviceClient
|
||||
|
||||
-(void)awakeFromNib {
|
||||
updateTimer = [NSTimer scheduledTimerWithTimeInterval: 4
|
||||
target: self
|
||||
selector: @selector(tick:)
|
||||
userInfo: nil
|
||||
repeats: YES];
|
||||
}
|
||||
|
||||
- (void)tick:(NSTimer *)theTimer {
|
||||
[self updateDomain];
|
||||
}
|
||||
|
||||
-(BOOL)updateDomain {
|
||||
usersIPv4 = [self fetchIPv4];
|
||||
NSString *storedIPv4 = [[NSUserDefaults standardUserDefaults] stringForKey:@"value.ip.ipv4"];
|
||||
if(storedIPv4 == nil) {
|
||||
[[NSUserDefaults standardUserDefaults] setObject:usersIPv4 forKey:@"value.ip.ipv4"];
|
||||
}
|
||||
|
||||
BOOL updateResult = NO;
|
||||
|
||||
if([self checkUpdateNeeded]) {
|
||||
NSLog(@"update resource: %@", WS_RESOURCE);
|
||||
updateResult = [self doWebServiceUpdate];
|
||||
} else {
|
||||
NSLog(@"no update");
|
||||
}
|
||||
|
||||
return updateResult;
|
||||
}
|
||||
|
||||
-(BOOL)checkUpdateNeeded {
|
||||
NSString *storedIPv4 = [[NSUserDefaults standardUserDefaults] stringForKey:@"value.ip.ipv4"];
|
||||
NSString *fetchedIPv4 = usersIPv4;
|
||||
|
||||
if(storedIPv4 == nil) {
|
||||
NSLog(@"stored IPv4 NULL");
|
||||
return YES;
|
||||
} else if(fetchedIPv4 == nil) {
|
||||
NSLog(@"fetched IPv4 NULL");
|
||||
return NO;
|
||||
} else {
|
||||
//NSLog(@"compare stored %@ with %@", storedIPv4, fetchedIPv4);
|
||||
BOOL changed = [storedIPv4 isEqualToString:fetchedIPv4];
|
||||
|
||||
if(changed) {
|
||||
[[NSUserDefaults standardUserDefaults] setObject:fetchedIPv4 forKey:@"value.ip.ipv4"];
|
||||
}
|
||||
|
||||
return changed;
|
||||
}
|
||||
}
|
||||
|
||||
-(BOOL)doWebServiceUpdate {
|
||||
NSString *settingTempName = [[NSUserDefaults standardUserDefaults] stringForKey:@"setting.name"];
|
||||
NSString *settingTempToken = [[NSUserDefaults standardUserDefaults] stringForKey:@"setting.token"];
|
||||
|
||||
NSURL *url = [NSURL URLWithString:WS_RESOURCE];
|
||||
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
|
||||
[request setHTTPMethod:@"POST"];
|
||||
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
|
||||
|
||||
NSDictionary *tmp = [[NSDictionary alloc] initWithObjectsAndKeys:
|
||||
settingTempName, @"domain",
|
||||
settingTempToken, @"token",
|
||||
nil];
|
||||
NSError *error;
|
||||
NSData *postData = [NSJSONSerialization dataWithJSONObject:tmp options:0 error:&error];
|
||||
[request setHTTPBody:postData];
|
||||
|
||||
NSLog(@"update ip to %@", usersIPv4);
|
||||
//NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
|
||||
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
|
||||
|
||||
NSString *strData = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
|
||||
NSLog(@"data: %@", strData);
|
||||
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 {
|
||||
NSURL *myIpAdress = [NSURL URLWithString:WS_GET_USERS_IP_SERVICE];
|
||||
NSString *contentOfURL = [NSString stringWithContentsOfURL:myIpAdress encoding:NSUTF8StringEncoding error:NULL];
|
||||
NSString *contentTrimmed = [contentOfURL stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
|
||||
|
||||
//NSLog(@"fetched IPv4: %@", contentTrimmed);
|
||||
return contentTrimmed;
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user