iOS SDK - Advanced Options

Advanced Options

Manually Managing Sessions

By default, if the app runs in the background for 60 seconds or more before returning to the foreground, the SDK will register a new session. To change this timeout value, use the setSessionTimeout method and add it to the Config.

setSessionTimeout Method
Description Change the session timeout value.
Signature (void)setSessionTimeout:(int)timeout
Usage Example
SwiftObjective-C
func getConfig() -> SingularConfig? {       
  // Singular Config Options     

  guard let config = SingularConfig(apiKey: Constants.APIKEY, andSecret:
    Constants.SECRET) else {         
      return nil     
      }     
  //...
  Singular.setSessionTimeout(120)      
  //...
  return config
}

Using the JavaScript Interface

Singular provides a JavaScript interface that you can use to call Singular in your app.

For example, if you set up the JavaScript interface, you can send events to Singular from JavaScript code as follows:

JavaScript
Singular.event('event');
Singular.event('test', JSON.stringify({"a1":"bar", "a2":"boo", "a3":"baz"}));

Supported Methods in JavaScript

The interface supports the following SDK methods:

  • setCustomUserID
  • unsetCustomUserID
  • event
  • revenue

Enabling the JavaScript Interface

Note: Starting in iOS 8.0+ Apple recommends using WKWebView to add web content to your app. Do not use UIWebView or WebView. See Apple's WKWebView documentation for more information.

To enable the JavaScript interface when using WKWebView, you need to add some code to the webView method of the WKNavigationDelegate protocol (this protocol helps you implement custom behaviors triggered when a web view handles a navigation request).

Swift
extension ViewController: WKNavigationDelegate { 
  func webView(_: WKWebView, decidePolicyFor: WKNavigationAction, 
    decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) { 
      // Singular handler 

      let js = "typeof(Singular)" 
      webView.evaluateJavaScript(js) { (result, error) -> Void in 
        if let resultString = result as? String { 
          if resultString.isEqual("undefined") { 
            do { 
              let contents = try String(contentsOfFile: 
                Bundle.main.path(forResource: "Singular", ofType: "js")!) 
              self.webView.evaluateJavaScript(contents, completionHandler: nil) 
            } catch { } 
          } else { 
            print(decidePolicyFor.request) 
            Singular.processJSRequestWK(self.webView, withURL:decidePolicyFor.request) 
          } 
        }  
      }
      // rest of your code goes here   

    }
}