Search code examples
iosobjective-cios8handoff

iOS handoff from phone to macbook safari/chrome


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?


Solution

  • 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