Skip to content

Commit

Permalink
Merge pull request #22 from one-signal/fix_missing_additional_data_me…
Browse files Browse the repository at this point in the history
…ta_fields

Added geotagging
  • Loading branch information
jkasten2 committed Mar 28, 2016
2 parents 8014bc5 + 343491b commit 81bd792
Show file tree
Hide file tree
Showing 8 changed files with 224 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
* limitations under the License.
*/

#import <Foundation/Foundation.h>
#import <objc/runtime.h>

typedef void (^OneSignalResultSuccessBlock)(NSDictionary* result);
typedef void (^OneSignalFailureBlock)(NSError* error);
typedef void (^OneSignalIdsAvailableBlock)(NSString* userId, NSString* pushToken);
Expand Down Expand Up @@ -105,5 +102,7 @@ typedef NS_ENUM(NSUInteger, ONE_S_LOG_LEVEL) {
- (void)postNotification:(NSDictionary*)jsonData onSuccess:(OneSignalResultSuccessBlock)successBlock onFailure:(OneSignalFailureBlock)failureBlock;
- (void)postNotificationWithJsonString:(NSString*)jsonData onSuccess:(OneSignalResultSuccessBlock)successBlock onFailure:(OneSignalFailureBlock)failureBlock;

- (void)promptLocation;

@end

Binary file modified iOS_SDK/Framework/OneSignal.framework/Versions/A/OneSignal
Binary file not shown.
6 changes: 6 additions & 0 deletions iOS_SDK/OneSignal.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
37E277281ACCA5B300D49C8D /* GameThrive.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 37E277241ACBB58D00D49C8D /* GameThrive.h */; };
37E2772A1ACCA67400D49C8D /* OneSignal.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 37E277041ACB940000D49C8D /* OneSignal.h */; };
37E6B2BB19D9CAF300D0C601 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 37E6B2BA19D9CAF300D0C601 /* UIKit.framework */; };
91F2D1031C94D10F00DE95D0 /* OneSignalLocation.m in Sources */ = {isa = PBXBuildFile; fileRef = 91F2D1021C94D10F00DE95D0 /* OneSignalLocation.m */; };
/* End PBXBuildFile section */

/* Begin PBXCopyFilesBuildPhase section */
Expand Down Expand Up @@ -75,6 +76,8 @@
37E277241ACBB58D00D49C8D /* GameThrive.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GameThrive.h; sourceTree = "<group>"; };
37E277251ACBB58D00D49C8D /* GameThrive.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GameThrive.m; sourceTree = "<group>"; };
37E6B2BA19D9CAF300D0C601 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
91F2D1011C94D0DD00DE95D0 /* OneSignalLocation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OneSignalLocation.h; sourceTree = "<group>"; };
91F2D1021C94D10F00DE95D0 /* OneSignalLocation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OneSignalLocation.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -147,6 +150,8 @@
37747FBD1914837300558FAD /* OneSignalHTTPClient.m */,
371FDED819F1A486001479B7 /* OneSignalTrackIAP.m */,
371FDEDA19F1F9D9001479B7 /* OneSignalTrackIAP.h */,
91F2D1011C94D0DD00DE95D0 /* OneSignalLocation.h */,
91F2D1021C94D10F00DE95D0 /* OneSignalLocation.m */,
);
path = OneSignal;
sourceTree = "<group>";
Expand Down Expand Up @@ -245,6 +250,7 @@
37D6E07F1B1EAE6500EC3624 /* OneSignalMobileProvision.m in Sources */,
37668EE91AA80B7E0095D813 /* OneSignalReachability.m in Sources */,
37E277051ACB940000D49C8D /* OneSignal.m in Sources */,
91F2D1031C94D10F00DE95D0 /* OneSignalLocation.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Binary file not shown.
5 changes: 2 additions & 3 deletions iOS_SDK/OneSignal/OneSignal.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
* limitations under the License.
*/

#import <Foundation/Foundation.h>
#import <objc/runtime.h>

