Skip to content

Commit eb00d9d

Browse files
authored
Merge pull request #927 from BranchMetrics/SDK-208-apple-receipt
SDK-208 apple receipt
2 parents 7dcb5d0 + 8df03a7 commit eb00d9d

File tree

10 files changed

+140
-3
lines changed

10 files changed

+140
-3
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//
2+
// BNCAppleReceiptTests.m
3+
// Branch-SDK-Tests
4+
//
5+
// Created by Ernest Cho on 7/15/19.
6+
// Copyright © 2019 Branch, Inc. All rights reserved.
7+
//
8+
9+
#import <XCTest/XCTest.h>
10+
#import "BNCAppleReceipt.h"
11+
12+
@interface BNCAppleReceiptTests : XCTestCase
13+
14+
@end
15+
16+
@implementation BNCAppleReceiptTests
17+
18+
- (void)setUp {
19+
// Put setup code here. This method is called before the invocation of each test method in the class.
20+
}
21+
22+
- (void)tearDown {
23+
// Put teardown code here. This method is called after the invocation of each test method in the class.
24+
}
25+
26+
- (void)testReceiptIsNil {
27+
BNCAppleReceipt *receipt = [BNCAppleReceipt new];
28+
XCTAssertNil([receipt installReceipt]);
29+
XCTAssertFalse([receipt isTestFlight]);
30+
}
31+
32+
@end

Branch-SDK-Tests/BranchInstallRequestTests.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ - (void)testRequestBody {
8888
@"link_identifier": @"foo-link-id",
8989
@"first_install_time": BNCWireFormatFromDate(appDate),
9090
@"uri_scheme": @"foo-uri-scheme",
91-
@"update": @0
91+
@"update": @0,
92+
@"apple_testflight": @0
9293
}];
9394
if (!self.class.isApplication) expectedParams[@"ios_team_id"] = nil;
9495

Branch-SDK-Tests/BranchOpenRequestTests.m

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ - (void)testRequestBodyWithNoFingerprintID {
9393
@"lastest_update_time": BNCWireFormatFromDate(installDate),
9494
@"first_install_time": BNCWireFormatFromDate(installDate),
9595
@"previous_update_time": BNCWireFormatFromDate(updateDate),
96-
@"update": @1
96+
@"update": @1,
97+
@"apple_testflight": @0
9798
}];
9899
if (!self.class.isApplication) expectedParams[@"ios_team_id"] = nil;
99100

@@ -173,7 +174,8 @@ - (void)testRequestBodyWithFingerprintID {
173174
@"lastest_update_time": BNCWireFormatFromDate(installDate),
174175
@"first_install_time": BNCWireFormatFromDate(installDate),
175176
@"previous_update_time": BNCWireFormatFromDate(updateDate),
176-
@"update": @1
177+
@"update": @1,
178+
@"apple_testflight": @0
177179
}];
178180
if (!self.class.isApplication) expectedParams[@"ios_team_id"] = nil;
179181

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//
2+
// BNCAppleReceipt.h
3+
// Branch
4+
//
5+
// Created by Ernest Cho on 7/11/19.
6+
// Copyright © 2019 Branch, Inc. All rights reserved.
7+
//
8+
9+
@import Foundation;
10+
11+
NS_ASSUME_NONNULL_BEGIN
12+
13+
@interface BNCAppleReceipt : NSObject
14+
15+
+ (BNCAppleReceipt *)instance;
16+
17+
// this is only available on builds from Apple
18+
- (nullable NSString *)installReceipt;
19+
- (BOOL)isTestFlight;
20+
21+
@end
22+
23+
NS_ASSUME_NONNULL_END
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//
2+
// BNCAppleReceipt.m
3+
// Branch
4+
//
5+
// Created by Ernest Cho on 7/11/19.
6+
// Copyright © 2019 Branch, Inc. All rights reserved.
7+
//
8+
9+
#import "BNCAppleReceipt.h"
10+
11+
@interface BNCAppleReceipt()
12+
@property (nonatomic, copy, readwrite) NSString *receipt;
13+
@property (nonatomic, assign, readwrite) BOOL isSandboxReceipt;
14+
@end
15+
16+
@implementation BNCAppleReceipt
17+
18+
+ (BNCAppleReceipt *)instance {
19+
static BNCAppleReceipt *singleton;
20+
static dispatch_once_t onceToken;
21+
dispatch_once(&onceToken, ^{
22+
singleton = [[BNCAppleReceipt alloc] init];
23+
});
24+
return singleton;
25+
}
26+
27+
- (instancetype)init {
28+
self = [super init];
29+
if (self) {
30+
[self readReceipt];
31+
}
32+
return self;
33+
}
34+
35+
- (void)readReceipt {
36+
NSURL *receiptURL = [[NSBundle mainBundle] appStoreReceiptURL];
37+
if (receiptURL) {
38+
self.isSandboxReceipt = [receiptURL.lastPathComponent isEqualToString:@"sandboxReceipt"];
39+
}
40+
41+
NSData *receiptData = [NSData dataWithContentsOfURL:receiptURL];
42+
if (receiptData) {
43+
self.receipt = [receiptData base64EncodedStringWithOptions:0];
44+
}
45+
}
46+
47+
- (nullable NSString *)installReceipt {
48+
return self.receipt;
49+
}
50+
51+
- (BOOL)isTestFlight {
52+
// sandbox receipts are from testflight
53+
return self.isSandboxReceipt;
54+
}
55+
56+
@end

