{"__v":24,"_id":"55a611a0249a40190051d928","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 1.0","version_clean":"1.0.0","version":"1"},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2015-07-15T07:54:08.281Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":2,"body":"1 - Import `<MOCA/MOCA.h>` header file into your app’s delegate implementation file.\n\n2 - initialize MOCA SDK is in you app’s delegate `application:didFinishLaunchingWithOptions:` method.\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"#import <MOCA.h>\\n\\n- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions\\n{\\n    // Initialize MOCA SDK.\\n    BOOL mocaReady = [MOCA initializeSDK];\\n    if (!mocaReady)\\n    {\\n        NSLog(:::at:::\\\"MOCA SDK initialization failed.\\\");\\n    }\\n    return YES;\\n}\\n\",\n      \"language\": \"objectivec\"\n    }\n  ]\n}\n[/block]\n- You must initialize the SDK before calling any other method.\n\n- On initialization, MOCA SDK will load configuration from MOCAConfig.plist file and perform all necessary framework setup.\n\n- The `[MOCA initializeSDK]` method call returns `YES` on success, and `NO` on error.\n\n3 - Handle local notifications in your AppDelegate:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification\\n{\\n    if (MOCA.initialized)\\n    {\\n        [MOCA handleLocalNotification:notification];\\n    }\\n}\",\n      \"language\": \"objectivec\"\n    }\n  ]\n}\n[/block]\n4 - If you use MOCA remote push notifications, integrate the following code (recommended):\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"-(void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler: (void (^)(UIBackgroundFetchResult))completionHandler{\\n    \\n   if (MOCA.initialized)\\n    {\\n        [MOCA handleRemoteNotification:userInfo fetchCompletionHandler:handler];\\n    }\\n}\",\n      \"language\": \"objectivec\"\n    }\n  ]\n}\n[/block]\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken\\n{\\n    NSLog(@\\\"deviceToken: %@\\\", deviceToken);\\n    if(![MOCA initialized]) {\\n        return;\\n    }\\n    [MOCA registerDeviceToken:deviceToken];\\n}\",\n      \"language\": \"objectivec\",\n      \"name\": \"Push token\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Important considerations: Power Consumption\"\n}\n[/block]\n&nbsp;\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/r4ZqBpdYSWK7mLtjejr1_moca-low-battery.png\",\n        \"moca-low-battery.png\",\n        \"200\",\n        \"95\",\n        \"#fc4464\",\n        \"\"\n      ]\n    }\n  ]\n}\n[/block]\n&nbsp;\nWhen backgrounded, your application will be woken up by iOS in order to respond to location events. That means that the `didFinishLaunchingWithOptions` **could be called several times** while the user is moving, or when the device is in the range of bluetooth beacons.\n**Be extra careful**: filter out these events so that MOCA SDK is initialized, but any other heavy tasks are not executed. If the `UIApplicationLaunchOptionsLocationKey` key is present in the `launchOptions`, you can tell that the App was launched to respond to a location event.\n\nExample:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"#import <MOCA.h>\\n\\n- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions\\n{\\n    [MOCA initializeSDK];\\n    //Filter out the events\\n    if([launchOptions objectForKey:@\\\"UIApplicationLaunchOptionsLocationKey\\\"]) {\\n    \\t\\treturn YES;\\n    }\\n  \\t//do heavy stuff afterwards\\n}\",\n      \"language\": \"objectivec\"\n    }\n  ]\n}\n[/block]\n\n[block:callout]\n{\n  \"type\": \"success\",\n  \"body\": \"At this point, you already have a proximity-aware app! Go to the [MOCA Console](https://console.mocaplatform.com), create some campaigns and test your Proximity Experiences.\"\n}\n[/block]\n\n[block:callout]\n{\n  \"type\": \"info\",\n  \"body\": \"Beacon detection cannot be tested in the simulator, you will need a real device (iPhone 4s or newer).\",\n  \"title\": \"Simulator\",\n  \"sidebar\": true\n}\n[/block]","excerpt":"This section describes how to integrate MOCA iOS SDK with your app code.","slug":"moca-ios-sdk-integration","type":"basic","title":"Integration"}

Integration

This section describes how to integrate MOCA iOS SDK with your app code.

1 - Import `<MOCA/MOCA.h>` header file into your app’s delegate implementation file. 2 - initialize MOCA SDK is in you app’s delegate `application:didFinishLaunchingWithOptions:` method. [block:code] { "codes": [ { "code": "#import <MOCA.h>\n\n- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions\n{\n // Initialize MOCA SDK.\n BOOL mocaReady = [MOCA initializeSDK];\n if (!mocaReady)\n {\n NSLog(@\"MOCA SDK initialization failed.\");\n }\n return YES;\n}\n", "language": "objectivec" } ] } [/block] - You must initialize the SDK before calling any other method. - On initialization, MOCA SDK will load configuration from MOCAConfig.plist file and perform all necessary framework setup. - The `[MOCA initializeSDK]` method call returns `YES` on success, and `NO` on error. 3 - Handle local notifications in your AppDelegate: [block:code] { "codes": [ { "code": "- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification\n{\n if (MOCA.initialized)\n {\n [MOCA handleLocalNotification:notification];\n }\n}", "language": "objectivec" } ] } [/block] 4 - If you use MOCA remote push notifications, integrate the following code (recommended): [block:code] { "codes": [ { "code": "-(void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler: (void (^)(UIBackgroundFetchResult))completionHandler{\n \n if (MOCA.initialized)\n {\n [MOCA handleRemoteNotification:userInfo fetchCompletionHandler:handler];\n }\n}", "language": "objectivec" } ] } [/block] [block:code] { "codes": [ { "code": "-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken\n{\n NSLog(@\"deviceToken: %@\", deviceToken);\n if(![MOCA initialized]) {\n return;\n }\n [MOCA registerDeviceToken:deviceToken];\n}", "language": "objectivec", "name": "Push token" } ] } [/block] [block:api-header] { "type": "basic", "title": "Important considerations: Power Consumption" } [/block] &nbsp; [block:image] { "images": [ { "image": [ "https://files.readme.io/r4ZqBpdYSWK7mLtjejr1_moca-low-battery.png", "moca-low-battery.png", "200", "95", "#fc4464", "" ] } ] } [/block] &nbsp; When backgrounded, your application will be woken up by iOS in order to respond to location events. That means that the `didFinishLaunchingWithOptions` **could be called several times** while the user is moving, or when the device is in the range of bluetooth beacons. **Be extra careful**: filter out these events so that MOCA SDK is initialized, but any other heavy tasks are not executed. If the `UIApplicationLaunchOptionsLocationKey` key is present in the `launchOptions`, you can tell that the App was launched to respond to a location event. Example: [block:code] { "codes": [ { "code": "#import <MOCA.h>\n\n- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions\n{\n [MOCA initializeSDK];\n //Filter out the events\n if([launchOptions objectForKey:@\"UIApplicationLaunchOptionsLocationKey\"]) {\n \t\treturn YES;\n }\n \t//do heavy stuff afterwards\n}", "language": "objectivec" } ] } [/block] [block:callout] { "type": "success", "body": "At this point, you already have a proximity-aware app! Go to the [MOCA Console](https://console.mocaplatform.com), create some campaigns and test your Proximity Experiences." } [/block] [block:callout] { "type": "info", "body": "Beacon detection cannot be tested in the simulator, you will need a real device (iPhone 4s or newer).", "title": "Simulator", "sidebar": true } [/block]