Invoking Feedback UI with a shake gesture

Introduction

One common gesture you may want to add to your app is to let users shake the device and provide them an easy way to contact you from anywhere in your app.

Our SDKs In-App Feedback feature provides a nice way to let your apps users get in touch with you and even send annotated screenshots. The following code will automatically create a screenshot when the device is shaken and will open the compose feedback message UI.

Prerequisites

This guide assumes you are an already integrated the HockeySDK into your app.

iOS

Integrate the following code in your root view controller:

  • Swift
import HockeySDK

override func canBecomeFirstResponder() -> Bool {
  return true
}
  
override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent?) {
  if motion == .MotionShake {
    BITHockeyManager.sharedHockeyManager().feedbackManager.showFeedbackComposeView()
  }
}
  • Objective-C
@import HockeySDK;

- (BOOL)canBecomeFirstResponder {
  return YES;
}

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
  if (motion == UIEventSubtypeMotionShake) {
    [[BITHockeyManager sharedHockeyManager].feedbackManager showFeedbackComposeView];
  }
}

Android

On Android there is no system API for detecting a shake gesture - but there is a plethora of third party libraries available. Here is an example using Seismic by square:

import android.app.*;
import android.hardware.SensorManager;

import com.squareup.seismic.ShakeDetector;

import net.hockeyapp.android.*;

public class MainActivity extends Activity implements ShakeDetector.Listener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        ShakeDetector detector = new ShakeDetector(this);
        detector.start(sensorManager);

        setContentView(R.layout.activity_main);
    }
    
    @Override
    public void hearShake() {
        FeedbackManager.showFeedbackActivity(this);
    }
}

Automatically capturing a screenshot and adding it to the message composer is currently not available on Android.

Xamarin iOS

Add the following code to your BaseView.cs file:

  public override void MotionEnded(UIEventSubType motion, UIEvent event)
  {
    if (motion == UIEventSubType.MotionShake)
    {
      BITHockeyManager.SharedHockeyManager.FeedbackManager.ShowFeedbackComposeViewWithGeneratedFeedback();
    }
  }