Layar Developer Documentation

Back to layar.com

Tutorial- Example use of iOS SDK Callbacks

As of version 8.0 the Layar SDK supports various callbacks. An overview of existing callbacks can be found in the documentation included in the SDK package.

You can now more deeply integrate the Layar SDK into your own apps by making use of new advanced features:

Please note that as of SDK 8.1,nstead of passing <layername> and <poiId> (strings) as parameters we pass objects containing:
-For layer: iconUrl, layerName and title
-For POI: contentType, PoiId and URL

In this step-by-step tutorial we demonstrate:

Launch a geo Layar from a Campaign

If your SDK supports both vision and geo, you might want to launch a geo-layer from a Vision layer/ Campaign. If you do not handle the intent correctly, the native Layar app will be launched. In order to avoid this situation you need to use one of the available callbacks.

In order to follow this tutorial you need:

In order to implement this we only need to modify the ViewCotroller.m file from our previous tutorial.

 We will add a method to set up the SDK info.

{
    NSString *consumerKey = @"yourkey"; // your key
    NSString *consumerSecret = @"yoursecret"; // your secret
    
    if (!self.layarSDK)
    {
        self.layarSDK = [LayarSDK layarSDKWithConsumerKey:consumerKey andConsumerSecret:consumerSecret andDelegate:self];
    }
}


Now we can change our LaunchLayar method to reflect these changes

- (IBAction)LaunchLayar:(id)sender {
    [self setUpSDK];
    
    [self.layarSDK viewControllerForScanningWithCompletion:
     ^(UIViewController *viewController)
     {
         UIButton* closeButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 40.0f, 80.0f, 30.0f)];
         [closeButton setTitle:@"Close" forState:UIControlStateNormal];
         
         [closeButton addTarget:self action:@selector(dismissModalViewController) forControlEvents:UIControlEventTouchUpInside];
         [viewController.view addSubview:closeButton];
         
         [self presentViewController:viewController animated:YES completion:
          ^{
          }];
         
     }];
    
}

 Next to that we will add a method called openURL the will launch and manage the geo-layer view:

- (void)openURL:(NSString*)url animated:(BOOL)animated fromViewController:(UIViewController*)fromViewController
{
	[self setUpSDK];
	
	[self.layarSDK viewControllerForURL:[NSURL URLWithString:url] withCompletion:
	 ^(UIViewController *viewController)
     {
         if (viewController)
         {
             if (viewController.layerSettingsAvailable)
             {
                 UIButton* filtersButton = [[UIButton alloc] initWithFrame:CGRectMake(190.0f, 55.0f, 80.0f, 40.0f)];
                 [filtersButton setTitle:@"Filters" forState:UIControlStateNormal];
                 [filtersButton addTarget:viewController action:@selector(showLayerSettings) forControlEvents:UIControlEventTouchUpInside];
                 [viewController.view addSubview:filtersButton];
             }
             
             if (viewController.mapViewAvailable)
             {
                 UIButton* mapButton = [[UIButton alloc] initWithFrame:CGRectMake(250.0f, 55.0f, 80.0f, 40.0f)];
                 [mapButton setTitle:@"Map" forState:UIControlStateNormal];
                 [mapButton addTarget:viewController action:@selector(showMapView) forControlEvents:UIControlEventTouchUpInside];
                 [viewController.view addSubview:mapButton];
             }
             
             UIButton* closeButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 55.0f, 80.0f, 40.0f)];
             [closeButton setTitle:@"Close" forState:UIControlStateNormal];
             
             [closeButton addTarget:fromViewController action:@selector(dismissModalViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
             [viewController.view addSubview:closeButton];
             
             [fromViewController presentViewController:viewController animated:animated completion:nil];
         }
     }];
}

In this method we define 3 UIbuttons for map view, filters and a close button. All buttons can be customised to much your app UI.  

By pressing the close button users will return to the AR view. 

Finally we will use the callback method shouldTriggerActionForPOIn  in order to define how the app should handle the button press tha launches a geo-layer.

- (BOOL)layarSDK:(LayarSDK*)layarSdk shouldTriggerActionForPOI:(NSString*)poiId
referenceImageName:(NSString*)referenceImageName
           layer:(NSString*)layerName
          action:(NSDictionary*)action
onViewController:(UIViewController*)viewController
{
	if ([((NSString*)action[@"uri"]) hasPrefix:@"layar://yourlayername"])
	{

		return NO;
	}
	
	return YES;
}

You can now save and run your project!

Add an automatic “Pop-out” functionality

In this example we will show how you can change the default behaviour of the "Pop- out" feature. The callbacks we use below are supported by Layar SDK 8.1 and up.

 In our example the screen will pop out automatically once the user points away from the trigger image. In case the the image was not recognised the last tracked image will appear.

 if (!self.layarSDK)
    {
        self.layarSDK = [LayarSDK layarSDKWithConsumerKey:consumerKey andConsumerSecret:consumerSecret andDelegate:self];
        self.layarSDK.popOutsEnabled = NO;
    }
//methond to show detach mode as soon as user poitns away
- (void)layarSDK:(LayarSDK*)layarSdk didStopTrackingReferenceImage:(NSString *)referenceImageName withLayerObject:(NSDictionary *)layerObject onViewController:(UIViewController<LayarSDKViewController> *)viewController{
   [viewController popOutReferenceImageID:referenceImageName];//API implementation   
}

Hide the trigger image in the pop-out

As of version 8.4 it is possible to choose whether to show to not the reference image when in Pop out mode. 

This is a simple boolean method and by default is set to true. To call this method you need touse  the LayarSDKViewController class. 

 [viewController setShowPreviewImageWhenPoppedOut:NO];

Re-attach the content back to the trigger image (8.4)

When a user is point away from the trigger image he is prompt to use the Pop Out mode. As of v8.4 it is possible to reattach the content back to the trigger image if the user points again back to it. 

To do that we need first to enable tracking while in Pop out mode. This can be done with the following callback:

viewController.trackingEnabledWhenPoppedOut = YES;

Then we need to reattach the content once it tracks the page. To do this you need to create a method that once tracks the images, closes the Pop out view. This can be done as follows:

- (void)layarSDK:(LayarSDK*)layarSdk didStartTrackingReferenceImage:(NSString*)referenceImageName
 withLayerObject:(NSDictionary*)layerObject
onViewController:(UIViewController<LayarSDKViewController>*)viewController
{
          [viewController exitPopout];
    
    
    }