iOS Swift 应用程序和 Bluemix Mobile Analytics 入门
- 为 Bluemix 创建 Mobile Analytics 实例。
- 将 Bluemix Mobile Services SDK 添加到你的 iOS 项目中。
- 安装 SDK 后,在 AppDelegate.swift 文件的顶部添加这些 import 语句:
import BMSCore
import BMSAnalytics
-
接下来,你需要在 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 }
-
将
Analytics.send()
添加到你的应用代码中,你希望将应用中存储的分析发送到 Mobile Analytics for Bluemix 服务。 -
运行你的应用程序你将在分析控制台中看到新用户和新会话。