Archive Project
This commit is contained in:
52
Notifications for YouTube/NYTUtil.m
Executable file
52
Notifications for YouTube/NYTUtil.m
Executable file
@@ -0,0 +1,52 @@
|
||||
//
|
||||
// NYTUtil.m
|
||||
// Notifications for YouTube
|
||||
//
|
||||
// Created by Kim Wittenburg on 21.07.13.
|
||||
// Copyright (c) 2013 Kim Wittenburg. All rights reserved.
|
||||
//
|
||||
|
||||
#import "NYTUtil.h"
|
||||
|
||||
// API Imports
|
||||
#import "GTMOAuth2Authentication.h"
|
||||
|
||||
// Core Imports
|
||||
#import "NYTAuthentication.h"
|
||||
|
||||
extern NSXMLDocument * LoadXMLDocumentSynchronous(NSString *url, GTMOAuth2Authentication *auth, NSError **outError)
|
||||
{
|
||||
// Group to wait for refresh of token (if neccessary)
|
||||
dispatch_group_t group = dispatch_group_create();
|
||||
dispatch_group_enter(group);
|
||||
|
||||
// Create the request; authenticate and authorize it.
|
||||
NSURL *actualURL = [NSURL URLWithString:url];
|
||||
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:actualURL];
|
||||
[request setValue:DeveloperKey forHTTPHeaderField:DeveloperKeyHTTPHeaderField];
|
||||
dispatch_sync(dispatch_get_main_queue(), ^{
|
||||
// Seems that authorization must be performed on the main queue
|
||||
[auth authorizeRequest:request completionHandler:^(NSError *error) {
|
||||
if (outError != NULL) {
|
||||
*outError = error;
|
||||
}
|
||||
dispatch_group_leave(group);
|
||||
}];
|
||||
});
|
||||
|
||||
// Wait for authorization
|
||||
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
|
||||
if (*outError) {
|
||||
// The authorization failed
|
||||
return nil;
|
||||
}
|
||||
|
||||
// Create the document
|
||||
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:NULL error:outError];
|
||||
if (!data) {
|
||||
return nil;
|
||||
}
|
||||
NSXMLDocument *document = [[NSXMLDocument alloc] initWithData:data options:NSXMLDocumentTidyXML error:outError];
|
||||
return document;
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user