ibm-bluemix Getting Started with iOS Swift apps and Mobile Analytics for Bluemix


Example

  1. Create an instance of Mobile Analytics for Bluemix.
  2. Add the Bluemix Mobile Services SDK to your iOS project.
  3. After installing the SDK, add these import statements at top of your AppDelegate.swift file:
import BMSCore
import BMSAnalytics
  1. Next you'll need to initialize and send mobile analytics in your didFinishLaunchingWithOptions method:

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        // Set api key, Bluemix region.  These are available in the Mobile Analytics console after you create an instance
        let api_key="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
        let bmixRegion=BMSClient.Region.usSouth
    
        //Provide a name for your app that will appear in the Mobile Analytics console.
        let appName="MyAppName"
    
        //Initialize Mobile Analytics in your Bluemix region
        BMSClient.sharedInstance.initialize(bluemixRegion: bmixRegion)
        Analytics.initialize(appName: appName, apiKey: api_key, hasUserContext: false,
    deviceEvents: DeviceEvent.LIFECYCLE)
    
       //Send analytics
       //Analytics.send()
    
       //Alternately send analytics and log to your xCode console
       Analytics.send { (response: Response?, error: NSError?) in
         if response?.statusCode == 201 {
            print("Successfully sent analytics: \(response?.responseText)")
         }
         else {
            print("Failed to send analytics: \(response?.responseText). Error: \(error?.localizedDescription)")
         }
       }
    
       return true
    }
    
  2. Add Analytics.send() into your app code everywhere you want the analytics stored in the app to be sent to the Mobile Analytics for Bluemix service.

  3. Run your app. You will see a new user and a new session in your analytics console.