Skip to content

Commit

Permalink
sessionConfiguration
Browse files Browse the repository at this point in the history
  • Loading branch information
MMasterson committed May 31, 2019
1 parent de57bd7 commit 1071efb
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 10 deletions.
8 changes: 4 additions & 4 deletions Example/TUSKit/Launch Screen.storyboard
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14460.31" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="01J-lp-oVM">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14490.70" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="01J-lp-oVM">
<device id="retina5_9" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14460.20"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14490.49"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
Expand All @@ -18,8 +18,8 @@
<rect key="frame" x="0.0" y="0.0" width="375" height="812"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Copyright © 2019 Michael Avila. All rights reserved." textAlignment="center" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" minimumFontSize="9" translatesAutoresizingMaskIntoConstraints="NO" id="obG-Y5-kRd">
<rect key="frame" x="40" y="717.33333333333337" width="295" height="40.666666666666629"/>
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="https://www.tus.io" textAlignment="center" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" minimumFontSize="9" translatesAutoresizingMaskIntoConstraints="NO" id="obG-Y5-kRd">
<rect key="frame" x="40" y="737.66666666666663" width="295" height="20.333333333333371"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<nil key="highlightedColor"/>
Expand Down
7 changes: 5 additions & 2 deletions Example/TUSKit/TKViewController.m
Expand Up @@ -45,7 +45,10 @@ -(void)viewDidLoad
NSURL * applicationSupportURL = [[[NSFileManager defaultManager] URLsForDirectory:NSApplicationSupportDirectory inDomains:NSUserDomainMask] firstObject];

TUSUploadStore * uploadStore = [[TUSFileUploadStore alloc] initWithURL:[applicationSupportURL URLByAppendingPathComponent:FILE_NAME]];
self.tusSession = [[TUSSession alloc] initWithEndpoint:[[NSURL alloc] initWithString:UPLOAD_ENDPOINT] dataStore:uploadStore allowsCellularAccess:YES];
// self.tusSession = [[TUSSession alloc] initWithEndpoint:[[NSURL alloc] initWithString:UPLOAD_ENDPOINT] dataStore:uploadStore allowsCellularAccess:YES];
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
sessionConfiguration.allowsCellularAccess = NO;
self.tusSession = [[TUSSession alloc] initWithEndpoint:[[NSURL alloc] initWithString:UPLOAD_ENDPOINT] dataStore:uploadStore sessionConfiguration:sessionConfiguration];
for (TUSResumableUpload * upload in [self.tusSession restoreAllUploads]){
upload.progressBlock = progressBlock;
upload.resultBlock = resultBlock;
Expand Down Expand Up @@ -107,7 +110,7 @@ - (void)imagePickerController:(UIImagePickerController *)picker didFinishPicking
}

// If a file has not been created yet by your TUS backend
TUSResumableUpload *upload = [self.tusSession createUploadFromFile:fileUrl retry:-1 headers:@{} metadata:@{} uploadUrl:[[NSURL alloc] initWithString:@""]];
TUSResumableUpload *upload = [self.tusSession createUploadFromFile:fileUrl retry:-1 headers:@{} metadata:@{}];

upload.progressBlock = progressBlock;
upload.resultBlock = resultBlock;
Expand Down
4 changes: 4 additions & 0 deletions TUSKit/TUSSession.h
Expand Up @@ -25,6 +25,10 @@
dataStore:(TUSUploadStore * _Nonnull)store
allowsCellularAccess:(BOOL)allowsCellularAccess;

- (id _Nonnull )initWithEndpoint:(NSURL * _Nonnull)endpoint
dataStore:(TUSUploadStore * _Nonnull)store
sessionConfiguration:(NSURLSessionConfiguration * _Nonnull)sessionConfiguration;

/**
Create an upload, but do not start it
*/
Expand Down
32 changes: 28 additions & 4 deletions TUSKit/TUSSession.m
Expand Up @@ -15,6 +15,7 @@ @interface TUSSession() <TUSResumableUploadDelegate, NSURLSessionDataDelegate>
@property (nonatomic, strong) TUSUploadStore *store; // Data store to save upload status in
@property (nonatomic, strong) NSMutableDictionary <NSString *, TUSResumableUpload *>* uploads;
@property (nonatomic, strong) NSMutableDictionary <NSURLSessionTask *, TUSResumableUpload *>* tasks;
@property (nonatomic, strong) NSURLSessionConfiguration *sessionConfiguration; // Session config to use for uploads

#pragma mark TUSResumableUploadDelegate method declarations
/**
Expand Down Expand Up @@ -57,10 +58,16 @@ -(void)setAllowsCellularAccess:(BOOL)allowsCellularAccess
-(NSURLSession *) session{
// Lazily instantiate a session
if (_session == nil){
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
sessionConfiguration.allowsCellularAccess = self.allowsCellularAccess;
sessionConfiguration.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
sessionConfiguration.URLCache = nil;
NSURLSessionConfiguration *sessionConfiguration;
if (_sessionConfiguration == nil) {
sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
sessionConfiguration.allowsCellularAccess = self.allowsCellularAccess;
sessionConfiguration.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
sessionConfiguration.URLCache = nil;
} else {
sessionConfiguration = _sessionConfiguration;
}

_session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:[NSOperationQueue mainQueue]];
}
return _session;
Expand All @@ -84,6 +91,23 @@ - (id)initWithEndpoint:(NSURL *)endpoint
return self;
}

- (id)initWithEndpoint:(NSURL *)endpoint
dataStore:(TUSUploadStore *)store
sessionConfiguration:(NSURLSessionConfiguration *)sessionConfiguration
{
self = [super init];

if (self) {
_store = store; // TODO: Load uploads from store
_createUploadURL = endpoint;
_uploads = [NSMutableDictionary new];
_tasks = [NSMutableDictionary new];
_allowsCellularAccess = YES; //default
_sessionConfiguration = sessionConfiguration;
}
return self;
}

#pragma mark public methods
- (TUSResumableUpload *) createUploadFromFile:(NSURL *)fileURL
retry:(int)retryCount
Expand Down

0 comments on commit 1071efb

Please sign in to comment.