No Description

GULNetwork.h 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright 2017 Google
  3. *
  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
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  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.
  15. */
  16. #import <Foundation/Foundation.h>
  17. #import "GULNetworkConstants.h"
  18. #import "GULNetworkLoggerProtocol.h"
  19. #import "GULNetworkURLSession.h"
  20. /// Delegate protocol for GULNetwork events.
  21. @protocol GULNetworkReachabilityDelegate
  22. /// Tells the delegate to handle events when the network reachability changes to connected or not
  23. /// connected.
  24. - (void)reachabilityDidChange;
  25. @end
  26. /// The Network component that provides network status and handles network requests and responses.
  27. /// This is not thread safe.
  28. ///
  29. /// NOTE:
  30. /// User must add FIRAnalytics handleEventsForBackgroundURLSessionID:completionHandler to the
  31. /// AppDelegate application:handleEventsForBackgroundURLSession:completionHandler:
  32. @interface GULNetwork : NSObject
  33. /// Indicates if network connectivity is available.
  34. @property(nonatomic, readonly, getter=isNetworkConnected) BOOL networkConnected;
  35. /// Indicates if there are any uploads in progress.
  36. @property(nonatomic, readonly, getter=hasUploadInProgress) BOOL uploadInProgress;
  37. /// An optional delegate that can be used in the event when network reachability changes.
  38. @property(nonatomic, weak) id<GULNetworkReachabilityDelegate> reachabilityDelegate;
  39. /// An optional delegate that can be used to log messages, warnings or errors that occur in the
  40. /// network operations.
  41. @property(nonatomic, weak) id<GULNetworkLoggerDelegate> loggerDelegate;
  42. /// Indicates whether the logger should display debug messages.
  43. @property(nonatomic, assign) BOOL isDebugModeEnabled;
  44. /// The time interval in seconds for the network request to timeout.
  45. @property(nonatomic, assign) NSTimeInterval timeoutInterval;
  46. /// Initializes with the default reachability host.
  47. - (instancetype)init;
  48. /// Initializes with a custom reachability host.
  49. - (instancetype)initWithReachabilityHost:(NSString *)reachabilityHost;
  50. /// Handles events when background session with the given ID has finished.
  51. + (void)handleEventsForBackgroundURLSessionID:(NSString *)sessionID
  52. completionHandler:(GULNetworkSystemCompletionHandler)completionHandler;
  53. /// Compresses and sends a POST request with the provided data to the URL. The session will be
  54. /// background session if usingBackgroundSession is YES. Otherwise, the POST session is default
  55. /// session. Returns a session ID or nil if an error occurs.
  56. - (NSString *)postURL:(NSURL *)url
  57. payload:(NSData *)payload
  58. queue:(dispatch_queue_t)queue
  59. usingBackgroundSession:(BOOL)usingBackgroundSession
  60. completionHandler:(GULNetworkCompletionHandler)handler;
  61. /// Sends a GET request with the provided data to the URL. The session will be background session
  62. /// if usingBackgroundSession is YES. Otherwise, the GET session is default session. Returns a
  63. /// session ID or nil if an error occurs.
  64. - (NSString *)getURL:(NSURL *)url
  65. headers:(NSDictionary *)headers
  66. queue:(dispatch_queue_t)queue
  67. usingBackgroundSession:(BOOL)usingBackgroundSession
  68. completionHandler:(GULNetworkCompletionHandler)handler;
  69. @end