As Nick pointed out, the OneNote app needs additional parameters to open successfully, without showing the "Cannot Open Link" alert. Here is what I did to solve the problem:
Note: In my app, the user is already logged in using the LiveSDK (Microsoft Account Login) so I have their Access Token saved.
I created an "Authorize OneNote" method that sends a request to the OneNote API to create a "Page" in the User's default notebook:
(void)AuthorizeOneNote
{
NSString *date = [NSString stringWithFormat:@"%@", [NSDate date]];
NSString *simpleHtml = [NSString stringWithFormat:
@"<html>"
"<head>"
"<title>\"%@\"</title>"
"<meta name=\"created\" content=\"%@\" />"
"</head>"
"<body>"
"<p></p>"
"</body>"
"</html>", @"Page Title", date];
NSData *presentation = [simpleHtml dataUsingEncoding:NSUTF8StringEncoding];
NSString *endpointToRequest = @"https://www.onenote.com/api/v1.0/pages";
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:endpointToRequest]];
request.HTTPMethod = @"POST";
request.HTTPBody = presentation;
[request addValue:@"text/html" forHTTPHeaderField:@"Content-Type"];
[request setValue:[@"Bearer " stringByAppendingString:@"LIVE-ID-ACCESS-TOKEN"] forHTTPHeaderField:@"Authorization"];
objConnectionOneNote = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
}
This is how I handle the response:
(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
if (connection == objConnectionOneNote)
{
NSString *strData = [[NSString alloc]initWithData:objData encoding:NSUTF8StringEncoding];
NSMutableDictionary *dictData1 = [[NSMutableDictionary alloc] init];
dictData1 = [NSJSONSerialization JSONObjectWithData:objData options:kNilOptions error:nil];
oneNoteClientUrl = [dictData1 valueForKeyPath:@"links.oneNoteClientUrl.href"];
NSLog(@"Response: %@",oneNoteClientUrl);
NSLog(@"Response: %@",strData);
// Launch OneNote Client
if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"onenote://"]])
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:oneNoteClientUrl]];
}
else
{
[[UIApplication sharedApplication] openURL: [NSURL URLWithString:@"https://itunes.apple.com/us/app/microsoft-onenote-for-iphone/id410395246?mt=8&uo=4"]];
}
}
There are couple things to note here:
This will create a new page every single time OneNote is launched from the app. In order to open the same page, you can persist the returned URL and use that to Open the client.
The Access Token from the MS Live Authentication expires in about an hour. When this happens, the OneNote API request returns a 401 Unauthorized response. To mitigate this, you can refresh the Access Token using the refresh token, which is also returned during Live Authentication.
Hope it helps!