Skip to content

Commit 08918b2

Browse files
authored
Merge pull request #899 from BranchMetrics/SDK-76-nsexception-issue
SDK-76 nsexception in setBranchKey
2 parents 0c166db + 8b52ff1 commit 08918b2

File tree

6 files changed

+349
-10
lines changed

6 files changed

+349
-10
lines changed

Branch-SDK/Branch-SDK/Branch.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,24 @@ typedef NS_ENUM(NSUInteger, BranchCreditHistoryOrder) {
225225
You can only set the Branch key once per app run.
226226
227227
@param branchKey The Branch key to use.
228+
@param error NSError will be set if Branch encounters a key error.
228229
*/
230+
+ (void) setBranchKey:(NSString*)branchKey error:(NSError **)error;
231+
232+
/**
233+
Directly sets the Branch key to be used. Branch usually reads the Branch key from your app's
234+
Info.plist file which is recommended and more convenient. But the Branch key can also be set
235+
with this method. See the documentation at
236+
https://dev.branch.io/getting-started/sdk-integration-guide/guide/ios/#configure-xcode-project
237+
for information about configuring your app with Branch keys.
238+
239+
You can only set the Branch key once per app run. Any errors are logged.
240+
241+
@param branchKey The Branch key to use.
242+
*/
229243
+ (void) setBranchKey:(NSString*)branchKey;
230244

245+
231246
/// @return Returns the current Branch key.
232247
+ (NSString*) branchKey;
233248

Branch-SDK/Branch-SDK/Branch.m

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,10 @@ + (BranchActivityItemProvider *)getBranchActivityItemWithParams:(NSDictionary *)
339339
static NSString *bnc_branchKey = nil;
340340
static BOOL bnc_enableFingerprintIDInCrashlyticsReports = YES;
341341

342+
+ (void) resetBranchKey {
343+
bnc_branchKey = nil;
344+
}
345+
342346
+ (void) setUseTestBranchKey:(BOOL)useTestKey {
343347
@synchronized (self) {
344348
if (bnc_branchKey && !!useTestKey != !!bnc_useTestBranchKey) {
@@ -355,21 +359,34 @@ + (BOOL) useTestBranchKey {
355359
}
356360
}
357361

358-
+ (void) setBranchKey:(NSString*)branchKey {
362+
+ (void)setBranchKey:(NSString *)branchKey {
363+
NSError *error;
364+
[self setBranchKey:branchKey error:&error];
365+
366+
if (error) {
367+
BNCLogError(@"Branch init error: %@", error.localizedDescription);
368+
}
369+
}
370+
371+
+ (void)setBranchKey:(NSString*)branchKey error:(NSError **)error {
359372
@synchronized (self) {
360373
if (bnc_branchKey) {
361374
if (branchKey &&
362375
[branchKey isKindOfClass:[NSString class]] &&
363376
[branchKey isEqualToString:bnc_branchKey]) {
364377
return;
365378
}
366-
BNCLogError(@"Branch key can only be set once.");
379+
380+
NSString *errorMessage = [NSString stringWithFormat:@"Branch key can only be set once."];
381+
*error = [NSError branchErrorWithCode:BNCInitError localizedMessage:errorMessage];
367382
return;
368383
}
384+
369385
if (![branchKey isKindOfClass:[NSString class]]) {
370386
NSString *typeName = (branchKey) ? NSStringFromClass(branchKey.class) : @"<nil>";
371-
[NSException raise:NSInternalInconsistencyException
372-
format:@"Invalid Branch key of type '%@'.", typeName];
387+
388+
NSString *errorMessage = [NSString stringWithFormat:@"Invalid Branch key of type '%@'.", typeName];
389+
*error = [NSError branchErrorWithCode:BNCInitError localizedMessage:errorMessage];
373390
return;
374391
}
375392

@@ -379,12 +396,13 @@ + (void) setBranchKey:(NSString*)branchKey {
379396
@"You are using your test app's Branch Key. "
380397
"Remember to change it to live Branch Key for production deployment."
381398
);
382-
} else
383-
if ([branchKey hasPrefix:@"key_live"]) {
399+
400+
} else if ([branchKey hasPrefix:@"key_live"]) {
384401
bnc_useTestBranchKey = NO;
402+
385403
} else {
386-
[NSException raise:NSInternalInconsistencyException
387-
format:@"Invalid Branch key format. Did you add your Branch key to your Info.plist? Passed key is '%@'.", branchKey];
404+
NSString *errorMessage = [NSString stringWithFormat:@"Invalid Branch key format. Did you add your Branch key to your Info.plist? Passed key is '%@'.", branchKey];
405+
*error = [NSError branchErrorWithCode:BNCInitError localizedMessage:errorMessage];
388406
return;
389407
}
390408

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
//
2+
// Branch_setBranchKeyTests.m
3+
// Branch-SDK-Unhosted-Tests
4+
//
5+
// Created by Ernest Cho on 12/3/18.
6+
// Copyright © 2018 Branch, Inc. All rights reserved.
7+
//
8+
9+
#import <XCTest/XCTest.h>
10+
#import "Branch.h"
11+
12+
// expose private methods used by tests
13+
@interface Branch()
14+
+ (void) resetBranchKey;
15+
@end
16+
17+
// Tests pre-init methods. These run unhosted in order to avoid initialization that the test app does.
18+
@interface Branch_setBranchKeyTests : XCTestCase
19+
20+
@end
21+
22+
@implementation Branch_setBranchKeyTests
23+
24+
- (void)setUp {
25+
[Branch resetBranchKey];
26+
}
27+
28+
- (void)tearDown {
29+
30+
}
31+
32+
- (void)testSetBranchKey_validKey {
33+
NSString *testKey = @"key_live_foo";
34+
35+
// previous implementation would throw exceptions on misconfiguration, this is deprecated behavior.
36+
XCTAssertNoThrow([Branch setBranchKey:testKey]);
37+
XCTAssert([[Branch branchKey] isEqualToString:testKey]);
38+
}
39+
40+
- (void)testSetBranchKey_nilKey {
41+
NSString *testKey = nil;
42+
43+
// previous implementation would throw exceptions on misconfiguration, this is deprecated behavior.
44+
XCTAssertNoThrow([Branch setBranchKey:testKey]);
45+
XCTAssert([Branch branchKey] == nil);
46+
}
47+
48+
- (void)testSetBranchKey_invalidKey {
49+
NSString *testKey = @"invalid_key";
50+
51+
// previous implementation would throw exceptions on misconfiguration, this is deprecated behavior.
52+
XCTAssertNoThrow([Branch setBranchKey:testKey]);
53+
XCTAssert([Branch branchKey] == nil);
54+
}
55+
56+
- (void)testSetBranchKeyWithError_validKey {
57+
NSString *testKey = @"key_live_foo";
58+
59+
NSError *error = nil;
60+
[Branch setBranchKey:testKey error:&error];
61+
XCTAssertNil(error);
62+
63+
XCTAssert([[Branch branchKey] isEqualToString:testKey]);
64+
}
65+
66+
- (void)testSetBranchKeyWithError_nilKey {
67+
NSError *error = nil;
68+
[Branch setBranchKey:nil error:&error];
69+
XCTAssertNotNil(error);
70+
71+
XCTAssert([error.localizedFailureReason isEqualToString:@"Invalid Branch key of type '<nil>'."]);
72+
XCTAssert([Branch branchKey] == nil);
73+
}
74+
75+
- (void)testSetBranchKeyWithError_invalidKey {
76+
NSError *error = nil;
77+
[Branch setBranchKey:@"invalid_key" error:&error];
78+
XCTAssertNotNil(error);
79+
80+
XCTAssert([error.localizedFailureReason isEqualToString:@"Invalid Branch key format. Did you add your Branch key to your Info.plist? Passed key is 'invalid_key'."]);
81+
XCTAssert([Branch branchKey] == nil);
82+
}
83+
84+
- (void)testSetBranchKeyWithError_validKeyTwice {
85+
NSString *testKey = @"key_live_foo";
86+
87+
NSError *error = nil;
88+
[Branch setBranchKey:testKey error:&error];
89+
XCTAssertNil(error);
90+
XCTAssert([[Branch branchKey] isEqualToString:testKey]);
91+
92+
// Cannot change the key after it is set once
93+
[Branch setBranchKey:@"key_live_bar" error:&error];
94+
XCTAssertNotNil(error);
95+
XCTAssert([error.localizedFailureReason isEqualToString:@"Branch key can only be set once."]);
96+
XCTAssert([[Branch branchKey] isEqualToString:testKey]);
97+
}
98+
99+
@end
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleDevelopmentRegion</key>
6+
<string>$(DEVELOPMENT_LANGUAGE)</string>
7+
<key>CFBundleExecutable</key>
8+
<string>$(EXECUTABLE_NAME)</string>
9+
<key>CFBundleIdentifier</key>
10+
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
11+
<key>CFBundleInfoDictionaryVersion</key>
12+
<string>6.0</string>
13+
<key>CFBundleName</key>
14+
<string>$(PRODUCT_NAME)</string>
15+
<key>CFBundlePackageType</key>
16+
<string>BNDL</string>
17+
<key>CFBundleShortVersionString</key>
18+
<string>1.0</string>
19+
<key>CFBundleVersion</key>
20+
<string>1</string>
21+
</dict>
22+
</plist>

0 commit comments

Comments
 (0)