1
This repository has been archived on 2022-08-08. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
Kim Wittenburg 15062dfb27 Archive Project
2017-07-25 17:21:13 +02:00

52 lines
1.6 KiB
Objective-C
Executable File

//
// 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;
}