Skip to content

Commit 1319a20

Browse files
jdeeE-B-Smith
authored andcommitted
BNCLog functions for Swift (#614)
* Added BNCLog functions for Swift. work in progress * Another approach: Added BNCLogMessageInternalSwift * Remove workaround for UIApplicationOpenURLOptionsKey problem (now fixed)
1 parent fc9cac1 commit 1319a20

File tree

8 files changed

+94
-6
lines changed

8 files changed

+94
-6
lines changed

Branch-SDK/Branch-SDK/BNCLog.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,14 @@ extern void BNCLogMessageInternal(
131131
...
132132
);
133133

134+
/// Swift-friendly wrapper for BNCLogMessageInternal
135+
extern void BNCLogMessageInternalSwift(
136+
BNCLogLevel logLevel,
137+
NSString *_Nonnull sourceFileName,
138+
NSUInteger sourceLineNumber,
139+
NSString *_Nonnull message
140+
);
141+
134142
/// This function synchronizes all outstanding log messages and writes them to the logging function
135143
/// set by BNCLogSetOutputFunction.
136144
extern void BNCLogFlushMessages();

Branch-SDK/Branch-SDK/BNCLog.m

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,15 @@ void BNCLogMessageInternal(
551551
}
552552
}
553553

554+
void BNCLogMessageInternalSwift(
555+
BNCLogLevel logLevel,
556+
NSString *_Nonnull file,
557+
NSUInteger lineNumber,
558+
NSString *_Nonnull message
559+
) {
560+
BNCLogMessageInternal(logLevel, file.UTF8String, (int)lineNumber, @"%@", message);
561+
}
562+
554563
void BNCLogFlushMessages() {
555564
if (BNCLogSynchronizeMessages()) {
556565
dispatch_sync(bnc_LogQueue, ^{

Examples/WebViewExample/WebViewExample.xcodeproj/project.pbxproj

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
/* Begin PBXBuildFile section */
1010
44B41F44A1783294F78E3DCC /* Pods_WebViewExample.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8529F4248FBBE54B064721F2 /* Pods_WebViewExample.framework */; };
11+
7B016F1F1E97F29000C34EBA /* BNCLog.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B016F1E1E97F29000C34EBA /* BNCLog.swift */; };
1112
7B132B5A1E8D7B43006FB28F /* BranchUniversalObject+PlanetData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B132B591E8D7B43006FB28F /* BranchUniversalObject+PlanetData.swift */; };
1213
7B132B5C1E8D878E006FB28F /* Style.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B132B5B1E8D878E006FB28F /* Style.swift */; };
1314
7B3B97841E8C1B650089C04B /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B3B97831E8C1B650089C04B /* AppDelegate.swift */; };
@@ -25,6 +26,7 @@
2526
/* Begin PBXFileReference section */
2627
6D6117909230007A384D2046 /* Pods-WebViewExample.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-WebViewExample.release.xcconfig"; path = "Pods/Target Support Files/Pods-WebViewExample/Pods-WebViewExample.release.xcconfig"; sourceTree = "<group>"; };
2728
73F43B2C303E9BF5A6714F1B /* Pods-WebViewExample.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-WebViewExample.debug.xcconfig"; path = "Pods/Target Support Files/Pods-WebViewExample/Pods-WebViewExample.debug.xcconfig"; sourceTree = "<group>"; };
29+
7B016F1E1E97F29000C34EBA /* BNCLog.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BNCLog.swift; sourceTree = "<group>"; };
2830
7B132B591E8D7B43006FB28F /* BranchUniversalObject+PlanetData.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "BranchUniversalObject+PlanetData.swift"; sourceTree = "<group>"; };
2931
7B132B5B1E8D878E006FB28F /* Style.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Style.swift; sourceTree = "<group>"; };
3032
7B3B97801E8C1B650089C04B /* WebViewExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = WebViewExample.app; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -63,6 +65,14 @@
6365
name = Frameworks;
6466
sourceTree = "<group>";
6567
};
68+
7B016F191E97F1BE00C34EBA /* Utilities */ = {
69+
isa = PBXGroup;
70+
children = (
71+
7B016F1E1E97F29000C34EBA /* BNCLog.swift */,
72+
);
73+
path = Utilities;
74+
sourceTree = "<group>";
75+
};
6676
7B3B97771E8C1B640089C04B = {
6777
isa = PBXGroup;
6878
children = (
@@ -84,6 +94,7 @@
8494
7B3B97821E8C1B650089C04B /* WebViewExample */ = {
8595
isa = PBXGroup;
8696
children = (
97+
7B016F191E97F1BE00C34EBA /* Utilities */,
8798
7B3B97AD1E8C40400089C04B /* WebViewExample.entitlements */,
8899
7B3B97831E8C1B650089C04B /* AppDelegate.swift */,
89100
7B3B97971E8C21F60089C04B /* ArticleListViewController.swift */,
@@ -146,6 +157,7 @@
146157
TargetAttributes = {
147158
7B3B977F1E8C1B650089C04B = {
148159
CreatedOnToolsVersion = 8.3;
160+
LastSwiftMigration = 0830;
149161
ProvisioningStyle = Automatic;
150162
SystemCapabilities = {
151163
com.apple.SafariKeychain = {
@@ -246,6 +258,7 @@
246258
7B3B979A1E8C23AA0089C04B /* PlanetData.swift in Sources */,
247259
7B3B97961E8C21510089C04B /* NavigationController.swift in Sources */,
248260
7B3B97AC1E8C32BD0089C04B /* ArticleView.swift in Sources */,
261+
7B016F1F1E97F29000C34EBA /* BNCLog.swift in Sources */,
249262
7B132B5C1E8D878E006FB28F /* Style.swift in Sources */,
250263
7B3B97841E8C1B650089C04B /* AppDelegate.swift in Sources */,
251264
);
@@ -373,12 +386,14 @@
373386
baseConfigurationReference = 73F43B2C303E9BF5A6714F1B /* Pods-WebViewExample.debug.xcconfig */;
374387
buildSettings = {
375388
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
389+
CLANG_ENABLE_MODULES = YES;
376390
CODE_SIGN_ENTITLEMENTS = WebViewExample/WebViewExample.entitlements;
377391
DEVELOPMENT_TEAM = "";
378392
INFOPLIST_FILE = WebViewExample/Info.plist;
379393
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
380394
PRODUCT_BUNDLE_IDENTIFIER = io.branch.WebViewExample;
381395
PRODUCT_NAME = "$(TARGET_NAME)";
396+
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
382397
SWIFT_VERSION = 3.0;
383398
TARGETED_DEVICE_FAMILY = "1,2";
384399
};
@@ -389,6 +404,7 @@
389404
baseConfigurationReference = 6D6117909230007A384D2046 /* Pods-WebViewExample.release.xcconfig */;
390405
buildSettings = {
391406
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
407+
CLANG_ENABLE_MODULES = YES;
392408
CODE_SIGN_ENTITLEMENTS = WebViewExample/WebViewExample.entitlements;
393409
DEVELOPMENT_TEAM = "";
394410
INFOPLIST_FILE = WebViewExample/Info.plist;

Examples/WebViewExample/WebViewExample/AppDelegate.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
1717
// MARK: - UIApplicationDelegate methods
1818

1919
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
20+
BNCLogSetOutputFunction(nil)
21+
2022
// Store the NavigationController for later link routing.
2123
navigationController = window?.rootViewController as? NavigationController
2224

2325
// Initialize Branch SDK
2426
Branch.getInstance().initSession(launchOptions: launchOptions) {
2527
(buo: BranchUniversalObject?, linkProperties: BranchLinkProperties?, error: Error?) in
2628
guard error == nil else {
27-
print("Error from Branch: \(error!)")
29+
BNCLogError("Error from Branch: \(error!)")
2830
return
2931
}
3032

Examples/WebViewExample/WebViewExample/ArticleView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class ArticleView: UIView, WKNavigationDelegate {
6363

6464
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
6565
hud.hide(animated: true)
66-
print("could not load \(planetData.url): \(error)")
66+
BNCLogError("could not load \(planetData.url): \(error)")
6767
}
6868

6969
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {

Examples/WebViewExample/WebViewExample/ArticleViewController.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class ArticleViewController: UIViewController, ArticleViewDelegate {
6464

6565
// Log a BNCRegisterViewEvent each time the user views the page.
6666
buo.userCompletedAction(BNCRegisterViewEvent)
67-
print("Logged BNCRegisterViewEvent on BUO")
67+
BNCLog("Logged BNCRegisterViewEvent on BUO")
6868
}
6969

7070
// MARK: - ArticleViewDelegate
@@ -79,7 +79,7 @@ class ArticleViewController: UIViewController, ArticleViewDelegate {
7979

8080
buo.showShareSheet(with: linkProperties, andShareText: "Read about the planet \(planetData.title).", from: self) {
8181
channel, success in
82-
print("Share to channel \(channel ?? "(nil)") complete. success = \(success)")
82+
BNCLog("Share to channel \(channel ?? "(nil)") complete. success = \(success)")
8383
}
8484
}
8585

@@ -89,6 +89,6 @@ class ArticleViewController: UIViewController, ArticleViewDelegate {
8989
// Initialization and configuration.
9090
buo = BranchUniversalObject(planetData: planetData)
9191

92-
print("Created Branch Universal Object")
92+
BNCLog("Created Branch Universal Object")
9393
}
9494
}

Examples/WebViewExample/WebViewExample/PlanetData.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ struct PlanetData {
4747
guard let title = branchUniversalObject.title,
4848
let urlString = branchUniversalObject.canonicalUrl,
4949
let url = URL(string: urlString) else {
50-
print("Could not get required data from BranchUniversalObject")
50+
BNCLogWarning("Could not get required data from BranchUniversalObject")
5151
return nil
5252
}
5353

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//
2+
// BNCLog.swift
3+
// WebViewExample
4+
//
5+
// Created by Jimmy Dee on 4/7/17.
6+
// Copyright © 2017 Branch Metrics. All rights reserved.
7+
//
8+
9+
import Branch
10+
11+
/**
12+
* Logs a message at BNCLogLevel.debug
13+
* - Parameters:
14+
* - message: the message to log
15+
* - file: (unused) provides the Swift source file path
16+
* - line: (unused) provides the Swift line number
17+
*/
18+
func BNCLogDebug(_ message: String, _ file: String=#file, _ line: UInt=#line) {
19+
BNCLogMessageInternalSwift(.debug, file, line, message)
20+
}
21+
22+
/**
23+
* Logs a message at BNCLogLevel.error
24+
* - Parameters:
25+
* - message: the message to log
26+
* - file: (unused) provides the Swift source file path
27+
* - line: (unused) provides the Swift line number
28+
*/
29+
func BNCLogError(_ message: String, _ file: String=#file, _ line: UInt=#line) {
30+
BNCLogMessageInternalSwift(.error, file, line, message)
31+
}
32+
33+
/**
34+
* Logs a message at BNCLogLevel.log
35+
* - Parameters:
36+
* - message: the message to log
37+
* - file: (unused) provides the Swift source file path
38+
* - line: (unused) provides the Swift line number
39+
*/
40+
func BNCLog(_ message: String, _ file: String=#file, _ line: UInt=#line) {
41+
BNCLogMessageInternalSwift(.log, file, line, message)
42+
}
43+
44+
/**
45+
* Logs a message at BNCLogLevel.warning
46+
* - Parameters:
47+
* - message: the message to log
48+
* - file: (unused) provides the Swift source file path
49+
* - line: (unused) provides the Swift line number
50+
*/
51+
func BNCLogWarning(_ message: String, _ file: String=#file, _ line: UInt=#line) {
52+
BNCLogMessageInternalSwift(.warning, file, line, message)
53+
}

0 commit comments

Comments
 (0)