typedef void (^OneSignalResultSuccessBlock)(NSDictionary* result);
typedef void (^OneSignalFailureBlock)(NSError* error);
typedef void (^OneSignalIdsAvailableBlock)(NSString* userId, NSString* pushToken);
Expand Down Expand Up @@ -105,5 +102,7 @@ typedef NS_ENUM(NSUInteger, ONE_S_LOG_LEVEL) {
- (void)postNotification:(NSDictionary*)jsonData onSuccess:(OneSignalResultSuccessBlock)successBlock onFailure:(OneSignalFailureBlock)failureBlock;
- (void)postNotificationWithJsonString:(NSString*)jsonData onSuccess:(OneSignalResultSuccessBlock)successBlock onFailure:(OneSignalFailureBlock)failureBlock;

- (void)promptLocation;

@end

87 changes: 86 additions & 1 deletion iOS_SDK/OneSignal/OneSignal.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#import "OneSignalJailbreakDetection.h"
#import "OneSignalReachability.h"
#import "OneSignalMobileProvision.h"
#import "OneSignalLocation.h"

#import <stdlib.h>
#import <stdio.h>
Expand Down Expand Up @@ -60,7 +61,7 @@ @interface OneSignal ()

@implementation OneSignal

NSString* const ONESIGNAL_VERSION = @"011103";
NSString* const ONESIGNAL_VERSION = @"011200";

@synthesize app_id = _GT_publicKey;
@synthesize httpClient = _GT_httpRequest;
Expand Down Expand Up @@ -90,6 +91,21 @@ @implementation OneSignal
bool mSubscriptionSet = true;
static NSString* mSDKType = @"native";


typedef struct os_location_coordinate {
double latitude;
double longitude;
} os_location_coordinate;

typedef struct os_last_location {
os_location_coordinate cords;
double verticalAccuracy;
double horizontalAccuracy;
} os_last_location;

static os_last_location *lastLocation;
static bool location_event_fired;

+ (void)setMSDKType:(NSString*)str {
mSDKType = str;
}
Expand Down Expand Up @@ -133,6 +149,8 @@ - (id)initWithLaunchOptions:(NSDictionary*)launchOptions appId:(NSString*)appId
onesignal_Log(ONE_S_LL_WARN, @"OneSignal Example AppID detected, please update to your app's id found on OneSignal.com");


[OneSignalLocation getLocation:self prompt:false];

if (self) {

handleNotification = callback;
Expand Down Expand Up @@ -421,6 +439,14 @@ - (void)registerUser {
NSData* postData = [NSJSONSerialization dataWithJSONObject:dataDic options:0 error:nil];
[request setHTTPBody:postData];

if (lastLocation) {
dataDic[@"lat"] = [NSNumber numberWithDouble:lastLocation->cords.latitude];
dataDic[@"long"] = [NSNumber numberWithDouble:lastLocation->cords.longitude];
dataDic[@"loc_acc_vert"] = [NSNumber numberWithDouble:lastLocation->verticalAccuracy];
dataDic[@"loc_acc"] = [NSNumber numberWithDouble:lastLocation->horizontalAccuracy];
lastLocation = nil;
}

[self enqueueRequest:request onSuccess:^(NSDictionary* results) {
oneSignalReg = true;
waitingForOneSReg = false;
Expand All @@ -435,6 +461,7 @@ - (void)registerUser {

if (tagsToSend != nil) {
[self sendTags:tagsToSend];
[self sendLocation:lastLocation];
tagsToSend = nil;
}

Expand Down Expand Up @@ -1147,6 +1174,64 @@ - (void)processLocalActionBasedNotification:(UILocalNotification*) notification
}
}

- (void) promptLocation {
[OneSignalLocation getLocation:self prompt:true];
}


- (void)locationManager:(id)manager didUpdateLocations:(NSArray*)locations {
[manager performSelector:@selector(stopUpdatingLocation)];

if (location_event_fired)
return;

location_event_fired = true;

id location = locations.lastObject;

SEL cord_selector = NSSelectorFromString(@"coordinate");
os_location_coordinate cords;
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[[location class] instanceMethodSignatureForSelector:cord_selector]];

[invocation setTarget:locations.lastObject];
[invocation setSelector:cord_selector];
[invocation invoke];
[invocation getReturnValue:&cords];

os_last_location *currentLocation = (os_last_location*)malloc(sizeof(os_last_location));
currentLocation->verticalAccuracy = [[location valueForKey:@"verticalAccuracy"] doubleValue];
currentLocation->horizontalAccuracy = [[location valueForKey:@"horizontalAccuracy"] doubleValue];
currentLocation->cords = cords;

if (mUserId == nil) {
lastLocation = currentLocation;
return;
}

[self sendLocation:currentLocation];
}

- (void) sendLocation:(os_last_location*)location {

NSMutableURLRequest* request = [self.httpClient requestWithMethod:@"PUT" path:[NSString stringWithFormat:@"players/%@", mUserId]];

NSDictionary* dataDic = [NSDictionary dictionaryWithObjectsAndKeys:
self.app_id, @"app_id",
[NSNumber numberWithDouble:location->cords.latitude], @"lat",
[NSNumber numberWithDouble:location->cords.longitude], @"long",
[NSNumber numberWithDouble:location->verticalAccuracy], @"loc_acc_vert",
[NSNumber numberWithDouble:location->horizontalAccuracy], @"loc_acc",
getNetType(), @"net_type",
nil];

NSData* postData = [NSJSONSerialization dataWithJSONObject:dataDic options:0 error:nil];
[request setHTTPBody:postData];

[self enqueueRequest:request
onSuccess:nil
onFailure:nil];
}

@end


Expand Down
35 changes: 35 additions & 0 deletions iOS_SDK/OneSignal/OneSignalLocation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Modified MIT License
*
* Copyright 2016 OneSignal
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* 1. The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 2. All copies of substantial portions of the Software may only be used in connection
* with services provided by OneSignal.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#ifndef OneSignalLocation_h
#define OneSignalLocation_h

@interface OneSignalLocation : NSObject
+ (void) getLocation:(id)delegate prompt:(bool)prompt;
@end

#endif /* OneSignalLocation_h */
93 changes: 93 additions & 0 deletions iOS_SDK/OneSignal/OneSignalLocation.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
* Modified MIT License
*
* Copyright 2016 OneSignal
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* 1. The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 2. All copies of substantial portions of the Software may only be used in connection
* with services provided by OneSignal.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

#import "OneSignalLocation.h"

@implementation OneSignalLocation

static id locationManager;
static bool started = false;
static bool hasDelayed = false;

// CoreLocation must be statically linked for geotagging to work on iOS 6 and possibly 7.
// plist NSLocationUsageDescription (iOS 6 & 7) and NSLocationWhenInUseUsageDescription (iOS 8+) keys also required.

// Suppressing undeclared selector warnings
// NSClassFromString and performSelector are used so OneSignal does not depend on CoreLocation to link the app.
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wundeclared-selector"
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"

+ (void) getLocation:(id)delegate prompt:(bool)prompt {
if (hasDelayed)
[OneSignalLocation internalGetLocation:delegate prompt:prompt];
else {
// Delay required for locationServicesEnabled and authorizationStatus return the correct values when CoreLocation is not staticly linked.
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 2.0 * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void) {
hasDelayed = true;
[OneSignalLocation internalGetLocation:delegate prompt:prompt];
});
}
}

+ (void) internalGetLocation:(id)delegate prompt:(bool)prompt {
NSLog(@"getLocation called:started:%d", started);
if (started)
return;

id clLocationManagerClass = NSClassFromString(@"CLLocationManager");

// Check for location in plist
if (![clLocationManagerClass performSelector:@selector(locationServicesEnabled)])
return;

if ([clLocationManagerClass performSelector:@selector(authorizationStatus)] == 0 && !prompt)
return;

locationManager = [[clLocationManagerClass alloc] init];
[locationManager setValue:delegate forKey:@"delegate"];
//locationManager.distanceFilter = kCLDistanceFilterNone;
//locationManager.desiredAccuracy = kCLLocationAccuracyBest;

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
[locationManager performSelector:@selector(requestWhenInUseAuthorization)];

// iOS 6 and 7 prompts for location here.
[locationManager performSelector:@selector(startUpdatingLocation)];

started = true;
}

#pragma clang diagnostic pop
#pragma GCC diagnostic pop

@end

0 comments on commit 81bd792

Please sign in to comment.