5

I want to send a URL from my app to open on a laptop web browser using handoff. I have added the activity type to my app's NSUserActivityTypes. here is my code so far:

- (void)startHandoff {
    NSUserActivity *activity = [[NSUserActivity alloc] initWithActivityType:@"com.me.browse"];
    activity.webpageURL = [NSURL URLWithString:_wakeUrl];
    [activity becomeCurrent];
}

It doesn't seem to be appearing on my dock - does it need a special Activity Type if you want to use safari?

Halpo
  • 2,982
  • 3
  • 25
  • 54
  • Does it work with Mobile Safari? If you open Safari in your phone, does your Mac allow you to resume from your Desktop ? I'm asking so we can see if phone to desktop handoff works correctly on your devices – Lefteris Jun 02 '15 at 10:14
  • yep it works fine with chrome – Halpo Jun 02 '15 at 10:15
  • Doublecheck that the specified activity in the `NSUserActivityTypes` matches the one you specify in your `init` method. – Lefteris Jun 02 '15 at 10:23

1 Answers1

4

Ok after tests, it seems that you need to declare the NSUserActivity as a instance Variable :

So this does not work:

@interface TestViewController () {
}

@end

@implementation TestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //init hand off
    NSUserActivity *activity = [[NSUserActivity alloc] initWithActivityType:@"com.app.browse"];
    activity.webpageURL = [NSURL URLWithString:@"http://www.stackoverflow.com"];
    [activity becomeCurrent];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

But this works fine:

@interface TestViewController () {
      NSUserActivity *activity;
}

@end

@implementation TestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //init hand off
   activity = [[NSUserActivity alloc] initWithActivityType:@"com.app.browse"];
    activity.webpageURL = [NSURL URLWithString:@"http://www.stackoverflow.com"];
    [activity becomeCurrent];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

Not sure why though, I'm looking into it now

Lefteris
  • 14,550
  • 2
  • 56
  • 95
  • 1
    this is correct, although I had other handoff features in my app / watch app that were not working - I just restarted the devices and that seemed to fix the issues – Halpo Jun 02 '15 at 11:01
  • you can also declare the activity as a property – Halpo Jun 04 '15 at 15:59
  • Yes, because by default, a readwrite property will be backed by an instance variable, which will again be synthesized automatically by the compiler. – Lefteris Jun 04 '15 at 16:09
  • 1
    I think your NSUserActivity object gets deallocated right after viewDidLoad has finished. That's why your first approach does not work. – RTasche Feb 20 '19 at 21:38