2 * Copyright 2017 Google
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #import <Foundation/Foundation.h>
19 #import "GULNetworkConstants.h"
20 #import "GULNetworkLoggerProtocol.h"
21 #import "GULNetworkURLSession.h"
23 /// Delegate protocol for GULNetwork events.
24 @protocol GULNetworkReachabilityDelegate
26 /// Tells the delegate to handle events when the network reachability changes to connected or not
28 - (void)reachabilityDidChange;
32 /// The Network component that provides network status and handles network requests and responses.
33 /// This is not thread safe.
36 /// User must add FIRAnalytics handleEventsForBackgroundURLSessionID:completionHandler to the
37 /// AppDelegate application:handleEventsForBackgroundURLSession:completionHandler:
38 @interface GULNetwork : NSObject
40 /// Indicates if network connectivity is available.
41 @property(nonatomic, readonly, getter=isNetworkConnected) BOOL networkConnected;
43 /// Indicates if there are any uploads in progress.
44 @property(nonatomic, readonly, getter=hasUploadInProgress) BOOL uploadInProgress;
46 /// An optional delegate that can be used in the event when network reachability changes.
47 @property(nonatomic, weak) id<GULNetworkReachabilityDelegate> reachabilityDelegate;
49 /// An optional delegate that can be used to log messages, warnings or errors that occur in the
50 /// network operations.
51 @property(nonatomic, weak) id<GULNetworkLoggerDelegate> loggerDelegate;
53 /// Indicates whether the logger should display debug messages.
54 @property(nonatomic, assign) BOOL isDebugModeEnabled;
56 /// The time interval in seconds for the network request to timeout.
57 @property(nonatomic, assign) NSTimeInterval timeoutInterval;
59 /// Initializes with the default reachability host.
62 /// Initializes with a custom reachability host.
63 - (instancetype)initWithReachabilityHost:(NSString *)reachabilityHost;
65 /// Handles events when background session with the given ID has finished.
66 + (void)handleEventsForBackgroundURLSessionID:(NSString *)sessionID
67 completionHandler:(GULNetworkSystemCompletionHandler)completionHandler;
69 /// Compresses and sends a POST request with the provided data to the URL. The session will be
70 /// background session if usingBackgroundSession is YES. Otherwise, the POST session is default
71 /// session. Returns a session ID or nil if an error occurs.
72 - (NSString *)postURL:(NSURL *)url
73 payload:(NSData *)payload
74 queue:(dispatch_queue_t)queue
75 usingBackgroundSession:(BOOL)usingBackgroundSession
76 completionHandler:(GULNetworkCompletionHandler)handler;
78 /// Sends a GET request with the provided data to the URL. The session will be background session
79 /// if usingBackgroundSession is YES. Otherwise, the GET session is default session. Returns a
80 /// session ID or nil if an error occurs.
81 - (NSString *)getURL:(NSURL *)url
82 headers:(NSDictionary *)headers
83 queue:(dispatch_queue_t)queue
84 usingBackgroundSession:(BOOL)usingBackgroundSession
85 completionHandler:(GULNetworkCompletionHandler)handler;