Branch-SDK/Branch-SDK/BranchConstants.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ extern NSString * const BRANCH_REQUEST_KEY_IS_SIMULATOR;
6565
extern NSString * const BRANCH_REQUEST_KEY_LOG;
6666
extern NSString * const BRANCH_REQUEST_KEY_EXTERNAL_INTENT_URI;
6767
extern NSString * const BRANCH_REQUEST_KEY_INSTRUMENTATION;
68+
extern NSString * const BRANCH_REQUEST_KEY_APPLE_RECEIPT;
69+
extern NSString * const BRANCH_REQUEST_KEY_APPLE_TESTFLIGHT;
6870

6971
extern NSString * const BRANCH_REQUEST_ENDPOINT_SET_IDENTITY;
7072
extern NSString * const BRANCH_REQUEST_ENDPOINT_APP_LINK_SETTINGS;

Branch-SDK/Branch-SDK/BranchConstants.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@
6060
NSString * const BRANCH_REQUEST_KEY_IS_SIMULATOR = @"is_simulator";
6161
NSString * const BRANCH_REQUEST_KEY_LOG = @"log";
6262
NSString * const BRANCH_REQUEST_KEY_INSTRUMENTATION = @"instrumentation";
63+
NSString * const BRANCH_REQUEST_KEY_APPLE_RECEIPT = @"apple_receipt";
64+
NSString * const BRANCH_REQUEST_KEY_APPLE_TESTFLIGHT = @"apple_testflight";
6365

6466
NSString * const BRANCH_REQUEST_ENDPOINT_SET_IDENTITY = @"profile";
6567
NSString * const BRANCH_REQUEST_ENDPOINT_APP_LINK_SETTINGS = @"app-link-settings";

Branch-SDK/Branch-SDK/Networking/Requests/BranchInstallRequest.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#import "BNCStrongMatchHelper.h"
1414
#import "BNCEncodingUtils.h"
1515
#import "BNCApplication.h"
16+
#import "BNCAppleReceipt.h"
1617

1718
@implementation BranchInstallRequest
1819

@@ -37,6 +38,9 @@ - (void)makeRequest:(BNCServerInterface *)serverInterface key:(NSString *)key ca
3738
[self safeSetValue:preferenceHelper.spotlightIdentifier forKey:BRANCH_REQUEST_KEY_SPOTLIGHT_IDENTIFIER onDict:params];
3839
[self safeSetValue:preferenceHelper.universalLinkUrl forKey:BRANCH_REQUEST_KEY_UNIVERSAL_LINK_URL onDict:params];
3940

41+
[self safeSetValue:[[BNCAppleReceipt instance] installReceipt] forKey:BRANCH_REQUEST_KEY_APPLE_RECEIPT onDict:params];
42+
[self safeSetValue:[NSNumber numberWithBool:[[BNCAppleReceipt instance] isTestFlight]] forKey:BRANCH_REQUEST_KEY_APPLE_TESTFLIGHT onDict:params];
43+
4044
params[BRANCH_REQUEST_KEY_DEBUG] = @(preferenceHelper.isDebug);
4145

4246
if (preferenceHelper.appleSearchAdNeedsSend) {

Branch-SDK/Branch-SDK/Networking/Requests/BranchOpenRequest.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#import "BNCDeviceInfo.h"
1818
#import "Branch.h"
1919
#import "BNCApplication.h"
20+
#import "BNCAppleReceipt.h"
2021

2122
@interface BranchOpenRequest ()
2223
@property (assign, nonatomic) BOOL isInstall;
@@ -64,6 +65,8 @@ - (void)makeRequest:(BNCServerInterface *)serverInterface key:(NSString *)key ca
6465
if (preferenceHelper.limitFacebookTracking)
6566
params[@"limit_facebook_tracking"] = (__bridge NSNumber*) kCFBooleanTrue;
6667

68+
[self safeSetValue:[NSNumber numberWithBool:[[BNCAppleReceipt instance] isTestFlight]] forKey:BRANCH_REQUEST_KEY_APPLE_TESTFLIGHT onDict:params];
69+
6770
NSMutableDictionary *cdDict = [[NSMutableDictionary alloc] init];
6871
BranchContentDiscoveryManifest *contentDiscoveryManifest = [BranchContentDiscoveryManifest getInstance];
6972
[cdDict bnc_safeSetObject:[contentDiscoveryManifest getManifestVersion] forKey:BRANCH_MANIFEST_VERSION_KEY];

Branch-TestBed/Branch-TestBed.xcodeproj/project.pbxproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,9 @@
168168
5F67F48E228F535500067429 /* BNCEncodingUtilsTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 5F67F48D228F535500067429 /* BNCEncodingUtilsTests.m */; };
169169
5F8B7B4021B5F5CD009CE0A6 /* libBranch.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 466B58381B17773000A69EDE /* libBranch.a */; };
170170
5F8B7B4721B5F5F0009CE0A6 /* Branch_setBranchKeyTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 5F8B7B4621B5F5F0009CE0A6 /* Branch_setBranchKeyTests.m */; };
171+
5FC7326822D81002006E6FBC /* BNCAppleReceipt.h in Headers */ = {isa = PBXBuildFile; fileRef = 5FC7326622D81002006E6FBC /* BNCAppleReceipt.h */; };
172+
5FC7326922D81002006E6FBC /* BNCAppleReceipt.m in Sources */ = {isa = PBXBuildFile; fileRef = 5FC7326722D81002006E6FBC /* BNCAppleReceipt.m */; };
173+
5FC7327022DD1F93006E6FBC /* BNCAppleReceiptTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 5FC7326F22DD1F93006E6FBC /* BNCAppleReceiptTests.m */; };
171174
5FC7327722DE9A44006E6FBC /* BNCServerInterfaceTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 5FC7327622DE9A44006E6FBC /* BNCServerInterfaceTests.m */; };
172175
63E4C4881D25E16A00A45FD8 /* LogOutputViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 63E4C4871D25E16A00A45FD8 /* LogOutputViewController.m */; };
173176
63E4C48B1D25E17B00A45FD8 /* NavigationController.m in Sources */ = {isa = PBXBuildFile; fileRef = 63E4C48A1D25E17B00A45FD8 /* NavigationController.m */; };
@@ -392,6 +395,9 @@
392395
5F8B7B3B21B5F5CD009CE0A6 /* Branch-SDK-Unhosted-Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "Branch-SDK-Unhosted-Tests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
393396
5F8B7B3F21B5F5CD009CE0A6 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
394397
5F8B7B4621B5F5F0009CE0A6 /* Branch_setBranchKeyTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = Branch_setBranchKeyTests.m; sourceTree = "<group>"; };
398+
5FC7326622D81002006E6FBC /* BNCAppleReceipt.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BNCAppleReceipt.h; sourceTree = "<group>"; };
399+
5FC7326722D81002006E6FBC /* BNCAppleReceipt.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BNCAppleReceipt.m; sourceTree = "<group>"; };
400+
5FC7326F22DD1F93006E6FBC /* BNCAppleReceiptTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = BNCAppleReceiptTests.m; sourceTree = "<group>"; };
395401
5FC7327622DE9A44006E6FBC /* BNCServerInterfaceTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = BNCServerInterfaceTests.m; sourceTree = "<group>"; };
396402
63E4C4861D25E16A00A45FD8 /* LogOutputViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LogOutputViewController.h; sourceTree = "<group>"; };
397403
63E4C4871D25E16A00A45FD8 /* LogOutputViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LogOutputViewController.m; sourceTree = "<group>"; };
@@ -582,6 +588,7 @@
582588
4D16837A2098C901008819E3 /* Branch-SDK-Tests */ = {
583589
isa = PBXGroup;
584590
children = (
591+
5FC7326F22DD1F93006E6FBC /* BNCAppleReceiptTests.m */,
585592
4D1683972098C901008819E3 /* BNCApplication.Test.m */,
586593
4D7881FB209CF2D4002B750F /* BNCApplication+BNCTest.h */,
587594
4D7881FC209CF2D4002B750F /* BNCApplication+BNCTest.m */,
@@ -732,6 +739,8 @@
732739
670016BB1946309100A9E103 /* Branch-SDK */ = {
733740
isa = PBXGroup;
734741
children = (
742+
5FC7326622D81002006E6FBC /* BNCAppleReceipt.h */,
743+
5FC7326722D81002006E6FBC /* BNCAppleReceipt.m */,
735744
4D59B51B2006979B00F89FD5 /* BNCApplication.h */,
736745
4D59B51C2006979B00F89FD5 /* BNCApplication.m */,
737746
4D8999EA1DC108FF00F7EE0A /* BNCAvailability.h */,
@@ -864,6 +873,7 @@
864873
4DCAC8181F426F7C00405D1D /* BranchLinkProperties.h in Headers */,
865874
4DCAC81A1F426F7C00405D1D /* BranchUniversalObject.h in Headers */,
866875
4DCAC8191F426F7C00405D1D /* BranchShareLink.h in Headers */,
876+
5FC7326822D81002006E6FBC /* BNCAppleReceipt.h in Headers */,
867877
4D41123D1FE8976400C44FA5 /* BranchView.h in Headers */,
868878
4DE6491A1FE1D7F500226507 /* BNCFieldDefines.h in Headers */,
869879
4DCAC81C1F426F7C00405D1D /* BranchViewHandler.h in Headers */,
@@ -1126,6 +1136,7 @@
11261136
isa = PBXSourcesBuildPhase;
11271137
buildActionMask = 2147483647;
11281138
files = (
1139+
5FC7326922D81002006E6FBC /* BNCAppleReceipt.m in Sources */,
11291140
7D58823A1CA1DF2700FF6358 /* BNCDeviceInfo.m in Sources */,
11301141
7B18DF4A1F1F00E200C25C84 /* BNCCrashlyticsWrapper.m in Sources */,
11311142
7D0C971D1D875465004E14B1 /* BranchContentDiscoveryManifest.m in Sources */,
@@ -1242,6 +1253,7 @@
12421253
4D7881FF209CF2D4002B750F /* BNCApplication+BNCTest.m in Sources */,
12431254
4D1683C02098C902008819E3 /* BranchUniversalObject.Test.m in Sources */,
12441255
4D1683B52098C902008819E3 /* BNCLocalization.Test.m in Sources */,
1256+
5FC7327022DD1F93006E6FBC /* BNCAppleReceiptTests.m in Sources */,
12451257
4D1683C82098C902008819E3 /* NSString+Branch.Test.m in Sources */,
12461258
4D1683C22098C902008819E3 /* BranchUserCompletedActionTests.m in Sources */,
12471259
4D1683AA2098C902008819E3 /* BranchOpenRequestTests.m in Sources */,

0 commit comments

Comments
 (0)