{"__v":11,"_id":"55a612ae249a40190051d92d","category":{"__v":11,"_id":"55a589ddaaf9cf1900114dd0","pages":["55a590d580c8a30d00b32404","55a5915680c8a30d00b3240a","55a5917a0f354f0d00fd0345","55a5921d80c8a30d00b32413","55a5a5cb80c8a30d00b32448","55a5b03880c8a30d00b32458","55a611a0249a40190051d928","55a611ec80c8a30d00b32502","55a612ae249a40190051d92d","55a612faaaf9cf1900114f1f","55a911aa27a17d210052523f"],"project":"559a61d2dbcfd20d00710b39","version":"559a61d2dbcfd20d00710b3c","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2015-07-14T22:14:53.033Z","from_sync":false,"order":1,"slug":"ios-sdk","title":"iOS SDK"},"parentDoc":null,"project":"559a61d2dbcfd20d00710b39","user":"55a575ebaaf9cf1900114d73","version":{"__v":23,"_id":"559a61d2dbcfd20d00710b3c","project":"559a61d2dbcfd20d00710b39","createdAt":"2015-07-06T11:09:06.510Z","releaseDate":"2015-07-06T11:09:06.510Z","categories":["559a61d3dbcfd20d00710b3d","55a589ddaaf9cf1900114dd0","55a589e30f354f0d00fd0312","55a589ea80c8a30d00b323cc","55a589f6aaf9cf1900114dd1","55a58d4e80c8a30d00b323e6","55a8e7a227a17d21005251a2","55a93098cf45e1390093f351","55afb085f202b12100cd9e83","55afb22e902fd51700f5f8bf","55afb260902fd51700f5f8c2","55afb28ec8a85321007a5462","55afb294f202b12100cd9e95","55afb29b902fd51700f5f8c5","55afb2a1c8a85321007a5463","55afb2a7902fd51700f5f8c7","55afb2ad902fd51700f5f8c8","55afb2b5902fd51700f5f8ca","55b74b2131bccb190081bedc","55bb441b54f9640d006e6cf2","565711085cb2420d00d70071","5681681330018c0d006bf7ff","588f38b5923d610f00c72dad"],"is_deprecated":false,"is_hidden":false,"is_beta":true,"is_stable":true,"codename":"","version_clean":"1.0.0","version":"1"},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2015-07-15T07:58:38.276Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":4,"body":"The typical integration scenarios include handling of the following in-app events:\n* Launch app\n* User Login\n* User Logout\n* Receive Push Notification \n* Registered for Remote Push Notifications\n* Fetch Campaigns\n* Enter Screen\n* Custom Events\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Launch app\"\n}\n[/block]\nThis code sample shows how to initialize MOCA SDK on app startup and also handle situations when the app is started by opening a push notification. \n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions\\n{\\n   // Init MOCA SDK\\n   [MOCA initializeSDK];\\n   if (launchOptions != nil) {\\n       // Launched from push notification\\n       NSDictionary *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];\\n       if (notification) {\\n           // Notify MOCA the app started from push\\n           [MOCA handleRemoteNotification:notification];\\n       }\\n   }\\n   // Optionally, set guest properties\\n   MOCAInstance * currentInstance = [MOCA currentInstance];\\n   if (currentInstance) {\\n        [currentInstance setValue::::at:::\\\"property_1_value\\\" forProperty:@\\\"guest_property_1\\\"];\\n        [currentInstance setValue:@\\\"property_2_value\\\" forProperty:@\\\"guest_property_2\\\"];\\n        [currentInstance setValue:[NSNumber numberWithDouble:property3_value] forProperty:@\\\"guest_property_3\\\"];\\n     \\n        // Submit change to the cloud \\n        [currentInstance saveWithBlock:^(MOCAInstance *instance, NSError *error) {\\n            if (error) {\\n               NSLog(@\\\"Save instance failed: %@\\\", error);\\n            }\\n        }];\\n   }\\n}\\n\",\n      \"language\": \"objectivec\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"User log-in\"\n}\n[/block]\nThis code sample shows how to inform MOCA that a user has logged into the app. This call should be executed each time the user performs the log in, even if the app persists user session on app close. \n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"   // Notify MOCA, the user logged in. Must be called after each app launch.\\n\\n   NSString * userId = ...\\n   MOCAUser *user = [[MOCA currentInstance] login:loginMgr.username];\\n   \\n   [user setValue:@”paidComplete\\\" forProperty:@\\\"paymentStatus\\\"];\\n   [user setValue:@\\\"...\\\" forProperty:@\\\"pictureStatus\\\"];\\n   [user setValue:@\\\"...\\\" forProperty:@\\\"merchantId\\\"];\\n   [user setValue:@\\\"...\\\" forProperty:@\\\"jobTitle\\\"];\\n   [user setValue:@\\\"...\\\" forProperty:@\\\"companyName\\\"];\\n   [user setValue:@\\\"...\\\" forProperty:@\\\"country\\\"];\\n   [user setValue:@\\\"...\\\" forProperty:@\\\"...\\\"];\\n        \\n   NSArray * arrValue = [NSArray arrayWithObjects: @\\\"Attr1\\\",@\\\"Attr2\\\",@\\\"Attr3\\\", nil];\\n   [user setValue:@\\\"...\\\" forProperty:@\\\"...\\\"];\\n    \\n   // Submit change to the cloud \\n   [user saveWithBlock:^(MOCAUser *user, NSError *error) {\\n      if (error) {\\n          NSLog(@\\\"Save user failed: %@\\\", error);\\n      }\\n   }];\\n}\\n\",\n      \"language\": \"objectivec\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"User log-out.\"\n}\n[/block]\nThis code sample shows how to inform MOCA that a user has logged out from the app.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"   . . . \\n   // Notify MOCA, the user logged out\\n   MOCAUser * loggedInUser = [[MOCA currentInstance] currentUser];\\n   if (loggedInUser) {\\n        [loggedInUser logout];\\n        // Submit change to the cloud \\n        [loggedInUser saveWithBlock:^(MOCAUser *user, NSError *error) {\\n            if (error) {\\n                NSLog(@\\\"Save user failed: %@\\\", error);\\n            }\\n        }];\\n   }\\n\",\n      \"language\": \"objectivec\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Receive push notification.\"\n}\n[/block]\nThis code sample shows how to handle MOCA-specific local push notifications and also track remote push notifications received by the app.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"#pragma mark Notifications\\n\\n- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo\\n{\\n    if (MOCA.initialized)\\n    {\\n        [MOCA handleRemoteNotification:userInfo];\\n    }\\n}\\n\\n- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification\\n{\\n    if (MOCA.initialized)\\n    {\\n        [MOCA handleLocalNotification:notification];\\n    }\\n}\\n// Called when your app has been activated by the user selecting an action from a local notification.\\n// A nil action identifier indicates the default action.\\n// You should call the completion handler as soon as you've finished handling the action.\\n- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier\\n    forLocalNotification:(UILocalNotification *)notification completionHandler:(void(^)())completionHandler\\n{\\n    if (MOCA.initialized && [MOCA isMocaNotification:notification])\\n    {\\n        [MOCA handleActionWithIdentifier:identifier\\n                    forLocalNotification:notification];\\n    }\\n    if (completionHandler) completionHandler ();\\n}\\n\",\n      \"language\": \"objectivec\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Register device token\"\n}\n[/block]\nThis code sample shows how to notify MOCA about an existing device token for push notification. This is used when the app is integrated with other third-party push SDKs such as Urban Airship SDK, Parse SDK or a SDK from any other push provider.\n\nIn such a scenario, MOCA is limited to handling its proper local push notifications and remains compatible with existing third-party SDKs.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"#pragma mark Notifications\\n\\n-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken\\n{\\n    if ([MOCA initialized]) {\\n        [MOCA registerDeviceToken:deviceToken];\\n    }\\n}\\n\",\n      \"language\": \"objectivec\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Fetch campaigns\"\n}\n[/block]\nThis code sample shows how to periodically fetch proximity campaigns from MOCA cloud backend. This example uses iOS Background Fetch mechanism to delegate handling of synchronization and content download to iOS.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"#pragma mark Fetch Campaigns\\n\\n/// Applications with the \\\"fetch\\\" background mode may be given opportunities to fetch updated\\n// content in the background or when it is convenient for the system. This method will be called\\n// in these situations. You should call the fetchCompletionHandler\\n// as soon as you're finished performing that operation, so the system can accurately\\n// estimate its power and data cost.\\n- (void)application:(UIApplication *)application performFetchWithCompletionHandler:\\n(void (^)(UIBackgroundFetchResult result))completionHandler\\n{\\n    if (MOCA.initialized)\\n    {\\n        [MOCA performFetchWithCompletionHandler:completionHandler];\\n    }\\n}\\n\",\n      \"language\": \"objectivec\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Force fetch campaigns\"\n}\n[/block]\nAdditionally, the app may force fetch the campaigns by calling performFetchWithCompletionHandler:completionHandler method in app-specific conditions. \n\nFor example, the app may force the synchronization on begin of each session.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"/// Asynchronously update campaigns (on request)\\n    if (MOCA.initialized)\\n    {\\n        [MOCA performFetchWithCompletionHandler:completionHandler];\\n    }\\n\",\n      \"language\": \"objectivec\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Track in-app navigation.\"\n}\n[/block]\nThis sample code shows how to track custom events with the app, in particular, how to record a user opening a particular app window.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"#Add to each View Controller to be tracked\\n\\n[MOCAEvent track:@\\\"view\\\" forItem:@\\\"screenName\\\" belongingTo:@\\\"screen\\\"];\\n\",\n      \"language\": \"objectivec\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Track custom event.\"\n}\n[/block]\nThis sample code shows how to track a generic, custom event within the app.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"# Track custom events\\n\\n[MOCAEvent track:@”verb\\\" forItem:@”item\\\" belongingTo:@”category” . . . ];\\n\",\n      \"language\": \"objectivec\"\n    }\n  ]\n}\n[/block]","excerpt":"","slug":"moca-ios-sdk-integration-examples","type":"basic","title":"Integration Examples"}

Integration Examples


The typical integration scenarios include handling of the following in-app events: * Launch app * User Login * User Logout * Receive Push Notification * Registered for Remote Push Notifications * Fetch Campaigns * Enter Screen * Custom Events [block:api-header] { "type": "basic", "title": "Launch app" } [/block] This code sample shows how to initialize MOCA SDK on app startup and also handle situations when the app is started by opening a push notification. [block:code] { "codes": [ { "code": "- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions\n{\n // Init MOCA SDK\n [MOCA initializeSDK];\n if (launchOptions != nil) {\n // Launched from push notification\n NSDictionary *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];\n if (notification) {\n // Notify MOCA the app started from push\n [MOCA handleRemoteNotification:notification];\n }\n }\n // Optionally, set guest properties\n MOCAInstance * currentInstance = [MOCA currentInstance];\n if (currentInstance) {\n [currentInstance setValue:@\"property_1_value\" forProperty:@\"guest_property_1\"];\n [currentInstance setValue:@\"property_2_value\" forProperty:@\"guest_property_2\"];\n [currentInstance setValue:[NSNumber numberWithDouble:property3_value] forProperty:@\"guest_property_3\"];\n \n // Submit change to the cloud \n [currentInstance saveWithBlock:^(MOCAInstance *instance, NSError *error) {\n if (error) {\n NSLog(@\"Save instance failed: %@\", error);\n }\n }];\n }\n}\n", "language": "objectivec" } ] } [/block] [block:api-header] { "type": "basic", "title": "User log-in" } [/block] This code sample shows how to inform MOCA that a user has logged into the app. This call should be executed each time the user performs the log in, even if the app persists user session on app close. [block:code] { "codes": [ { "code": " // Notify MOCA, the user logged in. Must be called after each app launch.\n\n NSString * userId = ...\n MOCAUser *user = [[MOCA currentInstance] login:loginMgr.username];\n \n [user setValue:@”paidComplete\" forProperty:@\"paymentStatus\"];\n [user setValue:@\"...\" forProperty:@\"pictureStatus\"];\n [user setValue:@\"...\" forProperty:@\"merchantId\"];\n [user setValue:@\"...\" forProperty:@\"jobTitle\"];\n [user setValue:@\"...\" forProperty:@\"companyName\"];\n [user setValue:@\"...\" forProperty:@\"country\"];\n [user setValue:@\"...\" forProperty:@\"...\"];\n \n NSArray * arrValue = [NSArray arrayWithObjects: @\"Attr1\",@\"Attr2\",@\"Attr3\", nil];\n [user setValue:@\"...\" forProperty:@\"...\"];\n \n // Submit change to the cloud \n [user saveWithBlock:^(MOCAUser *user, NSError *error) {\n if (error) {\n NSLog(@\"Save user failed: %@\", error);\n }\n }];\n}\n", "language": "objectivec" } ] } [/block] [block:api-header] { "type": "basic", "title": "User log-out." } [/block] This code sample shows how to inform MOCA that a user has logged out from the app. [block:code] { "codes": [ { "code": " . . . \n // Notify MOCA, the user logged out\n MOCAUser * loggedInUser = [[MOCA currentInstance] currentUser];\n if (loggedInUser) {\n [loggedInUser logout];\n // Submit change to the cloud \n [loggedInUser saveWithBlock:^(MOCAUser *user, NSError *error) {\n if (error) {\n NSLog(@\"Save user failed: %@\", error);\n }\n }];\n }\n", "language": "objectivec" } ] } [/block] [block:api-header] { "type": "basic", "title": "Receive push notification." } [/block] This code sample shows how to handle MOCA-specific local push notifications and also track remote push notifications received by the app. [block:code] { "codes": [ { "code": "#pragma mark Notifications\n\n- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo\n{\n if (MOCA.initialized)\n {\n [MOCA handleRemoteNotification:userInfo];\n }\n}\n\n- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification\n{\n if (MOCA.initialized)\n {\n [MOCA handleLocalNotification:notification];\n }\n}\n// Called when your app has been activated by the user selecting an action from a local notification.\n// A nil action identifier indicates the default action.\n// You should call the completion handler as soon as you've finished handling the action.\n- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier\n forLocalNotification:(UILocalNotification *)notification completionHandler:(void(^)())completionHandler\n{\n if (MOCA.initialized && [MOCA isMocaNotification:notification])\n {\n [MOCA handleActionWithIdentifier:identifier\n forLocalNotification:notification];\n }\n if (completionHandler) completionHandler ();\n}\n", "language": "objectivec" } ] } [/block] [block:api-header] { "type": "basic", "title": "Register device token" } [/block] This code sample shows how to notify MOCA about an existing device token for push notification. This is used when the app is integrated with other third-party push SDKs such as Urban Airship SDK, Parse SDK or a SDK from any other push provider. In such a scenario, MOCA is limited to handling its proper local push notifications and remains compatible with existing third-party SDKs. [block:code] { "codes": [ { "code": "#pragma mark Notifications\n\n-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken\n{\n if ([MOCA initialized]) {\n [MOCA registerDeviceToken:deviceToken];\n }\n}\n", "language": "objectivec" } ] } [/block] [block:api-header] { "type": "basic", "title": "Fetch campaigns" } [/block] This code sample shows how to periodically fetch proximity campaigns from MOCA cloud backend. This example uses iOS Background Fetch mechanism to delegate handling of synchronization and content download to iOS. [block:code] { "codes": [ { "code": "#pragma mark Fetch Campaigns\n\n/// Applications with the \"fetch\" background mode may be given opportunities to fetch updated\n// content in the background or when it is convenient for the system. This method will be called\n// in these situations. You should call the fetchCompletionHandler\n// as soon as you're finished performing that operation, so the system can accurately\n// estimate its power and data cost.\n- (void)application:(UIApplication *)application performFetchWithCompletionHandler:\n(void (^)(UIBackgroundFetchResult result))completionHandler\n{\n if (MOCA.initialized)\n {\n [MOCA performFetchWithCompletionHandler:completionHandler];\n }\n}\n", "language": "objectivec" } ] } [/block] [block:api-header] { "type": "basic", "title": "Force fetch campaigns" } [/block] Additionally, the app may force fetch the campaigns by calling performFetchWithCompletionHandler:completionHandler method in app-specific conditions. For example, the app may force the synchronization on begin of each session. [block:code] { "codes": [ { "code": "/// Asynchronously update campaigns (on request)\n if (MOCA.initialized)\n {\n [MOCA performFetchWithCompletionHandler:completionHandler];\n }\n", "language": "objectivec" } ] } [/block] [block:api-header] { "type": "basic", "title": "Track in-app navigation." } [/block] This sample code shows how to track custom events with the app, in particular, how to record a user opening a particular app window. [block:code] { "codes": [ { "code": "#Add to each View Controller to be tracked\n\n[MOCAEvent track:@\"view\" forItem:@\"screenName\" belongingTo:@\"screen\"];\n", "language": "objectivec" } ] } [/block] [block:api-header] { "type": "basic", "title": "Track custom event." } [/block] This sample code shows how to track a generic, custom event within the app. [block:code] { "codes": [ { "code": "# Track custom events\n\n[MOCAEvent track:@”verb\" forItem:@”item\" belongingTo:@”category” . . . ];\n", "language": "objectivec" } ] } [/block]