iOS Swift 應用程式和 Bluemix Mobile Analytics 入門

  1. 為 Bluemix 建立 Mobile Analytics 例項。
  2. 將 Bluemix Mobile Services SDK 新增到你的 iOS 專案中。
  3. 安裝 SDK 後,在 AppDelegate.swift 檔案的頂部新增這些 import 語句:
import BMSCore
import BMSAnalytics
  1. 接下來,你需要在 didFinishLaunchingWithOptions 方法中初始化併傳送移動分析:

    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. Analytics.send() 新增到你的應用程式碼中,你希望將應用中儲存的分析傳送到 Mobile Analytics for Bluemix 服務。

  3. 執行你的應用程式你將在分析控制檯中看到新使用者和新會話。