Commit 7f72958f authored by Alf Watt's avatar Alf Watt

Convert to NSURLSession

parent 66ce3113
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSHumanReadableCopyright</key>
<string>Copyright © 2015 iStumbler. All rights reserved.</string>
</dict>
</plist>
......@@ -15,6 +15,7 @@
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
75BE81011F5F7DA900453938 /* LiveBundle copy-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; name = "LiveBundle copy-Info.plist"; path = "/Users/alf/Source/LiveBundle/LiveBundle copy-Info.plist"; sourceTree = "<absolute>"; };
75E666CB1A36E4C2001E7DC4 /* LiveBundle.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = LiveBundle.framework; sourceTree = BUILT_PRODUCTS_DIR; };
75E666CF1A36E4C2001E7DC4 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
75E666D01A36E4C2001E7DC4 /* LiveBundle.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = LiveBundle.h; sourceTree = "<group>"; };
......@@ -42,6 +43,7 @@
75E666F41A36E56E001E7DC4 /* README.md */,
75E666CD1A36E4C2001E7DC4 /* LiveBundle */,
75E666CC1A36E4C2001E7DC4 /* Products */,
75BE81011F5F7DA900453938 /* LiveBundle copy-Info.plist */,
);
sourceTree = "<group>";
};
......@@ -256,6 +258,7 @@
PRODUCT_BUNDLE_IDENTIFIER = "net.istumbler.labs.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SUPPORTED_PLATFORMS = "macosx iphone iphonesimulator";
};
name = Debug;
};
......@@ -274,6 +277,7 @@
PRODUCT_BUNDLE_IDENTIFIER = "net.istumbler.labs.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SUPPORTED_PLATFORMS = "macosx iphone iphonesimulator";
};
name = Release;
};
......
......@@ -9,7 +9,7 @@ extern NSString* const ILLiveBundleResourceUpdateNote;
extern NSString* const ILPlistType;
@interface NSBundle (LiveBundle) <NSURLDownloadDelegate>
@interface NSBundle (LiveBundle) <NSURLSessionDelegate>
/*! @returns the NSBundle in the current application with the named resource of the type provided */
+ (NSBundle*) bundleWithResource:(NSString*) name ofType:(NSString*) extension;
......
......@@ -176,8 +176,15 @@ NSString* const ILPlistType = @"plist";
downloadRequest.timeoutInterval = 30; // shorter maybe?
downloadRequest.HTTPMethod = @"GET";
NSURLDownload* download = [[NSURLDownload alloc] initWithRequest:downloadRequest delegate:self];
[download setDestination:tempFilePath allowOverwrite:YES];
NSURLSession* session = [NSURLSession
sessionWithConfiguration:[NSURLSessionConfiguration ephemeralSessionConfiguration]
delegate:self
delegateQueue:[NSOperationQueue mainQueue]];
NSURLSessionTask* download = [session downloadTaskWithRequest:downloadRequest];
[download resume];
// NSURLDownload* download = [[NSURLDownload alloc] initWithRequest:downloadRequest delegate:self];
// [download setDestination:tempFilePath allowOverwrite:YES];
}
else NSLog(@"WARNING livePathForResource will not load resrouces over an insecure connection.\n\nUse https://letsencrypt.org to get free SSL certs for your site\n\n");
}
......@@ -191,7 +198,83 @@ exit:
return liveResourcePath;
}
#pragma mark -
#pragma mark - NSURLSessionDelegate Methods
- (void)URLSession:(NSURLSession *)session didBecomeInvalidWithError:(NSError *)error
{
if (error) {
NSLog(@"URLSession: %@ didBecomeInvalidWithError: %@", session, error);
}
}
#pragma mark - NSURLSessionTaskDelegate Methods
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesSent totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend
{
}
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task willPerformHTTPRedirection:(NSHTTPURLResponse *)response newRequest:(NSURLRequest *)request completionHandler:(void (^)(NSURLRequest *))completionHandler
{
}
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didFinishCollectingMetrics:(NSURLSessionTaskMetrics *)metrics
{
}
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
}
#pragma mark - NSURLSessionDownloadDelegate Methods
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes
{
}
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
{
}
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)download didFinishDownloadingToURL:(NSURL *)fileURL
{
NSString* liveResourcePath = [self livePathForResourceURL:download.originalRequest.URL];
NSFileManager* fm = [NSFileManager defaultManager];
NSError* error = nil;
// TODO check integrety of the temp file against HTTP MD5 header if provided
// is something at the liveResourcePath already? we should remove that
if( [fm fileExistsAtPath:liveResourcePath isDirectory:nil]) {
if( ![fm removeItemAtURL:[NSURL fileURLWithPath:liveResourcePath] error:&error]) {
NSLog(@"ERROR in connectionDidFinishLoading can't remove: %@ error: %@", liveResourcePath, error);
goto exit;
}
}
// the landing site it clear, move the temp file over to the resrouce path
if( ![fm moveItemAtPath:fileURL.path toPath:liveResourcePath error:&error]) {
NSLog(@"ERROR in connectionDidFinishLoading can't move: %@ -> %@ error: %@", fileURL.path, liveResourcePath, error);
goto exit;
}
// if( DEBUG) NSLog(@"LiveBundle updated: %@", liveResourcePath);
// file was moved into place sucessfully, tell the world
[[NSNotificationCenter defaultCenter] postNotificationName:ILLiveBundleResourceUpdateNote object:liveResourcePath];
exit:
return;
}
/*
#pragma mark - NSURLDownloadDelegate Methods
- (void)downloadDidBegin:(NSURLDownload*) download
{
......@@ -272,5 +355,6 @@ exit:
{
// NSLog(@"ERROR NSBundle+LiveBundle download: %@ didFailWithError: %@", download, error);
}
*/
@end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment