Skip to content

Commit

Permalink
Merge pull request #46 from tus/develop
Browse files Browse the repository at this point in the history
Patch 1.4.2 open session for TransloaditKit network sharing, session configuration
  • Loading branch information
MMasterson committed May 31, 2019
2 parents b7517f9 + 91b2e1b commit 907be6f
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 10 deletions.
8 changes: 4 additions & 4 deletions Example/TUSKit/Launch Screen.storyboard
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ To run the example project, clone the repo, and run `pod install` from the Examp
## The Protocol
You'll need a tus.io friendly server before using TUSKit or any other tus client. You can find a list of [tus implementations here](http://tus.io/implementations.html).

# Usage (1.4.0)
# Usage (1.4.2)
------
## TUSSession
A NSURLSession that manages, creates, and reloads TUS uploads using a single NSURLSession and data store.

A simple session can be setup as follows:

*by default, as of `1.4.2`, a simple session has no request caching.*

...
@property (strong, nonatomic) TUSSession *tusSession;
Expand All @@ -39,6 +43,23 @@ A NSURLSession that manages, creates, and reloads TUS uploads using a single NSU

**allowsCellularAccess** - Allow uploads over cell data.

---

However if you require more of an advanced setup and wish to use your own `NSURLSessionConfiguration`:

...
@property (strong, nonatomic) TUSSession *tusSession;
...
...
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
self.tusSession = [[TUSSession alloc] initWithEndpoint:[[NSURL alloc] initWithString:UPLOAD_ENDPOINT] dataStore:uploadStore sessionConfiguration:sessionConfiguration];

**Endpoint** - An NSURL of your tus.io server.

**dataStore** - The `TUSUploadStore` you've created for your uploads.

**sessionConfiguration** - Your custom `NSURLSessionConfiguration` object.


## TUSUploadStore
The data storage for uploads.
Expand Down
2 changes: 1 addition & 1 deletion TUSKit.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "TUSKit"
s.version = "1.4.1"
s.version = "1.4.2"
s.summary = "The tus client for iOS."
s.description = <<-DESC
An iOS implementation of the tus resumable video upload protocol.
Expand Down
5 changes: 5 additions & 0 deletions TUSKit/TUSSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
@interface TUSSession : NSObject
@property (nonatomic) BOOL allowsCellularAccess;
-(NSURLSession *_Nonnull) session;

/**
Initialize
Expand All @@ -24,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
30 changes: 28 additions & 2 deletions TUSKit/TUSSession.m
Original file line number Diff line number Diff line change
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,8 +58,16 @@ -(void)setAllowsCellularAccess:(BOOL)allowsCellularAccess
-(NSURLSession *) session{
// Lazily instantiate a session
if (_session == nil){
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
sessionConfiguration.allowsCellularAccess = self.allowsCellularAccess;
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 @@ -82,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 907be6f

Please sign in to comment.