- Add Facebook SDK

- Add PNUser Login
This commit is contained in:
Giuseppe Nucifora 2016-02-12 00:01:51 +01:00
parent f2885301b9
commit a6dcf672bb
370 changed files with 44655 additions and 2705 deletions

View File

@ -20,7 +20,7 @@
6003F5B2195388D20070C39A /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6003F591195388D20070C39A /* UIKit.framework */; };
6003F5BA195388D20070C39A /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 6003F5B8195388D20070C39A /* InfoPlist.strings */; };
6003F5BC195388D20070C39A /* Tests.m in Sources */ = {isa = PBXBuildFile; fileRef = 6003F5BB195388D20070C39A /* Tests.m */; };
686ABCCF1C68A439000B3F7A /* User.m in Sources */ = {isa = PBXBuildFile; fileRef = 686ABCCE1C68A439000B3F7A /* User.m */; };
68E32EDD1C6D43E4002F2A53 /* User.m in Sources */ = {isa = PBXBuildFile; fileRef = 68E32EDC1C6D43E4002F2A53 /* User.m */; };
DA43246C4FB8B8B587FC2343 /* Pods_PNObject_Tests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5CB6B6AE8DC4C8FBEC56DD55 /* Pods_PNObject_Tests.framework */; };
E1806AD43C05C089D52D136E /* Pods_PNObject_Example.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D73089A9DA261B19C7C342EA /* Pods_PNObject_Example.framework */; };
/* End PBXBuildFile section */
@ -58,8 +58,8 @@
6003F5B9195388D20070C39A /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = "<group>"; };
6003F5BB195388D20070C39A /* Tests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = Tests.m; sourceTree = "<group>"; };
606FC2411953D9B200FFA9A0 /* Tests-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Tests-Prefix.pch"; sourceTree = "<group>"; };
686ABCCD1C68A439000B3F7A /* User.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = User.h; sourceTree = "<group>"; };
686ABCCE1C68A439000B3F7A /* User.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = User.m; sourceTree = "<group>"; };
68E32EDB1C6D43E4002F2A53 /* User.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = User.h; path = "../../../../../../Purplenetwork/GIT/iOS/packman-ios/App/packman/Model/User/User.h"; sourceTree = "<group>"; };
68E32EDC1C6D43E4002F2A53 /* User.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = User.m; path = "../../../../../../Purplenetwork/GIT/iOS/packman-ios/App/packman/Model/User/User.m"; sourceTree = "<group>"; };
6ADA851464A35438E6A21617 /* Pods-PNObject_Example.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-PNObject_Example.debug.xcconfig"; path = "Pods/Target Support Files/Pods-PNObject_Example/Pods-PNObject_Example.debug.xcconfig"; sourceTree = "<group>"; };
75FCB4EFD17838CCA4C93E4A /* LICENSE */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text; name = LICENSE; path = ../LICENSE; sourceTree = "<group>"; };
8C017F9292D546DE8DBC84F8 /* PNObject.podspec */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text; name = PNObject.podspec; path = ../PNObject.podspec; sourceTree = "<group>"; };
@ -133,11 +133,12 @@
6003F593195388D20070C39A /* Example for PNObject */ = {
isa = PBXGroup;
children = (
686ABCCC1C68A439000B3F7A /* User */,
6003F59C195388D20070C39A /* PNObjectAppDelegate.h */,
6003F59D195388D20070C39A /* PNObjectAppDelegate.m */,
6003F5A5195388D20070C39A /* PNObjectViewController.h */,
6003F5A6195388D20070C39A /* PNObjectViewController.m */,
68E32EDB1C6D43E4002F2A53 /* User.h */,
68E32EDC1C6D43E4002F2A53 /* User.m */,
6003F5A8195388D20070C39A /* Images.xcassets */,
6003F594195388D20070C39A /* Supporting Files */,
);
@ -185,16 +186,6 @@
name = "Podspec Metadata";
sourceTree = "<group>";
};
686ABCCC1C68A439000B3F7A /* User */ = {
isa = PBXGroup;
children = (
686ABCCD1C68A439000B3F7A /* User.h */,
686ABCCE1C68A439000B3F7A /* User.m */,
);
name = User;
path = "../../../../../../Purplenetwork/GIT/iOS/packman-ios/packman/Model/User";
sourceTree = "<group>";
};
700947C2A719D2A125954296 /* Pods */ = {
isa = PBXGroup;
children = (
@ -402,7 +393,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
686ABCCF1C68A439000B3F7A /* User.m in Sources */,
68E32EDD1C6D43E4002F2A53 /* User.m in Sources */,
6003F59E195388D20070C39A /* PNObjectAppDelegate.m in Sources */,
6003F5A7195388D20070C39A /* PNObjectViewController.m in Sources */,
6003F59A195388D20070C39A /* main.m in Sources */,

View File

@ -41,6 +41,27 @@
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
<true/>
</dict>
<key>facebook.com</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
<false/>
</dict>
<key>fbcdn.net</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
<false/>
</dict>
<key>akamaihd.net</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
<false/>
</dict>
</dict>
</dict>
<key>UIRequiredDeviceCapabilities</key>
@ -63,5 +84,25 @@
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>fb213761522305123</string>
</array>
</dict>
</array>
<key>FacebookAppID</key>
<string>213761522305123</string>
<key>FacebookDisplayName</key>
<string>PNObject</string>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>fbapi</string>
<string>fb-messenger-api</string>
<string>fbauth2</string>
<string>fbshareextension</string>
</array>
</dict>
</plist>

View File

@ -14,12 +14,20 @@
#import "PNAddress.h"
#import "PNObject+PNObjectConnection.h"
#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <FBSDKLoginKit/FBSDKLoginKit.h>
#import <FBSDKShareKit/FBSDKShareKit.h>
@implementation PNObjectAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[FBSDKSettings setAppID:@"213761522305123"];
[[FBSDKApplicationDelegate sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions];
_window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
@ -61,9 +69,8 @@
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
- (void)applicationDidBecomeActive:(UIApplication *)application {
[FBSDKAppEvents activateApp];
}
- (void)applicationWillTerminate:(UIApplication *)application
@ -71,4 +78,14 @@
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
return [[FBSDKApplicationDelegate sharedInstance] application:application
openURL:url
sourceApplication:sourceApplication
annotation:annotation];
}
@end

View File

@ -23,6 +23,7 @@
@property (nonatomic, strong) UIButton *cancelToken;
@end
@implementation PNObjectViewController
@ -81,12 +82,10 @@
[_apiCall autoSetDimension:ALDimensionWidth toSize:140];
[_apiCall autoSetDimension:ALDimensionHeight toSize:44];
[_cancelToken autoAlignAxisToSuperviewAxis:ALAxisVertical];
[_cancelToken autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:_refreshToken withOffset:35];
[_cancelToken autoSetDimension:ALDimensionWidth toSize:140];
[_cancelToken autoSetDimension:ALDimensionHeight toSize:44];
}
[super updateViewConstraints];
}
@ -101,19 +100,32 @@
- (void) apiCallAction {
User *user = [User currentUser];
/*User *user = [User currentUser];
[user setFirstName:@"Giuseppe"];
[user setLastName:@"Nuficora"];
[user setEmail:@"giuseppe.nucifora@purplenetwork.it"];
[user setEmail:@"packman@giuseppenucifora.com"];
[user setPassword:@"asdasdasd"];
[user setConfirmPassword:@"asdasdasd"];
[user setHasAcceptedNewsletter:NO];
[user setHasAcceptedPrivacy:YES];
[user registerCurrentUserWithBlockSuccess:^(id _Nullable responseObject) {
[user saveLocally];
[user reloadFormServer];*/
/*[[User currentUser] socialLoginWithBlockSuccessFromViewController:self
blockSuccess:^(PNUser * _Nullable responseObject) {
} failure:^(NSError * _Nonnull error) {
}];*/
[[PNUser currentUser] loginCurrentUserWithEmail:@"packman@giuseppenucifora.com" password:@"asdasdasd" withBlockSuccess:^(PNUser * _Nullable responseObject) {
NSLog(@"response : %@",responseObject);
} failure:^(NSError * _Nonnull error) {
NSLog(@"response : %@",error);
}];
}

View File

@ -12,6 +12,9 @@ target 'PNObject_Example' do
pod 'CodFis-Helper'
pod 'StrongestPasswordValidator'
pod 'PureLayout'
pod 'FBSDKCoreKit'
pod 'FBSDKShareKit'
pod 'FBSDKLoginKit'
end

View File

@ -14,11 +14,23 @@ PODS:
- AFNetworking/Serialization (3.0.4)
- AFNetworking/UIKit (3.0.4):
- AFNetworking/NSURLSession
- Bolts (1.6.0):
- Bolts/AppLinks (= 1.6.0)
- Bolts/Tasks (= 1.6.0)
- Bolts/AppLinks (1.6.0):
- Bolts/Tasks
- Bolts/Tasks (1.6.0)
- CodFis-Helper (0.1.2)
- Expecta (1.0.5)
- Expecta+Snapshots (2.0.0):
- Expecta (~> 1.0)
- FBSnapshotTestCase/Core (~> 2.0.3)
- FBSDKCoreKit (4.10.0):
- Bolts (~> 1.5)
- FBSDKLoginKit (4.10.0):
- FBSDKCoreKit
- FBSDKShareKit (4.10.0):
- FBSDKCoreKit
- FBSnapshotTestCase (2.0.7):
- FBSnapshotTestCase/SwiftSupport (= 2.0.7)
- FBSnapshotTestCase/Core (2.0.7)
@ -28,9 +40,12 @@ PODS:
- NSString-Helper (1.0.2)
- nv-ios-http-status (0.0.1)
- PEAR-FileManager-iOS (1.3.1)
- PNObject (0.3.2):
- PNObject (0.3.6):
- AFNetworking
- CodFis-Helper
- FBSDKCoreKit
- FBSDKLoginKit
- FBSDKShareKit
- NSDate_Utils
- NSString-Helper
- nv-ios-http-status
@ -47,6 +62,9 @@ DEPENDENCIES:
- CodFis-Helper
- Expecta
- Expecta+Snapshots
- FBSDKCoreKit
- FBSDKLoginKit
- FBSDKShareKit
- FBSnapshotTestCase
- NSDate_Utils
- NSString-Helper
@ -64,20 +82,24 @@ EXTERNAL SOURCES:
SPEC CHECKSUMS:
AFNetworking: a0075feb321559dc78d9d85b55d11caa19eabb93
Bolts: f52a250053bb517ca874523c3913776359ab3def
CodFis-Helper: f303810699f22dbcba8fb8c600545ac91fc3ec42
Expecta: e1c022fcd33910b6be89c291d2775b3fe27a89fe
Expecta+Snapshots: 29b38dd695bc72a0ed2bea833937d78df41943ba
FBSDKCoreKit: 13bec8373fb3af94d44daf2aa1e0958687897fbd
FBSDKLoginKit: d3d4a2e9d31954deb00bfea964167a05ca1ea976
FBSDKShareKit: 54587b4624706ace1e810cf83412a918141f807a
FBSnapshotTestCase: 7e85180d0d141a0cf472352edda7e80d7eaeb547
NSDate_Utils: 4a57f91094123d5b7600c7de8c9ad9e1d43306a3
NSString-Helper: 0ee74919829a332f9838fa87b28cb2d1d991e92c
nv-ios-http-status: b6c2b5fc8656cc19e0d3000dadce2080b99d0e2f
PEAR-FileManager-iOS: 3bc403f68a53483f5629aa822f4649e40275c4d3
PNObject: f1a1126226e7f0dac81111a98e49b8653603f518
PNObject: 6f799e51e8206ec5e43dcf109b04a6d14b0475d0
PureLayout: f35f5384c9c4e4479df041dbe33ad7577b71ddfb
Specta: ac94d110b865115fe60ff2c6d7281053c6f8e8a2
StrongestPasswordValidator: 554de9038705e18904f0337903dfd3b85a6b271b
UIDevice-Utils: 0beb5f9d2bd256a3efe05c1e43a2a8b8702199c4
PODFILE CHECKSUM: 17e9b803760ee3243260eb8a4dbc6fd15054c221
PODFILE CHECKSUM: 2e494b482bc938579e08ab48dab20d64709c3062
COCOAPODS: 1.0.0.beta.3

View File

@ -0,0 +1,42 @@
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#import <Foundation/Foundation.h>
#import <Bolts/BFCancellationTokenRegistration.h>
NS_ASSUME_NONNULL_BEGIN
/*!
A block that will be called when a token is cancelled.
*/
typedef void(^BFCancellationBlock)();
/*!
The consumer view of a CancellationToken.
Propagates notification that operations should be canceled.
A BFCancellationToken has methods to inspect whether the token has been cancelled.
*/
@interface BFCancellationToken : NSObject
/*!
Whether cancellation has been requested for this token source.
*/
@property (nonatomic, assign, readonly, getter=isCancellationRequested) BOOL cancellationRequested;
/*!
Register a block to be notified when the token is cancelled.
If the token is already cancelled the delegate will be notified immediately.
*/
- (BFCancellationTokenRegistration *)registerCancellationObserverWithBlock:(BFCancellationBlock)block;
@end
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,141 @@
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#import "BFCancellationToken.h"
#import "BFCancellationTokenRegistration.h"
@interface BFCancellationToken ()
@property (nonatomic, assign, getter=isCancellationRequested) BOOL cancellationRequested;
@property (nonatomic, strong) NSMutableArray *registrations;
@property (nonatomic, strong) NSObject *lock;
@property (nonatomic) BOOL disposed;
@end
@interface BFCancellationTokenRegistration (BFCancellationToken)
+ (instancetype)registrationWithToken:(BFCancellationToken *)token delegate:(BFCancellationBlock)delegate;
- (void)notifyDelegate;
@end
@implementation BFCancellationToken
#pragma mark - Initializer
- (instancetype)init {
self = [super init];
if (!self) return nil;
_registrations = [NSMutableArray array];
_lock = [NSObject new];
return self;
}
#pragma mark - Custom Setters/Getters
- (BOOL)isCancellationRequested {
@synchronized(self.lock) {
[self throwIfDisposed];
return _cancellationRequested;
}
}
- (void)cancel {
NSArray *registrations;
@synchronized(self.lock) {
[self throwIfDisposed];
if (_cancellationRequested) {
return;
}
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(cancelPrivate) object:nil];
_cancellationRequested = YES;
registrations = [self.registrations copy];
}
[self notifyCancellation:registrations];
}
- (void)notifyCancellation:(NSArray *)registrations {
for (BFCancellationTokenRegistration *registration in registrations) {
[registration notifyDelegate];
}
}
- (BFCancellationTokenRegistration *)registerCancellationObserverWithBlock:(BFCancellationBlock)block {
@synchronized(self.lock) {
BFCancellationTokenRegistration *registration = [BFCancellationTokenRegistration registrationWithToken:self delegate:[block copy]];
[self.registrations addObject:registration];
return registration;
}
}
- (void)unregisterRegistration:(BFCancellationTokenRegistration *)registration {
@synchronized(self.lock) {
[self throwIfDisposed];
[self.registrations removeObject:registration];
}
}
// Delay on a non-public method to prevent interference with a user calling performSelector or
// cancelPreviousPerformRequestsWithTarget on the public method
- (void)cancelPrivate {
[self cancel];
}
- (void)cancelAfterDelay:(int)millis {
[self throwIfDisposed];
if (millis < -1) {
[NSException raise:NSInvalidArgumentException format:@"Delay must be >= -1"];
}
if (millis == 0) {
[self cancel];
return;
}
@synchronized(self.lock) {
[self throwIfDisposed];
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(cancelPrivate) object:nil];
if (self.cancellationRequested) {
return;
}
if (millis != -1) {
double delay = (double)millis / 1000;
[self performSelector:@selector(cancelPrivate) withObject:nil afterDelay:delay];
}
}
}
- (void)dispose {
@synchronized(self.lock) {
if (self.disposed) {
return;
}
self.disposed = YES;
for (BFCancellationTokenRegistration *registration in self.registrations) {
[registration dispose];
}
[self.registrations removeAllObjects];
}
}
- (void)throwIfDisposed {
if (self.disposed) {
[NSException raise:NSInternalInconsistencyException format:@"Object already disposed"];
}
}
@end

View File

@ -0,0 +1,29 @@
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
/*!
Represents the registration of a cancellation observer with a cancellation token.
Can be used to unregister the observer at a later time.
*/
@interface BFCancellationTokenRegistration : NSObject
/*!
Removes the cancellation observer registered with the token
and releases all resources associated with this registration.
*/
- (void)dispose;
@end
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,75 @@
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#import "BFCancellationTokenRegistration.h"
#import "BFCancellationToken.h"
@interface BFCancellationTokenRegistration ()
@property (nonatomic, weak) BFCancellationToken *token;
@property (nonatomic, strong) BFCancellationBlock cancellationObserverBlock;
@property (nonatomic, strong) NSObject *lock;
@property (nonatomic) BOOL disposed;
@end
@interface BFCancellationToken (BFCancellationTokenRegistration)
- (void)unregisterRegistration:(BFCancellationTokenRegistration *)registration;
@end
@implementation BFCancellationTokenRegistration
+ (instancetype)registrationWithToken:(BFCancellationToken *)token delegate:(BFCancellationBlock)delegate {
BFCancellationTokenRegistration *registration = [BFCancellationTokenRegistration new];
registration.token = token;
registration.cancellationObserverBlock = delegate;
return registration;
}
- (instancetype)init {
self = [super init];
if (!self) return nil;
_lock = [NSObject new];
return self;
}
- (void)dispose {
@synchronized(self.lock) {
if (self.disposed) {
return;
}
self.disposed = YES;
}
BFCancellationToken *token = self.token;
if (token != nil) {
[token unregisterRegistration:self];
self.token = nil;
}
self.cancellationObserverBlock = nil;
}
- (void)notifyDelegate {
@synchronized(self.lock) {
[self throwIfDisposed];
self.cancellationObserverBlock();
}
}
- (void)throwIfDisposed {
NSAssert(!self.disposed, @"Object already disposed");
}
@end

View File

@ -0,0 +1,60 @@
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@class BFCancellationToken;
/*!
BFCancellationTokenSource represents the producer side of a CancellationToken.
Signals to a CancellationToken that it should be canceled.
It is a cancellation token that also has methods
for changing the state of a token by cancelling it.
*/
@interface BFCancellationTokenSource : NSObject
/*!
Creates a new cancellation token source.
*/
+ (instancetype)cancellationTokenSource;
/*!
The cancellation token associated with this CancellationTokenSource.
*/
@property (nonatomic, strong, readonly) BFCancellationToken *token;
/*!
Whether cancellation has been requested for this token source.
*/
@property (nonatomic, assign, readonly, getter=isCancellationRequested) BOOL cancellationRequested;
/*!
Cancels the token if it has not already been cancelled.
*/
- (void)cancel;
/*!
Schedules a cancel operation on this CancellationTokenSource after the specified number of milliseconds.
@param millis The number of milliseconds to wait before completing the returned task.
If delay is `0` the cancel is executed immediately. If delay is `-1` any scheduled cancellation is stopped.
*/
- (void)cancelAfterDelay:(int)millis;
/*!
Releases all resources associated with this token source,
including disposing of all registrations.
*/
- (void)dispose;
@end
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,60 @@
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#import "BFCancellationTokenSource.h"
#import "BFCancellationToken.h"
@interface BFCancellationToken (BFCancellationTokenSource)
- (void)cancel;
- (void)cancelAfterDelay:(int)millis;
- (void)dispose;
- (void)throwIfDisposed;
@end
@implementation BFCancellationTokenSource
#pragma mark - Initializer
- (instancetype)init {
self = [super init];
if (!self) return nil;
_token = [BFCancellationToken new];
return self;
}
+ (instancetype)cancellationTokenSource {
return [BFCancellationTokenSource new];
}
#pragma mark - Custom Setters/Getters
- (BOOL)isCancellationRequested {
return _token.isCancellationRequested;
}
- (void)cancel {
[_token cancel];
}
- (void)cancelAfterDelay:(int)millis {
[_token cancelAfterDelay:millis];
}
- (void)dispose {
[_token dispose];
}
@end

View File

@ -0,0 +1,62 @@
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
/*!
An object that can run a given block.
*/
@interface BFExecutor : NSObject
/*!
Returns a default executor, which runs continuations immediately until the call stack gets too
deep, then dispatches to a new GCD queue.
*/
+ (instancetype)defaultExecutor;
/*!
Returns an executor that runs continuations on the thread where the previous task was completed.
*/
+ (instancetype)immediateExecutor;
/*!
Returns an executor that runs continuations on the main thread.
*/
+ (instancetype)mainThreadExecutor;
/*!
Returns a new executor that uses the given block to execute continuations.
@param block The block to use.
*/
+ (instancetype)executorWithBlock:(void(^)(void(^block)()))block;
/*!
Returns a new executor that runs continuations on the given queue.
@param queue The instance of `dispatch_queue_t` to dispatch all continuations onto.
*/
+ (instancetype)executorWithDispatchQueue:(dispatch_queue_t)queue;
/*!
Returns a new executor that runs continuations on the given queue.
@param queue The instance of `NSOperationQueue` to run all continuations on.
*/
+ (instancetype)executorWithOperationQueue:(NSOperationQueue *)queue;
/*!
Runs the given block using this executor's particular strategy.
@param block The block to execute.
*/
- (void)execute:(void(^)())block;
@end
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,132 @@
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#import "BFExecutor.h"
#import <pthread.h>
/*!
Get the remaining stack-size of the current thread.
@param totalSize The total stack size of the current thread.
@return The remaining size, in bytes, available to the current thread.
@note This function cannot be inlined, as otherwise the internal implementation could fail to report the proper
remaining stack space.
*/
__attribute__((noinline)) static size_t remaining_stack_size(size_t *__nonnull restrict totalSize) {
pthread_t currentThread = pthread_self();
// NOTE: We must store stack pointers as uint8_t so that the pointer math is well-defined
uint8_t *endStack = pthread_get_stackaddr_np(currentThread);
*totalSize = pthread_get_stacksize_np(currentThread);
// NOTE: If the function is inlined, this value could be incorrect
uint8_t *frameAddr = __builtin_frame_address(0);
return (*totalSize) - (endStack - frameAddr);
}
@interface BFExecutor ()
@property (nonatomic, copy) void(^block)(void(^block)());
@end
@implementation BFExecutor
#pragma mark - Executor methods
+ (instancetype)defaultExecutor {
static BFExecutor *defaultExecutor = NULL;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
defaultExecutor = [self executorWithBlock:^void(void(^block)()) {
// We prefer to run everything possible immediately, so that there is callstack information
// when debugging. However, we don't want the stack to get too deep, so if the remaining stack space
// is less than 10% of the total space, we dispatch to another GCD queue.
size_t totalStackSize = 0;
size_t remainingStackSize = remaining_stack_size(&totalStackSize);
if (remainingStackSize < (totalStackSize / 10)) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), block);
} else {
@autoreleasepool {
block();
}
}
}];
});
return defaultExecutor;
}
+ (instancetype)immediateExecutor {
static BFExecutor *immediateExecutor = NULL;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
immediateExecutor = [self executorWithBlock:^void(void(^block)()) {
block();
}];
});
return immediateExecutor;
}
+ (instancetype)mainThreadExecutor {
static BFExecutor *mainThreadExecutor = NULL;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
mainThreadExecutor = [self executorWithBlock:^void(void(^block)()) {
if (![NSThread isMainThread]) {
dispatch_async(dispatch_get_main_queue(), block);
} else {
@autoreleasepool {
block();
}
}
}];
});
return mainThreadExecutor;
}
+ (instancetype)executorWithBlock:(void(^)(void(^block)()))block {
return [[self alloc] initWithBlock:block];
}
+ (instancetype)executorWithDispatchQueue:(dispatch_queue_t)queue {
return [self executorWithBlock:^void(void(^block)()) {
dispatch_async(queue, block);
}];
}
+ (instancetype)executorWithOperationQueue:(NSOperationQueue *)queue {
return [self executorWithBlock:^void(void(^block)()) {
[queue addOperation:[NSBlockOperation blockOperationWithBlock:block]];
}];
}
#pragma mark - Initializer
- (instancetype)initWithBlock:(void(^)(void(^block)()))block {
self = [super init];
if (!self) return nil;
_block = block;
return self;
}
#pragma mark - Execution
- (void)execute:(void(^)())block {
self.block(block);
}
@end

261
Example/Pods/Bolts/Bolts/Common/BFTask.h generated Normal file
View File

@ -0,0 +1,261 @@
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#import <Foundation/Foundation.h>
#import <Bolts/BFCancellationToken.h>
NS_ASSUME_NONNULL_BEGIN
/*!
Error domain used if there was multiple errors on <BFTask taskForCompletionOfAllTasks:>.
*/
extern NSString *const BFTaskErrorDomain;
/*!
An error code used for <BFTask taskForCompletionOfAllTasks:>, if there were multiple errors.
*/
extern NSInteger const kBFMultipleErrorsError;
/*!
An exception that is thrown if there was multiple exceptions on <BFTask taskForCompletionOfAllTasks:>.
*/
extern NSString *const BFTaskMultipleExceptionsException;
@class BFExecutor;
@class BFTask;
/*!
The consumer view of a Task. A BFTask has methods to
inspect the state of the task, and to add continuations to
be run once the task is complete.
*/
@interface BFTask<__covariant ResultType> : NSObject
/*!
A block that can act as a continuation for a task.
*/
typedef __nullable id(^BFContinuationBlock)(BFTask<ResultType> *task);
/*!
Creates a task that is already completed with the given result.
@param result The result for the task.
*/
+ (instancetype)taskWithResult:(nullable ResultType)result;
/*!
Creates a task that is already completed with the given error.
@param error The error for the task.
*/
+ (instancetype)taskWithError:(NSError *)error;
/*!
Creates a task that is already completed with the given exception.
@param exception The exception for the task.
*/
+ (instancetype)taskWithException:(NSException *)exception;
/*!
Creates a task that is already cancelled.
*/
+ (instancetype)cancelledTask;
/*!
Returns a task that will be completed (with result == nil) once
all of the input tasks have completed.
@param tasks An `NSArray` of the tasks to use as an input.
*/
+ (instancetype)taskForCompletionOfAllTasks:(nullable NSArray<BFTask *> *)tasks;
/*!
Returns a task that will be completed once all of the input tasks have completed.
If all tasks complete successfully without being faulted or cancelled the result will be
an `NSArray` of all task results in the order they were provided.
@param tasks An `NSArray` of the tasks to use as an input.
*/
+ (instancetype)taskForCompletionOfAllTasksWithResults:(nullable NSArray<BFTask *> *)tasks;
/*!
Returns a task that will be completed a certain amount of time in the future.
@param millis The approximate number of milliseconds to wait before the
task will be finished (with result == nil).
*/
+ (instancetype)taskWithDelay:(int)millis;
/*!
Returns a task that will be completed a certain amount of time in the future.
@param millis The approximate number of milliseconds to wait before the
task will be finished (with result == nil).
@param token The cancellation token (optional).
*/
+ (instancetype)taskWithDelay:(int)millis cancellationToken:(nullable BFCancellationToken *)token;
/*!
Returns a task that will be completed after the given block completes with
the specified executor.
@param executor A BFExecutor responsible for determining how the
continuation block will be run.
@param block The block to immediately schedule to run with the given executor.
@returns A task that will be completed after block has run.
If block returns a BFTask, then the task returned from
this method will not be completed until that task is completed.
*/
+ (instancetype)taskFromExecutor:(BFExecutor *)executor withBlock:(nullable id (^)())block;
// Properties that will be set on the task once it is completed.
/*!
The result of a successful task.
*/
@property (nullable, nonatomic, strong, readonly) ResultType result;
/*!
The error of a failed task.
*/
@property (nullable, nonatomic, strong, readonly) NSError *error;
/*!
The exception of a failed task.
*/
@property (nullable, nonatomic, strong, readonly) NSException *exception;
/*!
Whether this task has been cancelled.
*/
@property (nonatomic, assign, readonly, getter=isCancelled) BOOL cancelled;
/*!
Whether this task has completed due to an error or exception.
*/
@property (nonatomic, assign, readonly, getter=isFaulted) BOOL faulted;
/*!
Whether this task has completed.
*/
@property (nonatomic, assign, readonly, getter=isCompleted) BOOL completed;
/*!
Enqueues the given block to be run once this task is complete.
This method uses a default execution strategy. The block will be
run on the thread where the previous task completes, unless the
the stack depth is too deep, in which case it will be run on a
dispatch queue with default priority.
@param block The block to be run once this task is complete.
@returns A task that will be completed after block has run.
If block returns a BFTask, then the task returned from
this method will not be completed until that task is completed.
*/
- (BFTask *)continueWithBlock:(BFContinuationBlock)block;
/*!
Enqueues the given block to be run once this task is complete.
This method uses a default execution strategy. The block will be
run on the thread where the previous task completes, unless the
the stack depth is too deep, in which case it will be run on a
dispatch queue with default priority.
@param block The block to be run once this task is complete.
@param cancellationToken The cancellation token (optional).
@returns A task that will be completed after block has run.
If block returns a BFTask, then the task returned from
this method will not be completed until that task is completed.
*/
- (BFTask *)continueWithBlock:(BFContinuationBlock)block cancellationToken:(nullable BFCancellationToken *)cancellationToken;
/*!
Enqueues the given block to be run once this task is complete.
@param executor A BFExecutor responsible for determining how the
continuation block will be run.
@param block The block to be run once this task is complete.
@returns A task that will be completed after block has run.
If block returns a BFTask, then the task returned from
this method will not be completed until that task is completed.
*/
- (BFTask *)continueWithExecutor:(BFExecutor *)executor withBlock:(BFContinuationBlock)block;
/*!
Enqueues the given block to be run once this task is complete.
@param executor A BFExecutor responsible for determining how the
continuation block will be run.
@param block The block to be run once this task is complete.
@param cancellationToken The cancellation token (optional).
@returns A task that will be completed after block has run.
If block returns a BFTask, then the task returned from
his method will not be completed until that task is completed.
*/
- (BFTask *)continueWithExecutor:(BFExecutor *)executor
block:(BFContinuationBlock)block
cancellationToken:(nullable BFCancellationToken *)cancellationToken;
/*!
Identical to continueWithBlock:, except that the block is only run
if this task did not produce a cancellation, error, or exception.
If it did, then the failure will be propagated to the returned
task.
@param block The block to be run once this task is complete.
@returns A task that will be completed after block has run.
If block returns a BFTask, then the task returned from
this method will not be completed until that task is completed.
*/
- (BFTask *)continueWithSuccessBlock:(BFContinuationBlock)block;
/*!
Identical to continueWithBlock:, except that the block is only run
if this task did not produce a cancellation, error, or exception.
If it did, then the failure will be propagated to the returned
task.
@param block The block to be run once this task is complete.
@param cancellationToken The cancellation token (optional).
@returns A task that will be completed after block has run.
If block returns a BFTask, then the task returned from
this method will not be completed until that task is completed.
*/
- (BFTask *)continueWithSuccessBlock:(BFContinuationBlock)block cancellationToken:(nullable BFCancellationToken *)cancellationToken;
/*!
Identical to continueWithExecutor:withBlock:, except that the block
is only run if this task did not produce a cancellation, error, or
exception. If it did, then the failure will be propagated to the
returned task.
@param executor A BFExecutor responsible for determining how the
continuation block will be run.
@param block The block to be run once this task is complete.
@returns A task that will be completed after block has run.
If block returns a BFTask, then the task returned from
this method will not be completed until that task is completed.
*/
- (BFTask *)continueWithExecutor:(BFExecutor *)executor withSuccessBlock:(BFContinuationBlock)block;
/*!
Identical to continueWithExecutor:withBlock:, except that the block
is only run if this task did not produce a cancellation, error, or
exception. If it did, then the failure will be propagated to the
returned task.
@param executor A BFExecutor responsible for determining how the
continuation block will be run.
@param block The block to be run once this task is complete.
@param cancellationToken The cancellation token (optional).
@returns A task that will be completed after block has run.
If block returns a BFTask, then the task returned from
this method will not be completed until that task is completed.
*/
- (BFTask *)continueWithExecutor:(BFExecutor *)executor
successBlock:(BFContinuationBlock)block
cancellationToken:(nullable BFCancellationToken *)cancellationToken;
/*!
Waits until this operation is completed.
This method is inefficient and consumes a thread resource while
it's running. It should be avoided. This method logs a warning
message if it is used on the main thread.
*/
- (void)waitUntilFinished;
@end
NS_ASSUME_NONNULL_END

472
Example/Pods/Bolts/Bolts/Common/BFTask.m generated Normal file
View File

@ -0,0 +1,472 @@
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#import "BFTask.h"
#import <libkern/OSAtomic.h>
#import "Bolts.h"
__attribute__ ((noinline)) void warnBlockingOperationOnMainThread() {
NSLog(@"Warning: A long-running operation is being executed on the main thread. \n"
" Break on warnBlockingOperationOnMainThread() to debug.");
}
NSString *const BFTaskErrorDomain = @"bolts";
NSInteger const kBFMultipleErrorsError = 80175001;
NSString *const BFTaskMultipleExceptionsException = @"BFMultipleExceptionsException";
@interface BFTask () {
id _result;
NSError *_error;
NSException *_exception;
}
@property (nonatomic, assign, readwrite, getter=isCancelled) BOOL cancelled;
@property (nonatomic, assign, readwrite, getter=isFaulted) BOOL faulted;
@property (nonatomic, assign, readwrite, getter=isCompleted) BOOL completed;
@property (nonatomic, strong) NSObject *lock;
@property (nonatomic, strong) NSCondition *condition;
@property (nonatomic, strong) NSMutableArray *callbacks;
@end
@implementation BFTask
#pragma mark - Initializer
- (instancetype)init {
self = [super init];
if (!self) return nil;
_lock = [[NSObject alloc] init];
_condition = [[NSCondition alloc] init];
_callbacks = [NSMutableArray array];
return self;
}
- (instancetype)initWithResult:(id)result {
self = [super init];
if (!self) return nil;
[self trySetResult:result];
return self;
}
- (instancetype)initWithError:(NSError *)error {
self = [super init];
if (!self) return nil;
[self trySetError:error];
return self;
}
- (instancetype)initWithException:(NSException *)exception {
self = [super init];
if (!self) return nil;
[self trySetException:exception];
return self;
}
- (instancetype)initCancelled {
self = [super init];
if (!self) return nil;
[self trySetCancelled];
return self;
}
#pragma mark - Task Class methods
+ (instancetype)taskWithResult:(id)result {
return [[self alloc] initWithResult:result];
}
+ (instancetype)taskWithError:(NSError *)error {
return [[self alloc] initWithError:error];
}
+ (instancetype)taskWithException:(NSException *)exception {
return [[self alloc] initWithException:exception];
}
+ (instancetype)cancelledTask {
return [[self alloc] initCancelled];
}
+ (instancetype)taskForCompletionOfAllTasks:(NSArray<BFTask *> *)tasks {
__block int32_t total = (int32_t)tasks.count;
if (total == 0) {
return [self taskWithResult:nil];
}
__block int32_t cancelled = 0;
NSObject *lock = [[NSObject alloc] init];
NSMutableArray *errors = [NSMutableArray array];
NSMutableArray *exceptions = [NSMutableArray array];
BFTaskCompletionSource *tcs = [BFTaskCompletionSource taskCompletionSource];
for (BFTask *task in tasks) {
[task continueWithBlock:^id(BFTask *task) {
if (task.exception) {
@synchronized (lock) {
[exceptions addObject:task.exception];
}
} else if (task.error) {
@synchronized (lock) {
[errors addObject:task.error];
}
} else if (task.cancelled) {
OSAtomicIncrement32(&cancelled);
}
if (OSAtomicDecrement32(&total) == 0) {
if (exceptions.count > 0) {
if (exceptions.count == 1) {
tcs.exception = [exceptions firstObject];
} else {
NSException *exception =
[NSException exceptionWithName:BFTaskMultipleExceptionsException
reason:@"There were multiple exceptions."
userInfo:@{ @"exceptions": exceptions }];
tcs.exception = exception;
}
} else if (errors.count > 0) {
if (errors.count == 1) {
tcs.error = [errors firstObject];
} else {
NSError *error = [NSError errorWithDomain:BFTaskErrorDomain
code:kBFMultipleErrorsError
userInfo:@{ @"errors": errors }];
tcs.error = error;
}
} else if (cancelled > 0) {
[tcs cancel];
} else {
tcs.result = nil;
}
}
return nil;
}];
}
return tcs.task;
}
+ (instancetype)taskForCompletionOfAllTasksWithResults:(NSArray<BFTask *> *)tasks {
return [[self taskForCompletionOfAllTasks:tasks] continueWithSuccessBlock:^id(BFTask *task) {
return [tasks valueForKey:@"result"];
}];
}
+ (instancetype)taskWithDelay:(int)millis {
BFTaskCompletionSource *tcs = [BFTaskCompletionSource taskCompletionSource];
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, millis * NSEC_PER_MSEC);
dispatch_after(popTime, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
tcs.result = nil;
});
return tcs.task;
}
+ (instancetype)taskWithDelay:(int)millis
cancellationToken:(BFCancellationToken *)token {
if (token.cancellationRequested) {
return [BFTask cancelledTask];
}
BFTaskCompletionSource *tcs = [BFTaskCompletionSource taskCompletionSource];
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, millis * NSEC_PER_MSEC);
dispatch_after(popTime, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
if (token.cancellationRequested) {
[tcs cancel];
return;
}
tcs.result = nil;
});
return tcs.task;
}
+ (instancetype)taskFromExecutor:(BFExecutor *)executor withBlock:(nullable id (^)())block {
return [[self taskWithResult:nil] continueWithExecutor:executor withBlock:^id(BFTask *task) {
return block();
}];
}
#pragma mark - Custom Setters/Getters
- (id)result {
@synchronized(self.lock) {
return _result;
}
}
- (BOOL)trySetResult:(id)result {
@synchronized(self.lock) {
if (self.completed) {
return NO;
}
self.completed = YES;
_result = result;
[self runContinuations];
return YES;
}
}
- (NSError *)error {
@synchronized(self.lock) {
return _error;
}
}
- (BOOL)trySetError:(NSError *)error {
@synchronized(self.lock) {
if (self.completed) {
return NO;
}
self.completed = YES;
self.faulted = YES;
_error = error;
[self runContinuations];
return YES;
}
}
- (NSException *)exception {
@synchronized(self.lock) {
return _exception;
}
}
- (BOOL)trySetException:(NSException *)exception {
@synchronized(self.lock) {
if (self.completed) {
return NO;
}
self.completed = YES;
self.faulted = YES;
_exception = exception;
[self runContinuations];
return YES;
}
}
- (BOOL)isCancelled {
@synchronized(self.lock) {
return _cancelled;
}
}
- (BOOL)isFaulted {
@synchronized(self.lock) {
return _faulted;
}
}
- (BOOL)trySetCancelled {
@synchronized(self.lock) {
if (self.completed) {
return NO;
}
self.completed = YES;
self.cancelled = YES;
[self runContinuations];
return YES;
}
}
- (BOOL)isCompleted {
@synchronized(self.lock) {
return _completed;
}
}
- (void)setCompleted {
@synchronized(self.lock) {
_completed = YES;
}
}
- (void)runContinuations {
@synchronized(self.lock) {
[self.condition lock];
[self.condition broadcast];
[self.condition unlock];
for (void (^callback)() in self.callbacks) {
callback();
}
[self.callbacks removeAllObjects];
}
}
#pragma mark - Chaining methods
- (BFTask *)continueWithExecutor:(BFExecutor *)executor
withBlock:(BFContinuationBlock)block {
return [self continueWithExecutor:executor block:block cancellationToken:nil];
}
- (BFTask *)continueWithExecutor:(BFExecutor *)executor
block:(BFContinuationBlock)block
cancellationToken:(BFCancellationToken *)cancellationToken {
BFTaskCompletionSource *tcs = [BFTaskCompletionSource taskCompletionSource];
// Capture all of the state that needs to used when the continuation is complete.
void (^wrappedBlock)() = ^() {
[executor execute:^{
if (cancellationToken.cancellationRequested) {
[tcs cancel];
return;
}
id result = nil;
@try {
result = block(self);
} @catch (NSException *exception) {
tcs.exception = exception;
return;
}
if ([result isKindOfClass:[BFTask class]]) {
id (^setupWithTask) (BFTask *) = ^id(BFTask *task) {
if (cancellationToken.cancellationRequested || task.cancelled) {
[tcs cancel];
} else if (task.exception) {
tcs.exception = task.exception;
} else if (task.error) {
tcs.error = task.error;
} else {
tcs.result = task.result;
}
return nil;
};
BFTask *resultTask = (BFTask *)result;
if (resultTask.completed) {
setupWithTask(resultTask);
} else {
[resultTask continueWithBlock:setupWithTask];
}
} else {
tcs.result = result;
}
}];
};
BOOL completed;
@synchronized(self.lock) {
completed = self.completed;
if (!completed) {
[self.callbacks addObject:[wrappedBlock copy]];
}
}
if (completed) {
wrappedBlock();
}
return tcs.task;
}
- (BFTask *)continueWithBlock:(BFContinuationBlock)block {
return [self continueWithExecutor:[BFExecutor defaultExecutor] block:block cancellationToken:nil];
}
- (BFTask *)continueWithBlock:(BFContinuationBlock)block
cancellationToken:(BFCancellationToken *)cancellationToken {
return [self continueWithExecutor:[BFExecutor defaultExecutor] block:block cancellationToken:cancellationToken];
}
- (BFTask *)continueWithExecutor:(BFExecutor *)executor
withSuccessBlock:(BFContinuationBlock)block {
return [self continueWithExecutor:executor successBlock:block cancellationToken:nil];
}
- (BFTask *)continueWithExecutor:(BFExecutor *)executor
successBlock:(BFContinuationBlock)block
cancellationToken:(BFCancellationToken *)cancellationToken {
if (cancellationToken.cancellationRequested) {
return [BFTask cancelledTask];
}
return [self continueWithExecutor:executor block:^id(BFTask *task) {
if (task.faulted || task.cancelled) {
return task;
} else {
return block(task);
}
} cancellationToken:cancellationToken];
}
- (BFTask *)continueWithSuccessBlock:(BFContinuationBlock)block {
return [self continueWithExecutor:[BFExecutor defaultExecutor] successBlock:block cancellationToken:nil];
}
- (BFTask *)continueWithSuccessBlock:(BFContinuationBlock)block
cancellationToken:(BFCancellationToken *)cancellationToken {
return [self continueWithExecutor:[BFExecutor defaultExecutor] successBlock:block cancellationToken:cancellationToken];
}
#pragma mark - Syncing Task (Avoid it)
- (void)warnOperationOnMainThread {
warnBlockingOperationOnMainThread();
}
- (void)waitUntilFinished {
if ([NSThread isMainThread]) {
[self warnOperationOnMainThread];
}
@synchronized(self.lock) {
if (self.completed) {
return;
}
[self.condition lock];
}
[self.condition wait];
[self.condition unlock];
}
#pragma mark - NSObject
- (NSString *)description {
// Acquire the data from the locked properties
BOOL completed;
BOOL cancelled;
BOOL faulted;
NSString *resultDescription = nil;
@synchronized(self.lock) {
completed = self.completed;
cancelled = self.cancelled;
faulted = self.faulted;
resultDescription = completed ? [NSString stringWithFormat:@" result = %@", self.result] : @"";
}
// Description string includes status information and, if available, the
// result since in some ways this is what a promise actually "is".
return [NSString stringWithFormat:@"<%@: %p; completed = %@; cancelled = %@; faulted = %@;%@>",
NSStringFromClass([self class]),
self,
completed ? @"YES" : @"NO",
cancelled ? @"YES" : @"NO",
faulted ? @"YES" : @"NO",
resultDescription];
}
@end

View File

@ -0,0 +1,89 @@
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@class BFTask<ResultType>;
/*!
A BFTaskCompletionSource represents the producer side of tasks.
It is a task that also has methods for changing the state of the
task by settings its completion values.
*/
@interface BFTaskCompletionSource<__covariant ResultType> : NSObject
/*!
Creates a new unfinished task.
*/
+ (instancetype)taskCompletionSource;
/*!
The task associated with this TaskCompletionSource.
*/
@property (nonatomic, strong, readonly) BFTask<ResultType> *task;
/*!
Completes the task by setting the result.
Attempting to set this for a completed task will raise an exception.
@param result The result of the task.
*/
- (void)setResult:(nullable ResultType)result;
/*!
Completes the task by setting the error.
Attempting to set this for a completed task will raise an exception.
@param error The error for the task.
*/
- (void)setError:(NSError *)error;
/*!
Completes the task by setting an exception.
Attempting to set this for a completed task will raise an exception.
@param exception The exception for the task.
*/
- (void)setException:(NSException *)exception;
/*!
Completes the task by marking it as cancelled.
Attempting to set this for a completed task will raise an exception.
*/
- (void)cancel;
/*!
Sets the result of the task if it wasn't already completed.
@returns whether the new value was set.
*/
- (BOOL)trySetResult:(nullable ResultType)result;
/*!
Sets the error of the task if it wasn't already completed.
@param error The error for the task.
@returns whether the new value was set.
*/
- (BOOL)trySetError:(NSError *)error;
/*!
Sets the exception of the task if it wasn't already completed.
@param exception The exception for the task.
@returns whether the new value was set.
*/
- (BOOL)trySetException:(NSException *)exception;
/*!
Sets the cancellation state of the task if it wasn't already completed.
@returns whether the new value was set.
*/
- (BOOL)trySetCancelled;
@end
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,93 @@
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#import "BFTaskCompletionSource.h"
#import "BFTask.h"
@interface BFTaskCompletionSource ()
@property (nonatomic, strong, readwrite) BFTask *task;
@end
@interface BFTask (BFTaskCompletionSource)
- (BOOL)trySetResult:(id)result;
- (BOOL)trySetError:(NSError *)error;
- (BOOL)trySetException:(NSException *)exception;
- (BOOL)trySetCancelled;
@end
@implementation BFTaskCompletionSource
#pragma mark - Initializer
+ (instancetype)taskCompletionSource {
return [[self alloc] init];
}
- (instancetype)init {
self = [super init];
if (!self) return nil;
_task = [[BFTask alloc] init];
return self;
}
#pragma mark - Custom Setters/Getters
- (void)setResult:(id)result {
if (![self.task trySetResult:result]) {
[NSException raise:NSInternalInconsistencyException
format:@"Cannot set the result on a completed task."];
}
}
- (void)setError:(NSError *)error {
if (![self.task trySetError:error]) {
[NSException raise:NSInternalInconsistencyException
format:@"Cannot set the error on a completed task."];
}
}
- (void)setException:(NSException *)exception {
if (![self.task trySetException:exception]) {
[NSException raise:NSInternalInconsistencyException
format:@"Cannot set the exception on a completed task."];
}
}
- (void)cancel {
if (![self.task trySetCancelled]) {
[NSException raise:NSInternalInconsistencyException
format:@"Cannot cancel a completed task."];
}
}
- (BOOL)trySetResult:(id)result {
return [self.task trySetResult:result];
}
- (BOOL)trySetError:(NSError *)error {
return [self.task trySetError:error];
}
- (BOOL)trySetException:(NSException *)exception {
return [self.task trySetException:exception];
}
- (BOOL)trySetCancelled {
return [self.task trySetCancelled];
}
@end

43
Example/Pods/Bolts/Bolts/Common/Bolts.h generated Normal file
View File

@ -0,0 +1,43 @@
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#import <Bolts/BoltsVersion.h>
#import <Bolts/BFCancellationToken.h>
#import <Bolts/BFCancellationTokenRegistration.h>
#import <Bolts/BFCancellationTokenSource.h>
#import <Bolts/BFExecutor.h>
#import <Bolts/BFTask.h>
#import <Bolts/BFTaskCompletionSource.h>
#if __has_include(<Bolts/BFAppLink.h>) && TARGET_OS_IPHONE && !TARGET_OS_WATCH && !TARGET_OS_TV
#import <Bolts/BFAppLink.h>
#import <Bolts/BFAppLinkNavigation.h>
#import <Bolts/BFAppLinkResolving.h>
#import <Bolts/BFAppLinkReturnToRefererController.h>
#import <Bolts/BFAppLinkReturnToRefererView.h>
#import <Bolts/BFAppLinkTarget.h>
#import <Bolts/BFMeasurementEvent.h>
#import <Bolts/BFURL.h>
#import <Bolts/BFWebViewAppLinkResolver.h>
#endif
NS_ASSUME_NONNULL_BEGIN
@interface Bolts : NSObject
/*!
Returns the version of the Bolts Framework as an NSString.
@returns The NSString representation of the current version.
*/
+ (NSString *)version;
@end
NS_ASSUME_NONNULL_END

19
Example/Pods/Bolts/Bolts/Common/Bolts.m generated Normal file
View File

@ -0,0 +1,19 @@
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#import "Bolts.h"
@implementation Bolts
+ (NSString *)version {
return BOLTS_VERSION;
}
@end

View File

@ -0,0 +1 @@
#define BOLTS_VERSION @"1.6.0"

49
Example/Pods/Bolts/Bolts/iOS/BFAppLink.h generated Normal file
View File

@ -0,0 +1,49 @@
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#import <Foundation/Foundation.h>
/*! The version of the App Link protocol that this library supports */
FOUNDATION_EXPORT NSString *const BFAppLinkVersion;
/*!
Contains App Link metadata relevant for navigation on this device
derived from the HTML at a given URL.
*/
@interface BFAppLink : NSObject
/*!
Creates a BFAppLink with the given list of BFAppLinkTargets and target URL.
Generally, this will only be used by implementers of the BFAppLinkResolving protocol,
as these implementers will produce App Link metadata for a given URL.
@param sourceURL the URL from which this App Link is derived
@param targets an ordered list of BFAppLinkTargets for this platform derived
from App Link metadata.
@param webURL the fallback web URL, if any, for the app link.
*/
+ (instancetype)appLinkWithSourceURL:(NSURL *)sourceURL
targets:(NSArray *)targets
webURL:(NSURL *)webURL;
/*! The URL from which this BFAppLink was derived */
@property (nonatomic, strong, readonly) NSURL *sourceURL;
/*!
The ordered list of targets applicable to this platform that will be used
for navigation.
*/
@property (nonatomic, copy, readonly) NSArray *targets;
/*! The fallback web URL to use if no targets are installed on this device. */
@property (nonatomic, strong, readonly) NSURL *webURL;
@end

62
Example/Pods/Bolts/Bolts/iOS/BFAppLink.m generated Normal file
View File

@ -0,0 +1,62 @@
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#import "BFAppLink_Internal.h"
NSString *const BFAppLinkDataParameterName = @"al_applink_data";
NSString *const BFAppLinkTargetKeyName = @"target_url";
NSString *const BFAppLinkUserAgentKeyName = @"user_agent";
NSString *const BFAppLinkExtrasKeyName = @"extras";
NSString *const BFAppLinkRefererAppLink = @"referer_app_link";
NSString *const BFAppLinkRefererAppName = @"app_name";
NSString *const BFAppLinkRefererUrl = @"url";
NSString *const BFAppLinkVersionKeyName = @"version";
NSString *const BFAppLinkVersion = @"1.0";
@interface BFAppLink ()
@property (nonatomic, strong, readwrite) NSURL *sourceURL;
@property (nonatomic, copy, readwrite) NSArray *targets;
@property (nonatomic, strong, readwrite) NSURL *webURL;
@property (nonatomic, assign, readwrite, getter=isBackToReferrer) BOOL backToReferrer;
@end
@implementation BFAppLink
+ (instancetype)appLinkWithSourceURL:(NSURL *)sourceURL
targets:(NSArray *)targets
webURL:(NSURL *)webURL
isBackToReferrer:(BOOL)isBackToReferrer {
BFAppLink *link = [[self alloc] initWithIsBackToReferrer:isBackToReferrer];
link.sourceURL = sourceURL;
link.targets = [targets copy];
link.webURL = webURL;
return link;
}
+ (instancetype)appLinkWithSourceURL:(NSURL *)sourceURL
targets:(NSArray *)targets
webURL:(NSURL *)webURL {
return [self appLinkWithSourceURL:sourceURL
targets:targets
webURL:webURL
isBackToReferrer:NO];
}
- (BFAppLink *)initWithIsBackToReferrer:(BOOL)backToReferrer {
if ((self = [super init])) {
_backToReferrer = backToReferrer;
}
return self;
}
@end

View File

@ -0,0 +1,93 @@
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#import <Foundation/Foundation.h>
#import <Bolts/BFAppLink.h>
/*!
The result of calling navigate on a BFAppLinkNavigation
*/
typedef NS_ENUM(NSInteger, BFAppLinkNavigationType) {
/*! Indicates that the navigation failed and no app was opened */
BFAppLinkNavigationTypeFailure,
/*! Indicates that the navigation succeeded by opening the URL in the browser */
BFAppLinkNavigationTypeBrowser,
/*! Indicates that the navigation succeeded by opening the URL in an app on the device */
BFAppLinkNavigationTypeApp
};
@protocol BFAppLinkResolving;
@class BFTask;
/*!
Represents a pending request to navigate to an App Link. Most developers will
simply use navigateToURLInBackground: to open a URL, but developers can build
custom requests with additional navigation and app data attached to them by
creating BFAppLinkNavigations themselves.
*/
@interface BFAppLinkNavigation : NSObject
/*!
The extras for the AppLinkNavigation. This will generally contain application-specific
data that should be passed along with the request, such as advertiser or affiliate IDs or
other such metadata relevant on this device.
*/
@property (nonatomic, copy, readonly) NSDictionary *extras;
/*!
The al_applink_data for the AppLinkNavigation. This will generally contain data common to
navigation attempts such as back-links, user agents, and other information that may be used
in routing and handling an App Link request.
*/
@property (nonatomic, copy, readonly) NSDictionary *appLinkData;
/*! The AppLink to navigate to */
@property (nonatomic, strong, readonly) BFAppLink *appLink;
/*! Creates an AppLinkNavigation with the given link, extras, and App Link data */
+ (instancetype)navigationWithAppLink:(BFAppLink *)appLink
extras:(NSDictionary *)extras
appLinkData:(NSDictionary *)appLinkData;
/*! Performs the navigation */
- (BFAppLinkNavigationType)navigate:(NSError **)error;
/*! Returns a BFAppLink for the given URL */
+ (BFTask *)resolveAppLinkInBackground:(NSURL *)destination;
/*! Returns a BFAppLink for the given URL using the given App Link resolution strategy */
+ (BFTask *)resolveAppLinkInBackground:(NSURL *)destination resolver:(id<BFAppLinkResolving>)resolver;
/*! Navigates to a BFAppLink and returns whether it opened in-app or in-browser */
+ (BFAppLinkNavigationType)navigateToAppLink:(BFAppLink *)link error:(NSError **)error;
/*! Navigates to a URL (an asynchronous action) and returns a BFNavigationType */
+ (BFTask *)navigateToURLInBackground:(NSURL *)destination;
/*!
Navigates to a URL (an asynchronous action) using the given App Link resolution
strategy and returns a BFNavigationType
*/
+ (BFTask *)navigateToURLInBackground:(NSURL *)destination resolver:(id<BFAppLinkResolving>)resolver;
/*!
Gets the default resolver to be used for App Link resolution. If the developer has not set one explicitly,
a basic, built-in resolver will be used.
*/
+ (id<BFAppLinkResolving>)defaultResolver;
/*!
Sets the default resolver to be used for App Link resolution. Setting this to nil will revert the
default resolver to the basic, built-in resolver provided by Bolts.
*/
+ (void)setDefaultResolver:(id<BFAppLinkResolving>)resolver;
@end

View File

@ -0,0 +1,248 @@
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#import <UIKit/UIKit.h>
#import "BFAppLinkNavigation.h"
#import "BFTaskCompletionSource.h"
#import "BFAppLinkTarget.h"
#import "BoltsVersion.h"
#import "BFWebViewAppLinkResolver.h"
#import "BFExecutor.h"
#import "BFTask.h"
#import "BFMeasurementEvent_Internal.h"
#import "BFAppLink_Internal.h"
FOUNDATION_EXPORT NSString *const BFAppLinkDataParameterName;
FOUNDATION_EXPORT NSString *const BFAppLinkTargetKeyName;
FOUNDATION_EXPORT NSString *const BFAppLinkUserAgentKeyName;
FOUNDATION_EXPORT NSString *const BFAppLinkExtrasKeyName;
FOUNDATION_EXPORT NSString *const BFAppLinkVersionKeyName;
static id<BFAppLinkResolving> defaultResolver;
@interface BFAppLinkNavigation ()
@property (nonatomic, copy, readwrite) NSDictionary *extras;
@property (nonatomic, copy, readwrite) NSDictionary *appLinkData;
@property (nonatomic, strong, readwrite) BFAppLink *appLink;
@end
@implementation BFAppLinkNavigation
+ (instancetype)navigationWithAppLink:(BFAppLink *)appLink
extras:(NSDictionary *)extras
appLinkData:(NSDictionary *)appLinkData {
BFAppLinkNavigation *navigation = [[self alloc] init];
navigation.appLink = appLink;
navigation.extras = extras;
navigation.appLinkData = appLinkData;
return navigation;
}
- (NSString *)stringByEscapingQueryString:(NSString *)string {
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_7_0 || __MAC_OS_X_VERSION_MIN_REQUIRED >= __MAC_10_9
return [string stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
#else
return (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL,
(CFStringRef)string,
NULL,
(CFStringRef) @":/?#[]@!$&'()*+,;=",
kCFStringEncodingUTF8));
#endif
}
- (NSURL *)appLinkURLWithTargetURL:(NSURL *)targetUrl error:(NSError **)error {
NSMutableDictionary *appLinkData = [NSMutableDictionary dictionaryWithDictionary:self.appLinkData ?: @{}];
// Add applink protocol data
if (!appLinkData[BFAppLinkUserAgentKeyName]) {
appLinkData[BFAppLinkUserAgentKeyName] = [NSString stringWithFormat:@"Bolts iOS %@", BOLTS_VERSION];
}
if (!appLinkData[BFAppLinkVersionKeyName]) {
appLinkData[BFAppLinkVersionKeyName] = BFAppLinkVersion;
}
appLinkData[BFAppLinkTargetKeyName] = [self.appLink.sourceURL absoluteString];
appLinkData[BFAppLinkExtrasKeyName] = self.extras ?: @{};
// JSON-ify the applink data
NSError *jsonError = nil;
NSData *jsonBlob = [NSJSONSerialization dataWithJSONObject:appLinkData options:0 error:&jsonError];
if (!jsonError) {
NSString *jsonString = [[NSString alloc] initWithData:jsonBlob encoding:NSUTF8StringEncoding];
NSString *encoded = [self stringByEscapingQueryString:jsonString];
NSString *endUrlString = [NSString stringWithFormat:@"%@%@%@=%@",
[targetUrl absoluteString],
targetUrl.query ? @"&" : @"?",
BFAppLinkDataParameterName,
encoded];
return [NSURL URLWithString:endUrlString];
} else {
if (error) {
*error = jsonError;
}
// If there was an error encoding the app link data, fail hard.
return nil;
}
}
- (BFAppLinkNavigationType)navigate:(NSError **)error {
NSURL *openedURL = nil;
NSError *encodingError = nil;
BFAppLinkNavigationType retType = BFAppLinkNavigationTypeFailure;
// Find the first eligible/launchable target in the BFAppLink.
for (BFAppLinkTarget *target in self.appLink.targets) {
NSURL *appLinkAppURL = [self appLinkURLWithTargetURL:target.URL error:&encodingError];
if (encodingError || !appLinkAppURL) {
if (error) {
*error = encodingError;
}
} else if ([[UIApplication sharedApplication] openURL:appLinkAppURL]) {
retType = BFAppLinkNavigationTypeApp;
openedURL = appLinkAppURL;
break;
}
}
if (!openedURL && self.appLink.webURL) {
// Fall back to opening the url in the browser if available.
NSURL *appLinkBrowserURL = [self appLinkURLWithTargetURL:self.appLink.webURL error:&encodingError];
if (encodingError || !appLinkBrowserURL) {
// If there was an error encoding the app link data, fail hard.
if (error) {
*error = encodingError;
}
} else if ([[UIApplication sharedApplication] openURL:appLinkBrowserURL]) {
// This was a browser navigation.
retType = BFAppLinkNavigationTypeBrowser;
openedURL = appLinkBrowserURL;
}
}
[self postAppLinkNavigateEventNotificationWithTargetURL:openedURL
error:error ? *error : nil
type:retType];
return retType;
}
- (void)postAppLinkNavigateEventNotificationWithTargetURL:(NSURL *)outputURL error:(NSError *)error type:(BFAppLinkNavigationType)type {
NSString *const EVENT_YES_VAL = @"1";
NSString *const EVENT_NO_VAL = @"0";
NSMutableDictionary *logData = [[NSMutableDictionary alloc] init];
NSString *outputURLScheme = [outputURL scheme];
NSString *outputURLString = [outputURL absoluteString];
if (outputURLScheme) {
logData[@"outputURLScheme"] = outputURLScheme;
}
if (outputURLString) {
logData[@"outputURL"] = outputURLString;
}
NSString *sourceURLString = [self.appLink.sourceURL absoluteString];
NSString *sourceURLHost = [self.appLink.sourceURL host];
NSString *sourceURLScheme = [self.appLink.sourceURL scheme];
if (sourceURLString) {
logData[@"sourceURL"] = sourceURLString;
}
if (sourceURLHost) {
logData[@"sourceHost"] = sourceURLHost;
}
if (sourceURLScheme) {
logData[@"sourceScheme"] = sourceURLScheme;
}
if ([error localizedDescription]) {
logData[@"error"] = [error localizedDescription];
}
NSString *success = nil; //no
NSString *linkType = nil; // unknown;
switch (type) {
case BFAppLinkNavigationTypeFailure:
success = EVENT_NO_VAL;
linkType = @"fail";
break;
case BFAppLinkNavigationTypeBrowser:
success = EVENT_YES_VAL;
linkType = @"web";
break;
case BFAppLinkNavigationTypeApp:
success = EVENT_YES_VAL;
linkType = @"app";
break;
default:
break;
}
if (success) {
logData[@"success"] = success;
}
if (linkType) {
logData[@"type"] = linkType;
}
if ([self.appLink isBackToReferrer]) {
[BFMeasurementEvent postNotificationForEventName:BFAppLinkNavigateBackToReferrerEventName args:logData];
} else {
[BFMeasurementEvent postNotificationForEventName:BFAppLinkNavigateOutEventName args:logData];
}
}
+ (BFTask *)resolveAppLinkInBackground:(NSURL *)destination resolver:(id<BFAppLinkResolving>)resolver {
return [resolver appLinkFromURLInBackground:destination];
}
+ (BFTask *)resolveAppLinkInBackground:(NSURL *)destination {
return [self resolveAppLinkInBackground:destination resolver:[self defaultResolver]];
}
+ (BFTask *)navigateToURLInBackground:(NSURL *)destination {
return [self navigateToURLInBackground:destination
resolver:[self defaultResolver]];
}
+ (BFTask *)navigateToURLInBackground:(NSURL *)destination
resolver:(id<BFAppLinkResolving>)resolver {
BFTask *resolutionTask = [self resolveAppLinkInBackground:destination
resolver:resolver];
return [resolutionTask continueWithExecutor:[BFExecutor mainThreadExecutor]
withSuccessBlock:^id(BFTask *task) {
NSError *error = nil;
BFAppLinkNavigationType result = [self navigateToAppLink:task.result
error:&error];
if (error) {
return [BFTask taskWithError:error];
} else {
return @(result);
}
}];
}
+ (BFAppLinkNavigationType)navigateToAppLink:(BFAppLink *)link error:(NSError **)error {
return [[BFAppLinkNavigation navigationWithAppLink:link
extras:nil
appLinkData:nil] navigate:error];
}
+ (id<BFAppLinkResolving>)defaultResolver {
if (defaultResolver) {
return defaultResolver;
}
return [BFWebViewAppLinkResolver sharedInstance];
}
+ (void)setDefaultResolver:(id<BFAppLinkResolving>)resolver {
defaultResolver = resolver;
}
@end

View File

@ -0,0 +1,30 @@
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#import <Foundation/Foundation.h>
@class BFTask;
/*!
Implement this protocol to provide an alternate strategy for resolving
App Links that may include pre-fetching, caching, or querying for App Link
data from an index provided by a service provider.
*/
@protocol BFAppLinkResolving <NSObject>
/*!
Asynchronously resolves App Link data for a given URL.
@param url The URL to resolve into an App Link.
@returns A BFTask that will return a BFAppLink for the given URL.
*/
- (BFTask *)appLinkFromURLInBackground:(NSURL *)url;
@end

View File

@ -0,0 +1,87 @@
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <Bolts/BFAppLinkReturnToRefererView.h>
@class BFAppLink;
@class BFAppLinkReturnToRefererController;
/*!
Protocol that a class can implement in order to be notified when the user has navigated back
to the referer of an App Link.
*/
@protocol BFAppLinkReturnToRefererControllerDelegate <NSObject>
@optional
/*! Called when the user has tapped to navigate, but before the navigation has been performed. */
- (void)returnToRefererController:(BFAppLinkReturnToRefererController *)controller
willNavigateToAppLink:(BFAppLink *)appLink;
/*! Called after the navigation has been attempted, with an indication of whether the referer
app link was successfully opened. */
- (void)returnToRefererController:(BFAppLinkReturnToRefererController *)controller
didNavigateToAppLink:(BFAppLink *)url
type:(BFAppLinkNavigationType)type;
@end
/*!
A controller class that implements default behavior for a BFAppLinkReturnToRefererView, including
the ability to display the view above the navigation bar for navigation-based apps.
*/
@interface BFAppLinkReturnToRefererController : NSObject <BFAppLinkReturnToRefererViewDelegate>
/*!
The delegate that will be notified when the user navigates back to the referer.
*/
@property (nonatomic, weak) id<BFAppLinkReturnToRefererControllerDelegate> delegate;
/*!
The BFAppLinkReturnToRefererView this controller is controlling.
*/
@property (nonatomic, strong) BFAppLinkReturnToRefererView *view;
/*!
Initializes a controller suitable for controlling a BFAppLinkReturnToRefererView that is to be displayed
contained within another UIView (i.e., not displayed above the navigation bar).
*/
- (instancetype)init;
/*!
Initializes a controller suitable for controlling a BFAppLinkReturnToRefererView that is to be displayed
displayed above the navigation bar.
*/
- (instancetype)initForDisplayAboveNavController:(UINavigationController *)navController;
/*!
Removes the view entirely from the navigation controller it is currently displayed in.
*/
- (void)removeFromNavController;
/*!
Shows the BFAppLinkReturnToRefererView with the specified referer information. If nil or missing data,
the view will not be displayed. */
- (void)showViewForRefererAppLink:(BFAppLink *)refererAppLink;
/*!
Shows the BFAppLinkReturnToRefererView with referer information extracted from the specified URL.
If nil or missing referer App Link data, the view will not be displayed. */
- (void)showViewForRefererURL:(NSURL *)url;
/*!
Closes the view, possibly animating it.
*/
- (void)closeViewAnimated:(BOOL)animated;
@end

View File

@ -0,0 +1,230 @@
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#import "BFAppLinkReturnToRefererController.h"
#import "BFAppLink.h"
#import "BFAppLinkReturnToRefererView_Internal.h"
#import "BFURL_Internal.h"
static const CFTimeInterval kBFViewAnimationDuration = 0.25f;
@implementation BFAppLinkReturnToRefererController {
UINavigationController *_navigationController;
BFAppLinkReturnToRefererView *_view;
}
#pragma mark - Object lifecycle
- (instancetype)init {
return [self initForDisplayAboveNavController:nil];
}
- (instancetype)initForDisplayAboveNavController:(UINavigationController *)navController {
self = [super init];
if (self) {
_navigationController = navController;
if (_navigationController != nil) {
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:@selector(statusBarFrameWillChange:)
name:UIApplicationWillChangeStatusBarFrameNotification
object:nil];
[nc addObserver:self
selector:@selector(statusBarFrameDidChange:)
name:UIApplicationDidChangeStatusBarFrameNotification
object:nil];
[nc addObserver:self
selector:@selector(orientationDidChange:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
}
}
return self;
}
- (void)dealloc {
_view.delegate = nil;
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark - Public API
- (BFAppLinkReturnToRefererView *)view {
if (!_view) {
self.view = [[BFAppLinkReturnToRefererView alloc] initWithFrame:CGRectZero];
if (_navigationController) {
[_navigationController.view addSubview:_view];
}
}
return _view;
}
- (void)setView:(BFAppLinkReturnToRefererView *)view {
if (_view != view) {
_view.delegate = nil;
}
_view = view;
_view.delegate = self;
if (_navigationController) {
_view.includeStatusBarInSize = BFIncludeStatusBarInSizeAlways;
}
}
- (void)showViewForRefererAppLink:(BFAppLink *)refererAppLink {
self.view.refererAppLink = refererAppLink;
[_view sizeToFit];
if (_navigationController) {
if (!_view.closed) {
dispatch_async(dispatch_get_main_queue(), ^{
[self moveNavigationBar];
});
}
}
}
- (void)showViewForRefererURL:(NSURL *)url {
BFAppLink *appLink = [BFURL URLForRenderBackToReferrerBarURL:url].appLinkReferer;
[self showViewForRefererAppLink:appLink];
}
- (void)removeFromNavController {
if (_navigationController) {
[_view removeFromSuperview];
_navigationController = nil;
}
}
#pragma mark - BFAppLinkReturnToRefererViewDelegate
- (void)returnToRefererViewDidTapInsideCloseButton:(BFAppLinkReturnToRefererView *)view {
[self closeViewAnimated:YES explicitlyClosed:YES];
}
- (void)returnToRefererViewDidTapInsideLink:(BFAppLinkReturnToRefererView *)view
link:(BFAppLink *)link {
[self openRefererAppLink:link];
[self closeViewAnimated:NO explicitlyClosed:NO];
}
#pragma mark - Private
- (void)statusBarFrameWillChange:(NSNotification *)notification {
NSValue *rectValue = [[notification userInfo] valueForKey:UIApplicationStatusBarFrameUserInfoKey];
CGRect newFrame;
[rectValue getValue:&newFrame];
if (_navigationController && !_view.closed) {
if (CGRectGetHeight(newFrame) == 40) {
UIViewAnimationOptions options = UIViewAnimationOptionBeginFromCurrentState;
[UIView animateWithDuration:kBFViewAnimationDuration delay:0.0 options:options animations:^{
_view.frame = CGRectMake(0.0, 0.0, CGRectGetWidth(_view.bounds), 0.0);
} completion:nil];
}
}
}
- (void)statusBarFrameDidChange:(NSNotification *)notification {
NSValue *rectValue = [[notification userInfo] valueForKey:UIApplicationStatusBarFrameUserInfoKey];
CGRect newFrame;
[rectValue getValue:&newFrame];
if (_navigationController && !_view.closed) {
if (CGRectGetHeight(newFrame) == 40) {
UIViewAnimationOptions options = UIViewAnimationOptionBeginFromCurrentState;
[UIView animateWithDuration:kBFViewAnimationDuration delay:0.0 options:options animations:^{
[_view sizeToFit];
[self moveNavigationBar];
} completion:nil];
}
}
}
- (void)orientationDidChange:(NSNotificationCenter *)notification {
if (_navigationController && !_view.closed && CGRectGetHeight(_view.bounds) > 0) {
dispatch_async(dispatch_get_main_queue(), ^{
[self moveNavigationBar];
});
}
}
- (void)moveNavigationBar {
if (_view.closed || !_view.refererAppLink) {
return;
}
[self updateNavigationBarY:CGRectGetHeight(_view.bounds)];
}
- (void)updateNavigationBarY:(CGFloat)y {
UINavigationBar *navigationBar = _navigationController.navigationBar;
CGRect navigationBarFrame = navigationBar.frame;
CGFloat oldContainerViewY = CGRectGetMaxY(navigationBarFrame);
navigationBarFrame.origin.y = y;
navigationBar.frame = navigationBarFrame;
CGFloat dy = CGRectGetMaxY(navigationBarFrame) - oldContainerViewY;
UIView *containerView = _navigationController.visibleViewController.view.superview;
containerView.frame = UIEdgeInsetsInsetRect(containerView.frame, UIEdgeInsetsMake(dy, 0.0, 0.0, 0.0));
}
- (void)closeViewAnimated:(BOOL)animated {
[self closeViewAnimated:animated explicitlyClosed:YES];
}
- (void)closeViewAnimated:(BOOL)animated explicitlyClosed:(BOOL)explicitlyClosed {
void (^closer)(void) = ^{
if (_navigationController) {
[self updateNavigationBarY:_view.statusBarHeight];
}
CGRect frame = _view.frame;
frame.size.height = 0.0;
_view.frame = frame;
};
if (animated) {
[UIView animateWithDuration:kBFViewAnimationDuration animations:^{
closer();
} completion:^(BOOL finished) {
if (explicitlyClosed) {
_view.closed = YES;
}
}];
} else {
closer();
if (explicitlyClosed) {
_view.closed = YES;
}
}
}
- (void)openRefererAppLink:(BFAppLink *)refererAppLink {
if (refererAppLink) {
id<BFAppLinkReturnToRefererControllerDelegate> delegate = _delegate;
if ([delegate respondsToSelector:@selector(returnToRefererController:willNavigateToAppLink:)]) {
[delegate returnToRefererController:self willNavigateToAppLink:refererAppLink];
}
NSError *error = nil;
BFAppLinkNavigationType type = [BFAppLinkNavigation navigateToAppLink:refererAppLink error:&error];
if ([delegate respondsToSelector:@selector(returnToRefererController:didNavigateToAppLink:type:)]) {
[delegate returnToRefererController:self didNavigateToAppLink:refererAppLink type:type];
}
}
}
@end

View File

@ -0,0 +1,77 @@
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <Bolts/BFAppLinkNavigation.h>
@class BFAppLinkReturnToRefererView;
@class BFURL;
typedef NS_ENUM(NSUInteger, BFIncludeStatusBarInSize) {
BFIncludeStatusBarInSizeNever,
BFIncludeStatusBarInSizeIOS7AndLater,
BFIncludeStatusBarInSizeAlways,
};
/*!
Protocol that a class can implement in order to be notified when the user has navigated back
to the referer of an App Link.
*/
@protocol BFAppLinkReturnToRefererViewDelegate <NSObject>
/*!
Called when the user has tapped inside the close button.
*/
- (void)returnToRefererViewDidTapInsideCloseButton:(BFAppLinkReturnToRefererView *)view;
/*!
Called when the user has tapped inside the App Link portion of the view.
*/
- (void)returnToRefererViewDidTapInsideLink:(BFAppLinkReturnToRefererView *)view
link:(BFAppLink *)link;
@end
/*!
Provides a UIView that displays a button allowing users to navigate back to the
application that launched the App Link currently being handled, if the App Link
contained referer data. The user can also close the view by clicking a close button
rather than navigating away. If the view is provided an App Link that does not contain
referer data, it will have zero size and no UI will be displayed.
*/
@interface BFAppLinkReturnToRefererView : UIView
/*!
The delegate that will be notified when the user navigates back to the referer.
*/
@property (nonatomic, weak) id<BFAppLinkReturnToRefererViewDelegate> delegate;
/*!
The color of the text label and close button.
*/
@property (nonatomic, strong) UIColor *textColor;
@property (nonatomic, strong) BFAppLink *refererAppLink;
/*!
Indicates whether to extend the size of the view to include the current status bar
size, for use in scenarios where the view might extend under the status bar on iOS 7 and
above; this property has no effect on earlier versions of iOS.
*/
@property (nonatomic, assign) BFIncludeStatusBarInSize includeStatusBarInSize;
/*!
Indicates whether the user has closed the view by clicking the close button.
*/
@property (nonatomic, assign) BOOL closed;
@end

View File

@ -0,0 +1,269 @@
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#import "BFAppLinkReturnToRefererView.h"
#import "BFAppLink.h"
#import "BFAppLinkTarget.h"
static const CGFloat BFMarginX = 8.5f;
static const CGFloat BFMarginY = 8.5f;
static NSString *const BFRefererAppLink = @"referer_app_link";
static NSString *const BFRefererAppName = @"app_name";
static NSString *const BFRefererUrl = @"url";
static const CGFloat BFCloseButtonWidth = 12.0;
static const CGFloat BFCloseButtonHeight = 12.0;
@interface BFAppLinkReturnToRefererView ()
@property (nonatomic, strong) UILabel *labelView;
@property (nonatomic, strong) UIButton *closeButton;
@property (nonatomic, strong) UITapGestureRecognizer *insideTapGestureRecognizer;
@property (nonatomic, strong) UIView *viewToMoveWithNavController;
@end
@implementation BFAppLinkReturnToRefererView {
BOOL _explicitlyHidden;
}
#pragma mark - Initialization
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self commonInit];
[self sizeToFit];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self commonInit];
}
return self;
}
- (void)commonInit {
// Initialization code
_includeStatusBarInSize = BFIncludeStatusBarInSizeIOS7AndLater;
// iOS 7 system blue color
self.backgroundColor = [UIColor colorWithRed:0.0f green:122.0f / 255.0f blue:1.0f alpha:1.0f];
self.textColor = [UIColor whiteColor];
self.clipsToBounds = YES;
[self initViews];
}
- (void)initViews {
if (!_labelView && !_closeButton) {
_closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
_closeButton.backgroundColor = [UIColor clearColor];
_closeButton.userInteractionEnabled = YES;
_closeButton.clipsToBounds = YES;
_closeButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin;
_closeButton.contentMode = UIViewContentModeCenter;
[_closeButton addTarget:self action:@selector(closeButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_closeButton];
_labelView = [[UILabel alloc] initWithFrame:CGRectZero];
_labelView.font = [UIFont systemFontOfSize:[UIFont smallSystemFontSize]];
_labelView.textColor = [UIColor whiteColor];
_labelView.backgroundColor = [UIColor clearColor];
#ifdef __IPHONE_6_0
_labelView.textAlignment = NSTextAlignmentCenter;
#else
_labelView.textAlignment = UITextAlignmentCenter;
#endif
_labelView.clipsToBounds = YES;
[self updateLabelText];
[self addSubview:_labelView];
_insideTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTapInside:)];
_labelView.userInteractionEnabled = YES;
[_labelView addGestureRecognizer:_insideTapGestureRecognizer];
[self updateColors];
}
}
#pragma mark - Layout
- (CGSize)intrinsicContentSize {
CGSize size = self.bounds.size;
if (_closed || !self.hasRefererData) {
size.height = 0.0;
} else {
CGSize labelSize = [_labelView sizeThatFits:size];
size = CGSizeMake(size.width, labelSize.height + 2 * BFMarginY + self.statusBarHeight);
}
return size;
}
- (void)layoutSubviews {
[super layoutSubviews];
CGRect bounds = self.bounds;
_labelView.preferredMaxLayoutWidth = _labelView.bounds.size.width;
CGSize labelSize = [_labelView sizeThatFits:bounds.size];
_labelView.frame = CGRectMake(BFMarginX,
CGRectGetMaxY(bounds) - labelSize.height - 1.5f * BFMarginY,
CGRectGetMaxX(bounds) - BFCloseButtonWidth - 3 * BFMarginX,
labelSize.height + BFMarginY);
_closeButton.frame = CGRectMake(CGRectGetMaxX(bounds) - BFCloseButtonWidth - 2 * BFMarginX,
_labelView.center.y - BFCloseButtonHeight / 2.0f - BFMarginY,
BFCloseButtonWidth + 2 * BFMarginX,
BFCloseButtonHeight + 2 * BFMarginY);
}
- (CGSize)sizeThatFits:(CGSize)size {
if (_closed || !self.hasRefererData) {
size = CGSizeMake(size.width, 0.0);
} else {
CGSize labelSize = [_labelView sizeThatFits:size];
size = CGSizeMake(size.width, labelSize.height + 2 * BFMarginY + self.statusBarHeight);
}
return size;
}
- (CGFloat)statusBarHeight {
UIApplication *application = [UIApplication sharedApplication];
BOOL include;
switch (_includeStatusBarInSize) {
case BFIncludeStatusBarInSizeAlways:
include = YES;
break;
case BFIncludeStatusBarInSizeIOS7AndLater: {
float systemVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
include = (systemVersion >= 7.0);
break;
}
case BFIncludeStatusBarInSizeNever:
include = NO;
break;
}
if (include && !application.statusBarHidden) {
BOOL landscape = UIInterfaceOrientationIsLandscape(application.statusBarOrientation);
CGRect statusBarFrame = application.statusBarFrame;
return landscape ? CGRectGetWidth(statusBarFrame) : CGRectGetHeight(statusBarFrame);
}
return 0;
}
#pragma mark - Public API
- (void)setIncludeStatusBarInSize:(BFIncludeStatusBarInSize)includeStatusBarInSize {
_includeStatusBarInSize = includeStatusBarInSize;
[self setNeedsLayout];
[self invalidateIntrinsicContentSize];
}
- (void)setTextColor:(UIColor *)textColor {
_textColor = textColor;
[self updateColors];
}
- (void)setRefererAppLink:(BFAppLink *)refererAppLink {
_refererAppLink = refererAppLink;
[self updateLabelText];
[self updateHidden];
[self invalidateIntrinsicContentSize];
}
- (void)setClosed:(BOOL)closed {
if (_closed != closed) {
_closed = closed;
[self updateHidden];
[self invalidateIntrinsicContentSize];
}
}
- (void)setHidden:(BOOL)hidden {
_explicitlyHidden = hidden;
[self updateHidden];
}
#pragma mark - Private
- (void)updateLabelText {
NSString *appName = (_refererAppLink && _refererAppLink.targets[0]) ? [_refererAppLink.targets[0] appName] : nil;
_labelView.text = [self localizedLabelForReferer:appName];
}
- (void)updateColors {
UIImage *closeButtonImage = [self drawCloseButtonImageWithColor:_textColor];
_labelView.textColor = _textColor;
[_closeButton setImage:closeButtonImage forState:UIControlStateNormal];
}
- (UIImage *)drawCloseButtonImageWithColor:(UIColor *)color {
UIGraphicsBeginImageContextWithOptions(CGSizeMake(BFCloseButtonWidth, BFCloseButtonHeight), NO, 0.0f);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, [color CGColor]);
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextSetLineWidth(context, 1.25f);
CGFloat inset = 0.5f;
CGContextMoveToPoint(context, inset, inset);
CGContextAddLineToPoint(context, BFCloseButtonWidth - inset, BFCloseButtonHeight - inset);
CGContextStrokePath(context);
CGContextMoveToPoint(context, BFCloseButtonWidth - inset, inset);
CGContextAddLineToPoint(context, inset, BFCloseButtonHeight - inset);
CGContextStrokePath(context);
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return result;
}
- (NSString *)localizedLabelForReferer:(NSString *)refererName {
if (!refererName) {
return nil;
}
NSString *format = NSLocalizedString(@"Touch to return to %1$@", @"Format for the string to return to a calling app.");
return [NSString stringWithFormat:format, refererName];
}
- (BOOL)hasRefererData {
return _refererAppLink && _refererAppLink.targets[0];
}
- (void)closeButtonTapped:(id)sender {
[_delegate returnToRefererViewDidTapInsideCloseButton:self];
}
- (void)onTapInside:(UIGestureRecognizer *)sender {
[_delegate returnToRefererViewDidTapInsideLink:self link:_refererAppLink];
}
- (void)updateHidden {
[super setHidden:_explicitlyHidden || _closed || !self.hasRefererData];
}
@end

View File

@ -0,0 +1,17 @@
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#import <Bolts/BFAppLinkReturnToRefererView.h>
@interface BFAppLinkReturnToRefererView (Internal)
- (CGFloat)statusBarHeight;
@end

View File

@ -0,0 +1,33 @@
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#import <Foundation/Foundation.h>
/*!
Represents a target defined in App Link metadata, consisting of at least
a URL, and optionally an App Store ID and name.
*/
@interface BFAppLinkTarget : NSObject
/*! Creates a BFAppLinkTarget with the given app site and target URL. */
+ (instancetype)appLinkTargetWithURL:(NSURL *)url
appStoreId:(NSString *)appStoreId
appName:(NSString *)appName;
/*! The URL prefix for this app link target */
@property (nonatomic, strong, readonly) NSURL *URL;
/*! The app ID for the app store */
@property (nonatomic, copy, readonly) NSString *appStoreId;
/*! The name of the app */
@property (nonatomic, copy, readonly) NSString *appName;
@end

View File

@ -0,0 +1,33 @@
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#import "BFAppLinkTarget.h"
@interface BFAppLinkTarget ()
@property (nonatomic, strong, readwrite) NSURL *URL;
@property (nonatomic, copy, readwrite) NSString *appStoreId;
@property (nonatomic, copy, readwrite) NSString *appName;
@end
@implementation BFAppLinkTarget
+ (instancetype)appLinkTargetWithURL:(NSURL *)url
appStoreId:(NSString *)appStoreId
appName:(NSString *)appName {
BFAppLinkTarget *target = [[self alloc] init];
target.URL = url;
target.appStoreId = appStoreId;
target.appName = appName;
return target;
}
@end

View File

@ -0,0 +1,32 @@
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#import <Bolts/BFAppLink.h>
FOUNDATION_EXPORT NSString *const BFAppLinkDataParameterName;
FOUNDATION_EXPORT NSString *const BFAppLinkTargetKeyName;
FOUNDATION_EXPORT NSString *const BFAppLinkUserAgentKeyName;
FOUNDATION_EXPORT NSString *const BFAppLinkExtrasKeyName;
FOUNDATION_EXPORT NSString *const BFAppLinkVersionKeyName;
FOUNDATION_EXPORT NSString *const BFAppLinkRefererAppLink;
FOUNDATION_EXPORT NSString *const BFAppLinkRefererAppName;
FOUNDATION_EXPORT NSString *const BFAppLinkRefererUrl;
@interface BFAppLink (Internal)
+ (instancetype)appLinkWithSourceURL:(NSURL *)sourceURL
targets:(NSArray *)targets
webURL:(NSURL *)webURL
isBackToReferrer:(BOOL)isBackToReferrer;
/*! return if this AppLink is to go back to referrer. */
@property (nonatomic, assign, readonly, getter=isBackToReferrer) BOOL backToReferrer;
@end

View File

@ -0,0 +1,45 @@
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#import <Foundation/Foundation.h>
/*! The name of the notification posted by BFMeasurementEvent */
FOUNDATION_EXPORT NSString *const BFMeasurementEventNotificationName;
/*! Defines keys in the userInfo object for the notification named BFMeasurementEventNotificationName */
/*! The string field for the name of the event */
FOUNDATION_EXPORT NSString *const BFMeasurementEventNameKey;
/*! The dictionary field for the arguments of the event */
FOUNDATION_EXPORT NSString *const BFMeasurementEventArgsKey;
/*! Bolts Events raised by BFMeasurementEvent for Applink */
/*!
The name of the event posted when [BFURL URLWithURL:] is called successfully. This represents the successful parsing of an app link URL.
*/
FOUNDATION_EXPORT NSString *const BFAppLinkParseEventName;
/*!
The name of the event posted when [BFURL URLWithInboundURL:] is called successfully.
This represents parsing an inbound app link URL from a different application
*/
FOUNDATION_EXPORT NSString *const BFAppLinkNavigateInEventName;
/*! The event raised when the user navigates from your app to other apps */
FOUNDATION_EXPORT NSString *const BFAppLinkNavigateOutEventName;
/*!
The event raised when the user navigates out from your app and back to the referrer app.
e.g when the user leaves your app after tapping the back-to-referrer navigation bar
*/
FOUNDATION_EXPORT NSString *const BFAppLinkNavigateBackToReferrerEventName;
@interface BFMeasurementEvent : NSObject
@end

View File

@ -0,0 +1,62 @@
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#import "BFMeasurementEvent_Internal.h"
NSString *const BFMeasurementEventNotificationName = @"com.parse.bolts.measurement_event";
NSString *const BFMeasurementEventNameKey = @"event_name";
NSString *const BFMeasurementEventArgsKey = @"event_args";
/* app Link Event raised by this BFURL */
NSString *const BFAppLinkParseEventName = @"al_link_parse";
NSString *const BFAppLinkNavigateInEventName = @"al_nav_in";
/*! AppLink events raised in this class */
NSString *const BFAppLinkNavigateOutEventName = @"al_nav_out";
NSString *const BFAppLinkNavigateBackToReferrerEventName = @"al_ref_back_out";
__attribute__((noinline)) void warnOnMissingEventName() {
NSLog(@"Warning: Missing event name when logging bolts measurement event. \n"
" Ignoring this event in logging.");
}
@implementation BFMeasurementEvent {
NSString *_name;
NSDictionary *_args;
}
- (void)postNotification {
if (!_name) {
warnOnMissingEventName();
return;
}
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
NSDictionary *userInfo = @{BFMeasurementEventNameKey : _name,
BFMeasurementEventArgsKey : _args};
[center postNotificationName:BFMeasurementEventNotificationName
object:self
userInfo:userInfo];
}
- (instancetype)initEventWithName:(NSString *)name args:(NSDictionary *)args {
if ((self = [super init])) {
_name = name;
_args = args ? args : @{};
}
return self;
}
+ (void)postNotificationForEventName:(NSString *)name args:(NSDictionary *)args {
[[[self alloc] initEventWithName:name args:args] postNotification];
}
@end

View File

@ -0,0 +1,19 @@
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#import <Bolts/BFMeasurementEvent.h>
/*!
Provides methods for posting notifications from the Bolts framework
*/
@interface BFMeasurementEvent (Internal)
+ (void)postNotificationForEventName:(NSString *)name args:(NSDictionary *)args;
@end

75
Example/Pods/Bolts/Bolts/iOS/BFURL.h generated Normal file
View File

@ -0,0 +1,75 @@
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#import <Foundation/Foundation.h>
@class BFAppLink;
/*!
Provides a set of utilities for working with NSURLs, such as parsing of query parameters
and handling for App Link requests.
*/
@interface BFURL : NSObject
/*!
Creates a link target from a raw URL.
On success, this posts the BFAppLinkParseEventName measurement event. If you are constructing the BFURL within your application delegate's
application:openURL:sourceApplication:annotation:, you should instead use URLWithInboundURL:sourceApplication:
to support better BFMeasurementEvent notifications
@param url The instance of `NSURL` to create BFURL from.
*/
+ (BFURL *)URLWithURL:(NSURL *)url;
/*!
Creates a link target from a raw URL received from an external application. This is typically called from the app delegate's
application:openURL:sourceApplication:annotation: and will post the BFAppLinkNavigateInEventName measurement event.
@param url The instance of `NSURL` to create BFURL from.
@param sourceApplication the bundle ID of the app that is requesting your app to open the URL. The same sourceApplication in application:openURL:sourceApplication:annotation:
*/
+ (BFURL *)URLWithInboundURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication;
/*!
Gets the target URL. If the link is an App Link, this is the target of the App Link.
Otherwise, it is the url that created the target.
*/
@property (nonatomic, strong, readonly) NSURL *targetURL;
/*!
Gets the query parameters for the target, parsed into an NSDictionary.
*/
@property (nonatomic, strong, readonly) NSDictionary *targetQueryParameters;
/*!
If this link target is an App Link, this is the data found in al_applink_data.
Otherwise, it is nil.
*/
@property (nonatomic, strong, readonly) NSDictionary *appLinkData;
/*!
If this link target is an App Link, this is the data found in extras.
*/
@property (nonatomic, strong, readonly) NSDictionary *appLinkExtras;
/*!
The App Link indicating how to navigate back to the referer app, if any.
*/
@property (nonatomic, strong, readonly) BFAppLink *appLinkReferer;
/*!
The URL that was used to create this BFURL.
*/
@property (nonatomic, strong, readonly) NSURL *inputURL;
/*!
The query parameters of the inputURL, parsed into an NSDictionary.
*/
@property (nonatomic, strong, readonly) NSDictionary *inputQueryParameters;
@end

142
Example/Pods/Bolts/Bolts/iOS/BFURL.m generated Normal file
View File

@ -0,0 +1,142 @@
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#import "BFURL_Internal.h"
#import "BFAppLink_Internal.h"
#import "BFAppLinkTarget.h"
#import "BFMeasurementEvent_Internal.h"
@implementation BFURL
- (instancetype)initWithURL:(NSURL *)url forOpenInboundURL:(BOOL)forOpenURLEvent sourceApplication:(NSString *)sourceApplication forRenderBackToReferrerBar:(BOOL)forRenderBackToReferrerBar {
self = [super init];
if (!self) return nil;
_inputURL = url;
_targetURL = url;
// Parse the query string parameters for the base URL
NSDictionary *baseQuery = [BFURL queryParametersForURL:url];
_inputQueryParameters = baseQuery;
_targetQueryParameters = baseQuery;
// Check for applink_data
NSString *appLinkDataString = baseQuery[BFAppLinkDataParameterName];
if (appLinkDataString) {
// Try to parse the JSON
NSError *error = nil;
NSDictionary *applinkData = [NSJSONSerialization JSONObjectWithData:[appLinkDataString dataUsingEncoding:NSUTF8StringEncoding]
options:0
error:&error];
if (!error && [applinkData isKindOfClass:[NSDictionary class]]) {
// If the version is not specified, assume it is 1.
NSString *version = applinkData[BFAppLinkVersionKeyName] ?: @"1.0";
NSString *target = applinkData[BFAppLinkTargetKeyName];
if ([version isKindOfClass:[NSString class]] &&
[version isEqual:BFAppLinkVersion]) {
// There's applink data! The target should actually be the applink target.
_appLinkData = applinkData;
id applinkExtras = applinkData[BFAppLinkExtrasKeyName];
if (applinkExtras && [applinkExtras isKindOfClass:[NSDictionary class]]) {
_appLinkExtras = applinkExtras;
}
_targetURL = ([target isKindOfClass:[NSString class]] ? [NSURL URLWithString:target] : url);
_targetQueryParameters = [BFURL queryParametersForURL:_targetURL];
NSDictionary *refererAppLink = _appLinkData[BFAppLinkRefererAppLink];
NSString *refererURLString = refererAppLink[BFAppLinkRefererUrl];
NSString *refererAppName = refererAppLink[BFAppLinkRefererAppName];
if (refererURLString && refererAppName) {
BFAppLinkTarget *target = [BFAppLinkTarget appLinkTargetWithURL:[NSURL URLWithString:refererURLString]
appStoreId:nil
appName:refererAppName];
_appLinkReferer = [BFAppLink appLinkWithSourceURL:[NSURL URLWithString:refererURLString]
targets:@[ target ]
webURL:nil
isBackToReferrer:YES];
}
// Raise Measurement Event
NSString *const EVENT_YES_VAL = @"1";
NSString *const EVENT_NO_VAL = @"0";
NSMutableDictionary *logData = [[NSMutableDictionary alloc] init];
logData[@"version"] = version;
if (refererURLString) {
logData[@"refererURL"] = refererURLString;
}
if (refererAppName) {
logData[@"refererAppName"] = refererAppName;
}
if (sourceApplication) {
logData[@"sourceApplication"] = sourceApplication;
}
if ([_targetURL absoluteString]) {
logData[@"targetURL"] = [_targetURL absoluteString];
}
if ([_inputURL absoluteString]) {
logData[@"inputURL"] = [_inputURL absoluteString];
}
if ([_inputURL scheme]) {
logData[@"inputURLScheme"] = [_inputURL scheme];
}
logData[@"forRenderBackToReferrerBar"] = forRenderBackToReferrerBar ? EVENT_YES_VAL : EVENT_NO_VAL;
logData[@"forOpenUrl"] = forOpenURLEvent ? EVENT_YES_VAL : EVENT_NO_VAL;
[BFMeasurementEvent postNotificationForEventName:BFAppLinkParseEventName args:logData];
if (forOpenURLEvent) {
[BFMeasurementEvent postNotificationForEventName:BFAppLinkNavigateInEventName args:logData];
}
}
}
}
return self;
}
+ (BFURL *)URLWithURL:(NSURL *)url {
return [[BFURL alloc] initWithURL:url forOpenInboundURL:NO sourceApplication:nil forRenderBackToReferrerBar:NO];
}
+ (BFURL *)URLWithInboundURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication {
return [[BFURL alloc] initWithURL:url forOpenInboundURL:YES sourceApplication:sourceApplication forRenderBackToReferrerBar:NO];
}
+ (BFURL *)URLForRenderBackToReferrerBarURL:(NSURL *)url {
return [[BFURL alloc] initWithURL:url forOpenInboundURL:NO sourceApplication:nil forRenderBackToReferrerBar:YES];
}
+ (NSString *)decodeURLString:(NSString *)string {
return (NSString *)CFBridgingRelease(CFURLCreateStringByReplacingPercentEscapes(NULL,
(CFStringRef)string,
CFSTR("")));
}
+ (NSDictionary *)queryParametersForURL:(NSURL *)url {
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
NSString *query = url.query;
if ([query isEqualToString:@""]) {
return @{};
}
NSArray *queryComponents = [query componentsSeparatedByString:@"&"];
for (NSString *component in queryComponents) {
NSRange equalsLocation = [component rangeOfString:@"="];
if (equalsLocation.location == NSNotFound) {
// There's no equals, so associate the key with NSNull
parameters[[self decodeURLString:component]] = [NSNull null];
} else {
NSString *key = [self decodeURLString:[component substringToIndex:equalsLocation.location]];
NSString *value = [self decodeURLString:[component substringFromIndex:equalsLocation.location + 1]];
parameters[key] = value;
}
}
return [NSDictionary dictionaryWithDictionary:parameters];
}
@end

View File

@ -0,0 +1,15 @@
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#import <Bolts/BFURL.h>
@interface BFURL (Internal)
+ (BFURL *)URLForRenderBackToReferrerBarURL:(NSURL *)url;
@end

View File

@ -0,0 +1,26 @@
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#import <Foundation/Foundation.h>
#import <Bolts/BFAppLinkResolving.h>
/*!
A reference implementation for an App Link resolver that uses a hidden UIWebView
to parse the HTML containing App Link metadata.
*/
@interface BFWebViewAppLinkResolver : NSObject <BFAppLinkResolving>
/*!
Gets the instance of a BFWebViewAppLinkResolver.
*/
+ (instancetype)sharedInstance;
@end

View File

@ -0,0 +1,302 @@
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#import <UIKit/UIKit.h>
#import "BFWebViewAppLinkResolver.h"
#import "BFAppLink.h"
#import "BFAppLinkTarget.h"
#import "BFTask.h"
#import "BFTaskCompletionSource.h"
#import "BFExecutor.h"
// Defines JavaScript to extract app link tags from HTML content
static NSString *const BFWebViewAppLinkResolverTagExtractionJavaScript = @""
"(function() {"
" var metaTags = document.getElementsByTagName('meta');"
" var results = [];"
" for (var i = 0; i < metaTags.length; i++) {"
" var property = metaTags[i].getAttribute('property');"
" if (property && property.substring(0, 'al:'.length) === 'al:') {"
" var tag = { \"property\": metaTags[i].getAttribute('property') };"
" if (metaTags[i].hasAttribute('content')) {"
" tag['content'] = metaTags[i].getAttribute('content');"
" }"
" results.push(tag);"
" }"
" }"
" return JSON.stringify(results);"
"})()";
static NSString *const BFWebViewAppLinkResolverIOSURLKey = @"url";
static NSString *const BFWebViewAppLinkResolverIOSAppStoreIdKey = @"app_store_id";
static NSString *const BFWebViewAppLinkResolverIOSAppNameKey = @"app_name";
static NSString *const BFWebViewAppLinkResolverDictionaryValueKey = @"_value";
static NSString *const BFWebViewAppLinkResolverPreferHeader = @"Prefer-Html-Meta-Tags";
static NSString *const BFWebViewAppLinkResolverMetaTagPrefix = @"al";
static NSString *const BFWebViewAppLinkResolverWebKey = @"web";
static NSString *const BFWebViewAppLinkResolverIOSKey = @"ios";
static NSString *const BFWebViewAppLinkResolverIPhoneKey = @"iphone";
static NSString *const BFWebViewAppLinkResolverIPadKey = @"ipad";
static NSString *const BFWebViewAppLinkResolverWebURLKey = @"url";
static NSString *const BFWebViewAppLinkResolverShouldFallbackKey = @"should_fallback";
@interface BFWebViewAppLinkResolverWebViewDelegate : NSObject <UIWebViewDelegate>
@property (nonatomic, copy) void (^didFinishLoad)(UIWebView *webView);
@property (nonatomic, copy) void (^didFailLoadWithError)(UIWebView *webView, NSError *error);
@property (nonatomic, assign) BOOL hasLoaded;
@end
@implementation BFWebViewAppLinkResolverWebViewDelegate
- (void)webViewDidFinishLoad:(UIWebView *)webView {
if (self.didFinishLoad) {
self.didFinishLoad(webView);
}
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
if (self.didFailLoadWithError) {
self.didFailLoadWithError(webView, error);
}
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (self.hasLoaded) {
// Consider loading a second resource to be "success", since it indicates an inner frame
// or redirect is happening. We can run the tag extraction script at this point.
self.didFinishLoad(webView);
return NO;
}
self.hasLoaded = YES;
return YES;
}
@end
@implementation BFWebViewAppLinkResolver
+ (instancetype)sharedInstance {
static id instance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[self alloc] init];
});
return instance;
}
- (BFTask *)followRedirects:(NSURL *)url {
// This task will be resolved with either the redirect NSURL
// or a dictionary with the response data to be returned.
BFTaskCompletionSource *tcs = [BFTaskCompletionSource taskCompletionSource];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setValue:BFWebViewAppLinkResolverMetaTagPrefix forHTTPHeaderField:BFWebViewAppLinkResolverPreferHeader];
void (^completion)(NSURLResponse *response, NSData *data, NSError *error) = ^(NSURLResponse *response, NSData *data, NSError *error) {
if (error) {
[tcs setError:error];
return;
}
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
// NSURLConnection usually follows redirects automatically, but the
// documentation is unclear what the default is. This helps it along.
if (httpResponse.statusCode >= 300 && httpResponse.statusCode < 400) {
NSString *redirectString = httpResponse.allHeaderFields[@"Location"];
NSURL *redirectURL = [NSURL URLWithString:redirectString];
[tcs setResult:redirectURL];
return;
}
}
[tcs setResult:@{ @"response" : response, @"data" : data }];
};
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_7_0 || __MAC_OS_X_VERSION_MIN_REQUIRED >= __MAC_10_9
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
completion(response, data, error);
}] resume];
#else
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:completion];
#endif
return [tcs.task continueWithSuccessBlock:^id(BFTask *task) {
// If we redirected, just keep recursing.
if ([task.result isKindOfClass:[NSURL class]]) {
return [self followRedirects:task.result];
}
return task;
}];
}
- (BFTask *)appLinkFromURLInBackground:(NSURL *)url {
return [[self followRedirects:url] continueWithExecutor:[BFExecutor mainThreadExecutor]
withSuccessBlock:^id(BFTask *task) {
NSData *responseData = task.result[@"data"];
NSHTTPURLResponse *response = task.result[@"response"];
BFTaskCompletionSource *tcs = [BFTaskCompletionSource taskCompletionSource];
UIWebView *webView = [[UIWebView alloc] init];
BFWebViewAppLinkResolverWebViewDelegate *listener = [[BFWebViewAppLinkResolverWebViewDelegate alloc] init];
__block BFWebViewAppLinkResolverWebViewDelegate *retainedListener = listener;
listener.didFinishLoad = ^(UIWebView *view) {
if (retainedListener) {
NSDictionary *ogData = [self getALDataFromLoadedPage:view];
[view removeFromSuperview];
view.delegate = nil;
retainedListener = nil;
[tcs setResult:[self appLinkFromALData:ogData destination:url]];
}
};
listener.didFailLoadWithError = ^(UIWebView* view, NSError *error) {
if (retainedListener) {
[view removeFromSuperview];
view.delegate = nil;
retainedListener = nil;
[tcs setError:error];
}
};
webView.delegate = listener;
webView.hidden = YES;
[webView loadData:responseData
MIMEType:response.MIMEType
textEncodingName:response.textEncodingName
baseURL:response.URL];
UIWindow *window = [UIApplication sharedApplication].windows.firstObject;
[window addSubview:webView];
return tcs.task;
}];
}
/*
Builds up a data structure filled with the app link data from the meta tags on a page.
The structure of this object is a dictionary where each key holds an array of app link
data dictionaries. Values are stored in a key called "_value".
*/
- (NSDictionary *)parseALData:(NSArray *)dataArray {
NSMutableDictionary *al = [NSMutableDictionary dictionary];
for (NSDictionary *tag in dataArray) {
NSString *name = tag[@"property"];
if (![name isKindOfClass:[NSString class]]) {
continue;
}
NSArray *nameComponents = [name componentsSeparatedByString:@":"];
if (![nameComponents[0] isEqualToString:BFWebViewAppLinkResolverMetaTagPrefix]) {
continue;
}
NSMutableDictionary *root = al;
for (int i = 1; i < nameComponents.count; i++) {
NSMutableArray *children = root[nameComponents[i]];
if (!children) {
children = [NSMutableArray array];
root[nameComponents[i]] = children;
}
NSMutableDictionary *child = children.lastObject;
if (!child || i == nameComponents.count - 1) {
child = [NSMutableDictionary dictionary];
[children addObject:child];
}
root = child;
}
if (tag[@"content"]) {
root[BFWebViewAppLinkResolverDictionaryValueKey] = tag[@"content"];
}
}
return al;
}
- (NSDictionary *)getALDataFromLoadedPage:(UIWebView *)webView {
// Run some JavaScript in the webview to fetch the meta tags.
NSString *jsonString = [webView stringByEvaluatingJavaScriptFromString:BFWebViewAppLinkResolverTagExtractionJavaScript];
NSError *error = nil;
NSArray *arr = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding]
options:0
error:&error];
return [self parseALData:arr];
}
/*
Converts app link data into a BFAppLink containing the targets relevant for this platform.
*/
- (BFAppLink *)appLinkFromALData:(NSDictionary *)appLinkDict destination:(NSURL *)destination {
NSMutableArray *linkTargets = [NSMutableArray array];
NSArray *platformData = nil;
switch (UI_USER_INTERFACE_IDIOM()) {
case UIUserInterfaceIdiomPad:
platformData = @[ appLinkDict[BFWebViewAppLinkResolverIPadKey] ?: @{},
appLinkDict[BFWebViewAppLinkResolverIOSKey] ?: @{} ];
break;
case UIUserInterfaceIdiomPhone:
platformData = @[ appLinkDict[BFWebViewAppLinkResolverIPhoneKey] ?: @{},
appLinkDict[BFWebViewAppLinkResolverIOSKey] ?: @{} ];
break;
#ifdef __TVOS_9_0
case UIUserInterfaceIdiomTV:
#endif
case UIUserInterfaceIdiomUnspecified:
default:
// Future-proofing. Other User Interface idioms should only hit ios.
platformData = @[ appLinkDict[BFWebViewAppLinkResolverIOSKey] ?: @{} ];
break;
}
for (NSArray *platformObjects in platformData) {
for (NSDictionary *platformDict in platformObjects) {
// The schema requires a single url/app store id/app name,
// but we could find multiple of them. We'll make a best effort
// to interpret this data.
NSArray *urls = platformDict[BFWebViewAppLinkResolverIOSURLKey];
NSArray *appStoreIds = platformDict[BFWebViewAppLinkResolverIOSAppStoreIdKey];
NSArray *appNames = platformDict[BFWebViewAppLinkResolverIOSAppNameKey];
NSUInteger maxCount = MAX(urls.count, MAX(appStoreIds.count, appNames.count));
for (NSUInteger i = 0; i < maxCount; i++) {
NSString *urlString = urls[i][BFWebViewAppLinkResolverDictionaryValueKey];
NSURL *url = urlString ? [NSURL URLWithString:urlString] : nil;
NSString *appStoreId = appStoreIds[i][BFWebViewAppLinkResolverDictionaryValueKey];
NSString *appName = appNames[i][BFWebViewAppLinkResolverDictionaryValueKey];
BFAppLinkTarget *target = [BFAppLinkTarget appLinkTargetWithURL:url
appStoreId:appStoreId
appName:appName];
[linkTargets addObject:target];
}
}
}
NSDictionary *webDict = appLinkDict[BFWebViewAppLinkResolverWebKey][0];
NSString *webUrlString = webDict[BFWebViewAppLinkResolverWebURLKey][0][BFWebViewAppLinkResolverDictionaryValueKey];
NSString *shouldFallbackString = webDict[BFWebViewAppLinkResolverShouldFallbackKey][0][BFWebViewAppLinkResolverDictionaryValueKey];
NSURL *webUrl = destination;
if (shouldFallbackString &&
[@[ @"no", @"false", @"0" ] containsObject:[shouldFallbackString lowercaseString]]) {
webUrl = nil;
}
if (webUrl && webUrlString) {
webUrl = [NSURL URLWithString:webUrlString];
}
return [BFAppLink appLinkWithSourceURL:destination
targets:linkTargets
webURL:webUrl];
}
@end

30
Example/Pods/Bolts/LICENSE generated Normal file
View File

@ -0,0 +1,30 @@
BSD License
For Bolts software
Copyright (c) 2013-present, Facebook, Inc. All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name Facebook nor the names of its contributors may be used to
endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

682
Example/Pods/Bolts/README.md generated Normal file
View File

@ -0,0 +1,682 @@
Bolts
============
[![Build Status](http://img.shields.io/travis/BoltsFramework/Bolts-iOS/master.svg?style=flat)](https://travis-ci.org/BoltsFramework/Bolts-iOS)
[![Coverage Status](https://codecov.io/github/BoltsFramework/Bolts-iOS/coverage.svg?branch=master)](https://codecov.io/github/BoltsFramework/Bolts-iOS?branch=master)
[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)
[![Pod Version](http://img.shields.io/cocoapods/v/Bolts.svg?style=flat)](http://cocoadocs.org/docsets/Bolts/)
[![Pod Platform](http://img.shields.io/cocoapods/p/Bolts.svg?style=flat)](http://cocoadocs.org/docsets/Bolts/)
[![Pod License](http://img.shields.io/cocoapods/l/Bolts.svg?style=flat)](https://github.com/BoltsFramework/Bolts-iOS/blob/master/LICENSE)
[![Reference Status](https://www.versioneye.com/objective-c/bolts/reference_badge.svg?style=flat)](https://www.versioneye.com/objective-c/bolts/references)
Bolts is a collection of low-level libraries designed to make developing mobile
apps easier. Bolts was designed by Parse and Facebook for our own internal use,
and we have decided to open source these libraries to make them available to
others. Using these libraries does not require using any Parse services. Nor
do they require having a Parse or Facebook developer account.
Bolts includes:
* "Tasks", which make organization of complex asynchronous code more manageable. A task is kind of like a JavaScript Promise, but available for iOS and Android.
* An implementation of the [App Links protocol](http://www.applinks.org), helping you link to content in other apps and handle incoming deep-links.
For more information, see the [Bolts iOS API Reference](http://boltsframework.github.io/docs/ios/).
# Tasks
To build a truly responsive iOS application, you must keep long-running operations off of the UI thread, and be careful to avoid blocking anything the UI thread might be waiting on. This means you will need to execute various operations in the background. To make this easier, we've added a class called `BFTask`. A task represents the result of an asynchronous operation. Typically, a `BFTask` is returned from an asynchronous function and gives the ability to continue processing the result of the task. When a task is returned from a function, it's already begun doing its job. A task is not tied to a particular threading model: it represents the work being done, not where it is executing. Tasks have many advantages over other methods of asynchronous programming, such as callbacks. `BFTask` is not a replacement for `NSOperation` or GCD. In fact, they play well together. But tasks do fill in some gaps that those technologies don't address.
* `BFTask` takes care of managing dependencies for you. Unlike using `NSOperation` for dependency management, you don't have to declare all dependencies before starting a `BFTask`. For example, imagine you need to save a set of objects and each one may or may not require saving child objects. With an `NSOperation`, you would normally have to create operations for each of the child saves ahead of time. But you don't always know before you start the work whether that's going to be necessary. That can make managing dependencies with `NSOperation` very painful. Even in the best case, you have to create your dependencies before the operations that depend on them, which results in code that appears in a different order than it executes. With `BFTask`, you can decide during your operation's work whether there will be subtasks and return the other task in just those cases.
* `BFTasks` release their dependencies. `NSOperation` strongly retains its dependencies, so if you have a queue of ordered operations and sequence them using dependencies, you have a leak, because every operation gets retained forever. `BFTasks` release their callbacks as soon as they are run, so everything cleans up after itself. This can reduce memory use, and simplify memory management.
* `BFTasks` keep track of the state of finished tasks: It tracks whether there was a returned value, the task was cancelled, or if an error occurred. It also has convenience methods for propagating errors. With `NSOperation`, you have to build all of this stuff yourself.
* `BFTasks` don't depend on any particular threading model. So it's easy to have some tasks perform their work with an operation queue, while others perform work using blocks with GCD. These tasks can depend on each other seamlessly.
* Performing several tasks in a row will not create nested "pyramid" code as you would get when using only callbacks.
* `BFTasks` are fully composable, allowing you to perform branching, parallelism, and complex error handling, without the spaghetti code of having many named callbacks.
* You can arrange task-based code in the order that it executes, rather than having to split your logic across scattered callback functions.
For the examples in this doc, assume there are async versions of some common Parse methods, called `saveAsync:` and `findAsync:` which return a `Task`. In a later section, we'll show how to define these functions yourself.
## The `continueWithBlock` Method
Every `BFTask` has a method named `continueWithBlock:` which takes a continuation block. A continuation is a block that will be executed when the task is complete. You can then inspect the task to check if it was successful and to get its result.
```objective-c
// Objective-C
[[self saveAsync:obj] continueWithBlock:^id(BFTask *task) {
if (task.isCancelled) {
// the save was cancelled.
} else if (task.error) {
// the save failed.
} else {
// the object was saved successfully.
PFObject *object = task.result;
}
return nil;
}];
```
```swift
// Swift
self.saveAsync(obj).continueWithBlock {
(task: BFTask!) -> BFTask in
if task.isCancelled() {
// the save was cancelled.
} else if task.error() {
// the save failed.
} else {
// the object was saved successfully.
var object = task.result() as PFObject
}
}
```
BFTasks use Objective-C blocks, so the syntax should be pretty straightforward. Let's look closer at the types involved with an example.
```objective-c
// Objective-C
/**
* Gets an NSString asynchronously.
*/
- (BFTask *)getStringAsync {
// Let's suppose getNumberAsync returns a BFTask whose result is an NSNumber.
return [[self getNumberAsync] continueWithBlock:^id(BFTask *task) {
// This continuation block takes the NSNumber BFTask as input,
// and provides an NSString as output.
NSNumber *number = task.result;
return [NSString stringWithFormat:@"%@", number];
)];
}
```
```swift
// Swift
/**
* Gets an NSString asynchronously.
*/
func getStringAsync() -> BFTask {
//Let's suppose getNumberAsync returns a BFTask whose result is an NSNumber.
return self.getNumberAsync().continueWithBlock {
(task: BFTask!) -> NSString in
// This continuation block takes the NSNumber BFTask as input,
// and provides an NSString as output.
let number = task.result() as NSNumber
return NSString(format:"%@", number)
}
}
```
In many cases, you only want to do more work if the previous task was successful, and propagate any errors or cancellations to be dealt with later. To do this, use the `continueWithSuccessBlock:` method instead of `continueWithBlock:`.
```objective-c
// Objective-C
[[self saveAsync:obj] continueWithSuccessBlock:^id(BFTask *task) {
// the object was saved successfully.
return nil;
}];
```
```swift
// Swift
self.saveAsync(obj).continueWithSuccessBlock {
(task: BFTask!) -> AnyObject! in
// the object was saved successfully.
return nil
}
```
## Chaining Tasks Together
BFTasks are a little bit magical, in that they let you chain them without nesting. If you return a BFTask from `continueWithBlock:`, then the task returned by `continueWithBlock:` will not be considered finished until the new task returned from the new continuation block. This lets you perform multiple actions without incurring the pyramid code you would get with callbacks. Likewise, you can return a `BFTask` from `continueWithSuccessBlock:`. So, return a `BFTask` to do more asynchronous work.
```objective-c
// Objective-C
PFQuery *query = [PFQuery queryWithClassName:@"Student"];
[query orderByDescending:@"gpa"];
[[[[[self findAsync:query] continueWithSuccessBlock:^id(BFTask *task) {
NSArray *students = task.result;
PFObject *valedictorian = [students objectAtIndex:0];
[valedictorian setObject:@YES forKey:@"valedictorian"];
return [self saveAsync:valedictorian];
}] continueWithSuccessBlock:^id(BFTask *task) {
PFObject *valedictorian = task.result;
return [self findAsync:query];
}] continueWithSuccessBlock:^id(BFTask *task) {
NSArray *students = task.result;
PFObject *salutatorian = [students objectAtIndex:1];
[salutatorian setObject:@YES forKey:@"salutatorian"];
return [self saveAsync:salutatorian];
}] continueWithSuccessBlock:^id(BFTask *task) {
// Everything is done!
return nil;
}];
```
```swift
// Swift
var query = PFQuery(className:"Student")
query.orderByDescending("gpa")
findAsync(query).continueWithSuccessBlock {
(task: BFTask!) -> BFTask in
let students = task.result() as NSArray
var valedictorian = students.objectAtIndex(0) as PFObject
valedictorian["valedictorian"] = true
return self.saveAsync(valedictorian)
}.continueWithSuccessBlock {
(task: BFTask!) -> BFTask in
var valedictorian = task.result() as PFObject
return self.findAsync(query)
}.continueWithSuccessBlock {
(task: BFTask!) -> BFTask in
let students = task.result() as NSArray
var salutatorian = students.objectAtIndex(1) as PFObject
salutatorian["salutatorian"] = true
return self.saveAsync(salutatorian)
}.continueWithSuccessBlock {
(task: BFTask!) -> AnyObject! in
// Everything is done!
return nil
}
```
## Error Handling
By carefully choosing whether to call `continueWithBlock:` or `continueWithSuccessBlock:`, you can control how errors are propagated in your application. Using `continueWithBlock:` lets you handle errors by transforming them or dealing with them. You can think of failed tasks kind of like throwing an exception. In fact, if you throw an exception inside a continuation, the resulting task will be faulted with that exception.
```objective-c
// Objective-C
PFQuery *query = [PFQuery queryWithClassName:@"Student"];
[query orderByDescending:@"gpa"];
[[[[[self findAsync:query] continueWithSuccessBlock:^id(BFTask *task) {
NSArray *students = task.result;
PFObject *valedictorian = [students objectAtIndex:0];
[valedictorian setObject:@YES forKey:@"valedictorian"];
// Force this callback to fail.
return [BFTask taskWithError:[NSError errorWithDomain:@"example.com"
code:-1
userInfo:nil]];
}] continueWithSuccessBlock:^id(BFTask *task) {
// Now this continuation will be skipped.
PFQuery *valedictorian = task.result;
return [self findAsync:query];
}] continueWithBlock:^id(BFTask *task) {
if (task.error) {
// This error handler WILL be called.
// The error will be the NSError returned above.
// Let's handle the error by returning a new value.
// The task will be completed with nil as its value.
return nil;
}
// This will also be skipped.
NSArray *students = task.result;
PFObject *salutatorian = [students objectAtIndex:1];
[salutatorian setObject:@YES forKey:@"salutatorian"];
return [self saveAsync:salutatorian];
}] continueWithSuccessBlock:^id(BFTask *task) {
// Everything is done! This gets called.
// The task's result is nil.
return nil;
}];
```
```swift
// Swift
var query = PFQuery(className:"Student")
query.orderByDescending("gpa")
findAsync(query).continueWithSuccessBlock {
(task: BFTask!) -> BFTask in
let students = task.result() as NSArray
var valedictorian = students.objectAtIndex(0) as PFObject
valedictorian["valedictorian"] = true
//Force this callback to fail.
return BFTask(error:NSError(domain:"example.com",
code:-1, userInfo: nil))
}.continueWithSuccessBlock {
(task: BFTask!) -> AnyObject! in
//Now this continuation will be skipped.
var valedictorian = task.result() as PFObject
return self.findAsync(query)
}.continueWithBlock {
(task: BFTask!) -> AnyObject! in
if task.error() {
// This error handler WILL be called.
// The error will be the NSError returned above.
// Let's handle the error by returning a new value.
// The task will be completed with nil as its value.
return nil
}
// This will also be skipped.
let students = task.result() as NSArray
var salutatorian = students.objectAtIndex(1) as PFObject
salutatorian["salutatorian"] = true
return self.saveAsync(salutatorian)
}.continueWithSuccessBlock {
(task: BFTask!) -> AnyObject! in
// Everything is done! This gets called.
// The tasks result is nil.
return nil
}
```
It's often convenient to have a long chain of success callbacks with only one error handler at the end.
## Creating Tasks
When you're getting started, you can just use the tasks returned from methods like `findAsync:` or `saveAsync:`. However, for more advanced scenarios, you may want to make your own tasks. To do that, you create a `BFTaskCompletionSource`. This object will let you create a new `BFTask`, and control whether it gets marked as finished or cancelled. After you create a `BFTaskCompletionSource`, you'll need to call `setResult:`, `setError:`, or `cancel` to trigger its continuations.
```objective-c
// Objective-C
- (BFTask *)successAsync {
BFTaskCompletionSource *successful = [BFTaskCompletionSource taskCompletionSource];
[successful setResult:@"The good result."];
return successful.task;
}
- (BFTask *)failAsync {
BFTaskCompletionSource *failed = [BFTaskCompletionSource taskCompletionSource];
[failed setError:[NSError errorWithDomain:@"example.com" code:-1 userInfo:nil]];
return failed.task;
}
```
```swift
// Swift
func successAsync() -> BFTask {
var successful = BFTaskCompletionSource()
successful.setResult("The good result.")
return successful.task
}
func failAsync() -> BFTask {
var failed = BFTaskCompletionSource()
failed.setError(NSError(domain:"example.com", code:-1, userInfo:nil))
return failed.task
}
```
If you know the result of a task at the time it is created, there are some convenience methods you can use.
```objective-c
// Objective-C
BFTask *successful = [BFTask taskWithResult:@"The good result."];
BFTask *failed = [BFTask taskWithError:anError];
```
```swift
// Swift
let successful = BFTask(result:"The good result")
let failed = BFTask(error:anError)
```
## Creating Async Methods
With these tools, it's easy to make your own asynchronous functions that return tasks. For example, you can make a task-based version of `fetchAsync:` easily.
```objective-c
// Objective-C
- (BFTask *) fetchAsync:(PFObject *)object {
BFTaskCompletionSource *task = [BFTaskCompletionSource taskCompletionSource];
[object fetchInBackgroundWithBlock:^(PFObject *object, NSError *error) {
if (!error) {
[task setResult:object];
} else {
[task setError:error];
}
}];
return task.task;
}
```
```swift
// Swift
func fetchAsync(object: PFObject) -> BFTask {
var task = BFTaskCompletionSource()
object.fetchInBackgroundWithBlock {
(object: PFObject?, error: NSError?) -> Void in
if error == nil {
task.setResult(object)
} else {
task.setError(error)
}
}
return task.task
}
```
It's similarly easy to create `saveAsync:`, `findAsync:` or `deleteAsync:`.
## Tasks in Series
`BFTasks` are convenient when you want to do a series of tasks in a row, each one waiting for the previous to finish. For example, imagine you want to delete all of the comments on your blog.
```objective-c
// Objective-C
PFQuery *query = [PFQuery queryWithClassName:@"Comments"];
[query whereKey:@"post" equalTo:@123];
[[[self findAsync:query] continueWithBlock:^id(BFTask *task) {
NSArray *results = task.result;
// Create a trivial completed task as a base case.
BFTask *task = [BFTask taskWithResult:nil];
for (PFObject *result in results) {
// For each item, extend the task with a function to delete the item.
task = [task continueWithBlock:^id(BFTask *task) {
// Return a task that will be marked as completed when the delete is finished.
return [self deleteAsync:result];
}];
}
return task;
}] continueWithBlock:^id(BFTask *task) {
// Every comment was deleted.
return nil;
}];
```
```swift
// Swift
var query = PFQuery(className:"Comments")
query.whereKey("post", equalTo:123)
findAsync(query).continueWithBlock {
(task: BFTask!) -> BFTask in
let results = task.result() as NSArray
// Create a trivial completed task as a base case.
let task = BFTask(result:nil)
for result : PFObject in results {
// For each item, extend the task with a function to delete the item.
task = task.continueWithBlock {
(task: BFTask!) -> BFTask in
return self.deleteAsync(result)
}
}
return task
}.continueWithBlock {
(task: BFTask!) -> AnyObject! in
// Every comment was deleted.
return nil
}
```
## Tasks in Parallel
You can also perform several tasks in parallel, using the `taskForCompletionOfAllTasks:` method. You can start multiple operations at once, and use `taskForCompletionOfAllTasks:` to create a new task that will be marked as completed when all of its input tasks are completed. The new task will be successful only if all of the passed-in tasks succeed. Performing operations in parallel will be faster than doing them serially, but may consume more system resources and bandwidth.
```objective-c
// Objective-C
PFQuery *query = [PFQuery queryWithClassName:@"Comments"];
[query whereKey:@"post" equalTo:@123];
[[[self findAsync:query] continueWithBlock:^id(BFTask *results) {
// Collect one task for each delete into an array.
NSMutableArray *tasks = [NSMutableArray array];
for (PFObject *result in results) {
// Start this delete immediately and add its task to the list.
[tasks addObject:[self deleteAsync:result]];
}
// Return a new task that will be marked as completed when all of the deletes are
// finished.
return [BFTask taskForCompletionOfAllTasks:tasks];
}] continueWithBlock:^id(BFTask *task) {
// Every comment was deleted.
return nil;
}];
```
```swift
// Swift
var query = PFQuery(className:"Comments")
query.whereKey("post", equalTo:123)
findAsync(query).continueWithBlock {
(task: BFTask!) -> BFTask in
// Collect one task for each delete into an array.
var tasks = NSMutableArray.array()
var results = task.result() as NSArray
for result : PFObject! in results {
// Start this delete immediately and add its task to the list.
tasks.addObject(self.deleteAsync(result))
}
// Return a new task that will be marked as completed when all of the deletes
// are finished.
return BFTask(forCompletionOfAllTasks:tasks)
}.continueWithBlock {
(task: BFTask!) -> AnyObject! in
// Every comment was deleted.
return nil
}
```
## Task Executors
Both `continueWithBlock:` and `continueWithSuccessBlock:` methods have another form that takes an instance of `BFExecutor`. These are `continueWithExecutor:withBlock:` and `continueWithExecutor:withSuccessBlock:`. These methods allow you to control how the continuation is executed. The default executor will dispatch to GCD, but you can provide your own executor to schedule work onto a different thread. For example, if you want to continue with work on the UI thread:
```objective-c
// Create a BFExecutor that uses the main thread.
BFExecutor *myExecutor = [BFExecutor executorWithBlock:^void(void(^block)()) {
dispatch_async(dispatch_get_main_queue(), block);
}];
// And use the Main Thread Executor like this. The executor applies only to the new
// continuation being passed into continueWithBlock.
[[self fetchAsync:object] continueWithExecutor:myExecutor withBlock:^id(BFTask *task) {
myTextView.text = [object objectForKey:@"name"];
}];
```
For common cases, such as dispatching on the main thread, we have provided default implementations of `BFExecutor`. These include `defaultExecutor`, `immediateExecutor`, `mainThreadExecutor`, `executorWithDispatchQueue:`, and `executorWithOperationQueue:`. For example:
```objective-c
// Continue on the Main Thread, using a built-in executor.
[[self fetchAsync:object] continueWithExecutor:[BFExecutor mainThreadExecutor] withBlock:^id(BFTask *task) {
myTextView.text = [object objectForKey:@"name"];
}];
```
## Task Cancellation
It's generally bad design to keep track of the `BFTaskCompletionSource` for cancellation. A better model is to create a "cancellation token" at the top level, and pass that to each async function that you want to be part of the same "cancelable operation". Then, in your continuation blocks, you can check whether the cancellation token has been cancelled and bail out early by returning a `[BFTask cancelledTask]`. For example:
```objective-c
- (void)doSomethingComplicatedAsync:(MYCancellationToken *)cancellationToken {
[[self doSomethingAsync:cancellationToken] continueWithBlock:^{
if (cancellationToken.isCancelled) {
return [BFTask cancelledTask];
}
// Do something that takes a while.
return result;
}];
}
// Somewhere else.
MYCancellationToken *cancellationToken = [[MYCancellationToken alloc] init];
[obj doSomethingComplicatedAsync:cancellationToken];
// When you get bored...
[cancellationToken cancel];
```
**Note:** The cancellation token implementation should be thread-safe.
We are likely to add some concept like this to Bolts at some point in the future.
# App Links
[App Links](http://www.applinks.org) provide a cross-platform mechanism that allows a developer to define and publish a deep-linking scheme for their content, allowing other apps to link directly to an experience optimized for the device they are running on. Whether you are building an app that receives incoming links or one that may link out to other apps' content, Bolts provides tools to simplify implementation of the [App Links protocol](http://www.applinks.org/documentation).
## Handling an App Link
The most common case will be making your app receive App Links. In-linking will allow your users to quickly access the richest, most native-feeling presentation of linked content on their devices. Bolts makes it easy to handle an inbound App Link (as well as general inbound deep-links) by providing utilities for processing an incoming URL.
For example, you can use the `BFURL` utility class to parse an incoming URL in your `AppDelegate`:
```objective-c
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
BFURL *parsedUrl = [BFURL URLWithInboundURL:url sourceApplication:sourceApplication];
// Use the target URL from the App Link to locate content.
if ([parsedUrl.targetURL.pathComponents[1] isEqualToString:@"profiles"]) {
// Open a profile viewer.
}
// You can also check the query string easily.
NSString *query = parsedUrl.targetQueryParameters[@"query"];
// Apps that have existing deep-linking support and map their App Links to existing
// deep-linking functionality may instead want to perform these operations on the input URL.
// Use the target URL from the App Link to locate content.
if ([parsedUrl.inputURL.pathComponents[1] isEqualToString:@"profiles"]) {
// Open a profile viewer.
}
// You can also check the query string easily.
NSString *query = parsedUrl.inputQueryParameters[@"query"];
// Apps can easily check the Extras and App Link data from the App Link as well.
NSString *fbAccessToken = parsedUrl.appLinkExtras[@"fb_access_token"];
NSDictionary *refererData = parsedUrl.appLinkExtras[@"referer"];
}
```
## Navigating to a URL
Following an App Link allows your app to provide the best user experience (as defined by the receiving app) when a user navigates to a link. Bolts makes this process simple, automating the steps required to follow a link:
1. Resolve the App Link by getting the App Link metadata from the HTML at the URL specified.
2. Step through App Link targets relevant to the device being used, checking whether the app that can handle the target is present on the device.
3. If an app is present, build a URL with the appropriate al_applink_data specified and navigate to that URL.
4. Otherwise, open the browser with the original URL specified.
In the simplest case, it takes just one line of code to navigate to a URL that may have an App Link:
```objective-c
[BFAppLinkNavigation navigateToURLInBackground:url];
```
### Adding App and Navigation Data
Under most circumstances, the data that will need to be passed along to an app during a navigation will be contained in the URL itself, so that whether or not the app is actually installed on the device, users are taken to the correct content. Occasionally, however, apps will want to pass along data that is relevant for app-to-app navigation, or will want to augment the App Link protocol with information that might be used by the app to adjust how the app should behave (e.g. showing a link back to the referring app).
If you want to take advantage of these features, you can break apart the navigation process. First, you must have an App Link to which you wish to navigate:
```objective-c
[[BFAppLinkNavigation resolveAppLinkInBackground:url] continueWithSuccessBlock:^id(BFTask *task) {
BFAppLink *link = task.result;
}];
```
Then, you can build an App Link request with any additional data you would like and navigate:
```objective-c
BFAppLinkNavigation *navigation = [BFAppLinkNavigation navigationWithAppLink:link
extras:@{ @"access_token": @"t0kEn" }
appLinkData:@{ @"ref": @"12345" }];
NSError *error = nil;
[navigation navigate:&error];
```
### Resolving App Link Metadata
Bolts allows for custom App Link resolution, which may be used as a performance optimization (e.g. caching the metadata) or as a mechanism to allow developers to use a centralized index for obtaining App Link metadata. A custom App Link resolver just needs to be able to take a URL and return a `BFAppLink` containing the ordered list of `BFAppLinkTarget`s that are applicable for this device. Bolts provides one of these out of the box that performs this resolution on the device using a hidden UIWebView.
You can use any resolver that implements the `BFAppLinkResolving` protocol by using one of the overloads on `BFAppLinkNavigation`:
```objective-c
[BFAppLinkNavigation navigateToURLInBackground:url
resolver:resolver];
```
Alternatively, a you can swap out the default resolver to be used by the built-in APIs:
```objective-c
[BFAppLinkNavigation setDefaultResolver:resolver];
[BFAppLinkNavigation navigateToURLInBackground:url];
```
## App Link Return-to-Referer View
When an application is opened via an App Link, a banner allowing the user to "Touch to return to <calling app>" should be displayed. The `BFAppLinkReturnToRefererView` provides this functionality. It will take an incoming App Link and parse the referer information to display the appropriate calling app name.
```objective-c
- (void)viewDidLoad {
[super viewDidLoad];
// Perform other view initialization.
self.returnToRefererController = [[BFAppLinkReturnToRefererController alloc] init];
// self.returnToRefererView is a BFAppLinkReturnToRefererView.
// You may initialize the view either by loading it from a NIB or programmatically.
self.returnToRefererController.view = self.returnToRefererView;
// If you have a UINavigationController in the view, then the bar must be shown above it.
[self.returnToRefererController]
}
```
The following code assumes that the view controller has an `openedAppLinkURL` `NSURL` property that has already been populated with the URL used to open the app. You can then do something like this to show the view:
```objective-c
- (void)viewWillAppear {
[super viewWillAppear];
// Show only if you have a back AppLink.
[self.returnToRefererController showViewForRefererURL:self.openedAppLinkURL];
}
```
In a navigaton-controller view hierarchy, the banner should be displayed above the navigation bar, and `BFAppLinkReturnToRefererController` provides an `initForDisplayAboveNavController` method to assist with this.
## Analytics
Bolts introduces Measurement Event. App Links posts three different Measurement Event notifications to the application, which can be caught and integrated with existing analytics components in your application.
* `al_nav_out` — Raised when your app switches out to an App Links URL.
* `al_nav_in` — Raised when your app opens an incoming App Links URL.
* `al_ref_back_out` — Raised when your app returns back the referrer app using the built-in top navigation back bar view.
### Listen for App Links Measurement Events
There are other analytics tools that are integrated with Bolts' App Links events, but you can also listen for these events yourself:
```objective-c
[[NSNotificationCenter defaultCenter] addObserverForName:BFMeasurementEventNotificationName object:nil queue:nil usingBlock:^(NSNotification *note) {
NSDictionary *event = note.userInfo;
NSDictionary *eventData = event[BFMeasurementEventArgsKey];
// Integrate to your logging/analytics component.
}];
```
### App Links Event Fields
App Links Measurement Events sends additional information from App Links Intents in flattened string key value pairs. Here are some of the useful fields for the three events.
* `al_nav_in`
* `inputURL`: the URL that opens the app.
* `inputURLScheme`: the scheme of `inputURL`.
* `refererURL`: the URL that the referrer app added into `al_applink_data`: `referer_app_link`.
* `refererAppName`: the app name that the referrer app added to `al_applink_data`: `referer_app_link`.
* `sourceApplication`: the bundle of referrer application.
* `targetURL`: the `target_url` field in `al_applink_data`.
* `version`: App Links API version.
* `al_nav_out` / `al_ref_back_out`
* `outputURL`: the URL used to open the other app (or browser). If there is an eligible app to open, this will be the custom scheme url/intent in `al_applink_data`.
* `outputURLScheme`: the scheme of `outputURL`.
* `sourceURL`: the URL of the page hosting App Links meta tags.
* `sourceURLHost`: the hostname of `sourceURL`.
* `success`: `“1”` to indicate success in opening the App Link in another app or browser; `“0”` to indicate failure to open the App Link.
* `type`: `“app”` for open in app, `“web”` for open in browser; `“fail”` when the success field is `“0”`.
* `version`: App Links API version.
# Installation
You can download the latest framework files from our [Releases page](https://github.com/BoltsFramework/Bolts-iOS/releases).
Bolts is also available through [CocoaPods](http://cocoapods.org). To install it simply add the following line to your Podfile:
pod 'Bolts'

View File

@ -0,0 +1,166 @@
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import <Foundation/Foundation.h>
#import <FBSDKCoreKit/FBSDKCopying.h>
#import <FBSDKCoreKit/FBSDKGraphRequestConnection.h>
#import <FBSDKCoreKit/FBSDKMacros.h>
/*!
@abstract Notification indicating that the `currentAccessToken` has changed.
@discussion the userInfo dictionary of the notification will contain keys
`FBSDKAccessTokenChangeOldKey` and
`FBSDKAccessTokenChangeNewKey`.
*/
FBSDK_EXTERN NSString *const FBSDKAccessTokenDidChangeNotification;
/*!
@abstract A key in the notification's userInfo that will be set
if and only if the user ID changed between the old and new tokens.
@discussion Token refreshes can occur automatically with the SDK
which do not change the user. If you're only interested in user
changes (such as logging out), you should check for the existence
of this key. The value is a NSNumber with a boolValue.
On a fresh start of the app where the SDK reads in the cached value
of an access token, this key will also exist since the access token
is moving from a null state (no user) to a non-null state (user).
*/
FBSDK_EXTERN NSString *const FBSDKAccessTokenDidChangeUserID;
/*
@abstract key in notification's userInfo object for getting the old token.
@discussion If there was no old token, the key will not be present.
*/
FBSDK_EXTERN NSString *const FBSDKAccessTokenChangeOldKey;
/*
@abstract key in notification's userInfo object for getting the new token.
@discussion If there is no new token, the key will not be present.
*/
FBSDK_EXTERN NSString *const FBSDKAccessTokenChangeNewKey;
/*!
@class FBSDKAccessToken
@abstract Represents an immutable access token for using Facebook services.
*/
@interface FBSDKAccessToken : NSObject<FBSDKCopying, NSSecureCoding>
/*!
@abstract Returns the app ID.
*/
@property (readonly, copy, nonatomic) NSString *appID;
/*!
@abstract Returns the known declined permissions.
*/
@property (readonly, copy, nonatomic) NSSet *declinedPermissions;
/*!
@abstract Returns the expiration date.
*/
@property (readonly, copy, nonatomic) NSDate *expirationDate;
/*!
@abstract Returns the known granted permissions.
*/
@property (readonly, copy, nonatomic) NSSet *permissions;
/*!
@abstract Returns the date the token was last refreshed.
*/
@property (readonly, copy, nonatomic) NSDate *refreshDate;
/*!
@abstract Returns the opaque token string.
*/
@property (readonly, copy, nonatomic) NSString *tokenString;
/*!
@abstract Returns the user ID.
*/
@property (readonly, copy, nonatomic) NSString *userID;
- (instancetype)init NS_UNAVAILABLE;
+ (instancetype)new NS_UNAVAILABLE;
/*!
@abstract Initializes a new instance.
@param tokenString the opaque token string.
@param permissions the granted permissions. Note this is converted to NSSet and is only
an NSArray for the convenience of literal syntax.
@param declinedPermissions the declined permissions. Note this is converted to NSSet and is only
an NSArray for the convenience of literal syntax.
@param appID the app ID.
@param userID the user ID.
@param expirationDate the optional expiration date (defaults to distantFuture).
@param refreshDate the optional date the token was last refreshed (defaults to today).
@discussion This initializer should only be used for advanced apps that
manage tokens explicitly. Typical login flows only need to use `FBSDKLoginManager`
along with `+currentAccessToken`.
*/
- (instancetype)initWithTokenString:(NSString *)tokenString
permissions:(NSArray *)permissions
declinedPermissions:(NSArray *)declinedPermissions
appID:(NSString *)appID
userID:(NSString *)userID
expirationDate:(NSDate *)expirationDate
refreshDate:(NSDate *)refreshDate
NS_DESIGNATED_INITIALIZER;
/*!
@abstract Convenience getter to determine if a permission has been granted
@param permission The permission to check.
*/
- (BOOL)hasGranted:(NSString *)permission;
/*!
@abstract Compares the receiver to another FBSDKAccessToken
@param token The other token
@return YES if the receiver's values are equal to the other token's values; otherwise NO
*/
- (BOOL)isEqualToAccessToken:(FBSDKAccessToken *)token;
/*!
@abstract Returns the "global" access token that represents the currently logged in user.
@discussion The `currentAccessToken` is a convenient representation of the token of the
current user and is used by other SDK components (like `FBSDKLoginManager`).
*/
+ (FBSDKAccessToken *)currentAccessToken;
/*!
@abstract Sets the "global" access token that represents the currently logged in user.
@param token The access token to set.
@discussion This will broadcast a notification and save the token to the app keychain.
*/
+ (void)setCurrentAccessToken:(FBSDKAccessToken *)token;
/*!
@abstract Refresh the current access token's permission state and extend the token's expiration date,
if possible.
@param completionHandler an optional callback handler that can surface any errors related to permission refreshing.
@discussion On a successful refresh, the currentAccessToken will be updated so you typically only need to
observe the `FBSDKAccessTokenDidChangeNotification` notification.
If a token is already expired, it cannot be refreshed.
*/
+ (void)refreshCurrentAccessToken:(FBSDKGraphRequestHandler)completionHandler;
@end

View File

@ -0,0 +1,200 @@
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import "FBSDKAccessToken.h"
#import "FBSDKGraphRequestPiggybackManager.h"
#import "FBSDKInternalUtility.h"
#import "FBSDKMath.h"
#import "FBSDKSettings+Internal.h"
NSString *const FBSDKAccessTokenDidChangeNotification = @"com.facebook.sdk.FBSDKAccessTokenData.FBSDKAccessTokenDidChangeNotification";
NSString *const FBSDKAccessTokenDidChangeUserID = @"FBSDKAccessTokenDidChangeUserID";
NSString *const FBSDKAccessTokenChangeNewKey = @"FBSDKAccessToken";
NSString *const FBSDKAccessTokenChangeOldKey = @"FBSDKAccessTokenOld";
static FBSDKAccessToken *g_currentAccessToken;
#define FBSDK_ACCESSTOKEN_TOKENSTRING_KEY @"tokenString"
#define FBSDK_ACCESSTOKEN_PERMISSIONS_KEY @"permissions"
#define FBSDK_ACCESSTOKEN_DECLINEDPERMISSIONS_KEY @"declinedPermissions"
#define FBSDK_ACCESSTOKEN_APPID_KEY @"appID"
#define FBSDK_ACCESSTOKEN_USERID_KEY @"userID"
#define FBSDK_ACCESSTOKEN_REFRESHDATE_KEY @"refreshDate"
#define FBSDK_ACCESSTOKEN_EXPIRATIONDATE_KEY @"expirationDate"
@implementation FBSDKAccessToken
- (instancetype)init NS_UNAVAILABLE
{
assert(0);
}
- (instancetype)initWithTokenString:(NSString *)tokenString
permissions:(NSArray *)permissions
declinedPermissions:(NSArray *)declinedPermissions
appID:(NSString *)appID
userID:(NSString *)userID
expirationDate:(NSDate *)expirationDate
refreshDate:(NSDate *)refreshDate
{
if ((self = [super init])) {
_tokenString = [tokenString copy];
_permissions = [NSSet setWithArray:permissions];
_declinedPermissions = [NSSet setWithArray:declinedPermissions];
_appID = [appID copy];
_userID = [userID copy];
_expirationDate = [expirationDate copy] ?: [NSDate distantFuture];
_refreshDate = [refreshDate copy] ?: [NSDate date];
}
return self;
}
- (BOOL)hasGranted:(NSString *)permission
{
return [self.permissions containsObject:permission];
}
+ (FBSDKAccessToken *)currentAccessToken
{
return g_currentAccessToken;
}
+ (void)setCurrentAccessToken:(FBSDKAccessToken *)token
{
if (token != g_currentAccessToken) {
NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
[FBSDKInternalUtility dictionary:userInfo setObject:token forKey:FBSDKAccessTokenChangeNewKey];
[FBSDKInternalUtility dictionary:userInfo setObject:g_currentAccessToken forKey:FBSDKAccessTokenChangeOldKey];
if (![g_currentAccessToken.userID isEqualToString:token.userID]) {
userInfo[FBSDKAccessTokenDidChangeUserID] = @YES;
}
g_currentAccessToken = token;
// Only need to keep current session in web view for the case when token is current
// When token is abandoned cookies must to be cleaned up immediatelly
if (token == nil) {
[FBSDKInternalUtility deleteFacebookCookies];
}
[[FBSDKSettings accessTokenCache] cacheAccessToken:token];
[[NSNotificationCenter defaultCenter] postNotificationName:FBSDKAccessTokenDidChangeNotification
object:[self class]
userInfo:userInfo];
}
}
+ (void)refreshCurrentAccessToken:(FBSDKGraphRequestHandler)completionHandler
{
if ([FBSDKAccessToken currentAccessToken]) {
FBSDKGraphRequestConnection *connection = [[FBSDKGraphRequestConnection alloc] init];
[FBSDKGraphRequestPiggybackManager addRefreshPiggyback:connection permissionHandler:completionHandler];
[connection start];
} else {
if (completionHandler) {
completionHandler(nil, nil, [FBSDKError errorWithCode:FBSDKAccessTokenRequiredErrorCode message:@"No current access token to refresh"]);
}
}
}
#pragma mark - Equality
- (NSUInteger)hash
{
NSUInteger subhashes[] = {
[self.tokenString hash],
[self.permissions hash],
[self.declinedPermissions hash],
[self.appID hash],
[self.userID hash],
[self.refreshDate hash],
[self.expirationDate hash]
};
return [FBSDKMath hashWithIntegerArray:subhashes count:sizeof(subhashes) / sizeof(subhashes[0])];
}
- (BOOL)isEqual:(id)object
{
if (self == object) {
return YES;
}
if (![object isKindOfClass:[FBSDKAccessToken class]]) {
return NO;
}
return [self isEqualToAccessToken:(FBSDKAccessToken *)object];
}
- (BOOL)isEqualToAccessToken:(FBSDKAccessToken *)token
{
return (token &&
[FBSDKInternalUtility object:self.tokenString isEqualToObject:token.tokenString] &&
[FBSDKInternalUtility object:self.permissions isEqualToObject:token.permissions] &&
[FBSDKInternalUtility object:self.declinedPermissions isEqualToObject:token.declinedPermissions] &&
[FBSDKInternalUtility object:self.appID isEqualToObject:token.appID] &&
[FBSDKInternalUtility object:self.userID isEqualToObject:token.userID] &&
[FBSDKInternalUtility object:self.refreshDate isEqualToObject:token.refreshDate] &&
[FBSDKInternalUtility object:self.expirationDate isEqualToObject:token.expirationDate] );
}
#pragma mark - NSCopying
- (id)copyWithZone:(NSZone *)zone
{
// we're immutable.
return self;
}
#pragma mark NSCoding
+ (BOOL)supportsSecureCoding
{
return YES;
}
- (id)initWithCoder:(NSCoder *)decoder
{
NSString *appID = [decoder decodeObjectOfClass:[NSString class] forKey:FBSDK_ACCESSTOKEN_APPID_KEY];
NSSet *declinedPermissions = [decoder decodeObjectOfClass:[NSSet class] forKey:FBSDK_ACCESSTOKEN_DECLINEDPERMISSIONS_KEY];
NSSet *permissions = [decoder decodeObjectOfClass:[NSSet class] forKey:FBSDK_ACCESSTOKEN_PERMISSIONS_KEY];
NSString *tokenString = [decoder decodeObjectOfClass:[NSString class] forKey:FBSDK_ACCESSTOKEN_TOKENSTRING_KEY];
NSString *userID = [decoder decodeObjectOfClass:[NSString class] forKey:FBSDK_ACCESSTOKEN_USERID_KEY];
NSDate *refreshDate = [decoder decodeObjectOfClass:[NSDate class] forKey:FBSDK_ACCESSTOKEN_REFRESHDATE_KEY];
NSDate *expirationDate = [decoder decodeObjectOfClass:[NSDate class] forKey:FBSDK_ACCESSTOKEN_EXPIRATIONDATE_KEY];
return [self initWithTokenString:tokenString
permissions:[permissions allObjects]
declinedPermissions:[declinedPermissions allObjects]
appID:appID
userID:userID
expirationDate:expirationDate
refreshDate:refreshDate];
}
- (void)encodeWithCoder:(NSCoder *)encoder
{
[encoder encodeObject:self.appID forKey:FBSDK_ACCESSTOKEN_APPID_KEY];
[encoder encodeObject:self.declinedPermissions forKey:FBSDK_ACCESSTOKEN_DECLINEDPERMISSIONS_KEY];
[encoder encodeObject:self.permissions forKey:FBSDK_ACCESSTOKEN_PERMISSIONS_KEY];
[encoder encodeObject:self.tokenString forKey:FBSDK_ACCESSTOKEN_TOKENSTRING_KEY];
[encoder encodeObject:self.userID forKey:FBSDK_ACCESSTOKEN_USERID_KEY];
[encoder encodeObject:self.expirationDate forKey:FBSDK_ACCESSTOKEN_EXPIRATIONDATE_KEY];
[encoder encodeObject:self.refreshDate forKey:FBSDK_ACCESSTOKEN_REFRESHDATE_KEY];
}
@end

View File

@ -0,0 +1,462 @@
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import <Foundation/Foundation.h>
#import "FBSDKMacros.h"
@class FBSDKAccessToken;
@class FBSDKGraphRequest;
/*! @abstract NSNotificationCenter name indicating a result of a failed log flush attempt. The posted object will be an NSError instance. */
FBSDK_EXTERN NSString *const FBSDKAppEventsLoggingResultNotification;
/*! @abstract optional plist key ("FacebookLoggingOverrideAppID") for setting `loggingOverrideAppID` */
FBSDK_EXTERN NSString *const FBSDKAppEventsOverrideAppIDBundleKey;
/*!
@typedef NS_ENUM (NSUInteger, FBSDKAppEventsFlushBehavior)
@abstract Specifies when `FBSDKAppEvents` sends log events to the server.
*/
typedef NS_ENUM(NSUInteger, FBSDKAppEventsFlushBehavior)
{
/*! Flush automatically: periodically (once a minute or every 100 logged events) and always at app reactivation. */
FBSDKAppEventsFlushBehaviorAuto = 0,
/*! Only flush when the `flush` method is called. When an app is moved to background/terminated, the
events are persisted and re-established at activation, but they will only be written with an
explicit call to `flush`. */
FBSDKAppEventsFlushBehaviorExplicitOnly,
};
/*!
@methodgroup Predefined event names for logging events common to many apps. Logging occurs through the `logEvent` family of methods on `FBSDKAppEvents`.
Common event parameters are provided in the `FBSDKAppEventsParameterNames*` constants.
*/
/*! Log this event when the user has achieved a level in the app. */
FBSDK_EXTERN NSString *const FBSDKAppEventNameAchievedLevel;
/*! Log this event when the user has entered their payment info. */
FBSDK_EXTERN NSString *const FBSDKAppEventNameAddedPaymentInfo;
/*! Log this event when the user has added an item to their cart. The valueToSum passed to logEvent should be the item's price. */
FBSDK_EXTERN NSString *const FBSDKAppEventNameAddedToCart;
/*! Log this event when the user has added an item to their wishlist. The valueToSum passed to logEvent should be the item's price. */
FBSDK_EXTERN NSString *const FBSDKAppEventNameAddedToWishlist;
/*! Log this event when a user has completed registration with the app. */
FBSDK_EXTERN NSString *const FBSDKAppEventNameCompletedRegistration;
/*! Log this event when the user has completed a tutorial in the app. */
FBSDK_EXTERN NSString *const FBSDKAppEventNameCompletedTutorial;
/*! Log this event when the user has entered the checkout process. The valueToSum passed to logEvent should be the total price in the cart. */
FBSDK_EXTERN NSString *const FBSDKAppEventNameInitiatedCheckout;
/*! Log this event when the user has rated an item in the app. The valueToSum passed to logEvent should be the numeric rating. */
FBSDK_EXTERN NSString *const FBSDKAppEventNameRated;
/*! Log this event when a user has performed a search within the app. */
FBSDK_EXTERN NSString *const FBSDKAppEventNameSearched;
/*! Log this event when the user has spent app credits. The valueToSum passed to logEvent should be the number of credits spent. */
FBSDK_EXTERN NSString *const FBSDKAppEventNameSpentCredits;
/*! Log this event when the user has unlocked an achievement in the app. */
FBSDK_EXTERN NSString *const FBSDKAppEventNameUnlockedAchievement;
/*! Log this event when a user has viewed a form of content in the app. */
FBSDK_EXTERN NSString *const FBSDKAppEventNameViewedContent;
/*!
@methodgroup Predefined event name parameters for common additional information to accompany events logged through the `logEvent` family
of methods on `FBSDKAppEvents`. Common event names are provided in the `FBAppEventName*` constants.
*/
/*! Parameter key used to specify an ID for the specific piece of content being logged about. Could be an EAN, article identifier, etc., depending on the nature of the app. */
FBSDK_EXTERN NSString *const FBSDKAppEventParameterNameContentID;
/*! Parameter key used to specify a generic content type/family for the logged event, e.g. "music", "photo", "video". Options to use will vary based upon what the app is all about. */
FBSDK_EXTERN NSString *const FBSDKAppEventParameterNameContentType;
/*! Parameter key used to specify currency used with logged event. E.g. "USD", "EUR", "GBP". See ISO-4217 for specific values. One reference for these is <http://en.wikipedia.org/wiki/ISO_4217>. */
FBSDK_EXTERN NSString *const FBSDKAppEventParameterNameCurrency;
/*! Parameter key used to specify a description appropriate to the event being logged. E.g., the name of the achievement unlocked in the `FBAppEventNameAchievementUnlocked` event. */
FBSDK_EXTERN NSString *const FBSDKAppEventParameterNameDescription;
/*! Parameter key used to specify the level achieved in a `FBAppEventNameAchieved` event. */
FBSDK_EXTERN NSString *const FBSDKAppEventParameterNameLevel;
/*! Parameter key used to specify the maximum rating available for the `FBAppEventNameRate` event. E.g., "5" or "10". */
FBSDK_EXTERN NSString *const FBSDKAppEventParameterNameMaxRatingValue;
/*! Parameter key used to specify how many items are being processed for an `FBAppEventNameInitiatedCheckout` or `FBAppEventNamePurchased` event. */
FBSDK_EXTERN NSString *const FBSDKAppEventParameterNameNumItems;
/*! Parameter key used to specify whether payment info is available for the `FBAppEventNameInitiatedCheckout` event. `FBSDKAppEventParameterValueYes` and `FBSDKAppEventParameterValueNo` are good canonical values to use for this parameter. */
FBSDK_EXTERN NSString *const FBSDKAppEventParameterNamePaymentInfoAvailable;
/*! Parameter key used to specify method user has used to register for the app, e.g., "Facebook", "email", "Twitter", etc */
FBSDK_EXTERN NSString *const FBSDKAppEventParameterNameRegistrationMethod;
/*! Parameter key used to specify the string provided by the user for a search operation. */
FBSDK_EXTERN NSString *const FBSDKAppEventParameterNameSearchString;
/*! Parameter key used to specify whether the activity being logged about was successful or not. `FBSDKAppEventParameterValueYes` and `FBSDKAppEventParameterValueNo` are good canonical values to use for this parameter. */
FBSDK_EXTERN NSString *const FBSDKAppEventParameterNameSuccess;
/*
@methodgroup Predefined values to assign to event parameters that accompany events logged through the `logEvent` family
of methods on `FBSDKAppEvents`. Common event parameters are provided in the `FBSDKAppEventParameterName*` constants.
*/
/*! Yes-valued parameter value to be used with parameter keys that need a Yes/No value */
FBSDK_EXTERN NSString *const FBSDKAppEventParameterValueYes;
/*! No-valued parameter value to be used with parameter keys that need a Yes/No value */
FBSDK_EXTERN NSString *const FBSDKAppEventParameterValueNo;
/*!
@class FBSDKAppEvents
@abstract
Client-side event logging for specialized application analytics available through Facebook App Insights
and for use with Facebook Ads conversion tracking and optimization.
@discussion
The `FBSDKAppEvents` static class has a few related roles:
+ Logging predefined and application-defined events to Facebook App Insights with a
numeric value to sum across a large number of events, and an optional set of key/value
parameters that define "segments" for this event (e.g., 'purchaserStatus' : 'frequent', or
'gamerLevel' : 'intermediate')
+ Logging events to later be used for ads optimization around lifetime value.
+ Methods that control the way in which events are flushed out to the Facebook servers.
Here are some important characteristics of the logging mechanism provided by `FBSDKAppEvents`:
+ Events are not sent immediately when logged. They're cached and flushed out to the Facebook servers
in a number of situations:
- when an event count threshold is passed (currently 100 logged events).
- when a time threshold is passed (currently 15 seconds).
- when an app has gone to background and is then brought back to the foreground.
+ Events will be accumulated when the app is in a disconnected state, and sent when the connection is
restored and one of the above 'flush' conditions are met.
+ The `FBSDKAppEvents` class is thread-safe in that events may be logged from any of the app's threads.
+ The developer can set the `flushBehavior` on `FBSDKAppEvents` to force the flushing of events to only
occur on an explicit call to the `flush` method.
+ The developer can turn on console debug output for event logging and flushing to the server by using
the `FBSDKLoggingBehaviorAppEvents` value in `[FBSettings setLoggingBehavior:]`.
Some things to note when logging events:
+ There is a limit on the number of unique event names an app can use, on the order of 1000.
+ There is a limit to the number of unique parameter names in the provided parameters that can
be used per event, on the order of 25. This is not just for an individual call, but for all
invocations for that eventName.
+ Event names and parameter names (the keys in the NSDictionary) must be between 2 and 40 characters, and
must consist of alphanumeric characters, _, -, or spaces.
+ The length of each parameter value can be no more than on the order of 100 characters.
*/
@interface FBSDKAppEvents : NSObject
/*
* Basic event logging
*/
/*!
@abstract
Log an event with just an eventName.
@param eventName The name of the event to record. Limitations on number of events and name length
are given in the `FBSDKAppEvents` documentation.
*/
+ (void)logEvent:(NSString *)eventName;
/*!
@abstract
Log an event with an eventName and a numeric value to be aggregated with other events of this name.
@param eventName The name of the event to record. Limitations on number of events and name length
are given in the `FBSDKAppEvents` documentation. Common event names are provided in `FBAppEventName*` constants.
@param valueToSum Amount to be aggregated into all events of this eventName, and App Insights will report
the cumulative and average value of this amount.
*/
+ (void)logEvent:(NSString *)eventName
valueToSum:(double)valueToSum;
/*!
@abstract
Log an event with an eventName and a set of key/value pairs in the parameters dictionary.
Parameter limitations are described above.
@param eventName The name of the event to record. Limitations on number of events and name construction
are given in the `FBSDKAppEvents` documentation. Common event names are provided in `FBAppEventName*` constants.
@param parameters Arbitrary parameter dictionary of characteristics. The keys to this dictionary must
be NSString's, and the values are expected to be NSString or NSNumber. Limitations on the number of
parameters and name construction are given in the `FBSDKAppEvents` documentation. Commonly used parameter names
are provided in `FBSDKAppEventParameterName*` constants.
*/
+ (void)logEvent:(NSString *)eventName
parameters:(NSDictionary *)parameters;
/*!
@abstract
Log an event with an eventName, a numeric value to be aggregated with other events of this name,
and a set of key/value pairs in the parameters dictionary.
@param eventName The name of the event to record. Limitations on number of events and name construction
are given in the `FBSDKAppEvents` documentation. Common event names are provided in `FBAppEventName*` constants.
@param valueToSum Amount to be aggregated into all events of this eventName, and App Insights will report
the cumulative and average value of this amount.
@param parameters Arbitrary parameter dictionary of characteristics. The keys to this dictionary must
be NSString's, and the values are expected to be NSString or NSNumber. Limitations on the number of
parameters and name construction are given in the `FBSDKAppEvents` documentation. Commonly used parameter names
are provided in `FBSDKAppEventParameterName*` constants.
*/
+ (void)logEvent:(NSString *)eventName
valueToSum:(double)valueToSum
parameters:(NSDictionary *)parameters;
/*!
@abstract
Log an event with an eventName, a numeric value to be aggregated with other events of this name,
and a set of key/value pairs in the parameters dictionary. Providing session lets the developer
target a particular <FBSession>. If nil is provided, then `[FBSession activeSession]` will be used.
@param eventName The name of the event to record. Limitations on number of events and name construction
are given in the `FBSDKAppEvents` documentation. Common event names are provided in `FBAppEventName*` constants.
@param valueToSum Amount to be aggregated into all events of this eventName, and App Insights will report
the cumulative and average value of this amount. Note that this is an NSNumber, and a value of `nil` denotes
that this event doesn't have a value associated with it for summation.
@param parameters Arbitrary parameter dictionary of characteristics. The keys to this dictionary must
be NSString's, and the values are expected to be NSString or NSNumber. Limitations on the number of
parameters and name construction are given in the `FBSDKAppEvents` documentation. Commonly used parameter names
are provided in `FBSDKAppEventParameterName*` constants.
@param accessToken The optional access token to log the event as.
*/
+ (void)logEvent:(NSString *)eventName
valueToSum:(NSNumber *)valueToSum
parameters:(NSDictionary *)parameters
accessToken:(FBSDKAccessToken *)accessToken;
/*
* Purchase logging
*/
/*!
@abstract
Log a purchase of the specified amount, in the specified currency.
@param purchaseAmount Purchase amount to be logged, as expressed in the specified currency. This value
will be rounded to the thousandths place (e.g., 12.34567 becomes 12.346).
@param currency Currency, is denoted as, e.g. "USD", "EUR", "GBP". See ISO-4217 for
specific values. One reference for these is <http://en.wikipedia.org/wiki/ISO_4217>.
@discussion This event immediately triggers a flush of the `FBSDKAppEvents` event queue, unless the `flushBehavior` is set
to `FBSDKAppEventsFlushBehaviorExplicitOnly`.
*/
+ (void)logPurchase:(double)purchaseAmount
currency:(NSString *)currency;
/*!
@abstract
Log a purchase of the specified amount, in the specified currency, also providing a set of
additional characteristics describing the purchase.
@param purchaseAmount Purchase amount to be logged, as expressed in the specified currency.This value
will be rounded to the thousandths place (e.g., 12.34567 becomes 12.346).
@param currency Currency, is denoted as, e.g. "USD", "EUR", "GBP". See ISO-4217 for
specific values. One reference for these is <http://en.wikipedia.org/wiki/ISO_4217>.
@param parameters Arbitrary parameter dictionary of characteristics. The keys to this dictionary must
be NSString's, and the values are expected to be NSString or NSNumber. Limitations on the number of
parameters and name construction are given in the `FBSDKAppEvents` documentation. Commonly used parameter names
are provided in `FBSDKAppEventParameterName*` constants.
@discussion This event immediately triggers a flush of the `FBSDKAppEvents` event queue, unless the `flushBehavior` is set
to `FBSDKAppEventsFlushBehaviorExplicitOnly`.
*/
+ (void)logPurchase:(double)purchaseAmount
currency:(NSString *)currency
parameters:(NSDictionary *)parameters;
/*!
@abstract
Log a purchase of the specified amount, in the specified currency, also providing a set of
additional characteristics describing the purchase, as well as an <FBSession> to log to.
@param purchaseAmount Purchase amount to be logged, as expressed in the specified currency.This value
will be rounded to the thousandths place (e.g., 12.34567 becomes 12.346).
@param currency Currency, is denoted as, e.g. "USD", "EUR", "GBP". See ISO-4217 for
specific values. One reference for these is <http://en.wikipedia.org/wiki/ISO_4217>.
@param parameters Arbitrary parameter dictionary of characteristics. The keys to this dictionary must
be NSString's, and the values are expected to be NSString or NSNumber. Limitations on the number of
parameters and name construction are given in the `FBSDKAppEvents` documentation. Commonly used parameter names
are provided in `FBSDKAppEventParameterName*` constants.
@param accessToken The optional access token to log the event as.
@discussion This event immediately triggers a flush of the `FBSDKAppEvents` event queue, unless the `flushBehavior` is set
to `FBSDKAppEventsFlushBehaviorExplicitOnly`.
*/
+ (void)logPurchase:(double)purchaseAmount
currency:(NSString *)currency
parameters:(NSDictionary *)parameters
accessToken:(FBSDKAccessToken *)accessToken;
/*!
@abstract
Notifies the events system that the app has launched and, when appropriate, logs an "activated app" event. Should typically be placed in the
app delegates' `applicationDidBecomeActive:` method.
This method also takes care of logging the event indicating the first time this app has been launched, which, among other things, is used to
track user acquisition and app install ads conversions.
@discussion
`activateApp` will not log an event on every app launch, since launches happen every time the app is backgrounded and then foregrounded.
"activated app" events will be logged when the app has not been active for more than 60 seconds. This method also causes a "deactivated app"
event to be logged when sessions are "completed", and these events are logged with the session length, with an indication of how much
time has elapsed between sessions, and with the number of background/foreground interruptions that session had. This data
is all visible in your app's App Events Insights.
*/
+ (void)activateApp;
/*
* Control over event batching/flushing
*/
/*!
@abstract
Get the current event flushing behavior specifying when events are sent back to Facebook servers.
*/
+ (FBSDKAppEventsFlushBehavior)flushBehavior;
/*!
@abstract
Set the current event flushing behavior specifying when events are sent back to Facebook servers.
@param flushBehavior The desired `FBSDKAppEventsFlushBehavior` to be used.
*/
+ (void)setFlushBehavior:(FBSDKAppEventsFlushBehavior)flushBehavior;
/*!
@abstract
Set the 'override' App ID for App Event logging.
@discussion
In some cases, apps want to use one Facebook App ID for login and social presence and another
for App Event logging. (An example is if multiple apps from the same company share an app ID for login, but
want distinct logging.) By default, this value is `nil`, and defers to the `FBSDKAppEventsOverrideAppIDBundleKey`
plist value. If that's not set, it defaults to `[FBSDKSettings appID]`.
This should be set before any other calls are made to `FBSDKAppEvents`. Thus, you should set it in your application
delegate's `application:didFinishLaunchingWithOptions:` delegate.
@param appID The Facebook App ID to be used for App Event logging.
*/
+ (void)setLoggingOverrideAppID:(NSString *)appID;
/*!
@abstract
Get the 'override' App ID for App Event logging.
@see setLoggingOverrideAppID:
*/
+ (NSString *)loggingOverrideAppID;
/*!
@abstract
Explicitly kick off flushing of events to Facebook. This is an asynchronous method, but it does initiate an immediate
kick off. Server failures will be reported through the NotificationCenter with notification ID `FBSDKAppEventsLoggingResultNotification`.
*/
+ (void)flush;
/*!
@abstract
Creates a request representing the Graph API call to retrieve a Custom Audience "third party ID" for the app's Facebook user.
Callers will send this ID back to their own servers, collect up a set to create a Facebook Custom Audience with,
and then use the resultant Custom Audience to target ads.
@param accessToken The access token to use to establish the user's identity for users logged into Facebook through this app.
If `nil`, then the `[FBSDKAccessToken currentAccessToken]` is used.
@discussion
The JSON in the request's response will include an "custom_audience_third_party_id" key/value pair, with the value being the ID retrieved.
This ID is an encrypted encoding of the Facebook user's ID and the invoking Facebook app ID.
Multiple calls with the same user will return different IDs, thus these IDs cannot be used to correlate behavior
across devices or applications, and are only meaningful when sent back to Facebook for creating Custom Audiences.
The ID retrieved represents the Facebook user identified in the following way: if the specified access token is valid,
the ID will represent the user associated with that token; otherwise the ID will represent the user logged into the
native Facebook app on the device. If there is no native Facebook app, no one is logged into it, or the user has opted out
at the iOS level from ad tracking, then a `nil` ID will be returned.
This method returns `nil` if either the user has opted-out (via iOS) from Ad Tracking, the app itself has limited event usage
via the `[FBSDKSettings limitEventAndDataUsage]` flag, or a specific Facebook user cannot be identified.
*/
+ (FBSDKGraphRequest *)requestForCustomAudienceThirdPartyIDWithAccessToken:(FBSDKAccessToken *)accessToken;
@end

View File

@ -0,0 +1,832 @@
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import "FBSDKAppEvents.h"
#import "FBSDKAppEvents+Internal.h"
#import <UIKit/UIApplication.h>
#import "FBSDKAccessToken.h"
#import "FBSDKAppEventsState.h"
#import "FBSDKAppEventsStateManager.h"
#import "FBSDKAppEventsUtility.h"
#import "FBSDKConstants.h"
#import "FBSDKError.h"
#import "FBSDKGraphRequest+Internal.h"
#import "FBSDKInternalUtility.h"
#import "FBSDKLogger.h"
#import "FBSDKPaymentObserver.h"
#import "FBSDKServerConfiguration.h"
#import "FBSDKServerConfigurationManager.h"
#import "FBSDKSettings.h"
#import "FBSDKTimeSpentData.h"
#import "FBSDKUtility.h"
//
// Public event names
//
// General purpose
NSString *const FBSDKAppEventNameCompletedRegistration = @"fb_mobile_complete_registration";
NSString *const FBSDKAppEventNameViewedContent = @"fb_mobile_content_view";
NSString *const FBSDKAppEventNameSearched = @"fb_mobile_search";
NSString *const FBSDKAppEventNameRated = @"fb_mobile_rate";
NSString *const FBSDKAppEventNameCompletedTutorial = @"fb_mobile_tutorial_completion";
NSString *const FBSDKAppEventParameterLaunchSource = @"fb_mobile_launch_source";
// Ecommerce related
NSString *const FBSDKAppEventNameAddedToCart = @"fb_mobile_add_to_cart";
NSString *const FBSDKAppEventNameAddedToWishlist = @"fb_mobile_add_to_wishlist";
NSString *const FBSDKAppEventNameInitiatedCheckout = @"fb_mobile_initiated_checkout";
NSString *const FBSDKAppEventNameAddedPaymentInfo = @"fb_mobile_add_payment_info";
// Gaming related
NSString *const FBSDKAppEventNameAchievedLevel = @"fb_mobile_level_achieved";
NSString *const FBSDKAppEventNameUnlockedAchievement = @"fb_mobile_achievement_unlocked";
NSString *const FBSDKAppEventNameSpentCredits = @"fb_mobile_spent_credits";
//
// Public event parameter names
//
NSString *const FBSDKAppEventParameterNameCurrency = @"fb_currency";
NSString *const FBSDKAppEventParameterNameRegistrationMethod = @"fb_registration_method";
NSString *const FBSDKAppEventParameterNameContentType = @"fb_content_type";
NSString *const FBSDKAppEventParameterNameContentID = @"fb_content_id";
NSString *const FBSDKAppEventParameterNameSearchString = @"fb_search_string";
NSString *const FBSDKAppEventParameterNameSuccess = @"fb_success";
NSString *const FBSDKAppEventParameterNameMaxRatingValue = @"fb_max_rating_value";
NSString *const FBSDKAppEventParameterNamePaymentInfoAvailable = @"fb_payment_info_available";
NSString *const FBSDKAppEventParameterNameNumItems = @"fb_num_items";
NSString *const FBSDKAppEventParameterNameLevel = @"fb_level";
NSString *const FBSDKAppEventParameterNameDescription = @"fb_description";
//
// Public event parameter values
//
NSString *const FBSDKAppEventParameterValueNo = @"0";
NSString *const FBSDKAppEventParameterValueYes = @"1";
//
// Event names internal to this file
//
NSString *const FBSDKAppEventNamePurchased = @"fb_mobile_purchase";
NSString *const FBSDKAppEventNameLoginViewUsage = @"fb_login_view_usage";
NSString *const FBSDKAppEventNameShareSheetLaunch = @"fb_share_sheet_launch";
NSString *const FBSDKAppEventNameShareSheetDismiss = @"fb_share_sheet_dismiss";
NSString *const FBSDKAppEventNamePermissionsUILaunch = @"fb_permissions_ui_launch";
NSString *const FBSDKAppEventNamePermissionsUIDismiss = @"fb_permissions_ui_dismiss";
NSString *const FBSDKAppEventNameFBDialogsPresentShareDialog = @"fb_dialogs_present_share";
NSString *const FBSDKAppEventNameFBDialogsPresentShareDialogPhoto = @"fb_dialogs_present_share_photo";
NSString *const FBSDKAppEventNameFBDialogsPresentShareDialogOG = @"fb_dialogs_present_share_og";
NSString *const FBSDKAppEventNameFBDialogsPresentLikeDialogOG = @"fb_dialogs_present_like_og";
NSString *const FBSDKAppEventNameFBDialogsPresentMessageDialog = @"fb_dialogs_present_message";
NSString *const FBSDKAppEventNameFBDialogsPresentMessageDialogPhoto = @"fb_dialogs_present_message_photo";
NSString *const FBSDKAppEventNameFBDialogsPresentMessageDialogOG = @"fb_dialogs_present_message_og";
NSString *const FBSDKAppEventNameFBDialogsNativeLoginDialogStart = @"fb_dialogs_native_login_dialog_start";
NSString *const FBSDKAppEventsNativeLoginDialogStartTime = @"fb_native_login_dialog_start_time";
NSString *const FBSDKAppEventNameFBDialogsNativeLoginDialogEnd = @"fb_dialogs_native_login_dialog_end";
NSString *const FBSDKAppEventsNativeLoginDialogEndTime = @"fb_native_login_dialog_end_time";
NSString *const FBSDKAppEventNameFBDialogsWebLoginCompleted = @"fb_dialogs_web_login_dialog_complete";
NSString *const FBSDKAppEventsWebLoginE2E = @"fb_web_login_e2e";
NSString *const FBSDKAppEventNameFBSessionAuthStart = @"fb_mobile_login_start";
NSString *const FBSDKAppEventNameFBSessionAuthEnd = @"fb_mobile_login_complete";
NSString *const FBSDKAppEventNameFBSessionAuthMethodStart = @"fb_mobile_login_method_start";
NSString *const FBSDKAppEventNameFBSessionAuthMethodEnd = @"fb_mobile_login_method_complete";
NSString *const FBSDKAppEventNameFBSDKLikeButtonImpression = @"fb_like_button_impression";
NSString *const FBSDKAppEventNameFBSDKLoginButtonImpression = @"fb_login_button_impression";
NSString *const FBSDKAppEventNameFBSDKSendButtonImpression = @"fb_send_button_impression";
NSString *const FBSDKAppEventNameFBSDKShareButtonImpression = @"fb_share_button_impression";
NSString *const FBSDKAppEventNameFBSDKLikeButtonDidTap = @"fb_like_button_did_tap";
NSString *const FBSDKAppEventNameFBSDKLoginButtonDidTap = @"fb_login_button_did_tap";
NSString *const FBSDKAppEventNameFBSDKSendButtonDidTap = @"fb_send_button_did_tap";
NSString *const FBSDKAppEventNameFBSDKShareButtonDidTap = @"fb_share_button_did_tap";
NSString *const FBSDKAppEventNameFBSDKLikeControlDidDisable = @"fb_like_control_did_disable";
NSString *const FBSDKAppEventNameFBSDKLikeControlDidLike = @"fb_like_control_did_like";
NSString *const FBSDKAppEventNameFBSDKLikeControlDidPresentDialog = @"fb_like_control_did_present_dialog";
NSString *const FBSDKAppEventNameFBSDKLikeControlDidTap = @"fb_like_control_did_tap";
NSString *const FBSDKAppEventNameFBSDKLikeControlDidUnlike = @"fb_like_control_did_unlike";
NSString *const FBSDKAppEventNameFBSDKLikeControlError = @"fb_like_control_error";
NSString *const FBSDKAppEventNameFBSDKLikeControlImpression = @"fb_like_control_impression";
NSString *const FBSDKAppEventNameFBSDKLikeControlNetworkUnavailable = @"fb_like_control_network_unavailable";
NSString *const FBSDLAppEventNameFBSDKEventShareDialogResult = @"fb_dialog_share_result";
NSString *const FBSDKAppEventNameFBSDKEventMessengerShareDialogResult = @"fb_messenger_dialog_share_result";
NSString *const FBSDKAppEventNameFBSDKEventAppInviteShareDialogResult = @"fb_app_invite_dialog_share_result";
NSString *const FBSDKAppEventNameFBSDKEventShareDialogShow = @"fb_dialog_share_show";
NSString *const FBSDKAppEventNameFBSDKEventMessengerShareDialogShow = @"fb_messenger_dialog_share_show";
NSString *const FBSDKAppEventNameFBSDKEventAppInviteShareDialogShow = @"fb_app_invite_share_show";
// Event Parameters internal to this file
NSString *const FBSDKAppEventParameterDialogOutcome = @"fb_dialog_outcome";
NSString *const FBSDKAppEventParameterDialogErrorMessage = @"fb_dialog_outcome_error_message";
NSString *const FBSDKAppEventParameterDialogMode = @"fb_dialog_mode";
NSString *const FBSDKAppEventParameterDialogShareContentType = @"fb_dialog_share_content_type";
// Event parameter values internal to this file
NSString *const FBSDKAppEventsDialogOutcomeValue_Completed = @"Completed";
NSString *const FBSDKAppEventsDialogOutcomeValue_Cancelled = @"Cancelled";
NSString *const FBSDKAppEventsDialogOutcomeValue_Failed = @"Failed";
NSString *const FBSDKAppEventsDialogShareModeAutomatic = @"Automatic";
NSString *const FBSDKAppEventsDialogShareModeBrowser = @"Browser";
NSString *const FBSDKAppEventsDialogShareModeNative = @"Native";
NSString *const FBSDKAppEventsDialogShareModeShareSheet = @"ShareSheet";
NSString *const FBSDKAppEventsDialogShareModeWeb = @"Web";
NSString *const FBSDKAppEventsDialogShareModeFeedBrowser = @"FeedBrowser";
NSString *const FBSDKAppEventsDialogShareModeFeedWeb = @"FeedWeb";
NSString *const FBSDKAppEventsDialogShareModeUnknown = @"Unknown";
NSString *const FBSDKAppEventsDialogShareContentTypeOpenGraph = @"OpenGraph";
NSString *const FBSDKAppEventsDialogShareContentTypeStatus = @"Status";
NSString *const FBSDKAppEventsDialogShareContentTypePhoto = @"Photo";
NSString *const FBSDKAppEventsDialogShareContentTypeVideo = @"Video";
NSString *const FBSDKAppEventsDialogShareContentTypeUnknown = @"Unknown";
NSString *const FBSDKAppEventsLoggingResultNotification = @"com.facebook.sdk:FBSDKAppEventsLoggingResultNotification";
NSString *const FBSDKAppEventsOverrideAppIDBundleKey = @"FacebookLoggingOverrideAppID";
#define NUM_LOG_EVENTS_TO_TRY_TO_FLUSH_AFTER 100
#define FLUSH_PERIOD_IN_SECONDS 15
#define APP_SUPPORTS_ATTRIBUTION_ID_RECHECK_PERIOD 60 * 60 * 24
static NSString *g_overrideAppID = nil;
@interface FBSDKAppEvents ()
@property (nonatomic, readwrite) FBSDKAppEventsFlushBehavior flushBehavior;
//for testing only.
@property (nonatomic, assign) BOOL disableTimer;
@end
@implementation FBSDKAppEvents
{
BOOL _explicitEventsLoggedYet;
NSTimer *_flushTimer;
NSTimer *_attributionIDRecheckTimer;
FBSDKServerConfiguration *_serverConfiguration;
FBSDKAppEventsState *_appEventsState;
}
#pragma mark - Object Lifecycle
+ (void)initialize
{
if (self == [FBSDKAppEvents class]) {
g_overrideAppID = [[[NSBundle mainBundle] objectForInfoDictionaryKey:FBSDKAppEventsOverrideAppIDBundleKey] copy];
}
}
- (FBSDKAppEvents *)init
{
self = [super init];
if (self) {
_flushBehavior = FBSDKAppEventsFlushBehaviorAuto;
_flushTimer = [NSTimer timerWithTimeInterval:FLUSH_PERIOD_IN_SECONDS
target:self
selector:@selector(flushTimerFired:)
userInfo:nil
repeats:YES];
_attributionIDRecheckTimer = [NSTimer timerWithTimeInterval:APP_SUPPORTS_ATTRIBUTION_ID_RECHECK_PERIOD
target:self
selector:@selector(appSettingsFetchStateResetTimerFired:)
userInfo:nil
repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:_flushTimer forMode:NSDefaultRunLoopMode];
[[NSRunLoop mainRunLoop] addTimer:_attributionIDRecheckTimer forMode:NSDefaultRunLoopMode];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(applicationMovingFromActiveStateOrTerminating)
name:UIApplicationWillResignActiveNotification
object:NULL];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(applicationMovingFromActiveStateOrTerminating)
name:UIApplicationWillTerminateNotification
object:NULL];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(applicationDidBecomeActive)
name:UIApplicationDidBecomeActiveNotification
object:NULL];
}
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
// technically these timers retain self so there's a cycle but
// we're a singleton anyway.
[_flushTimer invalidate];
[_attributionIDRecheckTimer invalidate];
}
#pragma mark - Public Methods
+ (void)logEvent:(NSString *)eventName
{
[FBSDKAppEvents logEvent:eventName
parameters:nil];
}
+ (void)logEvent:(NSString *)eventName
valueToSum:(double)valueToSum
{
[FBSDKAppEvents logEvent:eventName
valueToSum:valueToSum
parameters:nil];
}
+ (void)logEvent:(NSString *)eventName
parameters:(NSDictionary *)parameters
{
[FBSDKAppEvents logEvent:eventName
valueToSum:nil
parameters:parameters
accessToken:nil];
}
+ (void)logEvent:(NSString *)eventName
valueToSum:(double)valueToSum
parameters:(NSDictionary *)parameters
{
[FBSDKAppEvents logEvent:eventName
valueToSum:[NSNumber numberWithDouble:valueToSum]
parameters:parameters
accessToken:nil];
}
+ (void)logEvent:(NSString *)eventName
valueToSum:(NSNumber *)valueToSum
parameters:(NSDictionary *)parameters
accessToken:(FBSDKAccessToken *)accessToken
{
[[FBSDKAppEvents singleton] instanceLogEvent:eventName
valueToSum:valueToSum
parameters:parameters
isImplicitlyLogged:NO
accessToken:accessToken];
}
+ (void)logPurchase:(double)purchaseAmount
currency:(NSString *)currency
{
[FBSDKAppEvents logPurchase:purchaseAmount
currency:currency
parameters:nil];
}
+ (void)logPurchase:(double)purchaseAmount
currency:(NSString *)currency
parameters:(NSDictionary *)parameters
{
[FBSDKAppEvents logPurchase:purchaseAmount
currency:currency
parameters:parameters
accessToken:nil];
}
+ (void)logPurchase:(double)purchaseAmount
currency:(NSString *)currency
parameters:(NSDictionary *)parameters
accessToken:(FBSDKAccessToken *)accessToken
{
// A purchase event is just a regular logged event with a given event name
// and treating the currency value as going into the parameters dictionary.
NSDictionary *newParameters;
if (!parameters) {
newParameters = @{ FBSDKAppEventParameterNameCurrency : currency };
} else {
newParameters = [NSMutableDictionary dictionaryWithDictionary:parameters];
[newParameters setValue:currency forKey:FBSDKAppEventParameterNameCurrency];
}
[FBSDKAppEvents logEvent:FBSDKAppEventNamePurchased
valueToSum:[NSNumber numberWithDouble:purchaseAmount]
parameters:newParameters
accessToken:accessToken];
// Unless the behavior is set to only allow explicit flushing, we go ahead and flush, since purchase events
// are relatively rare and relatively high value and worth getting across on wire right away.
if ([FBSDKAppEvents flushBehavior] != FBSDKAppEventsFlushBehaviorExplicitOnly) {
[[FBSDKAppEvents singleton] flushForReason:FBSDKAppEventsFlushReasonEagerlyFlushingEvent];
}
}
+ (void)activateApp
{
[FBSDKAppEventsUtility ensureOnMainThread:NSStringFromSelector(_cmd) className:NSStringFromClass(self)];
// Fetch app settings and register for transaction notifications only if app supports implicit purchase
// events
FBSDKAppEvents *instance = [FBSDKAppEvents singleton];
[instance publishInstall];
[instance fetchServerConfiguration:NULL];
// Restore time spent data, indicating that we're being called from "activateApp", which will,
// when appropriate, result in logging an "activated app" and "deactivated app" (for the
// previous session) App Event.
[FBSDKTimeSpentData restore:YES];
}
+ (FBSDKAppEventsFlushBehavior)flushBehavior
{
return [FBSDKAppEvents singleton].flushBehavior;
}
+ (void)setFlushBehavior:(FBSDKAppEventsFlushBehavior)flushBehavior
{
[FBSDKAppEvents singleton].flushBehavior = flushBehavior;
}
+ (NSString *)loggingOverrideAppID
{
return g_overrideAppID;
}
+ (void)setLoggingOverrideAppID:(NSString *)appID
{
if (![g_overrideAppID isEqualToString:appID]) {
FBSDKConditionalLog(![FBSDKAppEvents singleton]->_explicitEventsLoggedYet,
FBSDKLoggingBehaviorDeveloperErrors,
@"[FBSDKAppEvents setLoggingOverrideAppID:] should only be called prior to any events being logged.");
g_overrideAppID = appID;
}
}
+ (void)flush
{
[[FBSDKAppEvents singleton] flushForReason:FBSDKAppEventsFlushReasonExplicit];
}
#pragma mark - Internal Methods
+ (void)logImplicitEvent:(NSString *)eventName
valueToSum:(NSNumber *)valueToSum
parameters:(NSDictionary *)parameters
accessToken:(FBSDKAccessToken *)accessToken
{
[[FBSDKAppEvents singleton] instanceLogEvent:eventName
valueToSum:valueToSum
parameters:parameters
isImplicitlyLogged:YES
accessToken:accessToken];
}
+ (FBSDKAppEvents *)singleton
{
static dispatch_once_t pred;
static FBSDKAppEvents *shared = nil;
dispatch_once(&pred, ^{
shared = [[FBSDKAppEvents alloc] init];
});
return shared;
}
- (void)flushForReason:(FBSDKAppEventsFlushReason)flushReason
{
// Always flush asynchronously, even on main thread, for two reasons:
// - most consistent code path for all threads.
// - allow locks being held by caller to be released prior to actual flushing work being done.
@synchronized (self) {
if (!_appEventsState) {
return;
}
FBSDKAppEventsState *copy = [_appEventsState copy];
_appEventsState = [[FBSDKAppEventsState alloc] initWithToken:copy.tokenString
appID:copy.appID];
dispatch_async(dispatch_get_main_queue(), ^{
[self flushOnMainQueue:copy forReason:flushReason];
});
}
}
#pragma mark - Private Methods
- (NSString *)appID
{
return [FBSDKAppEvents loggingOverrideAppID] ?: [FBSDKSettings appID];
}
- (void)publishInstall
{
NSString *appID = [self appID];
NSString *lastAttributionPingString = [NSString stringWithFormat:@"com.facebook.sdk:lastAttributionPing%@", appID];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:lastAttributionPingString]) {
return;
}
[self fetchServerConfiguration:^{
NSDictionary *params = [FBSDKAppEventsUtility activityParametersDictionaryForEvent:@"MOBILE_APP_INSTALL"
implicitEventsOnly:NO
shouldAccessAdvertisingID:_serverConfiguration.isAdvertisingIDEnabled];
NSString *path = [NSString stringWithFormat:@"%@/activities", appID];
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:path
parameters:params
tokenString:nil
HTTPMethod:@"POST"
flags:FBSDKGraphRequestFlagDoNotInvalidateTokenOnError | FBSDKGraphRequestFlagDisableErrorRecovery];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
[defaults setObject:[NSDate date] forKey:lastAttributionPingString];
NSString *lastInstallResponseKey = [NSString stringWithFormat:@"com.facebook.sdk:lastInstallResponse%@", appID];
[defaults setObject:result forKey:lastInstallResponseKey];
[defaults synchronize];
}
}];
}];
}
// app events can use a server configuration up to 24 hours old to minimize network traffic.
- (void)fetchServerConfiguration:(void (^)(void))callback
{
if (_serverConfiguration == nil) {
[FBSDKServerConfigurationManager loadServerConfigurationWithCompletionBlock:^(FBSDKServerConfiguration *serverConfiguration, NSError *error) {
_serverConfiguration = serverConfiguration;
if (_serverConfiguration.implicitPurchaseLoggingEnabled) {
[FBSDKPaymentObserver startObservingTransactions];
} else {
[FBSDKPaymentObserver stopObservingTransactions];
}
if (callback) {
callback();
}
}];
return;
}
if (callback) {
callback();
}
}
- (void)instanceLogEvent:(NSString *)eventName
valueToSum:(NSNumber *)valueToSum
parameters:(NSDictionary *)parameters
isImplicitlyLogged:(BOOL)isImplicitlyLogged
accessToken:(FBSDKAccessToken *)accessToken
{
if (isImplicitlyLogged && _serverConfiguration && !_serverConfiguration.isImplicitLoggingSupported) {
return;
}
if (!isImplicitlyLogged && !_explicitEventsLoggedYet) {
_explicitEventsLoggedYet = YES;
}
__block BOOL failed = NO;
if (![FBSDKAppEventsUtility validateIdentifier:eventName]) {
failed = YES;
}
// Make sure parameter dictionary is well formed. Log and exit if not.
[parameters enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
if (![key isKindOfClass:[NSString class]]) {
[FBSDKAppEventsUtility logAndNotify:[NSString stringWithFormat:@"The keys in the parameters must be NSStrings, '%@' is not.", key]];
failed = YES;
}
if (![FBSDKAppEventsUtility validateIdentifier:key]) {
failed = YES;
}
if (![obj isKindOfClass:[NSString class]] && ![obj isKindOfClass:[NSNumber class]]) {
[FBSDKAppEventsUtility logAndNotify:[NSString stringWithFormat:@"The values in the parameters dictionary must be NSStrings or NSNumbers, '%@' is not.", obj]];
failed = YES;
}
}
];
if (failed) {
return;
}
NSMutableDictionary *eventDictionary = [NSMutableDictionary dictionaryWithDictionary:parameters];
eventDictionary[@"_eventName"] = eventName;
eventDictionary[@"_logTime"] = @([FBSDKAppEventsUtility unixTimeNow]);
[FBSDKInternalUtility dictionary:eventDictionary setObject:valueToSum forKey:@"_valueToSum"];
if (isImplicitlyLogged) {
eventDictionary[@"_implicitlyLogged"] = @"1";
}
NSString *currentViewControllerName;
if ([NSThread isMainThread]) {
// We only collect the view controller when on the main thread, as the behavior off
// the main thread is unpredictable. Besides, UI state for off-main-thread computations
// isn't really relevant anyhow.
UIViewController *vc = [UIApplication sharedApplication].keyWindow.rootViewController.presentedViewController;
if (vc) {
currentViewControllerName = [[vc class] description];
} else {
currentViewControllerName = @"no_ui";
}
} else {
currentViewControllerName = @"off_thread";
}
eventDictionary[@"_ui"] = currentViewControllerName;
NSString *tokenString = [FBSDKAppEventsUtility tokenStringToUseFor:accessToken];
NSString *appID = [self appID];
@synchronized (self) {
if (!_appEventsState) {
_appEventsState = [[FBSDKAppEventsState alloc] initWithToken:tokenString appID:appID];
} else if (![_appEventsState isCompatibleWithTokenString:tokenString appID:appID]) {
if (self.flushBehavior == FBSDKAppEventsFlushBehaviorExplicitOnly) {
[FBSDKAppEventsStateManager persistAppEventsData:_appEventsState];
} else {
[self flushForReason:FBSDKAppEventsFlushReasonSessionChange];
}
_appEventsState = [[FBSDKAppEventsState alloc] initWithToken:tokenString appID:appID];
}
[_appEventsState addEvent:eventDictionary isImplicit:isImplicitlyLogged];
if (!isImplicitlyLogged) {
[FBSDKLogger singleShotLogEntry:FBSDKLoggingBehaviorAppEvents
formatString:@"FBSDKAppEvents: Recording event @ %ld: %@",
[FBSDKAppEventsUtility unixTimeNow],
eventDictionary];
}
[self checkPersistedEvents];
if (_appEventsState.events.count > NUM_LOG_EVENTS_TO_TRY_TO_FLUSH_AFTER &&
self.flushBehavior != FBSDKAppEventsFlushBehaviorExplicitOnly) {
[self flushForReason:FBSDKAppEventsFlushReasonEventThreshold];
}
}
}
// this fetches persisted event states.
// for those matching the currently tracked events, add it.
// otherwise, either flush (if not explicitonly behavior) or persist them back.
- (void)checkPersistedEvents
{
NSArray *existingEventsStates = [FBSDKAppEventsStateManager retrievePersistedAppEventsStates];
if (existingEventsStates.count == 0) {
return;
}
FBSDKAppEventsState *matchingEventsPreviouslySaved = nil;
// reduce lock time by creating a new FBSDKAppEventsState to collect matching persisted events.
@synchronized(self) {
if (_appEventsState) {
matchingEventsPreviouslySaved = [[FBSDKAppEventsState alloc] initWithToken:_appEventsState.tokenString
appID:_appEventsState.appID];
}
}
for (FBSDKAppEventsState *saved in existingEventsStates) {
if ([saved isCompatibleWithAppEventsState:matchingEventsPreviouslySaved]) {
[matchingEventsPreviouslySaved addEventsFromAppEventState:saved];
} else {
if (self.flushBehavior == FBSDKAppEventsFlushBehaviorExplicitOnly) {
[FBSDKAppEventsStateManager persistAppEventsData:saved];
} else {
dispatch_async(dispatch_get_main_queue(), ^{
[self flushOnMainQueue:saved forReason:FBSDKAppEventsFlushReasonPersistedEvents];
});
}
}
}
if (matchingEventsPreviouslySaved.events.count > 0) {
@synchronized(self) {
if ([_appEventsState isCompatibleWithAppEventsState:matchingEventsPreviouslySaved]) {
[_appEventsState addEventsFromAppEventState:matchingEventsPreviouslySaved];
}
}
}
}
- (void)flushOnMainQueue:(FBSDKAppEventsState *)appEventsState
forReason:(FBSDKAppEventsFlushReason)reason
{
if (appEventsState.events.count == 0) {
return;
}
[FBSDKAppEventsUtility ensureOnMainThread:NSStringFromSelector(_cmd) className:NSStringFromClass([self class])];
[self fetchServerConfiguration:^(void) {
NSString *JSONString = [appEventsState JSONStringForEvents:_serverConfiguration.implicitLoggingEnabled];
NSData *encodedEvents = [JSONString dataUsingEncoding:NSUTF8StringEncoding];
if (!encodedEvents) {
[FBSDKLogger singleShotLogEntry:FBSDKLoggingBehaviorAppEvents
logEntry:@"FBSDKAppEvents: Flushing skipped - no events after removing implicitly logged ones.\n"];
return;
}
NSMutableDictionary *postParameters = [FBSDKAppEventsUtility
activityParametersDictionaryForEvent:@"CUSTOM_APP_EVENTS"
implicitEventsOnly:appEventsState.areAllEventsImplicit
shouldAccessAdvertisingID:_serverConfiguration.advertisingIDEnabled];
postParameters[@"custom_events_file"] = encodedEvents;
if (appEventsState.numSkipped > 0) {
postParameters[@"num_skipped_events"] = [NSString stringWithFormat:@"%lu", (unsigned long)appEventsState.numSkipped];
}
NSString *loggingEntry = nil;
if ([[FBSDKSettings loggingBehavior] containsObject:FBSDKLoggingBehaviorAppEvents]) {
NSData *prettyJSONData = [NSJSONSerialization dataWithJSONObject:appEventsState.events
options:NSJSONWritingPrettyPrinted
error:NULL];
NSString *prettyPrintedJsonEvents = [[NSString alloc] initWithData:prettyJSONData
encoding:NSUTF8StringEncoding];
// Remove this param -- just an encoding of the events which we pretty print later.
NSMutableDictionary *paramsForPrinting = [postParameters mutableCopy];
[paramsForPrinting removeObjectForKey:@"custom_events_file"];
loggingEntry = [NSString stringWithFormat:@"FBSDKAppEvents: Flushed @ %ld, %lu events due to '%@' - %@\nEvents: %@",
[FBSDKAppEventsUtility unixTimeNow],
(unsigned long)appEventsState.events.count,
[FBSDKAppEventsUtility flushReasonToString:reason],
paramsForPrinting,
prettyPrintedJsonEvents];
}
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:[NSString stringWithFormat:@"%@/activities", appEventsState.appID]
parameters:postParameters
tokenString:appEventsState.tokenString
HTTPMethod:@"POST"
flags:FBSDKGraphRequestFlagDoNotInvalidateTokenOnError | FBSDKGraphRequestFlagDisableErrorRecovery];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
[self handleActivitiesPostCompletion:error
loggingEntry:loggingEntry
appEventsState:(FBSDKAppEventsState *)appEventsState];
}];
}];
}
- (void)handleActivitiesPostCompletion:(NSError *)error
loggingEntry:(NSString *)loggingEntry
appEventsState:(FBSDKAppEventsState *)appEventsState
{
typedef NS_ENUM(NSUInteger, FBSDKAppEventsFlushResult) {
FlushResultSuccess,
FlushResultServerError,
FlushResultNoConnectivity
};
[FBSDKAppEventsUtility ensureOnMainThread:NSStringFromSelector(_cmd) className:NSStringFromClass([self class])];
FBSDKAppEventsFlushResult flushResult = FlushResultSuccess;
if (error) {
NSInteger errorCode = [error.userInfo[FBSDKGraphRequestErrorHTTPStatusCodeKey] integerValue];
// We interpret a 400 coming back from FBRequestConnection as a server error due to improper data being
// sent down. Otherwise we assume no connectivity, or another condition where we could treat it as no connectivity.
flushResult = errorCode == 400 ? FlushResultServerError : FlushResultNoConnectivity;
}
if (flushResult == FlushResultServerError) {
// Only log events that developer can do something with (i.e., if parameters are incorrect).
// as opposed to cases where the token is bad.
if ([error.userInfo[FBSDKGraphRequestErrorCategoryKey] unsignedIntegerValue] == FBSDKGraphRequestErrorCategoryOther) {
NSString *message = [NSString stringWithFormat:@"Failed to send AppEvents: %@", error];
[FBSDKAppEventsUtility logAndNotify:message allowLogAsDeveloperError:!appEventsState.areAllEventsImplicit];
}
} else if (flushResult == FlushResultNoConnectivity) {
@synchronized(self) {
if ([appEventsState isCompatibleWithAppEventsState:_appEventsState]) {
[_appEventsState addEventsFromAppEventState:appEventsState];
} else {
// flush failed due to connectivity. Persist to be tried again later.
[FBSDKAppEventsStateManager persistAppEventsData:appEventsState];
}
}
}
NSString *resultString = @"<unknown>";
switch (flushResult) {
case FlushResultSuccess:
resultString = @"Success";
break;
case FlushResultNoConnectivity:
resultString = @"No Connectivity";
break;
case FlushResultServerError:
resultString = [NSString stringWithFormat:@"Server Error - %@", [error description]];
break;
}
[FBSDKLogger singleShotLogEntry:FBSDKLoggingBehaviorAppEvents
formatString:@"%@\nFlush Result : %@", loggingEntry, resultString];
}
- (void)flushTimerFired:(id)arg
{
[FBSDKAppEventsUtility ensureOnMainThread:NSStringFromSelector(_cmd) className:NSStringFromClass([self class])];
if (self.flushBehavior != FBSDKAppEventsFlushBehaviorExplicitOnly && !self.disableTimer) {
[self flushForReason:FBSDKAppEventsFlushReasonTimer];
}
}
- (void)appSettingsFetchStateResetTimerFired:(id)arg
{
_serverConfiguration = nil;
}
- (void)applicationDidBecomeActive
{
[FBSDKAppEventsUtility ensureOnMainThread:NSStringFromSelector(_cmd) className:NSStringFromClass([self class])];
[self checkPersistedEvents];
// Restore time spent data, indicating that we're not being called from "activateApp".
[FBSDKTimeSpentData restore:NO];
}
- (void)applicationMovingFromActiveStateOrTerminating
{
// When moving from active state, we don't have time to wait for the result of a flush, so
// just persist events to storage, and we'll process them at the next activation.
FBSDKAppEventsState *copy = nil;
@synchronized (self) {
copy = [_appEventsState copy];
_appEventsState = nil;
}
if (copy) {
[FBSDKAppEventsStateManager persistAppEventsData:copy];
}
[FBSDKTimeSpentData suspend];
}
#pragma mark - Custom Audience
+ (FBSDKGraphRequest *)requestForCustomAudienceThirdPartyIDWithAccessToken:(FBSDKAccessToken *)accessToken
{
accessToken = accessToken ?: [FBSDKAccessToken currentAccessToken];
// Rules for how we use the attribution ID / advertiser ID for an 'custom_audience_third_party_id' Graph API request
// 1) if the OS tells us that the user has Limited Ad Tracking, then just don't send, and return a nil in the token.
// 2) if the app has set 'limitEventAndDataUsage', this effectively implies that app-initiated ad targeting shouldn't happen,
// so use that data here to return nil as well.
// 3) if we have a user session token, then no need to send attribution ID / advertiser ID back as the udid parameter
// 4) otherwise, send back the udid parameter.
if ([FBSDKAppEventsUtility advertisingTrackingStatus] == FBSDKAdvertisingTrackingDisallowed || [FBSDKSettings limitEventAndDataUsage]) {
return nil;
}
NSString *tokenString = [FBSDKAppEventsUtility tokenStringToUseFor:accessToken];
NSString *udid = nil;
if (!accessToken) {
// We don't have a logged in user, so we need some form of udid representation. Prefer advertiser ID if
// available, and back off to attribution ID if not. Note that this function only makes sense to be
// called in the context of advertising.
udid = [FBSDKAppEventsUtility advertiserID];
if (!udid) {
udid = [FBSDKAppEventsUtility attributionID];
}
if (!udid) {
// No udid, and no user token. No point in making the request.
return nil;
}
}
NSDictionary *parameters = nil;
if (udid) {
parameters = @{ @"udid" : udid };
}
NSString *graphPath = [NSString stringWithFormat:@"%@/custom_audience_third_party_id", [[self singleton] appID]];
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:graphPath
parameters:parameters
tokenString:tokenString
HTTPMethod:nil
flags:FBSDKGraphRequestFlagDoNotInvalidateTokenOnError | FBSDKGraphRequestFlagDisableErrorRecovery];
return request;
}
@end

View File

@ -0,0 +1,82 @@
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import <Foundation/Foundation.h>
@class BFTask;
// Check if Bolts.framework is available for import
#if __has_include(<Bolts/BFAppLinkResolving.h>)
// Import it if it's available
# import <Bolts/BFAppLinkResolving.h>
#else
// Otherwise - redeclare BFAppLinkResolving protocol to resolve the problem of missing symbols
// Please note: Bolts.framework is still required for AppLink resolving to work,
// but this allows FBSDKCoreKit to weakly link Bolts.framework as well as this enables clang modulemaps to work.
/*!
Implement this protocol to provide an alternate strategy for resolving
App Links that may include pre-fetching, caching, or querying for App Link
data from an index provided by a service provider.
*/
@protocol BFAppLinkResolving <NSObject>
/*!
Asynchronously resolves App Link data for a given URL.
@param url The URL to resolve into an App Link.
@returns A BFTask that will return a BFAppLink for the given URL.
*/
- (BFTask *)appLinkFromURLInBackground:(NSURL *)url;
@end
#endif
/*!
@class FBSDKAppLinkResolver
@abstract
Provides an implementation of the BFAppLinkResolving protocol that uses the Facebook App Link
Index API to resolve App Links given a URL. It also provides an additional helper method that can resolve
multiple App Links in a single call.
@discussion
Usage of this type requires a client token. See `[FBSDKSettings setClientToken:]` and linking
Bolts.framework
*/
@interface FBSDKAppLinkResolver : NSObject<BFAppLinkResolving>
/*!
@abstract Asynchronously resolves App Link data for multiple URLs.
@param urls An array of NSURLs to resolve into App Links.
@returns A BFTask that will return dictionary mapping input NSURLs to their
corresponding BFAppLink.
@discussion
You should set the client token before making this call. See `[FBSDKSettings setClientToken:]`
*/
- (BFTask *)appLinksFromURLsInBackground:(NSArray *)urls;
/*!
@abstract Allocates and initializes a new instance of FBSDKAppLinkResolver.
*/
+ (instancetype)resolver;
@end

View File

@ -0,0 +1,195 @@
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import "FBSDKAppLinkResolver.h"
#import <UIKit/UIKit.h>
#import <Bolts/BFAppLink.h>
#import <Bolts/BFAppLinkTarget.h>
#import <Bolts/BFTask.h>
#import <Bolts/BFTaskCompletionSource.h>
#import "FBSDKGraphRequest+Internal.h"
#import "FBSDKGraphRequestConnection.h"
#import "FBSDKLogger.h"
#import "FBSDKSettings+Internal.h"
#import "FBSDKUtility.h"
static NSString *const kURLKey = @"url";
static NSString *const kIOSAppStoreIdKey = @"app_store_id";
static NSString *const kIOSAppNameKey = @"app_name";
static NSString *const kWebKey = @"web";
static NSString *const kIOSKey = @"ios";
static NSString *const kIPhoneKey = @"iphone";
static NSString *const kIPadKey = @"ipad";
static NSString *const kShouldFallbackKey = @"should_fallback";
static NSString *const kAppLinksKey = @"app_links";
static void FBSDKAppLinkResolverBoltsClassFromString(Class *clazz, NSString *className)
{
*clazz = NSClassFromString(className);
if (*clazz == nil) {
NSString *message = [NSString stringWithFormat:@"Unable to load class %@. Did you link Bolts.framework?", className];
@throw [NSException exceptionWithName:NSInternalInconsistencyException
reason:message
userInfo:nil];
}
}
@interface FBSDKAppLinkResolver ()
@property (nonatomic, strong) NSMutableDictionary *cachedLinks;
@property (nonatomic, assign) UIUserInterfaceIdiom userInterfaceIdiom;
@end
@implementation FBSDKAppLinkResolver
static Class g_BFTaskCompletionSourceClass;
static Class g_BFAppLinkTargetClass;
static Class g_BFAppLinkClass;
static Class g_BFTaskClass;
+ (void)initialize
{
if (self == [FBSDKAppLinkResolver class]) {
FBSDKAppLinkResolverBoltsClassFromString(&g_BFTaskCompletionSourceClass, @"BFTaskCompletionSource");
FBSDKAppLinkResolverBoltsClassFromString(&g_BFAppLinkTargetClass, @"BFAppLinkTarget");
FBSDKAppLinkResolverBoltsClassFromString(&g_BFAppLinkClass, @"BFAppLink");
FBSDKAppLinkResolverBoltsClassFromString(&g_BFTaskClass, @"BFTask");
}
}
- (id)initWithUserInterfaceIdiom:(UIUserInterfaceIdiom)userInterfaceIdiom
{
if (self = [super init]) {
self.cachedLinks = [NSMutableDictionary dictionary];
self.userInterfaceIdiom = userInterfaceIdiom;
}
return self;
}
- (BFTask *)appLinksFromURLsInBackground:(NSArray *)urls
{
if (![FBSDKSettings clientToken] && ![FBSDKAccessToken currentAccessToken]) {
[FBSDKLogger singleShotLogEntry:FBSDKLoggingBehaviorDeveloperErrors
logEntry:@"A user access token or clientToken is required to use FBAppLinkResolver"];
}
NSMutableDictionary *appLinks = [NSMutableDictionary dictionary];
NSMutableArray *toFind = [NSMutableArray array];
NSMutableArray *toFindStrings = [NSMutableArray array];
@synchronized (self.cachedLinks) {
for (NSURL *url in urls) {
if (self.cachedLinks[url]) {
appLinks[url] = self.cachedLinks[url];
} else {
[toFind addObject:url];
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
[toFindStrings addObject:[url.absoluteString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
#pragma clang diagnostic pop
}
}
}
if (toFind.count == 0) {
// All of the URLs have already been found.
return [g_BFTaskClass taskWithResult:appLinks];
}
NSMutableArray *fields = [NSMutableArray arrayWithObject:kIOSKey];
NSString *idiomSpecificField = nil;
switch (self.userInterfaceIdiom) {
case UIUserInterfaceIdiomPad:
idiomSpecificField = kIPadKey;
break;
case UIUserInterfaceIdiomPhone:
idiomSpecificField = kIPhoneKey;
break;
default:
break;
}
if (idiomSpecificField) {
[fields addObject:idiomSpecificField];
}
NSString *path = [NSString stringWithFormat:@"?fields=%@.fields(%@)&ids=%@",
kAppLinksKey,
[fields componentsJoinedByString:@","],
[toFindStrings componentsJoinedByString:@","]];
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:path
parameters:nil
flags:FBSDKGraphRequestFlagDoNotInvalidateTokenOnError | FBSDKGraphRequestFlagDisableErrorRecovery];
BFTaskCompletionSource *tcs = [g_BFTaskCompletionSourceClass taskCompletionSource];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (error) {
[tcs setError:error];
return;
}
for (NSURL *url in toFind) {
id nestedObject = [[result objectForKey:url.absoluteString] objectForKey:kAppLinksKey];
NSMutableArray *rawTargets = [NSMutableArray array];
if (idiomSpecificField) {
[rawTargets addObjectsFromArray:[nestedObject objectForKey:idiomSpecificField]];
}
[rawTargets addObjectsFromArray:[nestedObject objectForKey:kIOSKey]];
NSMutableArray *targets = [NSMutableArray arrayWithCapacity:rawTargets.count];
for (id rawTarget in rawTargets) {
[targets addObject:[g_BFAppLinkTargetClass appLinkTargetWithURL:[NSURL URLWithString:[rawTarget objectForKey:kURLKey]]
appStoreId:[rawTarget objectForKey:kIOSAppStoreIdKey]
appName:[rawTarget objectForKey:kIOSAppNameKey]]];
}
id webTarget = [nestedObject objectForKey:kWebKey];
NSString *webFallbackString = [webTarget objectForKey:kURLKey];
NSURL *fallbackUrl = webFallbackString ? [NSURL URLWithString:webFallbackString] : url;
NSNumber *shouldFallback = [webTarget objectForKey:kShouldFallbackKey];
if (shouldFallback && !shouldFallback.boolValue) {
fallbackUrl = nil;
}
BFAppLink *link = [g_BFAppLinkClass appLinkWithSourceURL:url
targets:targets
webURL:fallbackUrl];
@synchronized (self.cachedLinks) {
self.cachedLinks[url] = link;
}
appLinks[url] = link;
}
[tcs setResult:appLinks];
}];
return tcs.task;
}
- (BFTask *)appLinkFromURLInBackground:(NSURL *)url
{
// Implement in terms of appLinksFromURLsInBackground
BFTask *resolveTask = [self appLinksFromURLsInBackground:@[url]];
return [resolveTask continueWithSuccessBlock:^id(BFTask *task) {
return task.result[url];
}];
}
+ (id)resolver
{
return [[self alloc] initWithUserInterfaceIdiom:UI_USER_INTERFACE_IDIOM()];
}
@end

View File

@ -0,0 +1,55 @@
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import <Foundation/Foundation.h>
/*!
@abstract Describes the callback for fetchDeferredAppLink.
@param url the url representing the deferred App Link
@param error the error during the request, if any
@discussion The url may also have a fb_click_time_utc query parameter that
represents when the click occurred that caused the deferred App Link to be created.
*/
typedef void (^FBSDKDeferredAppLinkHandler)(NSURL *url, NSError *error);
/*!
@abstract Class containing App Links related utility methods.
*/
@interface FBSDKAppLinkUtility : NSObject
/*!
@abstract
Call this method from the main thread to fetch deferred applink data if you use Mobile App
Engagement Ads (https://developers.facebook.com/docs/ads-for-apps/mobile-app-ads-engagement).
This may require a network round trip. If successful, the handler is invoked with the link
data (this will only return a valid URL once, and future calls will result in a nil URL
value in the callback).
@param handler the handler to be invoked if there is deferred App Link data
@discussion The handler may contain an NSError instance to capture any errors. In the
common case where there simply was no app link data, the NSError instance will be nil.
This method should only be called from a location that occurs after any launching URL has
been processed (e.g., you should call this method from your application delegate's
applicationDidBecomeActive:).
*/
+ (void)fetchDeferredAppLink:(FBSDKDeferredAppLinkHandler)handler;
@end

View File

@ -0,0 +1,80 @@
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import "FBSDKAppLinkUtility.h"
#import "FBSDKAppEventsUtility.h"
#import "FBSDKGraphRequest.h"
#import "FBSDKSettings.h"
#import "FBSDKUtility.h"
static NSString *const FBSDKLastDeferredAppLink = @"com.facebook.sdk:lastDeferredAppLink%@";
static NSString *const FBSDKDeferredAppLinkEvent = @"DEFERRED_APP_LINK";
@implementation FBSDKAppLinkUtility {}
+ (void)fetchDeferredAppLink:(FBSDKDeferredAppLinkHandler)handler
{
NSAssert([NSThread isMainThread], @"FBSDKAppLink fetchDeferredAppLink: must be invoked from main thread.");
NSString *appID = [FBSDKSettings appID];
// Deferred app links are only currently used for engagement ads, thus we consider the app to be an advertising one.
// If this is considered for organic, non-ads scenarios, we'll need to retrieve the FBAppEventsUtility.shouldAccessAdvertisingID
// before we make this call.
NSMutableDictionary *deferredAppLinkParameters =
[FBSDKAppEventsUtility activityParametersDictionaryForEvent:FBSDKDeferredAppLinkEvent
implicitEventsOnly:NO
shouldAccessAdvertisingID:YES];
FBSDKGraphRequest *deferredAppLinkRequest = [[FBSDKGraphRequest alloc] initWithGraphPath:[NSString stringWithFormat:@"%@/activities", appID, nil]
parameters:deferredAppLinkParameters
tokenString:nil
version:nil
HTTPMethod:@"POST"];
[deferredAppLinkRequest startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
id result,
NSError *error) {
NSURL *applinkURL = nil;
if (!error) {
NSString *appLinkString = result[@"applink_url"];
if (appLinkString) {
applinkURL = [NSURL URLWithString:appLinkString];
NSString *createTimeUtc = result[@"click_time"];
if (createTimeUtc) {
// append/translate the create_time_utc so it can be used by clients
NSString *modifiedURLString = [[applinkURL absoluteString]
stringByAppendingFormat:@"%@fb_click_time_utc=%@",
([applinkURL query]) ? @"&" : @"?" ,
createTimeUtc ];
applinkURL = [NSURL URLWithString:modifiedURLString];
}
}
}
if (handler) {
dispatch_async(dispatch_get_main_queue(), ^{
handler(applinkURL, error);
});
}
}];
}
@end

View File

@ -0,0 +1,74 @@
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import <UIKit/UIKit.h>
/*!
@class FBSDKApplicationDelegate
@abstract
The FBSDKApplicationDelegate is designed to post process the results from Facebook Login
or Facebook Dialogs (or any action that requires switching over to the native Facebook
app or Safari).
@discussion
The methods in this class are designed to mirror those in UIApplicationDelegate, and you
should call them in the respective methods in your AppDelegate implementation.
*/
@interface FBSDKApplicationDelegate : NSObject
/*!
@abstract Gets the singleton instance.
*/
+ (instancetype)sharedInstance;
/*!
@abstract
Call this method from the [UIApplicationDelegate application:openURL:sourceApplication:annotation:] method
of the AppDelegate for your app. It should be invoked for the proper processing of responses during interaction
with the native Facebook app or Safari as part of SSO authorization flow or Facebook dialogs.
@param application The application as passed to [UIApplicationDelegate application:openURL:sourceApplication:annotation:].
@param url The URL as passed to [UIApplicationDelegate application:openURL:sourceApplication:annotation:].
@param sourceApplication The sourceApplication as passed to [UIApplicationDelegate application:openURL:sourceApplication:annotation:].
@param annotation The annotation as passed to [UIApplicationDelegate application:openURL:sourceApplication:annotation:].
@return YES if the url was intended for the Facebook SDK, NO if not.
*/
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation;
/*!
@abstract
Call this method from the [UIApplicationDelegate application:didFinishLaunchingWithOptions:] method
of the AppDelegate for your app. It should be invoked for the proper use of the Facebook SDK.
@param application The application as passed to [UIApplicationDelegate application:didFinishLaunchingWithOptions:].
@param launchOptions The launchOptions as passed to [UIApplicationDelegate application:didFinishLaunchingWithOptions:].
@return YES if the url was intended for the Facebook SDK, NO if not.
*/
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
@end

View File

@ -0,0 +1,449 @@
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import "FBSDKApplicationDelegate.h"
#import "FBSDKApplicationDelegate+Internal.h"
#import <objc/runtime.h>
#import "FBSDKAppEvents+Internal.h"
#import "FBSDKConstants.h"
#import "FBSDKDynamicFrameworkLoader.h"
#import "FBSDKError.h"
#import "FBSDKInternalUtility.h"
#import "FBSDKLogger.h"
#import "FBSDKServerConfiguration.h"
#import "FBSDKServerConfigurationManager.h"
#import "FBSDKSettings+Internal.h"
#import "FBSDKTimeSpentData.h"
#import "FBSDKUtility.h"
#if !TARGET_OS_TV
#import "FBSDKBoltsMeasurementEventListener.h"
#import "FBSDKBridgeAPIRequest.h"
#import "FBSDKBridgeAPIResponse.h"
#import "FBSDKContainerViewController.h"
#import "FBSDKProfile+Internal.h"
#endif
NSString *const FBSDKApplicationDidBecomeActiveNotification = @"com.facebook.sdk.FBSDKApplicationDidBecomeActiveNotification";
static NSString *const FBSDKAppLinkInboundEvent = @"fb_al_inbound";
@implementation FBSDKApplicationDelegate
{
#if !TARGET_OS_TV
FBSDKBridgeAPIRequest *_pendingRequest;
FBSDKBridgeAPICallbackBlock _pendingRequestCompletionBlock;
id<FBSDKURLOpening> _pendingURLOpen;
#endif
BOOL _expectingBackground;
UIViewController *_safariViewController;
}
#pragma mark - Class Methods
+ (void)load
{
// when the app becomes active by any means, kick off the initialization.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(initializeWithLaunchData:)
name:UIApplicationDidFinishLaunchingNotification
object:nil];
}
// Initialize SDK listeners
// Don't call this function in any place else. It should only be called when the class is loaded.
+ (void)initializeWithLaunchData:(NSNotification *)note
{
NSDictionary *launchData = note.userInfo;
#if !TARGET_OS_TV
// Register Listener for Bolts measurement events
[FBSDKBoltsMeasurementEventListener defaultListener];
#endif
// Set the SourceApplication for time spent data. This is not going to update the value if the app has already launched.
[FBSDKTimeSpentData setSourceApplication:launchData[UIApplicationLaunchOptionsSourceApplicationKey]
openURL:launchData[UIApplicationLaunchOptionsURLKey]];
// Register on UIApplicationDidEnterBackgroundNotification events to reset source application data when app backgrounds.
[FBSDKTimeSpentData registerAutoResetSourceApplication];
// Remove the observer
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
+ (instancetype)sharedInstance
{
static FBSDKApplicationDelegate *_sharedInstance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedInstance = [[self alloc] _init];
});
return _sharedInstance;
}
#pragma mark - Object Lifecycle
- (instancetype)_init
{
if ((self = [super init]) != nil) {
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter addObserver:self selector:@selector(applicationDidEnterBackground:) name:UIApplicationDidEnterBackgroundNotification object:nil];
[defaultCenter addObserver:self selector:@selector(applicationDidBecomeActive:) name:UIApplicationDidBecomeActiveNotification object:nil];
}
return self;
}
- (instancetype)init
{
return nil;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark - UIApplicationDelegate
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation
{
if (sourceApplication != nil && ![sourceApplication isKindOfClass:[NSString class]]) {
@throw [NSException exceptionWithName:NSInvalidArgumentException
reason:@"Expected 'sourceApplication' to be NSString. Please verify you are passing in 'sourceApplication' from your app delegate (not the UIApplication* parameter). If your app delegate implements iOS 9's application:openURL:options:, you should pass in options[UIApplicationOpenURLOptionsSourceApplicationKey]. "
userInfo:nil];
}
[FBSDKTimeSpentData setSourceApplication:sourceApplication openURL:url];
#if !TARGET_OS_TV
// if they completed a SFVC flow, dimiss it.
[_safariViewController.presentingViewController dismissViewControllerAnimated:YES completion: nil];
_safariViewController = nil;
if (_pendingURLOpen) {
id<FBSDKURLOpening> pendingURLOpen = _pendingURLOpen;
_pendingURLOpen = nil;
if ([pendingURLOpen application:application
openURL:url
sourceApplication:sourceApplication
annotation:annotation]) {
return YES;
}
}
if ([self _handleBridgeAPIResponseURL:url sourceApplication:sourceApplication]) {
return YES;
}
#endif
[self _logIfAppLinkEvent:url];
return NO;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
FBSDKAccessToken *cachedToken = [[FBSDKSettings accessTokenCache] fetchAccessToken];
[FBSDKAccessToken setCurrentAccessToken:cachedToken];
#if !TARGET_OS_TV
FBSDKProfile *cachedProfile = [FBSDKProfile fetchCachedProfile];
[FBSDKProfile setCurrentProfile:cachedProfile];
NSURL *launchedURL = launchOptions[UIApplicationLaunchOptionsURLKey];
NSString *sourceApplication = launchOptions[UIApplicationLaunchOptionsSourceApplicationKey];
if (launchedURL &&
sourceApplication) {
Class loginManagerClass = NSClassFromString(@"FBSDKLoginManager");
if (loginManagerClass) {
id annotation = launchOptions[UIApplicationLaunchOptionsAnnotationKey];
id<FBSDKURLOpening> loginManager = [[loginManagerClass alloc] init];
return [loginManager application:application
openURL:launchedURL
sourceApplication:sourceApplication
annotation:annotation];
}
}
#endif
return NO;
}
- (void)applicationDidEnterBackground:(NSNotification *)notification
{
_active = NO;
_expectingBackground = NO;
}
- (void)applicationDidBecomeActive:(NSNotification *)notification
{
// _expectingBackground can be YES if the caller started doing work (like login)
// within the app delegate's lifecycle like openURL, in which case there
// might have been a "didBecomeActive" event pending that we want to ignore.
if (!_expectingBackground && !_safariViewController) {
_active = YES;
#if !TARGET_OS_TV
[_pendingURLOpen applicationDidBecomeActive:[notification object]];
[self _cancelBridgeRequest];
#endif
[[NSNotificationCenter defaultCenter] postNotificationName:FBSDKApplicationDidBecomeActiveNotification object:self];
}
}
#pragma mark - Internal Methods
#pragma mark -- (non-tvos)
#if !TARGET_OS_TV
- (void)openURL:(NSURL *)url sender:(id<FBSDKURLOpening>)sender handler:(void(^)(BOOL))handler
{
_expectingBackground = YES;
_pendingURLOpen = sender;
dispatch_async(dispatch_get_main_queue(), ^{
// Dispatch openURL calls to prevent hangs if we're inside the current app delegate's openURL flow already
BOOL opened = [[UIApplication sharedApplication] openURL:url];
if ([url.scheme hasPrefix:@"http"] && !opened) {
NSOperatingSystemVersion iOS8Version = { .majorVersion = 8, .minorVersion = 0, .patchVersion = 0 };
if (![FBSDKInternalUtility isOSRunTimeVersionAtLeast:iOS8Version]) {
// Safari openURL calls can wrongly return NO on iOS 7 so manually overwrite that case to YES.
// Otherwise we would rather trust in the actual result of openURL
opened = YES;
}
}
if (handler) {
handler(opened);
}
});
}
- (void)openBridgeAPIRequest:(FBSDKBridgeAPIRequest *)request
useSafariViewController:(BOOL)useSafariViewController
fromViewController:(UIViewController *)fromViewController
completionBlock:(FBSDKBridgeAPICallbackBlock)completionBlock
{
if (!request) {
return;
}
NSError *error;
NSURL *requestURL = [request requestURL:&error];
if (!requestURL) {
FBSDKBridgeAPIResponse *response = [FBSDKBridgeAPIResponse bridgeAPIResponseWithRequest:request error:error];
completionBlock(response);
return;
}
_pendingRequest = request;
_pendingRequestCompletionBlock = [completionBlock copy];
void (^handler)(BOOL) = ^(BOOL openedURL) {
if (!openedURL) {
_pendingRequest = nil;
_pendingRequestCompletionBlock = nil;
NSError *openedURLError;
if ([request.scheme hasPrefix:@"http"]) {
openedURLError = [FBSDKError errorWithCode:FBSDKBrowswerUnavailableErrorCode
message:@"the app switch failed because the browser is unavailable"];
} else {
openedURLError = [FBSDKError errorWithCode:FBSDKAppVersionUnsupportedErrorCode
message:@"the app switch failed because the destination app is out of date"];
}
FBSDKBridgeAPIResponse *response = [FBSDKBridgeAPIResponse bridgeAPIResponseWithRequest:request
error:openedURLError];
completionBlock(response);
return;
}
};
if (useSafariViewController) {
[self openURLWithSafariViewController:requestURL sender:nil fromViewController:fromViewController handler:handler];
} else {
[self openURL:requestURL sender:nil handler:handler];
}
}
- (void)openURLWithSafariViewController:(NSURL *)url
sender:(id<FBSDKURLOpening>)sender
fromViewController:(UIViewController *)fromViewController
handler:(void(^)(BOOL))handler
{
if (![url.scheme hasPrefix:@"http"]) {
[self openURL:url sender:sender handler:handler];
return;
}
_expectingBackground = NO;
_pendingURLOpen = sender;
// trying to dynamically load SFSafariViewController class
// so for the cases when it is available we can send users through Safari View Controller flow
// in cases it is not available regular flow will be selected
Class SFSafariViewControllerClass = fbsdkdfl_SFSafariViewControllerClass();
if (SFSafariViewControllerClass) {
UIViewController *parent = fromViewController ?: [FBSDKInternalUtility topMostViewController];
NSURLComponents *components = [NSURLComponents componentsWithURL:url resolvingAgainstBaseURL:NO];
NSURLQueryItem *sfvcQueryItem = [[NSURLQueryItem alloc] initWithName:@"sfvc" value:@"1"];
[components setQueryItems:[components.queryItems arrayByAddingObject:sfvcQueryItem]];
url = components.URL;
FBSDKContainerViewController *container = [[FBSDKContainerViewController alloc] init];
container.delegate = self;
if (parent.transitionCoordinator != nil) {
// Wait until the transition is finished before presenting SafariVC to avoid a blank screen.
[parent.transitionCoordinator animateAlongsideTransition:NULL completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
// Note SFVC init must occur inside block to avoid blank screen.
_safariViewController = [[SFSafariViewControllerClass alloc] initWithURL:url];
[_safariViewController performSelector:@selector(setDelegate:) withObject:self];
[container displayChildController:_safariViewController];
[parent presentViewController:container animated:YES completion:nil];
}];
} else {
_safariViewController = [[SFSafariViewControllerClass alloc] initWithURL:url];
[_safariViewController performSelector:@selector(setDelegate:) withObject:self];
[container displayChildController:_safariViewController];
[parent presentViewController:container animated:YES completion:nil];
}
// Assuming Safari View Controller always opens
if (handler) {
handler(YES);
}
} else {
[self openURL:url sender:sender handler:handler];
}
}
#pragma mark -- SFSafariViewControllerDelegate
// This means the user tapped "Done" which we should treat as a cancellation.
- (void)safariViewControllerDidFinish:(UIViewController *)safariViewController
{
if (_pendingURLOpen) {
id<FBSDKURLOpening> pendingURLOpen = _pendingURLOpen;
_pendingURLOpen = nil;
[pendingURLOpen application:nil
openURL:nil
sourceApplication:nil
annotation:nil];
}
[self _cancelBridgeRequest];
_safariViewController = nil;
}
#pragma mark -- FBSDKContainerViewControllerDelegate
- (void)viewControllerDidDisappear:(FBSDKContainerViewController *)viewController animated:(BOOL)animated
{
if (_safariViewController) {
[FBSDKLogger singleShotLogEntry:FBSDKLoggingBehaviorDeveloperErrors
logEntry:@"**ERROR**:\n The SFSafariViewController's parent view controller was dismissed.\n"
"This can happen if you are triggering login from a UIAlertController. Instead, make sure your top most view "
"controller will not be prematurely dismissed."];
[self safariViewControllerDidFinish:_safariViewController];
}
}
#endif
#pragma mark - Helper Methods
- (void)_logIfAppLinkEvent:(NSURL *)url
{
if (!url) {
return;
}
NSDictionary *params = [FBSDKUtility dictionaryWithQueryString:url.query];
NSString *applinkDataString = params[@"al_applink_data"];
if (!applinkDataString) {
return;
}
NSDictionary * applinkData = [FBSDKInternalUtility objectForJSONString:applinkDataString error:NULL];
if (!applinkData) {
return;
}
NSURL *targetURL = [NSURL URLWithString:applinkData[@"target_url"]];
NSMutableDictionary *logData = [[NSMutableDictionary alloc] init];
[FBSDKInternalUtility dictionary:logData setObject:[targetURL absoluteString] forKey:@"targetURL"];
[FBSDKInternalUtility dictionary:logData setObject:[targetURL host] forKey:@"targetURLHost"];
NSDictionary *refererData = applinkData[@"referer_data"];
if (refererData) {
[FBSDKInternalUtility dictionary:logData setObject:refererData[@"target_url"] forKey:@"referralTargetURL"];
[FBSDKInternalUtility dictionary:logData setObject:refererData[@"url"] forKey:@"referralURL"];
[FBSDKInternalUtility dictionary:logData setObject:refererData[@"app_name"] forKey:@"referralAppName"];
}
[FBSDKInternalUtility dictionary:logData setObject:[url absoluteString] forKey:@"inputURL"];
[FBSDKInternalUtility dictionary:logData setObject:[url scheme] forKey:@"inputURLScheme"];
[FBSDKAppEvents logImplicitEvent:FBSDKAppLinkInboundEvent
valueToSum:nil
parameters:logData
accessToken:nil];
}
#pragma mark -- (non-tvos)
#if !TARGET_OS_TV
- (BOOL)_handleBridgeAPIResponseURL:(NSURL *)responseURL sourceApplication:(NSString *)sourceApplication
{
FBSDKBridgeAPIRequest *request = _pendingRequest;
FBSDKBridgeAPICallbackBlock completionBlock = _pendingRequestCompletionBlock;
_pendingRequest = nil;
_pendingRequestCompletionBlock = NULL;
if (![responseURL.scheme isEqualToString:[FBSDKInternalUtility appURLScheme]]) {
return NO;
}
if (![responseURL.host isEqualToString:@"bridge"]) {
return NO;
}
if (!request) {
return NO;
}
if (!completionBlock) {
return YES;
}
NSError *error;
FBSDKBridgeAPIResponse *response = [FBSDKBridgeAPIResponse bridgeAPIResponseWithRequest:request
responseURL:responseURL
sourceApplication:sourceApplication
error:&error];
if (response) {
completionBlock(response);
return YES;
} else if (error) {
completionBlock([FBSDKBridgeAPIResponse bridgeAPIResponseWithRequest:request error:error]);
return YES;
} else {
return NO;
}
}
- (void)_cancelBridgeRequest
{
if (_pendingRequest && _pendingRequestCompletionBlock) {
_pendingRequestCompletionBlock([FBSDKBridgeAPIResponse bridgeAPIResponseCancelledWithRequest:_pendingRequest]);
}
_pendingRequest = nil;
_pendingRequestCompletionBlock = NULL;
}
#endif
@end

View File

@ -0,0 +1,26 @@
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import <UIKit/UIKit.h>
/*!
@abstract A base class for common SDK buttons.
*/
@interface FBSDKButton : UIButton
@end

View File

@ -0,0 +1,457 @@
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import "FBSDKButton.h"
#import "FBSDKButton+Subclass.h"
#import "FBSDKAccessToken.h"
#import "FBSDKAppEvents+Internal.h"
#import "FBSDKAppEvents.h"
#import "FBSDKApplicationDelegate+Internal.h"
#import "FBSDKLogo.h"
#import "FBSDKMath.h"
#import "FBSDKUIUtility.h"
#import "FBSDKViewImpressionTracker.h"
#define HEIGHT_TO_FONT_SIZE 0.47
#define HEIGHT_TO_MARGIN 0.27
#define HEIGHT_TO_PADDING 0.23
#define HEIGHT_TO_TEXT_PADDING_CORRECTION 0.08
@implementation FBSDKButton
{
BOOL _skipIntrinsicContentSizing;
BOOL _isExplicitlyDisabled;
}
#pragma mark - Object Lifecycle
- (instancetype)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame])) {
_skipIntrinsicContentSizing = YES;
[self configureButton];
_skipIntrinsicContentSizing = NO;
}
return self;
}
- (void)awakeFromNib
{
[super awakeFromNib];
_skipIntrinsicContentSizing = YES;
[self configureButton];
_skipIntrinsicContentSizing = NO;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark - Properties
- (void)setEnabled:(BOOL)enabled
{
_isExplicitlyDisabled = !enabled;
[self checkImplicitlyDisabled];
}
#pragma mark - Layout
- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
if ([self isHidden] || CGRectIsEmpty(self.bounds)) {
return CGRectZero;
}
CGRect imageRect = UIEdgeInsetsInsetRect(contentRect, self.imageEdgeInsets);
CGFloat margin = [self _marginForHeight:[self _heightForContentRect:contentRect]];
imageRect = CGRectInset(imageRect, margin, margin);
imageRect.size.width = CGRectGetHeight(imageRect);
return imageRect;
}
- (CGSize)intrinsicContentSize
{
if (_skipIntrinsicContentSizing) {
return CGSizeZero;
}
_skipIntrinsicContentSizing = YES;
CGSize size = [self sizeThatFits:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX)];
_skipIntrinsicContentSizing = NO;
return size;
}
- (void)layoutSubviews
{
// automatic impression tracking if the button conforms to FBSDKButtonImpressionTracking
if ([self conformsToProtocol:@protocol(FBSDKButtonImpressionTracking)]) {
NSString *eventName = [(id<FBSDKButtonImpressionTracking>)self impressionTrackingEventName];
NSString *identifier = [(id<FBSDKButtonImpressionTracking>)self impressionTrackingIdentifier];
NSDictionary *parameters = [(id<FBSDKButtonImpressionTracking>)self analyticsParameters];
if (eventName && identifier) {
FBSDKViewImpressionTracker *impressionTracker = [FBSDKViewImpressionTracker impressionTrackerWithEventName:eventName];
[impressionTracker logImpressionWithIdentifier:identifier parameters:parameters];
}
}
[super layoutSubviews];
}
- (CGSize)sizeThatFits:(CGSize)size
{
if ([self isHidden]) {
return CGSizeZero;
}
CGSize normalSize = [self sizeThatFits:size title:[self titleForState:UIControlStateNormal]];
CGSize selectedSize = [self sizeThatFits:size title:[self titleForState:UIControlStateSelected]];
return CGSizeMake(MAX(normalSize.width, selectedSize.width), MAX(normalSize.height, selectedSize.height));
}
- (void)sizeToFit
{
CGRect bounds = self.bounds;
bounds.size = [self sizeThatFits:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX)];
self.bounds = bounds;
}
- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
if ([self isHidden] || CGRectIsEmpty(self.bounds)) {
return CGRectZero;
}
CGRect imageRect = [self imageRectForContentRect:contentRect];
CGFloat height = [self _heightForContentRect:contentRect];
CGFloat padding = [self _paddingForHeight:height];
CGFloat titleX = CGRectGetMaxX(imageRect) + padding;
CGRect titleRect = CGRectMake(titleX, 0.0, CGRectGetWidth(contentRect) - titleX, CGRectGetHeight(contentRect));
UIEdgeInsets titleEdgeInsets = UIEdgeInsetsZero;
if (!self.layer.needsLayout) {
UILabel *titleLabel = self.titleLabel;
if (titleLabel.textAlignment == NSTextAlignmentCenter) {
// if the text is centered, we need to adjust the frame for the titleLabel based on the size of the text in order
// to keep the text centered in the button without adding extra blank space to the right when unnecessary
// 1. the text fits centered within the button without colliding with the image (imagePaddingWidth)
// 2. the text would run into the image, so adjust the insets to effectively left align it (textPaddingWidth)
CGSize titleSize = FBSDKTextSize(titleLabel.text,
titleLabel.font,
titleRect.size,
titleLabel.lineBreakMode);
CGFloat titlePaddingWidth = (CGRectGetWidth(titleRect) - titleSize.width) / 2;
CGFloat imagePaddingWidth = titleX / 2;
CGFloat inset = MIN(titlePaddingWidth, imagePaddingWidth);
titleEdgeInsets.left -= inset;
titleEdgeInsets.right += inset;
}
}
return UIEdgeInsetsInsetRect(titleRect, titleEdgeInsets);
}
#pragma mark - Subclass Methods
- (void)logTapEventWithEventName:(NSString *)eventName parameters:(NSDictionary *)parameters
{
[FBSDKAppEvents logImplicitEvent:eventName
valueToSum:nil
parameters:parameters
accessToken:[FBSDKAccessToken currentAccessToken]];
}
- (void)checkImplicitlyDisabled
{
BOOL enabled = !_isExplicitlyDisabled && ![self isImplicitlyDisabled];
BOOL currentEnabled = [self isEnabled];
[super setEnabled:enabled];
if (currentEnabled != enabled) {
[self invalidateIntrinsicContentSize];
[self setNeedsLayout];
}
}
- (void)configureButton
{
[self configureWithIcon:[[self class] defaultIcon]
title:nil
backgroundColor:[[self class] defaultBackgroundColor]
highlightedColor:[[self class] defaultHighlightedColor]];
}
- (void)configureWithIcon:(FBSDKIcon *)icon
title:(NSString *)title
backgroundColor:(UIColor *)backgroundColor
highlightedColor:(UIColor *)highlightedColor
{
[self _configureWithIcon:icon
title:title
backgroundColor:backgroundColor
highlightedColor:highlightedColor
selectedTitle:nil
selectedIcon:nil
selectedColor:nil
selectedHighlightedColor:nil];
}
- (void)configureWithIcon:(FBSDKIcon *)icon
title:(NSString *)title
backgroundColor:(UIColor *)backgroundColor
highlightedColor:(UIColor *)highlightedColor
selectedTitle:(NSString *)selectedTitle
selectedIcon:(FBSDKIcon *)selectedIcon
selectedColor:(UIColor *)selectedColor
selectedHighlightedColor:(UIColor *)selectedHighlightedColor
{
if (!selectedColor) {
selectedColor = [self defaultSelectedColor];
}
if (!selectedHighlightedColor) {
selectedHighlightedColor = highlightedColor;
}
[self _configureWithIcon:icon
title:title
backgroundColor:backgroundColor
highlightedColor:highlightedColor
selectedTitle:selectedTitle
selectedIcon:selectedIcon
selectedColor:selectedColor
selectedHighlightedColor:selectedHighlightedColor];
}
- (UIColor *)defaultBackgroundColor
{
return [UIColor colorWithRed:65.0/255.0 green:93.0/255.0 blue:174.0/255.0 alpha:1.0];
}
- (UIColor *)defaultDisabledColor
{
return [UIColor colorWithRed:189.0/255.0 green:193.0/255.0 blue:201.0/255.0 alpha:1.0];
}
- (UIFont *)defaultFont
{
return [UIFont systemFontOfSize:14];
}
- (UIColor *)defaultHighlightedColor
{
return [UIColor colorWithRed:47.0/255.0 green:71.0/255.0 blue:122.0/255.0 alpha:1.0];
}
- (FBSDKIcon *)defaultIcon
{
return [[FBSDKLogo alloc] init];
}
- (UIColor *)defaultSelectedColor
{
return [UIColor colorWithRed:124.0/255.0 green:143.0/255.0 blue:200.0/255.0 alpha:1.0];
}
- (BOOL)isImplicitlyDisabled
{
return NO;
}
- (CGSize)sizeThatFits:(CGSize)size title:(NSString *)title
{
UIFont *font = self.titleLabel.font;
CGFloat height = [self _heightForFont:font];
UIEdgeInsets contentEdgeInsets = self.contentEdgeInsets;
CGSize constrainedContentSize = FBSDKEdgeInsetsInsetSize(size, contentEdgeInsets);
CGSize titleSize = FBSDKTextSize(title, font, constrainedContentSize, self.titleLabel.lineBreakMode);
CGFloat padding = [self _paddingForHeight:height];
CGFloat textPaddingCorrection = [self _textPaddingCorrectionForHeight:height];
CGSize contentSize = CGSizeMake(height + padding + titleSize.width - textPaddingCorrection, height);
return FBSDKEdgeInsetsOutsetSize(contentSize, contentEdgeInsets);
}
#pragma mark - Helper Methods
- (void)_applicationDidBecomeActiveNotification:(NSNotification *)notification
{
[self checkImplicitlyDisabled];
}
- (UIImage *)_backgroundImageWithColor:(UIColor *)color cornerRadius:(CGFloat)cornerRadius scale:(CGFloat)scale
{
CGFloat size = 1.0 + 2 * cornerRadius;
UIGraphicsBeginImageContextWithOptions(CGSizeMake(size, size), NO, scale);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, color.CGColor);
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, cornerRadius + 1.0, 0.0);
CGPathAddArcToPoint(path, NULL, size, 0.0, size, cornerRadius, cornerRadius);
CGPathAddLineToPoint(path, NULL, size, cornerRadius + 1.0);
CGPathAddArcToPoint(path, NULL, size, size, cornerRadius + 1.0, size, cornerRadius);
CGPathAddLineToPoint(path, NULL, cornerRadius, size);
CGPathAddArcToPoint(path, NULL, 0.0, size, 0.0, cornerRadius + 1.0, cornerRadius);
CGPathAddLineToPoint(path, NULL, 0.0, cornerRadius);
CGPathAddArcToPoint(path, NULL, 0.0, 0.0, cornerRadius, 0.0, cornerRadius);
CGPathCloseSubpath(path);
CGContextAddPath(context, path);
CGPathRelease(path);
CGContextFillPath(context);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
#if TARGET_OS_TV
return [image resizableImageWithCapInsets:UIEdgeInsetsMake(cornerRadius, cornerRadius, cornerRadius, cornerRadius)
resizingMode:UIImageResizingModeStretch];
#else
return [image stretchableImageWithLeftCapWidth:cornerRadius topCapHeight:cornerRadius];
#endif
}
- (void)_configureWithIcon:(FBSDKIcon *)icon
title:(NSString *)title
backgroundColor:(UIColor *)backgroundColor
highlightedColor:(UIColor *)highlightedColor
selectedTitle:(NSString *)selectedTitle
selectedIcon:(FBSDKIcon *)selectedIcon
selectedColor:(UIColor *)selectedColor
selectedHighlightedColor:(UIColor *)selectedHighlightedColor
{
[self checkImplicitlyDisabled];
if (!icon) {
icon = [self defaultIcon];
}
if (!backgroundColor) {
backgroundColor = [self defaultBackgroundColor];
}
if (!highlightedColor) {
highlightedColor = [self defaultHighlightedColor];
}
self.adjustsImageWhenDisabled = NO;
self.adjustsImageWhenHighlighted = NO;
self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
self.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
self.tintColor = [UIColor whiteColor];
BOOL forceSizeToFit = CGRectIsEmpty(self.bounds);
CGFloat scale = [UIScreen mainScreen].scale;
UIImage *backgroundImage;
backgroundImage = [self _backgroundImageWithColor:backgroundColor cornerRadius:3.0 scale:scale];
[self setBackgroundImage:backgroundImage forState:UIControlStateNormal];
#if TARGET_OS_TV
[self setBackgroundImage:backgroundImage forState:UIControlStateFocused];
#endif
backgroundImage = [self _backgroundImageWithColor:highlightedColor cornerRadius:3.0 scale:scale];
[self setBackgroundImage:backgroundImage forState:UIControlStateHighlighted];
backgroundImage = [self _backgroundImageWithColor:[self defaultDisabledColor] cornerRadius:3.0 scale:scale];
[self setBackgroundImage:backgroundImage forState:UIControlStateDisabled];
if (selectedColor) {
backgroundImage = [self _backgroundImageWithColor:selectedColor cornerRadius:3.0 scale:scale];
[self setBackgroundImage:backgroundImage forState:UIControlStateSelected];
}
if (selectedHighlightedColor) {
backgroundImage = [self _backgroundImageWithColor:selectedHighlightedColor cornerRadius:3.0 scale:scale];
[self setBackgroundImage:backgroundImage forState:UIControlStateSelected | UIControlStateHighlighted];
#if TARGET_OS_TV
[self setBackgroundImage:backgroundImage forState:UIControlStateSelected | UIControlStateFocused];
#endif
}
[self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self setTitle:title forState:UIControlStateNormal];
#if TARGET_OS_TV
[self setTitle:title forState:UIControlStateFocused];
#endif
if (selectedTitle) {
[self setTitle:selectedTitle forState:UIControlStateSelected];
[self setTitle:selectedTitle forState:UIControlStateSelected | UIControlStateHighlighted];
#if TARGET_OS_TV
[self setTitle:selectedTitle forState:UIControlStateSelected | UIControlStateFocused];
#endif
}
UILabel *titleLabel = self.titleLabel;
titleLabel.lineBreakMode = NSLineBreakByClipping;
UIFont *font = [self defaultFont];
titleLabel.font = font;
CGSize imageSize = CGSizeMake(font.pointSize, font.pointSize);
UIImage *image = [icon imageWithSize:imageSize];
image = [image resizableImageWithCapInsets:UIEdgeInsetsZero resizingMode:UIImageResizingModeStretch];
[self setImage:image forState:UIControlStateNormal];
#if TARGET_OS_TV
[self setImage:image forState:UIControlStateFocused];
#endif
if (selectedIcon) {
UIImage *selectedImage = [selectedIcon imageWithSize:imageSize];
selectedImage = [selectedImage resizableImageWithCapInsets:UIEdgeInsetsZero
resizingMode:UIImageResizingModeStretch];
[self setImage:selectedImage forState:UIControlStateSelected];
[self setImage:selectedImage forState:UIControlStateSelected | UIControlStateHighlighted];
#if TARGET_OS_TV
[self setImage:selectedImage forState:UIControlStateSelected | UIControlStateFocused];
#endif
}
if (forceSizeToFit) {
[self sizeToFit];
}
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(_applicationDidBecomeActiveNotification:)
name:FBSDKApplicationDidBecomeActiveNotification
object:[FBSDKApplicationDelegate sharedInstance]];
}
- (CGFloat)_fontSizeForHeight:(CGFloat)height
{
return floorf(height * HEIGHT_TO_FONT_SIZE);
}
- (CGFloat)_heightForContentRect:(CGRect)contentRect
{
UIEdgeInsets contentEdgeInsets = self.contentEdgeInsets;
return contentEdgeInsets.top + CGRectGetHeight(contentRect) + contentEdgeInsets.bottom;
}
- (CGFloat)_heightForFont:(UIFont *)font
{
return floorf(font.pointSize / (1 - 2 * HEIGHT_TO_MARGIN));
}
- (CGFloat)_marginForHeight:(CGFloat)height
{
return floorf(height * HEIGHT_TO_MARGIN);
}
- (CGFloat)_paddingForHeight:(CGFloat)height
{
return roundf(height * HEIGHT_TO_PADDING) - [self _textPaddingCorrectionForHeight:height];
}
- (CGFloat)_textPaddingCorrectionForHeight:(CGFloat)height
{
return floorf(height * HEIGHT_TO_TEXT_PADDING_CORRECTION);
}
@end

View File

@ -0,0 +1,210 @@
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import <Foundation/Foundation.h>
#import <FBSDKCoreKit/FBSDKMacros.h>
/*!
@abstract The error domain for all errors from FBSDKCoreKit.
@discussion Error codes from the SDK in the range 0-99 are reserved for this domain.
*/
FBSDK_EXTERN NSString *const FBSDKErrorDomain;
/*!
@typedef NS_ENUM(NSInteger, FBSDKErrorCode)
@abstract Error codes for FBSDKErrorDomain.
*/
typedef NS_ENUM(NSInteger, FBSDKErrorCode)
{
/*!
@abstract Reserved.
*/
FBSDKReservedErrorCode = 0,
/*!
@abstract The error code for errors from invalid encryption on incoming encryption URLs.
*/
FBSDKEncryptionErrorCode,
/*!
@abstract The error code for errors from invalid arguments to SDK methods.
*/
FBSDKInvalidArgumentErrorCode,
/*!
@abstract The error code for unknown errors.
*/
FBSDKUnknownErrorCode,
/*!
@abstract A request failed due to a network error. Use NSUnderlyingErrorKey to retrieve
the error object from the NSURLConnection for more information.
*/
FBSDKNetworkErrorCode,
/*!
@abstract The error code for errors encounted during an App Events flush.
*/
FBSDKAppEventsFlushErrorCode,
/*!
@abstract An endpoint that returns a binary response was used with FBSDKGraphRequestConnection.
@discussion Endpoints that return image/jpg, etc. should be accessed using NSURLRequest
*/
FBSDKGraphRequestNonTextMimeTypeReturnedErrorCode,
/*!
@abstract The operation failed because the server returned an unexpected response.
@discussion You can get this error if you are not using the most recent SDK, or you are accessing a version of the
Graph API incompatible with the current SDK.
*/
FBSDKGraphRequestProtocolMismatchErrorCode,
/*!
@abstract The Graph API returned an error.
@discussion See below for useful userInfo keys (beginning with FBSDKGraphRequestError*)
*/
FBSDKGraphRequestGraphAPIErrorCode,
/*!
@abstract The specified dialog configuration is not available.
@discussion This error may signify that the configuration for the dialogs has not yet been downloaded from the server
or that the dialog is unavailable. Subsequent attempts to use the dialog may succeed as the configuration is loaded.
*/
FBSDKDialogUnavailableErrorCode,
/*!
@abstract Indicates an operation failed because a required access token was not found.
*/
FBSDKAccessTokenRequiredErrorCode,
/*!
@abstract Indicates an app switch (typically for a dialog) failed because the destination app is out of date.
*/
FBSDKAppVersionUnsupportedErrorCode,
/*!
@abstract Indicates an app switch to the browser (typically for a dialog) failed.
*/
FBSDKBrowswerUnavailableErrorCode,
};
/*!
@typedef NS_ENUM(NSUInteger, FBSDKGraphRequestErrorCategory)
@abstract Describes the category of Facebook error. See `FBSDKGraphRequestErrorCategoryKey`.
*/
typedef NS_ENUM(NSUInteger, FBSDKGraphRequestErrorCategory)
{
/*! The default error category that is not known to be recoverable. Check `FBSDKLocalizedErrorDescriptionKey` for a user facing message. */
FBSDKGraphRequestErrorCategoryOther = 0,
/*! Indicates the error is temporary (such as server throttling). While a recoveryAttempter will be provided with the error instance, the attempt is guaranteed to succeed so you can simply retry the operation if you do not want to present an alert. */
FBSDKGraphRequestErrorCategoryTransient = 1,
/*! Indicates the error can be recovered (such as requiring a login). A recoveryAttempter will be provided with the error instance that can take UI action. */
FBSDKGraphRequestErrorCategoryRecoverable = 2
};
/*
@methodgroup error userInfo keys
*/
/*!
@abstract The userInfo key for the invalid collection for errors with FBSDKInvalidArgumentErrorCode.
@discussion If the invalid argument is a collection, the collection can be found with this key and the individual
invalid item can be found with FBSDKErrorArgumentValueKey.
*/
FBSDK_EXTERN NSString *const FBSDKErrorArgumentCollectionKey;
/*!
@abstract The userInfo key for the invalid argument name for errors with FBSDKInvalidArgumentErrorCode.
*/
FBSDK_EXTERN NSString *const FBSDKErrorArgumentNameKey;
/*!
@abstract The userInfo key for the invalid argument value for errors with FBSDKInvalidArgumentErrorCode.
*/
FBSDK_EXTERN NSString *const FBSDKErrorArgumentValueKey;
/*!
@abstract The userInfo key for the message for developers in NSErrors that originate from the SDK.
@discussion The developer message will not be localized and is not intended to be presented within the app.
*/
FBSDK_EXTERN NSString *const FBSDKErrorDeveloperMessageKey;
/*!
@abstract The userInfo key describing a localized description that can be presented to the user.
*/
FBSDK_EXTERN NSString *const FBSDKErrorLocalizedDescriptionKey;
/*!
@abstract The userInfo key describing a localized title that can be presented to the user, used with `FBSDKLocalizedErrorDescriptionKey`.
*/
FBSDK_EXTERN NSString *const FBSDKErrorLocalizedTitleKey;
/*
@methodgroup FBSDKGraphRequest error userInfo keys
*/
/*!
@abstract The userInfo key describing the error category, for error recovery purposes.
@discussion See `FBSDKGraphErrorRecoveryProcessor` and `[FBSDKGraphRequest disableErrorRecovery]`.
*/
FBSDK_EXTERN NSString *const FBSDKGraphRequestErrorCategoryKey;
/*
@abstract The userInfo key for the Graph API error code.
*/
FBSDK_EXTERN NSString *const FBSDKGraphRequestErrorGraphErrorCode;
/*
@abstract The userInfo key for the Graph API error subcode.
*/
FBSDK_EXTERN NSString *const FBSDKGraphRequestErrorGraphErrorSubcode;
/*
@abstract The userInfo key for the HTTP status code.
*/
FBSDK_EXTERN NSString *const FBSDKGraphRequestErrorHTTPStatusCodeKey;
/*
@abstract The userInfo key for the raw JSON response.
*/
FBSDK_EXTERN NSString *const FBSDKGraphRequestErrorParsedJSONResponseKey;
/*!
@abstract a formal protocol very similar to the informal protocol NSErrorRecoveryAttempting
*/
@protocol FBSDKErrorRecoveryAttempting<NSObject>
/*!
@abstract attempt the recovery
@param error the error
@param recoveryOptionIndex the selected option index
@param delegate the delegate
@param didRecoverSelector the callback selector, see discussion.
@param contextInfo context info to pass back to callback selector, see discussion.
@discussion
Given that an error alert has been presented document-modally to the user, and the user has chosen one of the error's recovery options, attempt recovery from the error, and send the selected message to the specified delegate. The option index is an index into the error's array of localized recovery options. The method selected by didRecoverSelector must have the same signature as:
- (void)didPresentErrorWithRecovery:(BOOL)didRecover contextInfo:(void *)contextInfo;
The value passed for didRecover must be YES if error recovery was completely successful, NO otherwise.
*/
- (void)attemptRecoveryFromError:(NSError *)error optionIndex:(NSUInteger)recoveryOptionIndex delegate:(id)delegate didRecoverSelector:(SEL)didRecoverSelector contextInfo:(void *)contextInfo;
@end

View File

@ -0,0 +1,34 @@
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import "FBSDKConstants.h"
NSString *const FBSDKErrorDomain = @"com.facebook.sdk.core";
NSString *const FBSDKErrorArgumentCollectionKey = @"com.facebook.sdk:FBSDKErrorArgumentCollectionKey";
NSString *const FBSDKErrorArgumentNameKey = @"com.facebook.sdk:FBSDKErrorArgumentNameKey";
NSString *const FBSDKErrorArgumentValueKey = @"com.facebook.sdk:FBSDKErrorArgumentValueKey";
NSString *const FBSDKErrorDeveloperMessageKey = @"com.facebook.sdk:FBSDKErrorDeveloperMessageKey";
NSString *const FBSDKErrorLocalizedDescriptionKey = @"com.facebook.sdk:FBSDKErrorLocalizedDescriptionKey";
NSString *const FBSDKErrorLocalizedTitleKey = @"com.facebook.sdk:FBSDKErrorLocalizedErrorTitleKey";
NSString *const FBSDKGraphRequestErrorCategoryKey = @"com.facebook.sdk:FBSDKGraphRequestErrorCategoryKey";
NSString *const FBSDKGraphRequestErrorGraphErrorCode = @"com.facebook.sdk:FBSDKGraphRequestErrorGraphErrorCode";
NSString *const FBSDKGraphRequestErrorGraphErrorSubcode = @"com.facebook.sdk:FBSDKGraphRequestErrorGraphErrorSubcode";
NSString *const FBSDKGraphRequestErrorHTTPStatusCodeKey = @"com.facebook.sdk:FBSDKGraphRequestErrorHTTPStatusCodeKey";
NSString *const FBSDKGraphRequestErrorParsedJSONResponseKey = @"com.facebook.sdk:FBSDKGraphRequestErrorParsedJSONResponseKey";

View File

@ -0,0 +1,33 @@
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import <Foundation/Foundation.h>
/*!
@abstract Extension protocol for NSCopying that adds the copy method, which is implemented on NSObject.
@discussion NSObject<NSCopying> implicitly conforms to this protocol.
*/
@protocol FBSDKCopying <NSCopying, NSObject>
/*!
@abstract Implemented by NSObject as a convenience to copyWithZone:.
@return A copy of the receiver.
*/
- (id)copy;
@end

View File

@ -0,0 +1,45 @@
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import <UIKit/UIKit.h>
#import <FBSDKCoreKit/FBSDKAccessToken.h>
#import <FBSDKCoreKit/FBSDKAppEvents.h>
#import <FBSDKCoreKit/FBSDKApplicationDelegate.h>
#import <FBSDKCoreKit/FBSDKButton.h>
#import <FBSDKCoreKit/FBSDKConstants.h>
#import <FBSDKCoreKit/FBSDKCopying.h>
#import <FBSDKCoreKit/FBSDKGraphRequest.h>
#import <FBSDKCoreKit/FBSDKGraphRequestConnection.h>
#import <FBSDKCoreKit/FBSDKGraphRequestDataAttachment.h>
#import <FBSDKCoreKit/FBSDKMacros.h>
#import <FBSDKCoreKit/FBSDKSettings.h>
#import <FBSDKCoreKit/FBSDKTestUsersManager.h>
#import <FBSDKCoreKit/FBSDKUtility.h>
#if !TARGET_OS_TV
#import <FBSDKCoreKit/FBSDKAppLinkResolver.h>
#import <FBSDKCoreKit/FBSDKAppLinkUtility.h>
#import <FBSDKCoreKit/FBSDKGraphErrorRecoveryProcessor.h>
#import <FBSDKCoreKit/FBSDKMutableCopying.h>
#import <FBSDKCoreKit/FBSDKProfile.h>
#import <FBSDKCoreKit/FBSDKProfilePictureView.h>
#endif
#define FBSDK_VERSION_STRING @"4.10.0"
#define FBSDK_TARGET_PLATFORM_VERSION @"v2.5"

View File

@ -0,0 +1,97 @@
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import <Foundation/Foundation.h>
#import "FBSDKConstants.h"
@class FBSDKGraphErrorRecoveryProcessor;
@class FBSDKGraphRequest;
/*!
@abstract Defines a delegate for `FBSDKGraphErrorRecoveryProcessor`.
*/
@protocol FBSDKGraphErrorRecoveryProcessorDelegate<NSObject>
/*!
@abstract Indicates the error recovery has been attempted.
@param processor the processor instance.
@param didRecover YES if the recovery was successful.
@param error the error that that was attempted to be recovered from.
*/
- (void)processorDidAttemptRecovery:(FBSDKGraphErrorRecoveryProcessor *)processor didRecover:(BOOL)didRecover error:(NSError *)error;
@optional
/*!
@abstract Indicates the processor is about to process the error.
@param processor the processor instance.
@param error the error is about to be processed.
@discussion return NO if the processor should not process the error. For example,
if you want to prevent alerts of localized messages but otherwise perform retries and recoveries,
you could return NO for errors where userInfo[FBSDKGraphRequestErrorCategoryKey] equal to FBSDKGraphRequestErrorCategoryOther
*/
- (BOOL)processorWillProcessError:(FBSDKGraphErrorRecoveryProcessor *)processor error:(NSError *)error;
@end
/*!
@abstract Defines a type that can process Facebook NSErrors with best practices.
@discussion Facebook NSErrors can contain FBSDKErrorRecoveryAttempting instances to recover from errors, or
localized messages to present to the user. This class will process the instances as follows:
1. If the error is temporary as indicated by FBSDKGraphRequestErrorCategoryKey, assume the recovery succeeded and
notify the delegate.
2. If a FBSDKErrorRecoveryAttempting instance is available, display an alert (dispatched to main thread)
with the recovery options and call the instance's [ attemptRecoveryFromError:optionIndex:...].
3. If a FBSDKErrorRecoveryAttempting is not available, check the userInfo for FBSDKLocalizedErrorDescriptionKey
and present that in an alert (dispatched to main thread).
By default, FBSDKGraphRequests use this type to process errors and retry the request upon a successful
recovery.
Note that Facebook recovery attempters can present UI or even cause app switches (such as to login). Any such
work is dispatched to the main thread (therefore your request handlers may then run on the main thread).
Login recovery requires FBSDKLoginKit. Login will use FBSDKLoginBehaviorNative and will prompt the user
for all permissions last granted. If any are declined on the new request, the recovery is not successful but
the `[FBSDKAccessToken currentAccessToken]` might still have been updated.
.
*/
@interface FBSDKGraphErrorRecoveryProcessor : NSObject
/*!
@abstract Gets the delegate. Note this is a strong reference, and is nil'ed out after recovery is complete.
*/
@property (nonatomic, strong, readonly) id<FBSDKGraphErrorRecoveryProcessorDelegate>delegate;
/*!
@abstract Attempts to process the error, return YES if the error can be processed.
@param error the error to process.
@param request the relateed request that may be reissued.
@param delegate the delegate that will be retained until recovery is complete.
*/
- (BOOL)processError:(NSError *)error request:(FBSDKGraphRequest *)request delegate:(id<FBSDKGraphErrorRecoveryProcessorDelegate>) delegate;
/*!
@abstract The callback for FBSDKErrorRecoveryAttempting
@param didRecover if the recovery succeeded
@param contextInfo unused
*/
- (void)didPresentErrorWithRecovery:(BOOL)didRecover contextInfo:(void *)contextInfo;
@end

View File

@ -0,0 +1,159 @@
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import "FBSDKGraphErrorRecoveryProcessor.h"
#import "FBSDKCoreKit+Internal.h"
#import "FBSDKErrorRecoveryAttempter.h"
@interface FBSDKGraphErrorRecoveryProcessor()<UIAlertViewDelegate>
{
FBSDKErrorRecoveryAttempter *_recoveryAttempter;
NSError *_error;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
UIAlertView *_alertView;
#pragma clang diagnostic pop
}
@property (nonatomic, strong, readwrite) id<FBSDKGraphErrorRecoveryProcessorDelegate>delegate;
@end
@implementation FBSDKGraphErrorRecoveryProcessor
- (void)dealloc
{
_alertView.delegate = nil;
}
- (BOOL)processError:(NSError *)error request:(FBSDKGraphRequest *)request delegate:(id<FBSDKGraphErrorRecoveryProcessorDelegate>) delegate
{
self.delegate = delegate;
if ([self.delegate respondsToSelector:@selector(processorWillProcessError:error:)]) {
if (![self.delegate processorWillProcessError:self error:error]) {
return NO;
}
}
FBSDKGraphRequestErrorCategory errorCategory = [error.userInfo[FBSDKGraphRequestErrorCategoryKey] unsignedIntegerValue];
switch (errorCategory) {
case FBSDKGraphRequestErrorCategoryTransient :
[self.delegate processorDidAttemptRecovery:self didRecover:YES error:nil];
self.delegate = nil;
return YES;
case FBSDKGraphRequestErrorCategoryRecoverable :
if ([request.tokenString isEqualToString:[FBSDKAccessToken currentAccessToken].tokenString]) {
_recoveryAttempter = error.recoveryAttempter;
BOOL isLoginRecoveryAttempter = [_recoveryAttempter isKindOfClass:NSClassFromString(@"_FBSDKLoginRecoveryAttempter")];
// Set up a block to do the typical recovery work so that we can chain it for ios auth special cases.
// the block returns YES if recovery UI is started (meaning we wait for the alertviewdelegate to resume control flow).
BOOL (^standardRecoveryWork)(void) = ^BOOL{
NSArray *recoveryOptionsTitles = error.userInfo[NSLocalizedRecoveryOptionsErrorKey];
if (recoveryOptionsTitles.count > 0 && _recoveryAttempter) {
NSString *recoverySuggestion = error.userInfo[NSLocalizedRecoverySuggestionErrorKey];
_error = error;
dispatch_async(dispatch_get_main_queue(), ^{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
_alertView = [[UIAlertView alloc] initWithTitle:nil
message:recoverySuggestion
delegate:self
cancelButtonTitle:nil
otherButtonTitles:nil];
#pragma clang diagnostic pop
for (NSString *option in recoveryOptionsTitles) {
[_alertView addButtonWithTitle:option];
}
[_alertView show];
});
return YES;
}
return NO;
};
if ([request.tokenString isEqualToString:[[FBSDKSystemAccountStoreAdapter sharedInstance] accessTokenString]] &&
isLoginRecoveryAttempter) {
// special system auth case: if user has granted permissions we can simply renew. On a successful
// renew, treat this as immediately recovered without the standard alert prompty.
// (for example, this can repair expired tokens seamlessly)
[[FBSDKSystemAccountStoreAdapter sharedInstance]
renewSystemAuthorization:^(ACAccountCredentialRenewResult result, NSError *renewError) {
dispatch_async(dispatch_get_main_queue(), ^{
if (result == ACAccountCredentialRenewResultRenewed) {
[self.delegate processorDidAttemptRecovery:self didRecover:YES error:nil];
self.delegate = nil;
} else if (!standardRecoveryWork()) {
[self.delegate processorDidAttemptRecovery:self didRecover:NO error:_error];
};
});
}];
// short-circuit YES so that the renew callback resumes the control flow.
return YES;
}
return standardRecoveryWork();
}
return NO;
case FBSDKGraphRequestErrorCategoryOther :
if ([request.tokenString isEqualToString:[FBSDKAccessToken currentAccessToken].tokenString]) {
NSString *message = error.userInfo[FBSDKErrorLocalizedDescriptionKey];
NSString *title = error.userInfo[FBSDKErrorLocalizedTitleKey];
if (message) {
dispatch_async(dispatch_get_main_queue(), ^{
NSString *localizedOK =
NSLocalizedStringWithDefaultValue(@"ErrorRecovery.Alert.OK", @"FacebookSDK", [FBSDKInternalUtility bundleForStrings],
@"OK",
@"The title of the label to dismiss the alert when presenting user facing error messages");
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
[[[UIAlertView alloc] initWithTitle:title
message:message
delegate:nil
cancelButtonTitle:localizedOK
otherButtonTitles:nil] show];
#pragma clang diagnostic pop
});
}
}
return NO;
}
return NO;
}
#pragma mark - UIAlertViewDelegate
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
[_recoveryAttempter attemptRecoveryFromError:_error optionIndex:buttonIndex delegate:self didRecoverSelector:@selector(didPresentErrorWithRecovery:contextInfo:) contextInfo:nil];
_alertView.delegate = nil;
_alertView = nil;
}
#pragma clang diagnostic pop
#pragma mark - FBSDKErrorRecoveryAttempting "delegate"
- (void)didPresentErrorWithRecovery:(BOOL)didRecover contextInfo:(void *)contextInfo
{
[self.delegate processorDidAttemptRecovery:self didRecover:didRecover error:_error];
self.delegate = nil;
}
@end

View File

@ -0,0 +1,120 @@
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import <Foundation/Foundation.h>
#import <FBSDKCoreKit/FBSDKGraphRequestConnection.h>
@class FBSDKAccessToken;
/*!
@abstract Represents a request to the Facebook Graph API.
@discussion `FBSDKGraphRequest` encapsulates the components of a request (the
Graph API path, the parameters, error recovery behavior) and should be
used in conjunction with `FBSDKGraphRequestConnection` to issue the request.
Nearly all Graph APIs require an access token. Unless specified, the
`[FBSDKAccessToken currentAccessToken]` is used. Therefore, most requests
will require login first (see `FBSDKLoginManager` in FBSDKLoginKit.framework).
A `- start` method is provided for convenience for single requests.
By default, FBSDKGraphRequest will attempt to recover any errors returned from
Facebook. You can disable this via `disableErrorRecovery:`.
@see FBSDKGraphErrorRecoveryProcessor
*/
@interface FBSDKGraphRequest : NSObject
/*!
@abstract Initializes a new instance that use use `[FBSDKAccessToken currentAccessToken]`.
@param graphPath the graph path (e.g., @"me").
@param parameters the optional parameters dictionary.
*/
- (instancetype)initWithGraphPath:(NSString *)graphPath
parameters:(NSDictionary *)parameters;
/*!
@abstract Initializes a new instance that use use `[FBSDKAccessToken currentAccessToken]`.
@param graphPath the graph path (e.g., @"me").
@param parameters the optional parameters dictionary.
@param HTTPMethod the optional HTTP method. nil defaults to @"GET".
*/
- (instancetype)initWithGraphPath:(NSString *)graphPath
parameters:(NSDictionary *)parameters
HTTPMethod:(NSString *)HTTPMethod;
/*!
@abstract Initializes a new instance.
@param graphPath the graph path (e.g., @"me").
@param parameters the optional parameters dictionary.
@param tokenString the token string to use. Specifying nil will cause no token to be used.
@param version the optional Graph API version (e.g., @"v2.0"). nil defaults to FBSDK_TARGET_PLATFORM_VERSION.
@param HTTPMethod the optional HTTP method (e.g., @"POST"). nil defaults to @"GET".
*/
- (instancetype)initWithGraphPath:(NSString *)graphPath
parameters:(NSDictionary *)parameters
tokenString:(NSString *)tokenString
version:(NSString *)version
HTTPMethod:(NSString *)HTTPMethod
NS_DESIGNATED_INITIALIZER;
/*!
@abstract The request parameters.
*/
@property (nonatomic, strong, readonly) NSMutableDictionary *parameters;
/*!
@abstract The access token string used by the request.
*/
@property (nonatomic, copy, readonly) NSString *tokenString;
/*!
@abstract The Graph API endpoint to use for the request, for example "me".
*/
@property (nonatomic, copy, readonly) NSString *graphPath;
/*!
@abstract The HTTPMethod to use for the request, for example "GET" or "POST".
*/
@property (nonatomic, copy, readonly) NSString *HTTPMethod;
/*!
@abstract The Graph API version to use (e.g., "v2.0")
*/
@property (nonatomic, copy, readonly) NSString *version;
/*!
@abstract If set, disables the automatic error recovery mechanism.
@param disable whether to disable the automatic error recovery mechanism
@discussion By default, non-batched FBSDKGraphRequest instances will automatically try to recover
from errors by constructing a `FBSDKGraphErrorRecoveryProcessor` instance that
re-issues the request on successful recoveries. The re-issued request will call the same
handler as the receiver but may occur with a different `FBSDKGraphRequestConnection` instance.
This will override [FBSDKSettings setGraphErrorRecoveryDisabled:].
*/
- (void)setGraphErrorRecoveryDisabled:(BOOL)disable;
/*!
@abstract Starts a connection to the Graph API.
@param handler The handler block to call when the request completes.
*/
- (FBSDKGraphRequestConnection *)startWithCompletionHandler:(FBSDKGraphRequestHandler)handler;
@end

View File

@ -0,0 +1,204 @@
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import "FBSDKGraphRequest+Internal.h"
#import <UIKit/UIKit.h>
#import "FBSDKAccessToken.h"
#import "FBSDKCoreKit.h"
#import "FBSDKGraphRequestConnection.h"
#import "FBSDKGraphRequestDataAttachment.h"
#import "FBSDKInternalUtility.h"
#import "FBSDKLogger.h"
#import "FBSDKSettings+Internal.h"
// constants
static NSString *const kGetHTTPMethod = @"GET";
@interface FBSDKGraphRequest()
@property (nonatomic, assign) FBSDKGraphRequestFlags flags;
@end
@implementation FBSDKGraphRequest
- (instancetype)init NS_UNAVAILABLE
{
assert(0);
}
- (instancetype)initWithGraphPath:(NSString *)graphPath
parameters:(NSDictionary *)parameters {
return [self initWithGraphPath:graphPath
parameters:parameters
flags:FBSDKGraphRequestFlagNone];
}
- (instancetype)initWithGraphPath:(NSString *)graphPath
parameters:(NSDictionary *)parameters
HTTPMethod:(NSString *)HTTPMethod {
return [self initWithGraphPath:graphPath
parameters:parameters
tokenString:[FBSDKAccessToken currentAccessToken].tokenString
version:nil
HTTPMethod:HTTPMethod];
}
- (instancetype)initWithGraphPath:(NSString *)graphPath
parameters:(NSDictionary *)parameters
flags:(FBSDKGraphRequestFlags)flags {
return [self initWithGraphPath:graphPath
parameters:parameters
tokenString:[FBSDKAccessToken currentAccessToken].tokenString
HTTPMethod:nil
flags:flags];
}
- (instancetype)initWithGraphPath:(NSString *)graphPath
parameters:(NSDictionary *)parameters
tokenString:(NSString *)tokenString
HTTPMethod:(NSString *)HTTPMethod
flags:(FBSDKGraphRequestFlags)flags {
if ((self = [self initWithGraphPath:graphPath
parameters:parameters
tokenString:tokenString
version:FBSDK_TARGET_PLATFORM_VERSION
HTTPMethod:HTTPMethod])) {
self.flags |= flags;
}
return self;
}
- (instancetype)initWithGraphPath:(NSString *)graphPath
parameters:(NSDictionary *)parameters
tokenString:(NSString *)tokenString
version:(NSString *)version
HTTPMethod:(NSString *)HTTPMethod {
if ((self = [super init])) {
_tokenString = [tokenString copy];
_version = version ? [version copy] : FBSDK_TARGET_PLATFORM_VERSION;
_graphPath = [graphPath copy];
_HTTPMethod = HTTPMethod ? [HTTPMethod copy] : kGetHTTPMethod;
_parameters = [[NSMutableDictionary alloc] initWithDictionary:parameters];
if ([FBSDKSettings isGraphErrorRecoveryDisabled]) {
_flags = FBSDKGraphRequestFlagDisableErrorRecovery;
}
}
return self;
}
- (BOOL)isGraphErrorRecoveryDisabled
{
return (self.flags & FBSDKGraphRequestFlagDisableErrorRecovery);
}
- (void)setGraphErrorRecoveryDisabled:(BOOL)disable
{
if (disable) {
self.flags |= FBSDKGraphRequestFlagDisableErrorRecovery;
} else {
self.flags &= ~FBSDKGraphRequestFlagDisableErrorRecovery;
}
}
- (BOOL)hasAttachments
{
__block BOOL hasAttachments = NO;
[self.parameters enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
if ([FBSDKGraphRequest isAttachment:obj]) {
hasAttachments = YES;
*stop = YES;
}
}];
return hasAttachments;
}
+ (BOOL)isAttachment:(id)item
{
return ([item isKindOfClass:[UIImage class]] ||
[item isKindOfClass:[NSData class]] ||
[item isKindOfClass:[FBSDKGraphRequestDataAttachment class]]);
}
+ (NSString *)serializeURL:(NSString *)baseUrl
params:(NSDictionary *)params {
return [self serializeURL:baseUrl params:params httpMethod:kGetHTTPMethod];
}
+ (NSString *)serializeURL:(NSString *)baseUrl
params:(NSDictionary *)params
httpMethod:(NSString *)httpMethod {
params = [self preprocessParams: params];
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
NSURL *parsedURL = [NSURL URLWithString:[baseUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
#pragma clang pop
NSString *queryPrefix = parsedURL.query ? @"&" : @"?";
NSString *query = [FBSDKInternalUtility queryStringWithDictionary:params error:NULL invalidObjectHandler:^id(id object, BOOL *stop) {
if ([self isAttachment:object]) {
if ([httpMethod isEqualToString:kGetHTTPMethod]) {
[FBSDKLogger singleShotLogEntry:FBSDKLoggingBehaviorDeveloperErrors logEntry:@"can not use GET to upload a file"];
}
return nil;
}
return object;
}];
return [NSString stringWithFormat:@"%@%@%@", baseUrl, queryPrefix, query];
}
+ (NSDictionary *)preprocessParams:(NSDictionary *)params
{
NSString *debugValue = [FBSDKSettings graphAPIDebugParamValue];
if (debugValue) {
NSMutableDictionary *mutableParams = [NSMutableDictionary dictionaryWithDictionary:params];
[mutableParams setObject:debugValue forKey:@"debug"];
return mutableParams;
}
return params;
}
- (FBSDKGraphRequestConnection *)startWithCompletionHandler:(FBSDKGraphRequestHandler)handler
{
FBSDKGraphRequestConnection *connection = [[FBSDKGraphRequestConnection alloc] init];
[connection addRequest:self completionHandler:handler];
[connection start];
return connection;
}
#pragma mark - Debugging helpers
- (NSString *)description
{
NSMutableString *result = [NSMutableString stringWithFormat:@"<%@: %p",
NSStringFromClass([self class]),
self];
if (self.graphPath) {
[result appendFormat:@", graphPath: %@", self.graphPath];
}
if (self.HTTPMethod) {
[result appendFormat:@", HTTPMethod: %@", self.HTTPMethod];
}
[result appendFormat:@", parameters: %@>", [self.parameters description]];
return result;
}
@end

View File

@ -0,0 +1,325 @@
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import <Foundation/Foundation.h>
#import <FBSDKCoreKit/FBSDKMacros.h>
@class FBSDKGraphRequest;
@class FBSDKGraphRequestConnection;
/*!
@typedef FBSDKGraphRequestHandler
@abstract
A block that is passed to addRequest to register for a callback with the results of that
request once the connection completes.
@discussion
Pass a block of this type when calling addRequest. This will be called once
the request completes. The call occurs on the UI thread.
@param connection The `FBSDKGraphRequestConnection` that sent the request.
@param result The result of the request. This is a translation of
JSON data to `NSDictionary` and `NSArray` objects. This
is nil if there was an error.
@param error The `NSError` representing any error that occurred.
*/
typedef void (^FBSDKGraphRequestHandler)(FBSDKGraphRequestConnection *connection,
id result,
NSError *error);
/*!
@protocol
@abstract
The `FBSDKGraphRequestConnectionDelegate` protocol defines the methods used to receive network
activity progress information from a <FBSDKGraphRequestConnection>.
*/
@protocol FBSDKGraphRequestConnectionDelegate <NSObject>
@optional
/*!
@method
@abstract
Tells the delegate the request connection will begin loading
@discussion
If the <FBSDKGraphRequestConnection> is created using one of the convenience factory methods prefixed with
start, the object returned from the convenience method has already begun loading and this method
will not be called when the delegate is set.
@param connection The request connection that is starting a network request
*/
- (void)requestConnectionWillBeginLoading:(FBSDKGraphRequestConnection *)connection;
/*!
@method
@abstract
Tells the delegate the request connection finished loading
@discussion
If the request connection completes without a network error occuring then this method is called.
Invocation of this method does not indicate success of every <FBSDKGraphRequest> made, only that the
request connection has no further activity. Use the error argument passed to the FBSDKGraphRequestHandler
block to determine success or failure of each <FBSDKGraphRequest>.
This method is invoked after the completion handler for each <FBSDKGraphRequest>.
@param connection The request connection that successfully completed a network request
*/
- (void)requestConnectionDidFinishLoading:(FBSDKGraphRequestConnection *)connection;
/*!
@method
@abstract
Tells the delegate the request connection failed with an error
@discussion
If the request connection fails with a network error then this method is called. The `error`
argument specifies why the network connection failed. The `NSError` object passed to the
FBSDKGraphRequestHandler block may contain additional information.
@param connection The request connection that successfully completed a network request
@param error The `NSError` representing the network error that occurred, if any. May be nil
in some circumstances. Consult the `NSError` for the <FBSDKGraphRequest> for reliable
failure information.
*/
- (void)requestConnection:(FBSDKGraphRequestConnection *)connection
didFailWithError:(NSError *)error;
/*!
@method
@abstract
Tells the delegate how much data has been sent and is planned to send to the remote host
@discussion
The byte count arguments refer to the aggregated <FBSDKGraphRequest> objects, not a particular <FBSDKGraphRequest>.
Like `NSURLConnection`, the values may change in unexpected ways if data needs to be resent.
@param connection The request connection transmitting data to a remote host
@param bytesWritten The number of bytes sent in the last transmission
@param totalBytesWritten The total number of bytes sent to the remote host
@param totalBytesExpectedToWrite The total number of bytes expected to send to the remote host
*/
- (void)requestConnection:(FBSDKGraphRequestConnection *)connection
didSendBodyData:(NSInteger)bytesWritten
totalBytesWritten:(NSInteger)totalBytesWritten
totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite;
@end
/*!
@class FBSDKGraphRequestConnection
@abstract
The `FBSDKGraphRequestConnection` represents a single connection to Facebook to service a request.
@discussion
The request settings are encapsulated in a reusable <FBSDKGraphRequest> object. The
`FBSDKGraphRequestConnection` object encapsulates the concerns of a single communication
e.g. starting a connection, canceling a connection, or batching requests.
*/
@interface FBSDKGraphRequestConnection : NSObject
/*!
@abstract
The delegate object that receives updates.
*/
@property (nonatomic, assign) id<FBSDKGraphRequestConnectionDelegate> delegate;
/*!
@abstract Gets or sets the timeout interval to wait for a response before giving up.
*/
@property (nonatomic) NSTimeInterval timeout;
/*!
@abstract
The raw response that was returned from the server. (readonly)
@discussion
This property can be used to inspect HTTP headers that were returned from
the server.
The property is nil until the request completes. If there was a response
then this property will be non-nil during the FBSDKGraphRequestHandler callback.
*/
@property (nonatomic, retain, readonly) NSHTTPURLResponse *URLResponse;
/*!
@methodgroup Class methods
*/
/*!
@method
@abstract
This method sets the default timeout on all FBSDKGraphRequestConnection instances. Defaults to 60 seconds.
@param defaultConnectionTimeout The timeout interval.
*/
+ (void)setDefaultConnectionTimeout:(NSTimeInterval)defaultConnectionTimeout;
/*!
@methodgroup Adding requests
*/
/*!
@method
@abstract
This method adds an <FBSDKGraphRequest> object to this connection.
@param request A request to be included in the round-trip when start is called.
@param handler A handler to call back when the round-trip completes or times out.
@discussion
The completion handler is retained until the block is called upon the
completion or cancellation of the connection.
*/
- (void)addRequest:(FBSDKGraphRequest *)request
completionHandler:(FBSDKGraphRequestHandler)handler;
/*!
@method
@abstract
This method adds an <FBSDKGraphRequest> object to this connection.
@param request A request to be included in the round-trip when start is called.
@param handler A handler to call back when the round-trip completes or times out.
The handler will be invoked on the main thread.
@param name An optional name for this request. This can be used to feed
the results of one request to the input of another <FBSDKGraphRequest> in the same
`FBSDKGraphRequestConnection` as described in
[Graph API Batch Requests]( https://developers.facebook.com/docs/reference/api/batch/ ).
@discussion
The completion handler is retained until the block is called upon the
completion or cancellation of the connection. This request can be named
to allow for using the request's response in a subsequent request.
*/
- (void)addRequest:(FBSDKGraphRequest *)request
completionHandler:(FBSDKGraphRequestHandler)handler
batchEntryName:(NSString *)name;
/*!
@method
@abstract
This method adds an <FBSDKGraphRequest> object to this connection.
@param request A request to be included in the round-trip when start is called.
@param handler A handler to call back when the round-trip completes or times out.
@param batchParameters The optional dictionary of parameters to include for this request
as described in [Graph API Batch Requests]( https://developers.facebook.com/docs/reference/api/batch/ ).
Examples include "depends_on", "name", or "omit_response_on_success".
@discussion
The completion handler is retained until the block is called upon the
completion or cancellation of the connection. This request can be named
to allow for using the request's response in a subsequent request.
*/
- (void)addRequest:(FBSDKGraphRequest *)request
completionHandler:(FBSDKGraphRequestHandler)handler
batchParameters:(NSDictionary *)batchParameters;
/*!
@methodgroup Instance methods
*/
/*!
@method
@abstract
Signals that a connection should be logically terminated as the
application is no longer interested in a response.
@discussion
Synchronously calls any handlers indicating the request was cancelled. Cancel
does not guarantee that the request-related processing will cease. It
does promise that all handlers will complete before the cancel returns. A call to
cancel prior to a start implies a cancellation of all requests associated
with the connection.
*/
- (void)cancel;
/*!
@method
@abstract
This method starts a connection with the server and is capable of handling all of the
requests that were added to the connection.
@discussion By default, a connection is scheduled on the current thread in the default mode when it is created.
See `setDelegateQueue:` for other options.
This method cannot be called twice for an `FBSDKGraphRequestConnection` instance.
*/
- (void)start;
/*!
@abstract Determines the operation queue that is used to call methods on the connection's delegate.
@param queue The operation queue to use when calling delegate methods.
@discussion By default, a connection is scheduled on the current thread in the default mode when it is created.
You cannot reschedule a connection after it has started.
This is very similar to `[NSURLConnection setDelegateQueue:]`.
*/
- (void)setDelegateQueue:(NSOperationQueue *)queue;
/*!
@method
@abstract
Overrides the default version for a batch request
@discussion
The SDK automatically prepends a version part, such as "v2.0" to API paths in order to simplify API versioning
for applications. If you want to override the version part while using batch requests on the connection, call
this method to set the version for the batch request.
@param version This is a string in the form @"v2.0" which will be used for the version part of an API path
*/
- (void)overrideVersionPartWith:(NSString *)version;
@end
/*!
@abstract The key in the result dictionary for requests to old versions of the Graph API
whose response is not a JSON object.
@discussion When a request returns a non-JSON response (such as a "true" literal), that response
will be wrapped into a dictionary using this const as the key. This only applies for very few Graph API
prior to v2.1.
*/
FBSDK_EXTERN NSString *const FBSDKNonJSONResponseProperty;

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,52 @@
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import <Foundation/Foundation.h>
/*!
@abstract A container class for data attachments so that additional metadata can be provided about the attachment.
*/
@interface FBSDKGraphRequestDataAttachment : NSObject
/*!
@abstract Initializes the receiver with the attachment data and metadata.
@param data The attachment data (retained, not copied)
@param filename The filename for the attachment
@param contentType The content type for the attachment
*/
- (instancetype)initWithData:(NSData *)data
filename:(NSString *)filename
contentType:(NSString *)contentType
NS_DESIGNATED_INITIALIZER;
/*!
@abstract The content type for the attachment.
*/
@property (nonatomic, copy, readonly) NSString *contentType;
/*!
@abstract The attachment data.
*/
@property (nonatomic, strong, readonly) NSData *data;
/*!
@abstract The filename for the attachment.
*/
@property (nonatomic, copy, readonly) NSString *filename;
@end

View File

@ -0,0 +1,41 @@
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import "FBSDKGraphRequestDataAttachment.h"
#import "FBSDKMacros.h"
@implementation FBSDKGraphRequestDataAttachment
- (instancetype)initWithData:(NSData *)data filename:(NSString *)filename contentType:(NSString *)contentType
{
if ((self = [super init])) {
_data = data;
_filename = [filename copy];
_contentType = [contentType copy];
}
return self;
}
- (instancetype)init
{
FBSDK_NOT_DESIGNATED_INITIALIZER(initWithData:filename:contentType:);
return [self initWithData:nil filename:nil contentType:nil];
}
@end

View File

@ -0,0 +1,39 @@
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import <Foundation/Foundation.h>
#ifdef __cplusplus
#define FBSDK_EXTERN extern "C" __attribute__((visibility ("default")))
#else
#define FBSDK_EXTERN extern __attribute__((visibility ("default")))
#endif
#define FBSDK_STATIC_INLINE static inline
#define FBSDK_NO_DESIGNATED_INITIALIZER() \
@throw [NSException exceptionWithName:NSInvalidArgumentException \
reason:[NSString stringWithFormat:@"unrecognized selector sent to instance %p", self] \
userInfo:nil]
#define FBSDK_NOT_DESIGNATED_INITIALIZER(DESIGNATED_INITIALIZER) \
@throw [NSException exceptionWithName:NSInvalidArgumentException \
reason:[NSString stringWithFormat:@"Please use the designated initializer [%p %@]", \
self, \
NSStringFromSelector(@selector(DESIGNATED_INITIALIZER))] \
userInfo:nil]

View File

@ -0,0 +1,35 @@
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import <Foundation/Foundation.h>
#import <FBSDKCoreKit/FBSDKCopying.h>
/*!
@abstract Extension protocol for NSMutableCopying that adds the mutableCopy method, which is implemented on NSObject.
@discussion NSObject<NSCopying, NSMutableCopying> implicitly conforms to this protocol.
*/
@protocol FBSDKMutableCopying <FBSDKCopying, NSMutableCopying>
/*!
@abstract Implemented by NSObject as a convenience to mutableCopyWithZone:.
@return A mutable copy of the receiver.
*/
- (id)mutableCopy;
@end

View File

@ -0,0 +1,148 @@
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import "FBSDKMacros.h"
#import "FBSDKProfilePictureView.h"
/*!
@abstract Notification indicating that the `currentProfile` has changed.
@discussion the userInfo dictionary of the notification will contain keys
`FBSDKProfileChangeOldKey` and
`FBSDKProfileChangeNewKey`.
*/
FBSDK_EXTERN NSString *const FBSDKProfileDidChangeNotification;
/* @abstract key in notification's userInfo object for getting the old profile.
@discussion If there was no old profile, the key will not be present.
*/
FBSDK_EXTERN NSString *const FBSDKProfileChangeOldKey;
/* @abstract key in notification's userInfo object for getting the new profile.
@discussion If there is no new profile, the key will not be present.
*/
FBSDK_EXTERN NSString *const FBSDKProfileChangeNewKey;
/*!
@abstract Represents an immutable Facebook profile
@discussion This class provides a global "currentProfile" instance to more easily
add social context to your application. When the profile changes, a notification is
posted so that you can update relevant parts of your UI and is persisted to NSUserDefaults.
Typically, you will want to call `enableUpdatesOnAccessTokenChange:YES` so that
it automatically observes changes to the `[FBSDKAccessToken currentAccessToken]`.
You can use this class to build your own `FBSDKProfilePictureView` or in place of typical requests to "/me".
*/
@interface FBSDKProfile : NSObject<NSCopying, NSSecureCoding>
/*!
@abstract initializes a new instance.
@param userID the user ID
@param firstName the user's first name
@param middleName the user's middle name
@param lastName the user's last name
@param name the user's complete name
@param linkURL the link for this profile
@param refreshDate the optional date this profile was fetched. Defaults to [NSDate date].
*/
- (instancetype)initWithUserID:(NSString *)userID
firstName:(NSString *)firstName
middleName:(NSString *)middleName
lastName:(NSString *)lastName
name:(NSString *)name
linkURL:(NSURL *)linkURL
refreshDate:(NSDate *)refreshDate NS_DESIGNATED_INITIALIZER;
/*!
@abstract The user id
*/
@property (nonatomic, readonly) NSString *userID;
/*!
@abstract The user's first name
*/
@property (nonatomic, readonly) NSString *firstName;
/*!
@abstract The user's middle name
*/
@property (nonatomic, readonly) NSString *middleName;
/*!
@abstract The user's last name
*/
@property (nonatomic, readonly) NSString *lastName;
/*!
@abstract The user's complete name
*/
@property (nonatomic, readonly) NSString *name;
/*!
@abstract A URL to the user's profile.
@discussion Consider using Bolts and `FBSDKAppLinkResolver` to resolve this
to an app link to link directly to the user's profile in the Facebook app.
*/
@property (nonatomic, readonly) NSURL *linkURL;
/*!
@abstract The last time the profile data was fetched.
*/
@property (nonatomic, readonly) NSDate *refreshDate;
/*!
@abstract Gets the current FBSDKProfile instance.
*/
+ (FBSDKProfile *)currentProfile;
/*!
@abstract Sets the current instance and posts the appropriate notification if the profile parameter is different
than the receiver.
@param profile the profile to set
@discussion This persists the profile to NSUserDefaults.
*/
+ (void)setCurrentProfile:(FBSDKProfile *)profile;
/*!
@abstract Indicates if `currentProfile` will automatically observe `FBSDKAccessTokenDidChangeNotification` notifications
@param enable YES is observing
@discussion If observing, this class will issue a graph request for public profile data when the current token's userID
differs from the current profile. You can observe `FBSDKProfileDidChangeNotification` for when the profile is updated.
Note that if `[FBSDKAccessToken currentAccessToken]` is unset, the `currentProfile` instance remains. It's also possible
for `currentProfile` to return nil until the data is fetched.
*/
+ (void)enableUpdatesOnAccessTokenChange:(BOOL)enable;
/*!
@abstract A convenience method for returning a complete `NSURL` for retrieving the user's profile image.
@param mode The picture mode
@param size The height and width. This will be rounded to integer precision.
*/
- (NSURL *)imageURLForPictureMode:(FBSDKProfilePictureMode)mode size:(CGSize)size;
/*!
@abstract A convenience method for returning a Graph API path for retrieving the user's profile image.
@deprecated use `imageURLForPictureMode:size:` instead
@discussion You can pass this to a `FBSDKGraphRequest` instance to download the image.
@param mode The picture mode
@param size The height and width. This will be rounded to integer precision.
*/
- (NSString *)imagePathForPictureMode:(FBSDKProfilePictureMode)mode size:(CGSize)size
__attribute__ ((deprecated("use imageURLForPictureMode:size: instead")));
/*!
@abstract Returns YES if the profile is equivalent to the receiver.
@param profile the profile to compare to.
*/
- (BOOL)isEqualToProfile:(FBSDKProfile *)profile;
@end

View File

@ -0,0 +1,268 @@
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import "FBSDKProfile+Internal.h"
#import "FBSDKCoreKit+Internal.h"
NSString *const FBSDKProfileDidChangeNotification = @"com.facebook.sdk.FBSDKProfile.FBSDKProfileDidChangeNotification";;
NSString *const FBSDKProfileChangeOldKey = @"FBSDKProfileOld";
NSString *const FBSDKProfileChangeNewKey = @"FBSDKProfileNew";
static NSString *const FBSDKProfileUserDefaultsKey = @"com.facebook.sdk.FBSDKProfile.currentProfile";
static FBSDKProfile *g_currentProfile;
#define FBSDKPROFILE_USERID_KEY @"userID"
#define FBSDKPROFILE_FIRSTNAME_KEY @"firstName"
#define FBSDKPROFILE_MIDDLENAME_KEY @"middleName"
#define FBSDKPROFILE_LASTNAME_KEY @"lastName"
#define FBSDKPROFILE_NAME_KEY @"name"
#define FBSDKPROFILE_LINKURL_KEY @"linkURL"
#define FBSDKPROFILE_REFRESHDATE_KEY @"refreshDate"
// Once a day
#define FBSDKPROFILE_STALE_IN_SECONDS (60 * 60 * 24)
@implementation FBSDKProfile
- (instancetype)init NS_UNAVAILABLE
{
assert(0);
}
- (instancetype)initWithUserID:(NSString *)userID
firstName:(NSString *)firstName
middleName:(NSString *)middleName
lastName:(NSString *)lastName
name:(NSString *)name
linkURL:(NSURL *)linkURL
refreshDate:(NSDate *)refreshDate
{
if ((self = [super init])) {
_userID = [userID copy];
_firstName = [firstName copy];
_middleName = [middleName copy];
_lastName = [lastName copy];
_name = [name copy];
_linkURL = [linkURL copy];
_refreshDate = [refreshDate copy] ?: [NSDate date];
}
return self;
}
+ (FBSDKProfile *)currentProfile
{
return g_currentProfile;
}
+ (void)setCurrentProfile:(FBSDKProfile *)profile
{
if (profile != g_currentProfile && ![profile isEqualToProfile:g_currentProfile]) {
[[self class] cacheProfile:profile];
NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
[FBSDKInternalUtility dictionary:userInfo setObject:profile forKey:FBSDKProfileChangeNewKey];
[FBSDKInternalUtility dictionary:userInfo setObject:g_currentProfile forKey:FBSDKProfileChangeOldKey];
g_currentProfile = profile;
[[NSNotificationCenter defaultCenter] postNotificationName:FBSDKProfileDidChangeNotification
object:[self class]
userInfo:userInfo];
}
}
- (NSURL *)imageURLForPictureMode:(FBSDKProfilePictureMode)mode size:(CGSize)size
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
NSString *path = [self imagePathForPictureMode:FBSDKProfilePictureModeNormal size:size];
#pragma clang diagnostic pop
return [FBSDKInternalUtility facebookURLWithHostPrefix:@"graph"
path:path
queryParameters:nil
error:NULL];
}
- (NSString *)imagePathForPictureMode:(FBSDKProfilePictureMode)mode size:(CGSize)size
{
NSString *type;
switch (mode) {
case FBSDKProfilePictureModeNormal: type = @"normal"; break;
case FBSDKProfilePictureModeSquare: type = @"square"; break;
}
return [NSString stringWithFormat:@"%@/picture?type=%@&width=%d&height=%d",
_userID,
type,
(int) roundf(size.width),
(int) roundf(size.height)];
}
+ (void)enableUpdatesOnAccessTokenChange:(BOOL)enable
{
if (enable) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(observeChangeAccessTokenChange:)
name:FBSDKAccessTokenDidChangeNotification
object:nil];
} else {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
}
#pragma mark - NSCopying
- (instancetype)copyWithZone:(NSZone *)zone
{
//immutable
return self;
}
#pragma mark - Equality
- (NSUInteger)hash
{
NSUInteger subhashes[] = {
[self.userID hash],
[self.firstName hash],
[self.middleName hash],
[self.lastName hash],
[self.name hash],
[self.linkURL hash],
[self.refreshDate hash]
};
return [FBSDKMath hashWithIntegerArray:subhashes count:sizeof(subhashes) / sizeof(subhashes[0])];
}
- (BOOL)isEqual:(id)object
{
if (self == object) {
return YES;
}
if (![object isKindOfClass:[FBSDKProfile class]]){
return NO;
}
return [self isEqualToProfile:object];
}
- (BOOL)isEqualToProfile:(FBSDKProfile *)profile
{
return ([_userID isEqualToString:profile.userID] &&
[_firstName isEqualToString:profile.firstName] &&
[_middleName isEqualToString:profile.middleName] &&
[_lastName isEqualToString:profile.lastName] &&
[_name isEqualToString:profile.name] &&
[_linkURL isEqual:profile.linkURL] &&
[_refreshDate isEqualToDate:profile.refreshDate]);
}
#pragma mark NSCoding
+ (BOOL)supportsSecureCoding
{
return YES;
}
- (id)initWithCoder:(NSCoder *)decoder
{
NSString *userID = [decoder decodeObjectOfClass:[NSString class] forKey:FBSDKPROFILE_USERID_KEY];
NSString *firstName = [decoder decodeObjectOfClass:[NSString class] forKey:FBSDKPROFILE_FIRSTNAME_KEY];
NSString *middleName = [decoder decodeObjectOfClass:[NSString class] forKey:FBSDKPROFILE_MIDDLENAME_KEY];
NSString *lastName = [decoder decodeObjectOfClass:[NSString class] forKey:FBSDKPROFILE_LASTNAME_KEY];
NSString *name = [decoder decodeObjectOfClass:[NSString class] forKey:FBSDKPROFILE_NAME_KEY];
NSURL *linkURL = [decoder decodeObjectOfClass:[NSURL class] forKey:FBSDKPROFILE_LINKURL_KEY];
NSDate *refreshDate = [decoder decodeObjectOfClass:[NSURL class] forKey:FBSDKPROFILE_REFRESHDATE_KEY];
return [self initWithUserID:userID
firstName:firstName
middleName:middleName
lastName:lastName
name:name
linkURL:linkURL
refreshDate:refreshDate];
}
- (void)encodeWithCoder:(NSCoder *)encoder
{
[encoder encodeObject:self.userID forKey:FBSDKPROFILE_USERID_KEY];
[encoder encodeObject:self.firstName forKey:FBSDKPROFILE_FIRSTNAME_KEY];
[encoder encodeObject:self.middleName forKey:FBSDKPROFILE_MIDDLENAME_KEY];
[encoder encodeObject:self.lastName forKey:FBSDKPROFILE_LASTNAME_KEY];
[encoder encodeObject:self.name forKey:FBSDKPROFILE_NAME_KEY];
[encoder encodeObject:self.linkURL forKey:FBSDKPROFILE_LINKURL_KEY];
[encoder encodeObject:self.refreshDate forKey:FBSDKPROFILE_REFRESHDATE_KEY];
}
#pragma mark - Private
+ (void)observeChangeAccessTokenChange:(NSNotification *)notification
{
FBSDKAccessToken *token = notification.userInfo[FBSDKAccessTokenChangeNewKey];
static FBSDKGraphRequestConnection *executingRequestConnection = nil;
BOOL isStale = [[NSDate date] timeIntervalSinceDate:g_currentProfile.refreshDate] > FBSDKPROFILE_STALE_IN_SECONDS;
if (token &&
(isStale || ![g_currentProfile.userID isEqualToString:token.userID])) {
FBSDKProfile *expectedCurrentProfile = g_currentProfile;
NSString *graphPath = @"me?fields=id,first_name,middle_name,last_name,name,link";
[executingRequestConnection cancel];
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:graphPath
parameters:nil
flags:FBSDKGraphRequestFlagDoNotInvalidateTokenOnError | FBSDKGraphRequestFlagDisableErrorRecovery];
executingRequestConnection = [request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (expectedCurrentProfile != g_currentProfile) {
// current profile has already changed since request was started. Let's not overwrite.
return;
}
FBSDKProfile *profile = nil;
if (!error) {
profile = [[FBSDKProfile alloc] initWithUserID:result[@"id"]
firstName:result[@"first_name"]
middleName:result[@"middle_name"]
lastName:result[@"last_name"]
name:result[@"name"]
linkURL:[NSURL URLWithString:result[@"link"]]
refreshDate:[NSDate date]];
}
[[self class] setCurrentProfile:profile];
}];
}
}
@end
@implementation FBSDKProfile(Internal)
+ (void)cacheProfile:(FBSDKProfile *) profile
{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
if (profile) {
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:profile];
[userDefaults setObject:data forKey:FBSDKProfileUserDefaultsKey];
} else {
[userDefaults removeObjectForKey:FBSDKProfileUserDefaultsKey];
}
[userDefaults synchronize];
}
+ (FBSDKProfile *)fetchCachedProfile
{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSData *data = [userDefaults objectForKey:FBSDKProfileUserDefaultsKey];
return (data != nil)
? [NSKeyedUnarchiver unarchiveObjectWithData:data]
: nil;
}
@end

View File

@ -0,0 +1,59 @@
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import <UIKit/UIKit.h>
/*!
@typedef FBSDKProfilePictureMode enum
@abstract Defines the aspect ratio mode for the source image of the profile picture.
*/
typedef NS_ENUM(NSUInteger, FBSDKProfilePictureMode)
{
/*!
@abstract A square cropped version of the image will be included in the view.
*/
FBSDKProfilePictureModeSquare,
/*!
@abstract The original picture's aspect ratio will be used for the source image in the view.
*/
FBSDKProfilePictureModeNormal,
};
/*!
@abstract A view to display a profile picture.
*/
@interface FBSDKProfilePictureView : UIView
/*!
@abstract The mode for the receiver to determine the aspect ratio of the source image.
*/
@property (nonatomic, assign) FBSDKProfilePictureMode pictureMode;
/*!
@abstract The profile ID to show the picture for.
*/
@property (nonatomic, copy) NSString *profileID;
/*!
@abstract Explicitly marks the receiver as needing to update the image.
@discussion This method is called whenever any properties that affect the source image are modified, but this can also
be used to trigger a manual update of the image if it needs to be re-downloaded.
*/
- (void)setNeedsImageUpdate;
@end

View File

@ -0,0 +1,368 @@
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import "FBSDKProfilePictureView.h"
#import "FBSDKAccessToken.h"
#import "FBSDKInternalUtility.h"
#import "FBSDKMaleSilhouetteIcon.h"
#import "FBSDKMath.h"
#import "FBSDKURLConnection.h"
#import "FBSDKUtility.h"
@interface FBSDKProfilePictureViewState : NSObject
- (instancetype)initWithProfileID:(NSString *)profileID
size:(CGSize)size
scale:(CGFloat)scale
pictureMode:(FBSDKProfilePictureMode)pictureMode
imageShouldFit:(BOOL)imageShouldFit;
@property (nonatomic, assign, readonly) BOOL imageShouldFit;
@property (nonatomic, assign, readonly) FBSDKProfilePictureMode pictureMode;
@property (nonatomic, copy, readonly) NSString *profileID;
@property (nonatomic, assign, readonly) CGFloat scale;
@property (nonatomic, assign, readonly) CGSize size;
- (BOOL)isEqualToState:(FBSDKProfilePictureViewState *)other;
- (BOOL)isValidForState:(FBSDKProfilePictureViewState *)other;
@end
@implementation FBSDKProfilePictureViewState
- (instancetype)initWithProfileID:(NSString *)profileID
size:(CGSize)size
scale:(CGFloat)scale
pictureMode:(FBSDKProfilePictureMode)pictureMode
imageShouldFit:(BOOL)imageShouldFit
{
if ((self = [super init])) {
_profileID = [profileID copy];
_size = size;
_scale = scale;
_pictureMode = pictureMode;
_imageShouldFit = imageShouldFit;
}
return self;
}
- (NSUInteger)hash
{
NSUInteger subhashes[] = {
(NSUInteger)_imageShouldFit,
(NSUInteger)_size.width,
(NSUInteger)_size.height,
(NSUInteger)_scale,
(NSUInteger)_pictureMode,
[_profileID hash],
};
return [FBSDKMath hashWithIntegerArray:subhashes count:sizeof(subhashes) / sizeof(subhashes[0])];
}
- (BOOL)isEqual:(id)object
{
if (![object isKindOfClass:[FBSDKProfilePictureViewState class]]) {
return NO;
}
FBSDKProfilePictureViewState *other = (FBSDKProfilePictureViewState *)object;
return [self isEqualToState:other];
}
- (BOOL)isEqualToState:(FBSDKProfilePictureViewState *)other
{
return ([self isValidForState:other] &&
CGSizeEqualToSize(_size, other->_size) &&
(_scale == other->_scale));
}
- (BOOL)isValidForState:(FBSDKProfilePictureViewState *)other
{
return (other != nil &&
(_imageShouldFit == other->_imageShouldFit) &&
(_pictureMode == other->_pictureMode) &&
[FBSDKInternalUtility object:_profileID isEqualToObject:other->_profileID]);
}
@end
@implementation FBSDKProfilePictureView
{
BOOL _hasProfileImage;
UIImageView *_imageView;
FBSDKProfilePictureViewState *_lastState;
BOOL _needsImageUpdate;
BOOL _placeholderImageIsValid;
}
#pragma mark - Object Lifecycle
- (instancetype)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame])) {
[self _configureProfilePictureView];
}
return self;
}
- (id)initWithCoder:(NSCoder *)decoder
{
if ((self = [super initWithCoder:decoder])) {
[self _configureProfilePictureView];
}
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark - Properties
- (void)setBounds:(CGRect)bounds
{
CGRect currentBounds = self.bounds;
if (!CGRectEqualToRect(currentBounds, bounds)) {
[super setBounds:bounds];
if (!CGSizeEqualToSize(currentBounds.size, bounds.size)) {
_placeholderImageIsValid = NO;
[self setNeedsImageUpdate];
}
}
}
- (UIViewContentMode)contentMode
{
return _imageView.contentMode;
}
- (void)setContentMode:(UIViewContentMode)contentMode
{
if (_imageView.contentMode != contentMode) {
_imageView.contentMode = contentMode;
[super setContentMode:contentMode];
[self setNeedsImageUpdate];
}
}
- (void)setMode:(FBSDKProfilePictureMode)pictureMode
{
if (_pictureMode != pictureMode) {
_pictureMode = pictureMode;
[self setNeedsImageUpdate];
}
}
- (void)setProfileID:(NSString *)profileID
{
if (![FBSDKInternalUtility object:_profileID isEqualToObject:profileID]) {
_profileID = [profileID copy];
_placeholderImageIsValid = NO;
[self setNeedsImageUpdate];
}
}
#pragma mark - Public Methods
- (void)setNeedsImageUpdate
{
if (!_imageView || CGRectIsEmpty(self.bounds)) {
// we can't do anything with an empty view, so just bail out until we have a size
return;
}
// ensure that we have an image. do this here so we can draw the placeholder image synchronously if we don't have one
if (!_placeholderImageIsValid && !_hasProfileImage) {
[self _setPlaceholderImage];
}
// debounce calls to needsImage against the main runloop
if (_needsImageUpdate) {
return;
}
_needsImageUpdate = YES;
__weak FBSDKProfilePictureView *weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf _needsImageUpdate];
});
}
#pragma mark - Helper Methods
+ (void)_downloadImageWithState:(FBSDKProfilePictureViewState *)state
completionBlock:(void(^)(NSData *data))completionBlock;
{
NSURL *imageURL = [self _imageURLWithState:state];
if (!imageURL) {
return;
}
FBSDKURLConnectionHandler completionHandler = ^(FBSDKURLConnection *connection,
NSError *error,
NSURLResponse *response,
NSData *responseData) {
if (!error && [responseData length]) {
completionBlock(responseData);
}
};
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:imageURL];
[[[FBSDKURLConnection alloc] initWithRequest:request completionHandler:completionHandler] start];
}
+ (NSURL *)_imageURLWithState:(FBSDKProfilePictureViewState *)state
{
FBSDKAccessToken *accessToken = [FBSDKAccessToken currentAccessToken];
if ([state.profileID isEqualToString:@"me"] && !accessToken) {
return nil;
}
NSString *path = [[NSString alloc] initWithFormat:@"/%@/picture", [FBSDKUtility URLEncode:state.profileID]];
CGSize size = state.size;
NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
parameters[@"width"] = @(size.width);
parameters[@"height"] = @(size.height);
[FBSDKInternalUtility dictionary:parameters setObject:accessToken.tokenString forKey:@"access_token"];
return [FBSDKInternalUtility facebookURLWithHostPrefix:@"graph" path:path queryParameters:parameters error:NULL];
}
- (void)_accessTokenDidChangeNotification:(NSNotification *)notification
{
if (![_profileID isEqualToString:@"me"] || !notification.userInfo[FBSDKAccessTokenDidChangeUserID]) {
return;
}
_lastState = nil;
[self setNeedsImageUpdate];
}
- (void)_configureProfilePictureView
{
_imageView = [[UIImageView alloc] initWithFrame:self.bounds];
_imageView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
[self addSubview:_imageView];
_profileID = @"me";
self.backgroundColor = [UIColor whiteColor];
self.contentMode = UIViewContentModeScaleAspectFit;
self.userInteractionEnabled = NO;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(_accessTokenDidChangeNotification:)
name:FBSDKAccessTokenDidChangeNotification
object:nil];
[self setNeedsImageUpdate];
}
- (BOOL)_imageShouldFit
{
switch (self.contentMode) {
case UIViewContentModeBottom:
case UIViewContentModeBottomLeft:
case UIViewContentModeBottomRight:
case UIViewContentModeCenter:
case UIViewContentModeLeft:
case UIViewContentModeRedraw:
case UIViewContentModeRight:
case UIViewContentModeScaleAspectFit:
case UIViewContentModeTop:
case UIViewContentModeTopLeft:
case UIViewContentModeTopRight:
return YES;
case UIViewContentModeScaleAspectFill:
case UIViewContentModeScaleToFill:
return NO;
}
}
- (CGSize)_imageSize:(BOOL)imageShouldFit scale:(CGFloat)scale
{
// get the image size based on the contentMode and pictureMode
CGSize size = self.bounds.size;
switch (_pictureMode) {
case FBSDKProfilePictureModeSquare:{
CGFloat imageSize;
if (imageShouldFit) {
imageSize = MIN(size.width, size.height);
} else {
imageSize = MAX(size.width, size.height);
}
size = CGSizeMake(imageSize, imageSize);
break;
}
case FBSDKProfilePictureModeNormal:
// use the bounds size
break;
}
// adjust for the screen scale
size = CGSizeMake(size.width * scale, size.height * scale);
return size;
}
- (void)_needsImageUpdate
{
_needsImageUpdate = NO;
if (!_profileID) {
if (!_placeholderImageIsValid) {
[self _setPlaceholderImage];
}
return;
}
// if the current image is no longer representative of the current state, clear the current value out; otherwise,
// leave the current value until the new resolution image is downloaded
BOOL imageShouldFit = [self _imageShouldFit];
UIScreen *screen = self.window.screen ?: [UIScreen mainScreen];
CGFloat scale = [screen scale];
CGSize imageSize = [self _imageSize:imageShouldFit scale:scale];
FBSDKProfilePictureViewState *state = [[FBSDKProfilePictureViewState alloc] initWithProfileID:_profileID
size:imageSize
scale:scale
pictureMode:_pictureMode
imageShouldFit:imageShouldFit];
if (![_lastState isValidForState:state]) {
[self _setPlaceholderImage];
}
_lastState = state;
__weak FBSDKProfilePictureView *weakSelf = self;
[[self class] _downloadImageWithState:state completionBlock:^(NSData *data) {
[weakSelf _updateImageWithData:data state:state];
}];
}
- (void)_setPlaceholderImage
{
UIColor *fillColor = [UIColor colorWithRed:157.0/255.0 green:177.0/255.0 blue:204.0/255.0 alpha:1.0];
_imageView.image = [[[FBSDKMaleSilhouetteIcon alloc] initWithColor:fillColor] imageWithSize:_imageView.bounds.size];
_placeholderImageIsValid = YES;
_hasProfileImage = NO;
}
- (void)_updateImageWithData:(NSData *)data state:(FBSDKProfilePictureViewState *)state
{
// make sure we haven't updated the state since we began fetching the image
if (![state isValidForState:_lastState]) {
return;
}
UIImage *image = [[UIImage alloc] initWithData:data scale:state.scale];
_imageView.image = image;
_hasProfileImage = YES;
}
@end

View File

@ -0,0 +1,209 @@
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import <UIKit/UIKit.h>
#import <FBSDKCoreKit/FBSDKMacros.h>
/*
* Constants defining logging behavior. Use with <[FBSDKSettings setLoggingBehavior]>.
*/
/*! Include access token in logging. */
FBSDK_EXTERN NSString *const FBSDKLoggingBehaviorAccessTokens;
/*! Log performance characteristics */
FBSDK_EXTERN NSString *const FBSDKLoggingBehaviorPerformanceCharacteristics;
/*! Log FBSDKAppEvents interactions */
FBSDK_EXTERN NSString *const FBSDKLoggingBehaviorAppEvents;
/*! Log Informational occurrences */
FBSDK_EXTERN NSString *const FBSDKLoggingBehaviorInformational;
/*! Log cache errors. */
FBSDK_EXTERN NSString *const FBSDKLoggingBehaviorCacheErrors;
/*! Log errors from SDK UI controls */
FBSDK_EXTERN NSString *const FBSDKLoggingBehaviorUIControlErrors;
/*! Log debug warnings from API response, i.e. when friends fields requested, but user_friends permission isn't granted. */
FBSDK_EXTERN NSString *const FBSDKLoggingBehaviorGraphAPIDebugWarning;
/*! Log warnings from API response, i.e. when requested feature will be deprecated in next version of API.
Info is the lowest level of severity, using it will result in logging all previously mentioned levels.
*/
FBSDK_EXTERN NSString *const FBSDKLoggingBehaviorGraphAPIDebugInfo;
/*! Log errors from SDK network requests */
FBSDK_EXTERN NSString *const FBSDKLoggingBehaviorNetworkRequests;
/*! Log errors likely to be preventable by the developer. This is in the default set of enabled logging behaviors. */
FBSDK_EXTERN NSString *const FBSDKLoggingBehaviorDeveloperErrors;
@interface FBSDKSettings : NSObject
/*!
@abstract Get the Facebook App ID used by the SDK.
@discussion If not explicitly set, the default will be read from the application's plist (FacebookAppID).
*/
+ (NSString *)appID;
/*!
@abstract Set the Facebook App ID to be used by the SDK.
@param appID The Facebook App ID to be used by the SDK.
*/
+ (void)setAppID:(NSString *)appID;
/*!
@abstract Get the default url scheme suffix used for sessions.
@discussion If not explicitly set, the default will be read from the application's plist (FacebookUrlSchemeSuffix).
*/
+ (NSString *)appURLSchemeSuffix;
/*!
@abstract Set the app url scheme suffix used by the SDK.
@param appURLSchemeSuffix The url scheme suffix to be used by the SDK.
*/
+ (void)setAppURLSchemeSuffix:(NSString *)appURLSchemeSuffix;
/*!
@abstract Retrieve the Client Token that has been set via [FBSDKSettings setClientToken].
@discussion If not explicitly set, the default will be read from the application's plist (FacebookClientToken).
*/
+ (NSString *)clientToken;
/*!
@abstract Sets the Client Token for the Facebook App.
@discussion This is needed for certain API calls when made anonymously, without a user-based access token.
@param clientToken The Facebook App's "client token", which, for a given appid can be found in the Security
section of the Advanced tab of the Facebook App settings found at <https://developers.facebook.com/apps/[your-app-id]>
*/
+ (void)setClientToken:(NSString *)clientToken;
/*!
@abstract A convenient way to toggle error recovery for all FBSDKGraphRequest instances created after this is set.
@param disableGraphErrorRecovery YES or NO.
*/
+ (void)setGraphErrorRecoveryDisabled:(BOOL)disableGraphErrorRecovery;
/*!
@abstract Get the Facebook Display Name used by the SDK.
@discussion If not explicitly set, the default will be read from the application's plist (FacebookDisplayName).
*/
+ (NSString *)displayName;
/*!
@abstract Set the default Facebook Display Name to be used by the SDK.
@discussion This should match the Display Name that has been set for the app with the corresponding Facebook App ID,
in the Facebook App Dashboard.
@param displayName The Facebook Display Name to be used by the SDK.
*/
+ (void)setDisplayName:(NSString *)displayName;
/*!
@abstract Get the Facebook domain part.
@discussion If not explicitly set, the default will be read from the application's plist (FacebookDomainPart).
*/
+ (NSString *)facebookDomainPart;
/*!
@abstract Set the subpart of the Facebook domain.
@discussion This can be used to change the Facebook domain (e.g. @"beta") so that requests will be sent to
graph.beta.facebook.com
@param facebookDomainPart The domain part to be inserted into facebook.com.
*/
+ (void)setFacebookDomainPart:(NSString *)facebookDomainPart;
/*!
@abstract The quality of JPEG images sent to Facebook from the SDK.
@discussion If not explicitly set, the default is 0.9.
@see [UIImageJPEGRepresentation](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIKitFunctionReference/#//apple_ref/c/func/UIImageJPEGRepresentation) */
+ (CGFloat)JPEGCompressionQuality;
/*!
@abstract Set the quality of JPEG images sent to Facebook from the SDK.
@param JPEGCompressionQuality The quality for JPEG images, expressed as a value from 0.0 to 1.0.
@see [UIImageJPEGRepresentation](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIKitFunctionReference/#//apple_ref/c/func/UIImageJPEGRepresentation) */
+ (void)setJPEGCompressionQuality:(CGFloat)JPEGCompressionQuality;
/*!
@abstract
Gets whether data such as that generated through FBSDKAppEvents and sent to Facebook should be restricted from being used for other than analytics and conversions. Defaults to NO. This value is stored on the device and persists across app launches.
*/
+ (BOOL)limitEventAndDataUsage;
/*!
@abstract
Sets whether data such as that generated through FBSDKAppEvents and sent to Facebook should be restricted from being used for other than analytics and conversions. Defaults to NO. This value is stored on the device and persists across app launches.
@param limitEventAndDataUsage The desired value.
*/
+ (void)setLimitEventAndDataUsage:(BOOL)limitEventAndDataUsage;
/*!
@abstract Retrieve the current iOS SDK version.
*/
+ (NSString *)sdkVersion;
/*!
@abstract Retrieve the current Facebook SDK logging behavior.
*/
+ (NSSet *)loggingBehavior;
/*!
@abstract Set the current Facebook SDK logging behavior. This should consist of strings defined as
constants with FBSDKLoggingBehavior*.
@param loggingBehavior A set of strings indicating what information should be logged. If nil is provided, the logging
behavior is reset to the default set of enabled behaviors. Set to an empty set in order to disable all logging.
@discussion You can also define this via an array in your app plist with key "FacebookLoggingBehavior" or add and remove individual values via enableLoggingBehavior: or disableLogginBehavior:
*/
+ (void)setLoggingBehavior:(NSSet *)loggingBehavior;
/*!
@abstract Enable a particular Facebook SDK logging behavior.
@param loggingBehavior The LoggingBehavior to enable. This should be a string defined as a constant with FBSDKLoggingBehavior*.
*/
+ (void)enableLoggingBehavior:(NSString *)loggingBehavior;
/*!
@abstract Disable a particular Facebook SDK logging behavior.
@param loggingBehavior The LoggingBehavior to disable. This should be a string defined as a constant with FBSDKLoggingBehavior*.
*/
+ (void)disableLoggingBehavior:(NSString *)loggingBehavior;
/*!
@abstract Set the user defaults key used by legacy token caches.
@param tokenInformationKeyName the key used by legacy token caches.
@discussion Use this only if you customized FBSessionTokenCachingStrategy in v3.x of
the Facebook SDK for iOS.
*/
+ (void)setLegacyUserDefaultTokenInformationKeyName:(NSString *)tokenInformationKeyName;
/*!
@abstract Get the user defaults key used by legacy token caches.
*/
+ (NSString *)legacyUserDefaultTokenInformationKeyName;
@end

View File

@ -0,0 +1,223 @@
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import "FBSDKSettings+Internal.h"
#import "FBSDKCoreKit.h"
#define FBSDKSETTINGS_PLIST_CONFIGURATION_SETTING_IMPL(TYPE, PLIST_KEY, GETTER, SETTER, DEFAULT_VALUE) \
static TYPE *g_##PLIST_KEY = nil; \
+ (TYPE *)GETTER \
{ \
if (!g_##PLIST_KEY) { \
g_##PLIST_KEY = [[[NSBundle mainBundle] objectForInfoDictionaryKey:@#PLIST_KEY] copy] ?: DEFAULT_VALUE; \
} \
return g_##PLIST_KEY; \
} \
+ (void)SETTER:(TYPE *)value { \
g_##PLIST_KEY = [value copy]; \
}
NSString *const FBSDKLoggingBehaviorAccessTokens = @"include_access_tokens";
NSString *const FBSDKLoggingBehaviorPerformanceCharacteristics = @"perf_characteristics";
NSString *const FBSDKLoggingBehaviorAppEvents = @"app_events";
NSString *const FBSDKLoggingBehaviorInformational = @"informational";
NSString *const FBSDKLoggingBehaviorCacheErrors = @"cache_errors";
NSString *const FBSDKLoggingBehaviorUIControlErrors = @"ui_control_errors";
NSString *const FBSDKLoggingBehaviorDeveloperErrors = @"developer_errors";
NSString *const FBSDKLoggingBehaviorGraphAPIDebugWarning = @"graph_api_debug_warning";
NSString *const FBSDKLoggingBehaviorGraphAPIDebugInfo = @"graph_api_debug_info";
NSString *const FBSDKLoggingBehaviorNetworkRequests = @"network_requests";
static FBSDKAccessTokenCache *g_tokenCache;
static NSMutableSet *g_loggingBehavior;
static NSString *g_legacyUserDefaultTokenInformationKeyName = @"FBAccessTokenInformationKey";
static NSString *const FBSDKSettingsLimitEventAndDataUsage = @"com.facebook.sdk:FBSDKSettingsLimitEventAndDataUsage";
static BOOL g_disableErrorRecovery;
static NSString *g_userAgentSuffix;
@implementation FBSDKSettings
+ (void)initialize
{
if (self == [FBSDKSettings class]) {
g_tokenCache = [[FBSDKAccessTokenCache alloc] init];
}
}
#pragma mark - Plist Configuration Settings
FBSDKSETTINGS_PLIST_CONFIGURATION_SETTING_IMPL(NSString, FacebookAppID, appID, setAppID, nil);
FBSDKSETTINGS_PLIST_CONFIGURATION_SETTING_IMPL(NSString, FacebookUrlSchemeSuffix, appURLSchemeSuffix, setAppURLSchemeSuffix, nil);
FBSDKSETTINGS_PLIST_CONFIGURATION_SETTING_IMPL(NSString, FacebookClientToken, clientToken, setClientToken, nil);
FBSDKSETTINGS_PLIST_CONFIGURATION_SETTING_IMPL(NSString, FacebookDisplayName, displayName, setDisplayName, nil);
FBSDKSETTINGS_PLIST_CONFIGURATION_SETTING_IMPL(NSString, FacebookDomainPart, facebookDomainPart, setFacebookDomainPart, nil);
FBSDKSETTINGS_PLIST_CONFIGURATION_SETTING_IMPL(NSNumber, FacebookJpegCompressionQuality, _JPEGCompressionQualityNumber, _setJPEGCompressionQualityNumber, @(0.9));
+ (void)setGraphErrorRecoveryDisabled:(BOOL)disableGraphErrorRecovery {
g_disableErrorRecovery = disableGraphErrorRecovery;
}
+ (BOOL)isGraphErrorRecoveryDisabled {
return g_disableErrorRecovery;
}
+ (CGFloat)JPEGCompressionQuality
{
return [[self _JPEGCompressionQualityNumber] floatValue];
}
+ (void)setJPEGCompressionQuality:(CGFloat)JPEGCompressionQuality
{
[self _setJPEGCompressionQualityNumber:@(JPEGCompressionQuality)];
}
+ (BOOL)limitEventAndDataUsage
{
NSNumber *storedValue = [[NSUserDefaults standardUserDefaults] objectForKey:FBSDKSettingsLimitEventAndDataUsage];
if (storedValue == nil) {
return NO;
}
return storedValue.boolValue;
}
+ (void)setLimitEventAndDataUsage:(BOOL)limitEventAndDataUsage
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:@(limitEventAndDataUsage) forKey:FBSDKSettingsLimitEventAndDataUsage];
[defaults synchronize];
}
+ (NSSet *)loggingBehavior
{
if (!g_loggingBehavior) {
NSArray *bundleLoggingBehaviors = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"FacebookLoggingBehavior"];
if (bundleLoggingBehaviors) {
g_loggingBehavior = [[NSMutableSet alloc] initWithArray:bundleLoggingBehaviors];
} else {
// Establish set of default enabled logging behaviors. You can completely disable logging by
// specifying an empty array for FacebookLoggingBehavior in your Info.plist.
g_loggingBehavior = [[NSMutableSet alloc] initWithObjects:FBSDKLoggingBehaviorDeveloperErrors, nil];
}
}
return [g_loggingBehavior copy];
}
+ (void)setLoggingBehavior:(NSSet *)loggingBehavior
{
if (![g_loggingBehavior isEqualToSet:loggingBehavior]) {
g_loggingBehavior = [loggingBehavior mutableCopy];
[self updateGraphAPIDebugBehavior];
}
}
+ (void)enableLoggingBehavior:(NSString *)loggingBehavior
{
if (!g_loggingBehavior) {
[self loggingBehavior];
}
[g_loggingBehavior addObject:loggingBehavior];
[self updateGraphAPIDebugBehavior];
}
+ (void)disableLoggingBehavior:(NSString *)loggingBehavior
{
if (!g_loggingBehavior) {
[self loggingBehavior];
}
[g_loggingBehavior removeObject:loggingBehavior];
[self updateGraphAPIDebugBehavior];
}
+ (void)setLegacyUserDefaultTokenInformationKeyName:(NSString *)tokenInformationKeyName
{
if (![g_legacyUserDefaultTokenInformationKeyName isEqualToString:tokenInformationKeyName]) {
g_legacyUserDefaultTokenInformationKeyName = tokenInformationKeyName;
}
}
+ (NSString *)legacyUserDefaultTokenInformationKeyName
{
return g_legacyUserDefaultTokenInformationKeyName;
}
#pragma mark - Readonly Configuration Settings
+ (NSString *)sdkVersion
{
return FBSDK_VERSION_STRING;
}
#pragma mark - Object Lifecycle
- (instancetype)init
{
FBSDK_NO_DESIGNATED_INITIALIZER();
return nil;
}
#pragma mark - Internal
+ (FBSDKAccessTokenCache *)accessTokenCache
{
return g_tokenCache;
}
- (void)setAccessTokenCache:(FBSDKAccessTokenCache *)cache
{
if (g_tokenCache != cache) {
g_tokenCache = cache;
}
}
+ (NSString *)userAgentSuffix
{
return g_userAgentSuffix;
}
+ (void)setUserAgentSuffix:(NSString *)suffix
{
if (![g_userAgentSuffix isEqualToString:suffix]) {
g_userAgentSuffix = suffix;
}
}
#pragma mark - Internal - Graph API Debug
+ (void)updateGraphAPIDebugBehavior
{
// Enable Warnings everytime Info is enabled
if ([g_loggingBehavior containsObject:FBSDKLoggingBehaviorGraphAPIDebugInfo]
&& ![g_loggingBehavior containsObject:FBSDKLoggingBehaviorGraphAPIDebugWarning]) {
[g_loggingBehavior addObject:FBSDKLoggingBehaviorGraphAPIDebugWarning];
}
}
+ (NSString *)graphAPIDebugParamValue
{
if ([[self loggingBehavior] containsObject:FBSDKLoggingBehaviorGraphAPIDebugInfo]) {
return @"info";
} else if ([[self loggingBehavior] containsObject:FBSDKLoggingBehaviorGraphAPIDebugWarning]) {
return @"warning";
}
return nil;
}
@end

View File

@ -0,0 +1,102 @@
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import <Foundation/Foundation.h>
@class FBSDKAccessToken;
/*!
@typedef
@abstract Callback block for returning an array of FBSDKAccessToken instances (and possibly `NSNull` instances); or an error.
*/
typedef void (^FBSDKTestUsersManagerRetrieveTestAccountTokensHandler)(NSArray *tokens, NSError *error) ;
/*!
@typedef
@abstract Callback block for removing a test user.
*/
typedef void (^FBSDKTestUsersManagerRemoveTestAccountHandler)(NSError *error) ;
/*!
@class FBSDKTestUsersManager
@abstract Provides methods for managing test accounts for testing Facebook integration.
@discussion Facebook allows developers to create test accounts for testing their applications'
Facebook integration (see https://developers.facebook.com/docs/test_users/). This class
simplifies use of these accounts for writing tests. It is not designed for use in
production application code.
This class will make Graph API calls on behalf of your app to manage test accounts and requires
an app id and app secret. You will typically use this class to write unit or integration tests.
Make sure you NEVER include your app secret in your production app.
*/
@interface FBSDKTestUsersManager : NSObject
/*!
@abstract construct or return the shared instance
@param appID the Facebook app id
@param appSecret the Facebook app secret
*/
+ (instancetype)sharedInstanceForAppID:(NSString *)appID appSecret:(NSString *)appSecret;
/*!
@abstract retrieve FBSDKAccessToken instances for test accounts with the specific permissions.
@param arraysOfPermissions an array of permissions sets, such as @[ [NSSet setWithObject:@"email"], [NSSet setWithObject:@"user_birthday"]]
if you needed two test accounts with email and birthday permissions, respectively. You can pass in empty nested sets
if you need two arbitrary test accounts. For convenience, passing nil is treated as @[ [NSSet set] ]
for fetching a single test user.
@param createIfNotFound if YES, new test accounts are created if no test accounts existed that fit the permissions
requirement
@param handler the callback to invoke which will return an array of `FBAccessTokenData` instances or an `NSError`.
If param `createIfNotFound` is NO, the array may contain `[NSNull null]` instances.
@discussion If you are requesting test accounts with differing number of permissions, try to order
`arrayOfPermissionsArrays` so that the most number of permissions come first to minimize creation of new
test accounts.
*/
- (void)requestTestAccountTokensWithArraysOfPermissions:(NSArray *)arraysOfPermissions
createIfNotFound:(BOOL)createIfNotFound
completionHandler:(FBSDKTestUsersManagerRetrieveTestAccountTokensHandler)handler;
/*!
@abstract add a test account with the specified permissions
@param permissions the set of permissions, e.g., [NSSet setWithObjects:@"email", @"user_friends"]
@param handler the callback handler
*/
- (void)addTestAccountWithPermissions:(NSSet *)permissions
completionHandler:(FBSDKTestUsersManagerRetrieveTestAccountTokensHandler)handler;
/*!
@abstract remove a test account for the given user id
@param userId the user id
@param handler the callback handler
*/
- (void)removeTestAccount:(NSString *)userId completionHandler:(FBSDKTestUsersManagerRemoveTestAccountHandler)handler;
/*!
@abstract Make two test users friends with each other.
@param first the token of the first user
@param second the token of the second user
@param callback the callback handler
*/
- (void)makeFriendsWithFirst:(FBSDKAccessToken *)first second:(FBSDKAccessToken *)second callback:(void (^)(NSError *))callback;
@end

View File

@ -0,0 +1,329 @@
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import "FBSDKTestUsersManager.h"
#import "FBSDKCoreKit+Internal.h"
static NSString *const kFBGraphAPITestUsersPathFormat = @"%@/accounts/test-users";
static NSString *const kAccountsDictionaryTokenKey = @"access_token";
static NSString *const kAccountsDictionaryPermissionsKey = @"permissions";
static NSMutableDictionary *gInstancesDictionary;
@interface FBSDKTestUsersManager()
- (instancetype)initWithAppID:(NSString *)appID appSecret:(NSString *)appSecret NS_DESIGNATED_INITIALIZER;
@end
@implementation FBSDKTestUsersManager
{
NSString *_appID;
NSString *_appSecret;
// dictionary with format like:
// { user_id : { kAccountsDictionaryTokenKey : "token",
// kAccountsDictionaryPermissionsKey : [ permissions ] }
NSMutableDictionary *_accounts;
}
- (instancetype)initWithAppID:(NSString *)appID appSecret:(NSString *)appSecret {
if ((self = [super init])) {
_appID = [appID copy];
_appSecret = [appSecret copy];
_accounts = [NSMutableDictionary dictionary];
}
return self;
}
- (instancetype)init
{
FBSDK_NOT_DESIGNATED_INITIALIZER(initWithAppID:appSecret:);
return [self initWithAppID:nil appSecret:nil];
}
+ (instancetype)sharedInstanceForAppID:(NSString *)appID appSecret:(NSString *)appSecret {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
gInstancesDictionary = [NSMutableDictionary dictionary];
});
NSString *instanceKey = [NSString stringWithFormat:@"%@|%@", appID, appSecret];
if (!gInstancesDictionary[instanceKey]) {
gInstancesDictionary[instanceKey] = [[FBSDKTestUsersManager alloc] initWithAppID:appID appSecret:appSecret];
}
return gInstancesDictionary[instanceKey];
}
- (void)requestTestAccountTokensWithArraysOfPermissions:(NSArray *)arraysOfPermissions
createIfNotFound:(BOOL)createIfNotFound
completionHandler:(FBSDKTestUsersManagerRetrieveTestAccountTokensHandler)handler {
arraysOfPermissions = arraysOfPermissions ?: @[[NSSet set]];
// wrap work in a block so that we can chain it to after a fetch of existing accounts if we need to.
void (^helper)(NSError *) = ^(NSError *error){
if (error) {
if (handler) {
handler(nil, error);
}
return;
}
NSMutableArray *tokenDatum = [NSMutableArray arrayWithCapacity:arraysOfPermissions.count];
NSMutableSet *collectedUserIds = [NSMutableSet setWithCapacity:arraysOfPermissions.count];
__block BOOL canInvokeHandler = YES;
__weak id weakSelf = self;
[arraysOfPermissions enumerateObjectsUsingBlock:^(NSSet *desiredPermissions, NSUInteger idx, BOOL *stop) {
NSArray* userIdAndTokenPair = [self userIdAndTokenOfExistingAccountWithPermissions:desiredPermissions skip:collectedUserIds];
if (!userIdAndTokenPair) {
if (createIfNotFound) {
[self addTestAccountWithPermissions:desiredPermissions
completionHandler:^(NSArray *tokens, NSError *addError) {
if (addError) {
if (handler) {
handler(nil, addError);
}
} else {
[weakSelf requestTestAccountTokensWithArraysOfPermissions:arraysOfPermissions
createIfNotFound:createIfNotFound
completionHandler:handler];
}
}];
// stop the enumeration (ane flag so that callback to addTestAccount* will resolve our handler now).
canInvokeHandler = NO;
*stop = YES;
return;
} else {
[tokenDatum addObject:[NSNull null]];
}
} else {
NSString *userId = userIdAndTokenPair[0];
NSString *tokenString = userIdAndTokenPair[1];
[collectedUserIds addObject:userId];
[tokenDatum addObject:[self tokenDataForTokenString:tokenString
permissions:desiredPermissions
userId:userId]];
}
}];
if (canInvokeHandler && handler) {
handler(tokenDatum, nil);
}
};
if (_accounts.count == 0) {
[self fetchExistingTestAccountsWithAfterCursor:nil handler:helper];
} else {
helper(NULL);
}
}
- (void)addTestAccountWithPermissions:(NSSet *)permissions
completionHandler:(FBSDKTestUsersManagerRetrieveTestAccountTokensHandler)handler {
NSDictionary *params = @{
@"installed" : @"true",
@"permissions" : [[permissions allObjects] componentsJoinedByString:@","],
@"access_token" : self.appAccessToken
};
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:[NSString stringWithFormat:kFBGraphAPITestUsersPathFormat, _appID]
parameters:params
tokenString:[self appAccessToken]
version:nil
HTTPMethod:@"POST"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (error) {
if (handler) {
handler(nil, error);
}
} else {
NSMutableDictionary *accountData = [NSMutableDictionary dictionaryWithCapacity:2];
accountData[kAccountsDictionaryPermissionsKey] = [NSSet setWithSet:permissions];
accountData[kAccountsDictionaryTokenKey] = result[@"access_token"];
_accounts[result[@"id"]] = accountData;
if (handler) {
FBSDKAccessToken *token = [self tokenDataForTokenString:accountData[kAccountsDictionaryTokenKey]
permissions:permissions
userId:result[@"id"]];
handler(@[token], nil);
}
}
}];
}
- (void)makeFriendsWithFirst:(FBSDKAccessToken *)first second:(FBSDKAccessToken *)second callback:(void (^)(NSError *))callback
{
__block int expectedCount = 2;
void (^complete)(NSError *) = ^(NSError *error) {
// ignore if they're already friends or pending request
if ([error.userInfo[FBSDKGraphRequestErrorGraphErrorCode] integerValue] == 522 ||
[error.userInfo[FBSDKGraphRequestErrorGraphErrorCode] integerValue] == 520) {
error = nil;
}
if (--expectedCount == 0 || error) {
callback(error);
}
};
FBSDKGraphRequest *one = [[FBSDKGraphRequest alloc] initWithGraphPath:[NSString stringWithFormat:@"%@/friends/%@", first.userID, second.userID]
parameters:nil
tokenString:first.tokenString
version:nil
HTTPMethod:@"POST"];
FBSDKGraphRequest *two = [[FBSDKGraphRequest alloc] initWithGraphPath:[NSString stringWithFormat:@"%@/friends/%@", second.userID, first.userID]
parameters:nil
tokenString:second.tokenString
version:nil
HTTPMethod:@"POST"];
FBSDKGraphRequestConnection *conn = [[FBSDKGraphRequestConnection alloc] init];
[conn addRequest:one completionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
complete(error);
} batchEntryName:@"first"];
[conn addRequest:two completionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
complete(error);
} batchParameters:@{ @"depends_on" : @"first"} ];
[conn start];
}
- (void)removeTestAccount:(NSString *)userId completionHandler:(FBSDKTestUsersManagerRemoveTestAccountHandler)handler {
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:userId
parameters:nil
tokenString:self.appAccessToken
version:nil
HTTPMethod:@"DELETE"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (handler) {
handler(error);
}
}];
[_accounts removeObjectForKey:userId];
}
#pragma mark - private methods
- (FBSDKAccessToken *)tokenDataForTokenString:(NSString *)tokenString permissions:(NSSet *)permissions userId:(NSString *)userId{
return [[FBSDKAccessToken alloc] initWithTokenString:tokenString
permissions:[permissions allObjects]
declinedPermissions:nil
appID:_appID
userID:userId
expirationDate:nil
refreshDate:nil];
}
- (NSArray *)userIdAndTokenOfExistingAccountWithPermissions:(NSSet *)permissions skip:(NSSet *)setToSkip {
__block NSString *userId = nil;
__block NSString *token = nil;
[_accounts enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSDictionary *accountData, BOOL *stop) {
if ([setToSkip containsObject:key]) {
return;
}
NSSet *accountPermissions = accountData[kAccountsDictionaryPermissionsKey];
if ([permissions isSubsetOfSet:accountPermissions]) {
token = accountData[kAccountsDictionaryTokenKey];
userId = key;
*stop = YES;
}
}];
if (userId && token) {
return @[userId, token];
} else {
return nil;
}
}
- (NSString *)appAccessToken {
return [NSString stringWithFormat:@"%@|%@", _appID, _appSecret];
}
- (void)fetchExistingTestAccountsWithAfterCursor:(NSString *)after handler:(void(^)(NSError *error))handler {
FBSDKGraphRequestConnection *connection = [[FBSDKGraphRequestConnection alloc] init];
FBSDKGraphRequest *requestForAccountIds = [[FBSDKGraphRequest alloc] initWithGraphPath:[NSString stringWithFormat:kFBGraphAPITestUsersPathFormat, _appID]
parameters:@{@"limit" : @"50",
@"after" : after ?: @"",
@"fields": @""
}
tokenString:self.appAccessToken
version:nil
HTTPMethod:nil];
__block NSString *afterCursor = nil;
__block NSInteger expectedTestAccounts = 0;
FBSDKGraphRequestConnection *permissionConnection = [[FBSDKGraphRequestConnection alloc] init];
[connection addRequest:requestForAccountIds completionHandler:^(FBSDKGraphRequestConnection *innerConnection, id result, NSError *error) {
if (error) {
if (handler) {
handler(error);
}
// on errors, clear out accounts since it may be in a bad state
[_accounts removeAllObjects];
return;
} else {
for (NSDictionary *account in result[@"data"]) {
NSString *userId = account[@"id"];
NSString *token = account[@"access_token"];
if (userId && token) {
_accounts[userId] = [NSMutableDictionary dictionaryWithCapacity:2];
_accounts[userId][kAccountsDictionaryTokenKey] = token;
expectedTestAccounts++;
[permissionConnection addRequest:[[FBSDKGraphRequest alloc] initWithGraphPath:[NSString stringWithFormat:@"%@?fields=permissions", userId]
parameters:nil
tokenString:self.appAccessToken
version:nil
HTTPMethod:nil]
completionHandler:^(FBSDKGraphRequestConnection *innerConnection2, id innerResult, NSError *innerError) {
if (_accounts.count == 0) {
// indicates an earlier error that was already passed to handler, so just short circuit.
return;
}
if (innerError) {
if (handler) {
handler(innerError);
}
[_accounts removeAllObjects];
return;
} else {
NSMutableSet *grantedPermissions = [NSMutableSet set];
NSArray *resultPermissionsDictionaries = innerResult[@"permissions"][@"data"];
[resultPermissionsDictionaries enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL *stop) {
if ([obj[@"status"] isEqualToString:@"granted"]) {
[grantedPermissions addObject:obj[@"permission"]];
}
}];
_accounts[userId][kAccountsDictionaryPermissionsKey] = grantedPermissions;
}
expectedTestAccounts--;
if (!expectedTestAccounts) {
if (afterCursor) {
[self fetchExistingTestAccountsWithAfterCursor:afterCursor handler:handler];
} else if (handler) {
handler(nil);
}
}
}
];
}
}
afterCursor = result[@"paging"][@"cursors"][@"after"];
}
if (expectedTestAccounts) {
// finished fetching ids and tokens, now kick off the request for all the permissions
[permissionConnection start];
} else {
if (handler) {
handler(nil);
}
}
}];
[connection start];
}
@end

View File

@ -0,0 +1,55 @@
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import <Foundation/Foundation.h>
/*!
@abstract Class to contain common utility methods.
*/
@interface FBSDKUtility : NSObject
/*!
@abstract Parses a query string into a dictionary.
@param queryString The query string value.
@return A dictionary with the key/value pairs.
*/
+ (NSDictionary *)dictionaryWithQueryString:(NSString *)queryString;
/*!
@abstract Constructs a query string from a dictionary.
@param dictionary The dictionary with key/value pairs for the query string.
@param errorRef If an error occurs, upon return contains an NSError object that describes the problem.
@result Query string representation of the parameters.
*/
+ (NSString *)queryStringWithDictionary:(NSDictionary *)dictionary error:(NSError *__autoreleasing *)errorRef;
/*!
@abstract Decodes a value from an URL.
@param value The value to decode.
@result The decoded value.
*/
+ (NSString *)URLDecode:(NSString *)value;
/*!
@abstract Encodes a value for an URL.
@param value The value to encode.
@result The encoded value.
*/
+ (NSString *)URLEncode:(NSString *)value;
@end

View File

@ -0,0 +1,90 @@
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import "FBSDKUtility.h"
#import "FBSDKInternalUtility.h"
#import "FBSDKMacros.h"
@implementation FBSDKUtility
+ (NSDictionary *)dictionaryWithQueryString:(NSString *)queryString
{
NSMutableDictionary *result = [[NSMutableDictionary alloc] init];
NSArray *parts = [queryString componentsSeparatedByString:@"&"];
for (NSString *part in parts) {
if ([part length] == 0) {
continue;
}
NSRange index = [part rangeOfString:@"="];
NSString *key;
NSString *value;
if (index.location == NSNotFound) {
key = part;
value = @"";
} else {
key = [part substringToIndex:index.location];
value = [part substringFromIndex:index.location + index.length];
}
key = [self URLDecode:key];
value = [self URLDecode:value];
if (key && value) {
result[key] = value;
}
}
return result;
}
+ (NSString *)queryStringWithDictionary:(NSDictionary *)dictionary error:(NSError *__autoreleasing *)errorRef
{
return [FBSDKInternalUtility queryStringWithDictionary:dictionary error:errorRef invalidObjectHandler:NULL];
}
+ (NSString *)URLDecode:(NSString *)value
{
value = [value stringByReplacingOccurrencesOfString:@"+" withString:@" "];
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
value = [value stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
#pragma clang diagnostic pop
return value;
}
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
+ (NSString *)URLEncode:(NSString *)value
{
return (__bridge_transfer NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,
(CFStringRef)value,
NULL, // characters to leave unescaped
CFSTR(":!*();@/&?+$,='"),
kCFStringEncodingUTF8);
}
#pragma clang diagnostic pop
- (instancetype)init
{
FBSDK_NO_DESIGNATED_INITIALIZER();
return nil;
}
@end

View File

@ -0,0 +1,158 @@
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import <FBSDKCoreKit/FBSDKAppEvents.h>
#import <FBSDKCoreKit/FBSDKMacros.h>
#import "FBSDKAppEventsUtility.h"
@class FBSDKGraphRequest;
// Internally known event names
FBSDK_EXTERN NSString *const FBSDKAppEventNamePurchased;
/*! Use to log that the share dialog was launched */
FBSDK_EXTERN NSString *const FBSDKAppEventNameShareSheetLaunch;
/*! Use to log that the share dialog was dismissed */
FBSDK_EXTERN NSString *const FBSDKAppEventNameShareSheetDismiss;
/*! Use to log that the permissions UI was launched */
FBSDK_EXTERN NSString *const FBSDKAppEventNamePermissionsUILaunch;
/*! Use to log that the permissions UI was dismissed */
FBSDK_EXTERN NSString *const FBSDKAppEventNamePermissionsUIDismiss;
/*! Use to log that the login view was used */
FBSDK_EXTERN NSString *const FBSDKAppEventNameLoginViewUsage;
// Internally known event parameters
/*! String parameter specifying the outcome of a dialog invocation */
FBSDK_EXTERN NSString *const FBSDKAppEventParameterDialogOutcome;
/*! Parameter key used to specify which application launches this application. */
FBSDK_EXTERN NSString *const FBSDKAppEventParameterLaunchSource;
/*! Use to log the result of a call to FBDialogs presentShareDialogWithParams: */
FBSDK_EXTERN NSString *const FBSDKAppEventNameFBDialogsPresentShareDialog;
/*! Use to log the result of a call to FBDialogs presentShareDialogWithOpenGraphActionParams: */
FBSDK_EXTERN NSString *const FBSDKAppEventNameFBDialogsPresentShareDialogOG;
/*! Use to log the result of a call to FBDialogs presentLikeDialogWithLikeParams: */
FBSDK_EXTERN NSString *const FBSDKAppEventNameFBDialogsPresentLikeDialogOG;
FBSDK_EXTERN NSString *const FBSDKAppEventNameFBDialogsPresentShareDialogPhoto;
FBSDK_EXTERN NSString *const FBSDKAppEventNameFBDialogsPresentMessageDialog;
FBSDK_EXTERN NSString *const FBSDKAppEventNameFBDialogsPresentMessageDialogPhoto;
FBSDK_EXTERN NSString *const FBSDKAppEventNameFBDialogsPresentMessageDialogOG;
/*! Use to log the start of an auth request that cannot be fulfilled by the token cache */
FBSDK_EXTERN NSString *const FBSDKAppEventNameFBSessionAuthStart;
/*! Use to log the end of an auth request that was not fulfilled by the token cache */
FBSDK_EXTERN NSString *const FBSDKAppEventNameFBSessionAuthEnd;
/*! Use to log the start of a specific auth method as part of an auth request */
FBSDK_EXTERN NSString *const FBSDKAppEventNameFBSessionAuthMethodStart;
/*! Use to log the end of the last tried auth method as part of an auth request */
FBSDK_EXTERN NSString *const FBSDKAppEventNameFBSessionAuthMethodEnd;
/*! Use to log the timestamp for the transition to the Facebook native login dialog */
FBSDK_EXTERN NSString *const FBSDKAppEventNameFBDialogsNativeLoginDialogStart;
/*! Use to log the timestamp for the transition back to the app after the Facebook native login dialog */
FBSDK_EXTERN NSString *const FBSDKAppEventNameFBDialogsNativeLoginDialogEnd;
/*! Use to log the e2e timestamp metrics for web login */
FBSDK_EXTERN NSString *const FBSDKAppEventNameFBDialogsWebLoginCompleted;
/*! Use to log the results of a share dialog */
FBSDK_EXTERN NSString *const FBSDLAppEventNameFBSDKEventShareDialogResult;
FBSDK_EXTERN NSString *const FBSDKAppEventNameFBSDKEventMessengerShareDialogResult;
FBSDK_EXTERN NSString *const FBSDKAppEventNameFBSDKEventAppInviteShareDialogResult;
FBSDK_EXTERN NSString *const FBSDKAppEventNameFBSDKEventShareDialogShow;
FBSDK_EXTERN NSString *const FBSDKAppEventNameFBSDKEventMessengerShareDialogShow;
FBSDK_EXTERN NSString *const FBSDKAppEventNameFBSDKEventAppInviteShareDialogShow;
FBSDK_EXTERN NSString *const FBSDKAppEventParameterDialogMode;
FBSDK_EXTERN NSString *const FBSDKAppEventParameterDialogShareContentType;
// Internally known event parameter values
FBSDK_EXTERN NSString *const FBSDKAppEventsDialogOutcomeValue_Completed;
FBSDK_EXTERN NSString *const FBSDKAppEventsDialogOutcomeValue_Cancelled;
FBSDK_EXTERN NSString *const FBSDKAppEventsDialogOutcomeValue_Failed;
FBSDK_EXTERN NSString *const FBSDKAppEventsDialogShareContentTypeOpenGraph;
FBSDK_EXTERN NSString *const FBSDKAppEventsDialogShareContentTypeStatus;
FBSDK_EXTERN NSString *const FBSDKAppEventsDialogShareContentTypePhoto;
FBSDK_EXTERN NSString *const FBSDKAppEventsDialogShareContentTypeVideo;
FBSDK_EXTERN NSString *const FBSDKAppEventsDialogShareContentTypeUnknown;
FBSDK_EXTERN NSString *const FBSDKAppEventsDialogShareModeAutomatic;
FBSDK_EXTERN NSString *const FBSDKAppEventsDialogShareModeBrowser;
FBSDK_EXTERN NSString *const FBSDKAppEventsDialogShareModeNative;
FBSDK_EXTERN NSString *const FBSDKAppEventsDialogShareModeShareSheet;
FBSDK_EXTERN NSString *const FBSDKAppEventsDialogShareModeWeb;
FBSDK_EXTERN NSString *const FBSDKAppEventsDialogShareModeFeedBrowser;
FBSDK_EXTERN NSString *const FBSDKAppEventsDialogShareModeFeedWeb;
FBSDK_EXTERN NSString *const FBSDKAppEventsDialogShareModeUnknown;
FBSDK_EXTERN NSString *const FBSDKAppEventsNativeLoginDialogStartTime;
FBSDK_EXTERN NSString *const FBSDKAppEventsNativeLoginDialogEndTime;
FBSDK_EXTERN NSString *const FBSDKAppEventsWebLoginE2E;
FBSDK_EXTERN NSString *const FBSDKAppEventNameFBSDKLikeButtonImpression;
FBSDK_EXTERN NSString *const FBSDKAppEventNameFBSDKLoginButtonImpression;
FBSDK_EXTERN NSString *const FBSDKAppEventNameFBSDKSendButtonImpression;
FBSDK_EXTERN NSString *const FBSDKAppEventNameFBSDKShareButtonImpression;
FBSDK_EXTERN NSString *const FBSDKAppEventNameFBSDKLikeButtonDidTap;
FBSDK_EXTERN NSString *const FBSDKAppEventNameFBSDKLoginButtonDidTap;
FBSDK_EXTERN NSString *const FBSDKAppEventNameFBSDKSendButtonDidTap;
FBSDK_EXTERN NSString *const FBSDKAppEventNameFBSDKShareButtonDidTap;
FBSDK_EXTERN NSString *const FBSDKAppEventNameFBSDKLikeControlDidDisable;
FBSDK_EXTERN NSString *const FBSDKAppEventNameFBSDKLikeControlDidLike;
FBSDK_EXTERN NSString *const FBSDKAppEventNameFBSDKLikeControlDidPresentDialog;
FBSDK_EXTERN NSString *const FBSDKAppEventNameFBSDKLikeControlDidTap;
FBSDK_EXTERN NSString *const FBSDKAppEventNameFBSDKLikeControlDidUnlike;
FBSDK_EXTERN NSString *const FBSDKAppEventNameFBSDKLikeControlError;
FBSDK_EXTERN NSString *const FBSDKAppEventNameFBSDKLikeControlImpression;
FBSDK_EXTERN NSString *const FBSDKAppEventNameFBSDKLikeControlNetworkUnavailable;
FBSDK_EXTERN NSString *const FBSDKAppEventParameterDialogErrorMessage;
@interface FBSDKAppEvents (Internal)
+ (void)logImplicitEvent:(NSString *)eventName
valueToSum:(NSNumber *)valueToSum
parameters:(NSDictionary *)parameters
accessToken:(FBSDKAccessToken *)accessToken;
+ (FBSDKAppEvents *)singleton;
- (void)flushForReason:(FBSDKAppEventsFlushReason)flushReason;
@end

View File

@ -0,0 +1,25 @@
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import <Foundation/Foundation.h>
@interface FBSDKAppEventsDeviceInfo : NSObject
+ (void)extendDictionaryWithDeviceInfo:(NSMutableDictionary *)dictionary;
@end

View File

@ -0,0 +1,258 @@
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import "FBSDKAppEventsDeviceInfo.h"
#import <sys/sysctl.h>
#import <sys/utsname.h>
#if !TARGET_OS_TV
#import <CoreTelephony/CTCarrier.h>
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#endif
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "FBSDKAppEvents+Internal.h"
#import "FBSDKDynamicFrameworkLoader.h"
#import "FBSDKInternalUtility.h"
#import "FBSDKUtility.h"
#define FB_ARRAY_COUNT(x) sizeof(x) / sizeof(x[0])
static const u_int FB_GROUP1_RECHECK_DURATION = 30 * 60; // seconds
// Apple reports storage in binary gigabytes (1024^3) in their About menus, etc.
static const u_int FB_GIGABYTE = 1024 * 1024 * 1024; // bytes
@implementation FBSDKAppEventsDeviceInfo
// Ephemeral data, may change during the lifetime of an app. We collect them in different
// 'group' frequencies - group1 may gets collected once every 30 minutes.
// group1
NSString *_carrierName;
NSString *_timeZoneAbbrev;
unsigned long long _remainingDiskSpaceGB;
// Persistent data, but we maintain it to make rebuilding the device info as fast as possible.
NSString *_bundleIdentifier;
NSString *_longVersion;
NSString *_shortVersion;
NSString *_sysVersion;
NSString *_machine;
NSString *_language;
unsigned long long _totalDiskSpaceGB;
unsigned long long _coreCount;
CGFloat _width;
CGFloat _height;
CGFloat _density;
// Other state
long _lastGroup1CheckTime;
BOOL _isEncodingDirty = YES;
NSString *_encodedDeviceInfo;
static FBSDKAppEventsDeviceInfo *g_singleton;
#pragma mark - Public Methods
+ (void)extendDictionaryWithDeviceInfo:(NSMutableDictionary *)dictionary
{
dictionary[@"extinfo"] = [g_singleton encodedDeviceInfo];
}
#pragma mark - Internal Methods
+ (void)initialize
{
if (self == [FBSDKAppEventsDeviceInfo class]) {
g_singleton = [[FBSDKAppEventsDeviceInfo alloc] init];
[g_singleton _collectPersistentData];
}
}
- (NSString *)encodedDeviceInfo
{
@synchronized (self) {
BOOL isGroup1Expired = [self _isGroup1Expired];
BOOL isEncodingExpired = isGroup1Expired; // Can || other groups in if we add them
// As long as group1 hasn't expired, we can just return the last generated value
if (_encodedDeviceInfo && !isEncodingExpired) {
return _encodedDeviceInfo;
}
if (isGroup1Expired) {
[self _collectGroup1Data];
}
if (_isEncodingDirty) {
self.encodedDeviceInfo = [self _generateEncoding];
_isEncodingDirty = NO;
}
return _encodedDeviceInfo;
}
}
- (void)setEncodedDeviceInfo:(NSString *)encodedDeviceInfo
{
@synchronized (self) {
if (![_encodedDeviceInfo isEqualToString:encodedDeviceInfo]) {
_encodedDeviceInfo = [encodedDeviceInfo copy];
}
}
}
// This data need only be collected once.
- (void)_collectPersistentData
{
// Bundle stuff
NSBundle *mainBundle = [NSBundle mainBundle];
_bundleIdentifier = mainBundle.bundleIdentifier;
_longVersion = [mainBundle objectForInfoDictionaryKey:@"CFBundleVersion"];
_shortVersion = [mainBundle objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
// Locale stuff
_language = [[NSLocale currentLocale] localeIdentifier];
// Device stuff
UIDevice *device = [UIDevice currentDevice];
_sysVersion = device.systemVersion;
_coreCount = [FBSDKAppEventsDeviceInfo _coreCount];
UIScreen *sc = [UIScreen mainScreen];
CGRect sr = sc.bounds;
_width = sr.size.width;
_height = sr.size.height;
_density = sc.scale;
struct utsname systemInfo;
uname(&systemInfo);
_machine = @(systemInfo.machine);
// Disk space stuff
float totalDiskSpace = [[FBSDKAppEventsDeviceInfo _getTotalDiskSpace] floatValue];
_totalDiskSpaceGB = (unsigned long long)round(totalDiskSpace / FB_GIGABYTE);
}
- (BOOL)_isGroup1Expired
{
return ([FBSDKAppEventsUtility unixTimeNow] - _lastGroup1CheckTime) > FB_GROUP1_RECHECK_DURATION;
}
// This data is collected only once every GROUP1_RECHECK_DURATION.
- (void)_collectGroup1Data
{
// Carrier
NSString *newCarrierName = [FBSDKAppEventsDeviceInfo _getCarrier];
if (![newCarrierName isEqualToString:_carrierName]) {
_carrierName = newCarrierName;
_isEncodingDirty = YES;
}
// Time zone
NSString *newTimeZoneAbbrev = [[NSTimeZone systemTimeZone] abbreviation];
if (![newTimeZoneAbbrev isEqualToString:_timeZoneAbbrev]) {
_timeZoneAbbrev = newTimeZoneAbbrev;
_isEncodingDirty = YES;
}
// Remaining disk space
float remainingDiskSpace = [[FBSDKAppEventsDeviceInfo _getRemainingDiskSpace] floatValue];
unsigned long long newRemainingDiskSpaceGB = (unsigned long long)round(remainingDiskSpace / FB_GIGABYTE);
if (_remainingDiskSpaceGB != newRemainingDiskSpaceGB) {
_remainingDiskSpaceGB = newRemainingDiskSpaceGB;
_isEncodingDirty = YES;
}
_lastGroup1CheckTime = [FBSDKAppEventsUtility unixTimeNow];
}
- (NSString *)_generateEncoding
{
// Keep a bit of precision on density as it's the most likely to become non-integer.
NSString *densityString = _density ? [NSString stringWithFormat:@"%.02f", _density] : @"";
NSArray *arr = @[
@"i2", // version - starts with 'i' for iOS, we'll use 'a' for Android
_bundleIdentifier ?: @"",
_longVersion ?: @"",
_shortVersion ?: @"",
_sysVersion ?: @"",
_machine ?: @"",
_language ?: @"",
_timeZoneAbbrev ?: @"",
_carrierName ?: @"",
_width ? @((unsigned long)_width) : @"",
_height ? @((unsigned long)_height) : @"",
densityString,
@(_coreCount) ?: @"",
@(_totalDiskSpaceGB) ?: @"",
@(_remainingDiskSpaceGB) ?: @"",
];
return [FBSDKInternalUtility JSONStringForObject:arr error:NULL invalidObjectHandler:NULL];
}
#pragma mark - Helper Methods
+ (NSNumber *)_getTotalDiskSpace
{
NSDictionary *attrs = [[[NSFileManager alloc] init] attributesOfFileSystemForPath:NSHomeDirectory()
error:nil];
return [attrs objectForKey:NSFileSystemSize];
}
+ (NSNumber *)_getRemainingDiskSpace
{
NSDictionary *attrs = [[[NSFileManager alloc] init] attributesOfFileSystemForPath:NSHomeDirectory()
error:nil];
return [attrs objectForKey:NSFileSystemFreeSize];
}
+ (uint)_coreCount
{
return [FBSDKAppEventsDeviceInfo _readSysCtlUInt:CTL_HW type:HW_AVAILCPU];
}
+ (uint)_readSysCtlUInt:(int)ctl type:(int)type
{
int mib[2] = {ctl, type};
uint value;
size_t size = sizeof value;
if (0 != sysctl(mib, FB_ARRAY_COUNT(mib), &value, &size, NULL, 0)) {
return 0;
}
return value;
}
+ (NSString *)_getCarrier
{
#if TARGET_OS_TV
return @"NoCarrier";
#else
// Dynamically load class for this so calling app doesn't need to link framework in.
CTTelephonyNetworkInfo *networkInfo = [[fbsdkdfl_CTTelephonyNetworkInfoClass() alloc] init];
CTCarrier *carrier = [networkInfo subscriberCellularProvider];
return [carrier carrierName] ?: @"NoCarrier";
#endif
}
@end

View File

@ -0,0 +1,38 @@
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import <Foundation/Foundation.h>
// this type is not thread safe.
@interface FBSDKAppEventsState : NSObject<NSCopying, NSSecureCoding>
@property (readonly, copy) NSArray *events;
@property (readonly, assign) NSUInteger numSkipped;
@property (readonly, copy) NSString *tokenString;
@property (readonly, copy) NSString *appID;
- (instancetype)initWithToken:(NSString *)tokenString appID:(NSString *)appID NS_DESIGNATED_INITIALIZER;
- (void)addEvent:(NSDictionary *)eventDictionary isImplicit:(BOOL)isImplicit;
- (void)addEventsFromAppEventState:(FBSDKAppEventsState *)appEventsState;
- (BOOL)areAllEventsImplicit;
- (BOOL)isCompatibleWithAppEventsState:(FBSDKAppEventsState *)appEventsState;
- (BOOL)isCompatibleWithTokenString:(NSString *)tokenString appID:(NSString *)appID;
- (NSString *)JSONStringForEvents:(BOOL)includeImplicitEvents;
@end

View File

@ -0,0 +1,161 @@
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import "FBSDKAppEventsState.h"
#import "FBSDKInternalUtility.h"
#import "FBSDKMacros.h"
#define FBSDK_APPEVENTSTATE_ISIMPLICIT_KEY @"isImplicit"
#define FBSDK_APPEVENTSSTATE_MAX_EVENTS 1000
#define FBSDK_APPEVENTSSTATE_APPID_KEY @"appID"
#define FBSDK_APPEVENTSSTATE_EVENTS_KEY @"events"
#define FBSDK_APPEVENTSSTATE_NUMSKIPPED_KEY @"numSkipped"
#define FBSDK_APPEVENTSSTATE_TOKENSTRING_KEY @"tokenString"
@implementation FBSDKAppEventsState
{
NSMutableArray *_mutableEvents;
}
- (instancetype)init
{
FBSDK_NOT_DESIGNATED_INITIALIZER(initWithToken:appID:);
return [self initWithToken:nil appID:nil];
}
- (instancetype)initWithToken:(NSString *)tokenString appID:(NSString *)appID
{
if ((self = [super init])) {
_tokenString = [tokenString copy];
_appID = [appID copy];
_mutableEvents = [NSMutableArray array];
}
return self;
}
- (instancetype)copyWithZone:(NSZone *)zone
{
FBSDKAppEventsState *copy = [[FBSDKAppEventsState allocWithZone:zone] initWithToken:_tokenString appID:_appID];
if (copy) {
[copy->_mutableEvents addObjectsFromArray:_mutableEvents];
copy->_numSkipped = _numSkipped;
}
return copy;
}
#pragma mark - NSCoding
+ (BOOL)supportsSecureCoding
{
return YES;
}
- (id)initWithCoder:(NSCoder *)decoder
{
NSString *appID = [decoder decodeObjectOfClass:[NSString class] forKey:FBSDK_APPEVENTSSTATE_APPID_KEY];
NSString *tokenString = [decoder decodeObjectOfClass:[NSString class] forKey:FBSDK_APPEVENTSSTATE_TOKENSTRING_KEY];
NSArray *events = [decoder decodeObjectOfClass:[NSArray class] forKey:FBSDK_APPEVENTSSTATE_EVENTS_KEY];
NSUInteger numSkipped = [[decoder decodeObjectOfClass:[NSNumber class] forKey:FBSDK_APPEVENTSSTATE_NUMSKIPPED_KEY] unsignedIntegerValue];
if ((self = [self initWithToken:tokenString appID:appID])) {
_mutableEvents = [NSMutableArray arrayWithArray:events];
_numSkipped = numSkipped;
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)encoder
{
[encoder encodeObject:_appID forKey:FBSDK_APPEVENTSSTATE_APPID_KEY];
[encoder encodeObject:_tokenString forKey:FBSDK_APPEVENTSSTATE_TOKENSTRING_KEY];
[encoder encodeObject:@(_numSkipped) forKey:FBSDK_APPEVENTSSTATE_NUMSKIPPED_KEY];
[encoder encodeObject:_mutableEvents forKey:FBSDK_APPEVENTSSTATE_EVENTS_KEY];
}
#pragma mark - Implementation
- (NSArray *)events
{
return [_mutableEvents copy];
}
- (void)addEventsFromAppEventState:(FBSDKAppEventsState *)appEventsState
{
NSArray *toAdd = appEventsState->_mutableEvents;
NSInteger excess = _mutableEvents.count + toAdd.count - FBSDK_APPEVENTSSTATE_MAX_EVENTS;
if (excess > 0) {
NSInteger range = FBSDK_APPEVENTSSTATE_MAX_EVENTS - _mutableEvents.count;
toAdd = [toAdd subarrayWithRange:NSMakeRange(0, range)];
_numSkipped += excess;
}
[_mutableEvents addObjectsFromArray:toAdd];
}
- (void)addEvent:(NSDictionary *)eventDictionary
isImplicit:(BOOL)isImplicit {
if (_mutableEvents.count >= FBSDK_APPEVENTSSTATE_MAX_EVENTS) {
_numSkipped++;
} else {
[_mutableEvents addObject:@{
@"event" : eventDictionary,
FBSDK_APPEVENTSTATE_ISIMPLICIT_KEY : @(isImplicit)
}];
}
}
- (BOOL)areAllEventsImplicit
{
for (NSDictionary *event in _mutableEvents) {
if (![[event valueForKey:FBSDK_APPEVENTSTATE_ISIMPLICIT_KEY] boolValue]) {
return NO;
}
}
return YES;
}
- (BOOL)isCompatibleWithAppEventsState:(FBSDKAppEventsState *)appEventsState
{
return ([self isCompatibleWithTokenString:appEventsState.tokenString appID:appEventsState.appID]);
}
- (BOOL)isCompatibleWithTokenString:(NSString *)tokenString appID:(NSString *)appID
{
// token strings can be nil (e.g., no user token) but appIDs should not.
BOOL tokenCompatible = ([self.tokenString isEqualToString:tokenString] ||
(self.tokenString == nil && tokenString == nil));
return (tokenCompatible &&
[self.appID isEqualToString:appID]);
}
- (NSString *)JSONStringForEvents:(BOOL)includeImplicitEvents
{
NSMutableArray *events = [[NSMutableArray alloc] initWithCapacity:_mutableEvents.count];
for (NSDictionary *eventAndImplicitFlag in _mutableEvents) {
if (!includeImplicitEvents && [eventAndImplicitFlag[FBSDK_APPEVENTSTATE_ISIMPLICIT_KEY] boolValue]) {
continue;
}
[events addObject:eventAndImplicitFlag[@"event"]];
}
return [FBSDKInternalUtility JSONStringForObject:events error:NULL invalidObjectHandler:NULL];
}
@end

View File

@ -0,0 +1,34 @@
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import <Foundation/Foundation.h>
@class FBSDKAppEventsState;
@interface FBSDKAppEventsStateManager : NSObject
+ (void)clearPersistedAppEventsStates;
// reads all saved event states, appends the param, and writes them all.
+ (void)persistAppEventsData:(FBSDKAppEventsState *)appEventsState;
// returns the array of saved app event states and deletes them.
+ (NSArray *)retrievePersistedAppEventsStates;
@end

View File

@ -0,0 +1,78 @@
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import "FBSDKAppEventsStateManager.h"
#import <Foundation/Foundation.h>
#import "FBSDKAppEventsState.h"
#import "FBSDKAppEventsUtility.h"
#import "FBSDKLogger.h"
#import "FBSDKSettings.h"
// A quick optimization to allow returning empty array if we know there are no persisted events.
static BOOL g_canSkipDiskCheck = NO;
@implementation FBSDKAppEventsStateManager
+ (void)clearPersistedAppEventsStates
{
[FBSDKLogger singleShotLogEntry:FBSDKLoggingBehaviorAppEvents
logEntry:@"FBSDKAppEvents Persist: Clearing"];
[[NSFileManager defaultManager] removeItemAtPath:[[self class] filePath]
error:NULL];
g_canSkipDiskCheck = YES;
}
+ (void)persistAppEventsData:(FBSDKAppEventsState *)appEventsState
{
[FBSDKLogger singleShotLogEntry:FBSDKLoggingBehaviorAppEvents
formatString:@"FBSDKAppEvents Persist: Writing %lu events", (unsigned long)appEventsState.events.count];
if (!appEventsState.events.count) {
return;
}
NSMutableArray *existingEvents = [NSMutableArray arrayWithArray:[[self class] retrievePersistedAppEventsStates]];
[existingEvents addObject:appEventsState];
[NSKeyedArchiver archiveRootObject:existingEvents toFile:[[self class] filePath]];
g_canSkipDiskCheck = NO;
}
+ (NSArray *)retrievePersistedAppEventsStates
{
NSMutableArray *eventsStates = [NSMutableArray array];
if (!g_canSkipDiskCheck) {
[eventsStates addObjectsFromArray:[NSKeyedUnarchiver unarchiveObjectWithFile:[[self class] filePath]]];
[FBSDKLogger singleShotLogEntry:FBSDKLoggingBehaviorAppEvents
formatString:@"FBSDKAppEvents Persist: Read %lu event states. First state has %lu events",
(unsigned long)eventsStates.count,
(unsigned long)(eventsStates.count > 0 ? ((FBSDKAppEventsState *)eventsStates[0]).events.count : 0)];
[[self class] clearPersistedAppEventsStates];
}
return eventsStates;
}
#pragma mark - Private Helpers
+ (NSString *)filePath
{
return [FBSDKAppEventsUtility persistenceFilePath:@"com-facebook-sdk-AppEventsPersistedEvents.json"];
}
@end

View File

@ -0,0 +1,57 @@
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import <Foundation/Foundation.h>
@class FBSDKAccessToken;
typedef NS_ENUM(NSUInteger, FBSDKAdvertisingTrackingStatus)
{
FBSDKAdvertisingTrackingAllowed,
FBSDKAdvertisingTrackingDisallowed,
FBSDKAdvertisingTrackingUnspecified
};
typedef NS_ENUM(NSUInteger, FBSDKAppEventsFlushReason)
{
FBSDKAppEventsFlushReasonExplicit,
FBSDKAppEventsFlushReasonTimer,
FBSDKAppEventsFlushReasonSessionChange,
FBSDKAppEventsFlushReasonPersistedEvents,
FBSDKAppEventsFlushReasonEventThreshold,
FBSDKAppEventsFlushReasonEagerlyFlushingEvent
};
@interface FBSDKAppEventsUtility : NSObject
+ (NSMutableDictionary *)activityParametersDictionaryForEvent:(NSString *)eventCategory
implicitEventsOnly:(BOOL)implicitEventsOnly
shouldAccessAdvertisingID:(BOOL)shouldAccessAdvertisingID;
+ (NSString *)advertiserID;
+ (FBSDKAdvertisingTrackingStatus)advertisingTrackingStatus;
+ (NSString *)attributionID;
+ (void)ensureOnMainThread:(NSString *)methodName className:(NSString *)className;
+ (NSString *)flushReasonToString:(FBSDKAppEventsFlushReason)flushReason;
+ (void)logAndNotify:(NSString *)msg allowLogAsDeveloperError:(BOOL)allowLogAsDeveloperError;
+ (void)logAndNotify:(NSString *)msg;
+ (NSString *)persistenceFilePath:(NSString *)filename;
+ (NSString *)tokenStringToUseFor:(FBSDKAccessToken *)token;
+ (long)unixTimeNow;
+ (BOOL)validateIdentifier:(NSString *)identifier;
@end

View File

@ -0,0 +1,313 @@
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import "FBSDKAppEventsUtility.h"
#import <AdSupport/AdSupport.h>
#import "FBSDKAccessToken.h"
#import "FBSDKAppEvents.h"
#import "FBSDKAppEventsDeviceInfo.h"
#import "FBSDKConstants.h"
#import "FBSDKDynamicFrameworkLoader.h"
#import "FBSDKError.h"
#import "FBSDKInternalUtility.h"
#import "FBSDKLogger.h"
#import "FBSDKMacros.h"
#import "FBSDKSettings.h"
#import "FBSDKTimeSpentData.h"
#define FBSDK_APPEVENTSUTILITY_ANONYMOUSIDFILENAME @"com-facebook-sdk-PersistedAnonymousID.json"
#define FBSDK_APPEVENTSUTILITY_ANONYMOUSID_KEY @"anon_id"
#define FBSDK_APPEVENTSUTILITY_MAX_IDENTIFIER_LENGTH 40
@implementation FBSDKAppEventsUtility
+ (NSMutableDictionary *)activityParametersDictionaryForEvent:(NSString *)eventCategory
implicitEventsOnly:(BOOL)implicitEventsOnly
shouldAccessAdvertisingID:(BOOL)shouldAccessAdvertisingID {
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
parameters[@"event"] = eventCategory;
NSString *attributionID = [[self class] attributionID]; // Only present on iOS 6 and below.
[FBSDKInternalUtility dictionary:parameters setObject:attributionID forKey:@"attribution"];
if (!implicitEventsOnly && shouldAccessAdvertisingID) {
NSString *advertiserID = [[self class] advertiserID];
[FBSDKInternalUtility dictionary:parameters setObject:advertiserID forKey:@"advertiser_id"];
}
parameters[FBSDK_APPEVENTSUTILITY_ANONYMOUSID_KEY] = [self anonymousID];
FBSDKAdvertisingTrackingStatus advertisingTrackingStatus = [[self class] advertisingTrackingStatus];
if (advertisingTrackingStatus != FBSDKAdvertisingTrackingUnspecified) {
BOOL allowed = (advertisingTrackingStatus == FBSDKAdvertisingTrackingAllowed);
parameters[@"advertiser_tracking_enabled"] = [@(allowed) stringValue];
}
parameters[@"application_tracking_enabled"] = [@(!FBSDKSettings.limitEventAndDataUsage) stringValue];
[FBSDKAppEventsDeviceInfo extendDictionaryWithDeviceInfo:parameters];
static dispatch_once_t fetchBundleOnce;
static NSMutableArray *urlSchemes;
dispatch_once(&fetchBundleOnce, ^{
NSBundle *mainBundle = [NSBundle mainBundle];
urlSchemes = [[NSMutableArray alloc] init];
for (NSDictionary *fields in [mainBundle objectForInfoDictionaryKey:@"CFBundleURLTypes"]) {
NSArray *schemesForType = [fields objectForKey:@"CFBundleURLSchemes"];
if (schemesForType) {
[urlSchemes addObjectsFromArray:schemesForType];
}
}
});
if (urlSchemes.count > 0) {
[parameters setObject:[FBSDKInternalUtility JSONStringForObject:urlSchemes error:NULL invalidObjectHandler:NULL]
forKey:@"url_schemes"];
}
return parameters;
}
+ (NSString *)advertiserID
{
NSString *result = nil;
Class ASIdentifierManagerClass = fbsdkdfl_ASIdentifierManagerClass();
if ([ASIdentifierManagerClass class]) {
ASIdentifierManager *manager = [ASIdentifierManagerClass sharedManager];
result = [[manager advertisingIdentifier] UUIDString];
}
return result;
}
+ (FBSDKAdvertisingTrackingStatus)advertisingTrackingStatus
{
static dispatch_once_t fetchAdvertisingTrackingStatusOnce;
static FBSDKAdvertisingTrackingStatus status;
dispatch_once(&fetchAdvertisingTrackingStatusOnce, ^{
status = FBSDKAdvertisingTrackingUnspecified;
Class ASIdentifierManagerClass = fbsdkdfl_ASIdentifierManagerClass();
if ([ASIdentifierManagerClass class]) {
ASIdentifierManager *manager = [ASIdentifierManagerClass sharedManager];
if (manager) {
status = [manager isAdvertisingTrackingEnabled] ? FBSDKAdvertisingTrackingAllowed : FBSDKAdvertisingTrackingDisallowed;
}
}
});
return status;
}
+ (NSString *)anonymousID
{
// Grab previously written anonymous ID and, if none have been generated, create and
// persist a new one which will remain associated with this app.
NSString *result = [[self class] retrievePersistedAnonymousID];
if (!result) {
// Generate a new anonymous ID. Create as a UUID, but then prepend the fairly
// arbitrary 'XZ' to the front so it's easily distinguishable from IDFA's which
// will only contain hex.
result = [NSString stringWithFormat:@"XZ%@", [[NSUUID UUID] UUIDString]];
[self persistAnonymousID:result];
}
return result;
}
+ (NSString *)attributionID
{
#if TARGET_OS_TV
return nil;
#else
return [[UIPasteboard pasteboardWithName:@"fb_app_attribution" create:NO] string];
#endif
}
// for tests only.
+ (void)clearLibraryFiles
{
[[NSFileManager defaultManager] removeItemAtPath:[[self class] persistenceFilePath:FBSDK_APPEVENTSUTILITY_ANONYMOUSIDFILENAME]
error:NULL];
[[NSFileManager defaultManager] removeItemAtPath:[[self class] persistenceFilePath:FBSDKTimeSpentFilename]
error:NULL];
}
+ (void)ensureOnMainThread:(NSString *)methodName className:(NSString *)className
{
FBSDKConditionalLog([NSThread isMainThread],
FBSDKLoggingBehaviorDeveloperErrors,
@"*** <%@, %@> is not called on the main thread. This can lead to errors.",
methodName,
className);
}
+ (NSString *)flushReasonToString:(FBSDKAppEventsFlushReason)flushReason
{
NSString *result = @"Unknown";
switch (flushReason) {
case FBSDKAppEventsFlushReasonExplicit:
result = @"Explicit";
break;
case FBSDKAppEventsFlushReasonTimer:
result = @"Timer";
break;
case FBSDKAppEventsFlushReasonSessionChange:
result = @"SessionChange";
break;
case FBSDKAppEventsFlushReasonPersistedEvents:
result = @"PersistedEvents";
break;
case FBSDKAppEventsFlushReasonEventThreshold:
result = @"EventCountThreshold";
break;
case FBSDKAppEventsFlushReasonEagerlyFlushingEvent:
result = @"EagerlyFlushingEvent";
break;
}
return result;
}
+ (void)logAndNotify:(NSString *)msg
{
[[self class] logAndNotify:msg allowLogAsDeveloperError:YES];
}
+ (void)logAndNotify:(NSString *)msg allowLogAsDeveloperError:(BOOL)allowLogAsDeveloperError
{
NSString *behaviorToLog = FBSDKLoggingBehaviorAppEvents;
if (allowLogAsDeveloperError) {
if ([[FBSDKSettings loggingBehavior] containsObject:FBSDKLoggingBehaviorDeveloperErrors]) {
// Rather than log twice, prefer 'DeveloperErrors' if it's set over AppEvents.
behaviorToLog = FBSDKLoggingBehaviorDeveloperErrors;
}
}
[FBSDKLogger singleShotLogEntry:behaviorToLog logEntry:msg];
NSError *error = [FBSDKError errorWithCode:FBSDKAppEventsFlushErrorCode message:msg];
[[NSNotificationCenter defaultCenter] postNotificationName:FBSDKAppEventsLoggingResultNotification object:error];
}
+ (BOOL)regexValidateIdentifier:(NSString *)identifier
{
static NSRegularExpression *regex;
static dispatch_once_t onceToken;
static NSMutableSet *cachedIdentifiers;
dispatch_once(&onceToken, ^{
NSString *regexString = @"^[0-9a-zA-Z_]+[0-9a-zA-Z _-]*$";
regex = [NSRegularExpression regularExpressionWithPattern:regexString
options:0
error:NULL];
cachedIdentifiers = [[NSMutableSet alloc] init];
});
@synchronized(self) {
if (![cachedIdentifiers containsObject:identifier]) {
NSUInteger numMatches = [regex numberOfMatchesInString:identifier options:0 range:NSMakeRange(0, identifier.length)];
if (numMatches > 0) {
[cachedIdentifiers addObject:identifier];
} else {
return NO;
}
}
}
return YES;
}
+ (BOOL)validateIdentifier:(NSString *)identifier
{
if (identifier == nil || identifier.length == 0 || identifier.length > FBSDK_APPEVENTSUTILITY_MAX_IDENTIFIER_LENGTH || ![[self class] regexValidateIdentifier:identifier]) {
[[self class] logAndNotify:[NSString stringWithFormat:@"Invalid identifier: '%@'. Must be between 1 and %d characters, and must be contain only alphanumerics, _, - or spaces, starting with alphanumeric or _.",
identifier, FBSDK_APPEVENTSUTILITY_MAX_IDENTIFIER_LENGTH]];
return NO;
}
return YES;
}
+ (void)persistAnonymousID:(NSString *)anonymousID
{
[[self class] ensureOnMainThread:NSStringFromSelector(_cmd) className:NSStringFromClass(self)];
NSDictionary *data = @{ FBSDK_APPEVENTSUTILITY_ANONYMOUSID_KEY : anonymousID };
NSString *content = [FBSDKInternalUtility JSONStringForObject:data error:NULL invalidObjectHandler:NULL];
[content writeToFile:[[self class] persistenceFilePath:FBSDK_APPEVENTSUTILITY_ANONYMOUSIDFILENAME]
atomically:YES
encoding:NSASCIIStringEncoding
error:nil];
}
+ (NSString *)persistenceFilePath:(NSString *)filename
{
NSSearchPathDirectory directory = NSLibraryDirectory;
NSArray *paths = NSSearchPathForDirectoriesInDomains(directory, NSUserDomainMask, YES);
NSString *docDirectory = [paths objectAtIndex:0];
return [docDirectory stringByAppendingPathComponent:filename];
}
+ (NSString *)retrievePersistedAnonymousID
{
[[self class] ensureOnMainThread:NSStringFromSelector(_cmd) className:NSStringFromClass(self)];
NSString *file = [[self class] persistenceFilePath:FBSDK_APPEVENTSUTILITY_ANONYMOUSIDFILENAME];
NSString *content = [[NSString alloc] initWithContentsOfFile:file
encoding:NSASCIIStringEncoding
error:nil];
NSDictionary *results = [FBSDKInternalUtility objectForJSONString:content error:NULL];
return [results objectForKey:FBSDK_APPEVENTSUTILITY_ANONYMOUSID_KEY];
}
// Given a candidate token (which may be nil), find the real token to string to use.
// Precedence: 1) provided token, 2) current token, 3) app | client token, 4) fully anonymous session.
+ (NSString *)tokenStringToUseFor:(FBSDKAccessToken *)token
{
if (!token) {
token = [FBSDKAccessToken currentAccessToken];
}
NSString *appID = [FBSDKAppEvents loggingOverrideAppID] ?: token.appID ?: [FBSDKSettings appID];
NSString *tokenString = token.tokenString;
if (!tokenString || ![appID isEqualToString:token.appID]) {
// If there's an logging override app id present, then we don't want to use the client token since the client token
// is intended to match up with the primary app id (and AppEvents doesn't require a client token).
NSString *clientTokenString = [FBSDKSettings clientToken];
if (clientTokenString && appID && [appID isEqualToString:token.appID]){
tokenString = [NSString stringWithFormat:@"%@|%@", appID, clientTokenString];
} else if (appID) {
tokenString = nil;
}
}
return tokenString;
}
+ (long)unixTimeNow
{
return (long)round([[NSDate date] timeIntervalSince1970]);
}
- (instancetype)init
{
FBSDK_NO_DESIGNATED_INITIALIZER();
return nil;
}
@end

View File

@ -0,0 +1,25 @@
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import <Foundation/Foundation.h>
// Class to encapsulate implicit logging of purchase events
@interface FBSDKPaymentObserver : NSObject
+ (void)startObservingTransactions;
+ (void)stopObservingTransactions;
@end

View File

@ -0,0 +1,281 @@
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import "FBSDKPaymentObserver.h"
#import <StoreKit/StoreKit.h>
#import "FBSDKAppEvents+Internal.h"
#import "FBSDKDynamicFrameworkLoader.h"
#import "FBSDKLogger.h"
#import "FBSDKSettings.h"
static NSString *const FBSDKAppEventParameterImplicitlyLoggedPurchase = @"_implicitlyLoggedPurchaseEvent";
static NSString *const FBSDKAppEventNamePurchaseFailed = @"fb_mobile_purchase_failed";
static NSString *const FBSDKAppEventParameterNameProductTitle = @"fb_content_title";
static NSString *const FBSDKAppEventParameterNameTransactionID = @"fb_transaction_id";
static int const FBSDKMaxParameterValueLength = 100;
static NSMutableArray *g_pendingRequestors;
@interface FBSDKPaymentProductRequestor : NSObject<SKProductsRequestDelegate>
@property (nonatomic, retain) SKPaymentTransaction *transaction;
- (instancetype)initWithTransaction:(SKPaymentTransaction*)transaction;
- (void)resolveProducts;
@end
@interface FBSDKPaymentObserver() <SKPaymentTransactionObserver>
@end
@implementation FBSDKPaymentObserver
{
BOOL _observingTransactions;
}
+ (void)startObservingTransactions
{
[[self singleton] startObservingTransactions];
}
+ (void)stopObservingTransactions
{
[[self singleton] stopObservingTransactions];
}
//
// Internal methods
//
+ (FBSDKPaymentObserver *)singleton
{
static dispatch_once_t pred;
static FBSDKPaymentObserver *shared = nil;
dispatch_once(&pred, ^{
shared = [[FBSDKPaymentObserver alloc] init];
});
return shared;
}
- (instancetype) init
{
self = [super init];
if (self) {
_observingTransactions = NO;
}
return self;
}
- (void)startObservingTransactions
{
@synchronized (self) {
if (!_observingTransactions) {
[(SKPaymentQueue *)[fbsdkdfl_SKPaymentQueueClass() defaultQueue] addTransactionObserver:self];
_observingTransactions = YES;
}
}
}
- (void)stopObservingTransactions
{
@synchronized (self) {
if (_observingTransactions) {
[(SKPaymentQueue *)[fbsdkdfl_SKPaymentQueueClass() defaultQueue] removeTransactionObserver:self];
_observingTransactions = NO;
}
}
}
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
for (SKPaymentTransaction *transaction in transactions) {
switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchasing:
case SKPaymentTransactionStatePurchased:
case SKPaymentTransactionStateFailed:
[self handleTransaction:transaction];
break;
case SKPaymentTransactionStateDeferred:
case SKPaymentTransactionStateRestored:
break;
}
}
}
- (void)handleTransaction:(SKPaymentTransaction *)transaction
{
FBSDKPaymentProductRequestor *productRequest = [[FBSDKPaymentProductRequestor alloc] initWithTransaction:transaction];
[productRequest resolveProducts];
}
@end
@interface FBSDKPaymentProductRequestor()
@property (nonatomic, retain) SKProductsRequest *productRequest;
@end
@implementation FBSDKPaymentProductRequestor
+ (void)initialize
{
if ([self class] == [FBSDKPaymentProductRequestor class]) {
g_pendingRequestors = [[NSMutableArray alloc] init];
}
}
- (instancetype)initWithTransaction:(SKPaymentTransaction*)transaction
{
self = [super init];
if (self) {
_transaction = transaction;
}
return self;
}
- (void)setProductRequest:(SKProductsRequest *)productRequest
{
if (productRequest != _productRequest) {
if (_productRequest) {
_productRequest.delegate = nil;
}
_productRequest = productRequest;
}
}
- (void)resolveProducts
{
NSString *productId = self.transaction.payment.productIdentifier;
NSSet *productIdentifiers = [NSSet setWithObjects:productId, nil];
self.productRequest = [[fbsdkdfl_SKProductsRequestClass() alloc] initWithProductIdentifiers:productIdentifiers];
self.productRequest.delegate = self;
@synchronized(g_pendingRequestors) {
[g_pendingRequestors addObject:self];
}
[self.productRequest start];
}
- (NSString *)getTruncatedString:(NSString *)inputString
{
if (!inputString) {
return @"";
}
return [inputString length] <= FBSDKMaxParameterValueLength ? inputString : [inputString substringToIndex:FBSDKMaxParameterValueLength];
}
- (void)logTransactionEvent:(SKProduct *)product
{
NSString *eventName = nil;
NSString *transactionID = nil;
switch (self.transaction.transactionState) {
case SKPaymentTransactionStatePurchasing:
eventName = FBSDKAppEventNameInitiatedCheckout;
break;
case SKPaymentTransactionStatePurchased:
eventName = FBSDKAppEventNamePurchased;
transactionID = self.transaction.transactionIdentifier;
break;
case SKPaymentTransactionStateFailed:
eventName = FBSDKAppEventNamePurchaseFailed;
break;
case SKPaymentTransactionStateDeferred:
case SKPaymentTransactionStateRestored:
return;
}
if (!eventName) {
[FBSDKLogger singleShotLogEntry:FBSDKLoggingBehaviorAppEvents
formatString:@"FBSDKPaymentObserver logTransactionEvent: event name cannot be nil"];
return;
}
SKPayment *payment = self.transaction.payment;
NSMutableDictionary *eventParameters = [NSMutableDictionary dictionaryWithDictionary: @{
FBSDKAppEventParameterNameContentID: payment.productIdentifier ?: @"",
FBSDKAppEventParameterNameNumItems: @(payment.quantity),
}];
double totalAmount = 0;
if (product) {
totalAmount = payment.quantity * product.price.doubleValue;
[eventParameters addEntriesFromDictionary: @{
FBSDKAppEventParameterNameCurrency: [product.priceLocale objectForKey:NSLocaleCurrencyCode],
FBSDKAppEventParameterNameNumItems: @(payment.quantity),
FBSDKAppEventParameterNameProductTitle: [self getTruncatedString:product.localizedTitle],
FBSDKAppEventParameterNameDescription: [self getTruncatedString:product.localizedDescription],
}];
if (transactionID) {
[eventParameters setObject:transactionID forKey:FBSDKAppEventParameterNameTransactionID];
}
}
[self logImplicitPurchaseEvent:eventName
valueToSum:totalAmount
parameters:eventParameters];
}
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
NSArray* products = response.products;
NSArray* invalidProductIdentifiers = response.invalidProductIdentifiers;
if (products.count + invalidProductIdentifiers.count != 1) {
[FBSDKLogger singleShotLogEntry:FBSDKLoggingBehaviorAppEvents
formatString:@"FBSDKPaymentObserver: Expect to resolve one product per request"];
}
SKProduct *product = nil;
if (products.count) {
product = products[0];
}
[self logTransactionEvent:product];
}
- (void)requestDidFinish:(SKRequest *)request
{
[self cleanUp];
}
- (void)request:(SKRequest *)request didFailWithError:(NSError *)error
{
[self logTransactionEvent:nil];
[self cleanUp];
}
- (void)cleanUp
{
@synchronized(g_pendingRequestors) {
[g_pendingRequestors removeObject:self];
}
}
- (void)logImplicitPurchaseEvent:(NSString *)eventName
valueToSum:(double)valueToSum
parameters:(NSDictionary *)parameters {
NSMutableDictionary *eventParameters = [NSMutableDictionary dictionaryWithDictionary:parameters];
[eventParameters setObject:@"1" forKey:FBSDKAppEventParameterImplicitlyLoggedPurchase];
[FBSDKAppEvents logEvent:eventName
valueToSum:valueToSum
parameters:parameters];
// Unless the behavior is set to only allow explicit flushing, we go ahead and flush, since purchase events
// are relatively rare and relatively high value and worth getting across on wire right away.
if ([FBSDKAppEvents flushBehavior] != FBSDKAppEventsFlushBehaviorExplicitOnly) {
[[FBSDKAppEvents singleton] flushForReason:FBSDKAppEventsFlushReasonEagerlyFlushingEvent];
}
}
@end

View File

@ -0,0 +1,36 @@
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import <Foundation/Foundation.h>
#import <FBSDKCoreKit/FBSDKMacros.h>
FBSDK_EXTERN NSString *const FBSDKTimeSpentFilename;
// Class to encapsulate persisting of time spent data collected by [FBSDKAppEvents activateApp]. The activate app App Event is
// logged when restore: is called with sufficient time since the last deactivation.
@interface FBSDKTimeSpentData : NSObject
+ (void)suspend;
+ (void)restore:(BOOL)calledFromActivateApp;
+ (void)setSourceApplication:(NSString *)sourceApplication openURL:(NSURL *)url;
+ (void)setSourceApplication:(NSString *)sourceApplication isFromAppLink:(BOOL)isFromAppLink;
+ (void)registerAutoResetSourceApplication;
@end

View File

@ -0,0 +1,305 @@
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import "FBSDKTimeSpentData.h"
#import "FBSDKAppEvents+Internal.h"
#import "FBSDKAppEventsUtility.h"
#import "FBSDKInternalUtility.h"
#import "FBSDKLogger.h"
#import "FBSDKSettings.h"
// Filename and keys for session length
NSString *const FBSDKTimeSpentFilename = @"com-facebook-sdk-AppEventsTimeSpent.json";
static NSString *const FBSDKTimeSpentPersistKeySessionSecondsSpent = @"secondsSpentInCurrentSession";
static NSString *const FBSDKTimeSpentPersistKeySessionNumInterruptions = @"numInterruptions";
static NSString *const FBSDKTimeSpentPersistKeyLastSuspendTime = @"lastSuspendTime";
static NSString *const FBSDKAppEventNameActivatedApp = @"fb_mobile_activate_app";
static NSString *const FBSDKAppEventNameDeactivatedApp = @"fb_mobile_deactivate_app";
static NSString *const FBSDKAppEventParameterNameSessionInterruptions = @"fb_mobile_app_interruptions";
static NSString *const FBSDKAppEventParameterNameTimeBetweenSessions = @"fb_mobile_time_between_sessions";
static const int NUM_SECONDS_IDLE_TO_BE_NEW_SESSION = 60;
static const int SECS_PER_MIN = 60;
static const int SECS_PER_HOUR = 60 * SECS_PER_MIN;
static const int SECS_PER_DAY = 24 * SECS_PER_HOUR;
static NSString *g_sourceApplication;
static BOOL g_isOpenedFromAppLink;
// Will be translated and displayed in App Insights. Need to maintain same number and value of quanta on the server.
static const long INACTIVE_SECONDS_QUANTA[] =
{
5 * SECS_PER_MIN,
15 * SECS_PER_MIN,
30 * SECS_PER_MIN,
1 * SECS_PER_HOUR,
6 * SECS_PER_HOUR,
12 * SECS_PER_HOUR,
1 * SECS_PER_DAY,
2 * SECS_PER_DAY,
3 * SECS_PER_DAY,
7 * SECS_PER_DAY,
14 * SECS_PER_DAY,
21 * SECS_PER_DAY,
28 * SECS_PER_DAY,
60 * SECS_PER_DAY,
90 * SECS_PER_DAY,
120 * SECS_PER_DAY,
150 * SECS_PER_DAY,
180 * SECS_PER_DAY,
365 * SECS_PER_DAY,
LONG_MAX, // keep as LONG_MAX to guarantee loop will terminate
};
/**
* This class encapsulates the notion of an app 'session' - the length of time that the user has
* spent in the app that can be considered a single usage of the app. Apps may be frequently interrupted
* do to other device activity, like a text message, so this class allows those interruptions to be smoothed
* out and the time actually spent in the app excluding this interruption time to be accumulated. Also,
* once a certain amount of time has gone by where the app is not in the foreground, we consider the
* session to be complete, and a new session beginning. When this occurs, we log an 'activate app' event
* with the duration of the previous session as the 'value' of this event, along with the number of
* interruptions from that previous session as an event parameter.
*/
@interface FBSDKTimeSpentData()
@property (nonatomic) NSInteger numSecondsIdleToBeNewSession;
@end
@implementation FBSDKTimeSpentData
{
BOOL _isCurrentlyLoaded;
BOOL _shouldLogActivateEvent;
BOOL _shouldLogDeactivateEvent;
long _secondsSpentInCurrentSession;
long _timeSinceLastSuspend;
int _numInterruptionsInCurrentSession;
long _lastRestoreTime;
}
//
// Public methods
//
+ (void)suspend
{
[self.singleton instanceSuspend];
}
+ (void)restore:(BOOL)calledFromActivateApp
{
[self.singleton instanceRestore:calledFromActivateApp];
}
//
// Internal methods
//
- (instancetype)init
{
if ((self = [super init])) {
_numSecondsIdleToBeNewSession = NUM_SECONDS_IDLE_TO_BE_NEW_SESSION;
}
return self;
}
+ (FBSDKTimeSpentData *)singleton
{
static dispatch_once_t pred;
static FBSDKTimeSpentData *shared = nil;
dispatch_once(&pred, ^{
shared = [[FBSDKTimeSpentData alloc] init];
});
return shared;
}
// Calculate and persist time spent data for this instance of the app activation.
- (void)instanceSuspend
{
[FBSDKAppEventsUtility ensureOnMainThread:NSStringFromSelector(_cmd) className:NSStringFromClass([self class])];
if (!_isCurrentlyLoaded) {
FBSDKConditionalLog(YES, FBSDKLoggingBehaviorInformational, @"[FBSDKTimeSpentData suspend] invoked without corresponding restore");
return;
}
long now = [FBSDKAppEventsUtility unixTimeNow];
long timeSinceRestore = now - _lastRestoreTime;
// Can happen if the clock on the device is changed
if (timeSinceRestore < 0) {
[FBSDKLogger singleShotLogEntry:FBSDKLoggingBehaviorAppEvents
formatString:@"Clock skew detected"];
timeSinceRestore = 0;
}
_secondsSpentInCurrentSession += timeSinceRestore;
NSDictionary *timeSpentData =
@{
FBSDKTimeSpentPersistKeySessionSecondsSpent : @(_secondsSpentInCurrentSession),
FBSDKTimeSpentPersistKeySessionNumInterruptions : @(_numInterruptionsInCurrentSession),
FBSDKTimeSpentPersistKeyLastSuspendTime : @(now)
};
NSString *content = [FBSDKInternalUtility JSONStringForObject:timeSpentData error:NULL invalidObjectHandler:NULL];
[content writeToFile:[FBSDKAppEventsUtility persistenceFilePath:FBSDKTimeSpentFilename]
atomically:YES
encoding:NSASCIIStringEncoding
error:nil];
[FBSDKLogger singleShotLogEntry:FBSDKLoggingBehaviorAppEvents
formatString:@"FBSDKTimeSpentData Persist: %@", content];
_isCurrentlyLoaded = NO;
}
// Called during activation - either through an explicit 'activateApp' call or implicitly when the app is foregrounded.
// In both cases, we restore the persisted event data. In the case of the activateApp, we log an 'app activated'
// event if there's been enough time between the last deactivation and now.
- (void)instanceRestore:(BOOL)calledFromActivateApp
{
[FBSDKAppEventsUtility ensureOnMainThread:NSStringFromSelector(_cmd) className:NSStringFromClass([self class])];
// It's possible to call this multiple times during the time the app is in the foreground. If this is the case,
// just restore persisted data the first time.
if (!_isCurrentlyLoaded) {
NSString *content =
[[NSString alloc] initWithContentsOfFile:[FBSDKAppEventsUtility persistenceFilePath:FBSDKTimeSpentFilename]
usedEncoding:nil
error:nil];
[FBSDKLogger singleShotLogEntry:FBSDKLoggingBehaviorAppEvents
formatString:@"FBSDKTimeSpentData Restore: %@", content];
long now = [FBSDKAppEventsUtility unixTimeNow];
if (!content) {
// Nothing persisted, so this is the first launch.
_secondsSpentInCurrentSession = 0;
_numInterruptionsInCurrentSession = 0;
// We want to log the app activation event on the first launch, but not the deactivate event
_shouldLogActivateEvent = YES;
_shouldLogDeactivateEvent = NO;
} else {
NSDictionary *results = [FBSDKInternalUtility objectForJSONString:content error:NULL];
long lastActiveTime = [[results objectForKey:FBSDKTimeSpentPersistKeyLastSuspendTime] longValue];
_timeSinceLastSuspend = now - lastActiveTime;
_secondsSpentInCurrentSession = [[results objectForKey:FBSDKTimeSpentPersistKeySessionSecondsSpent] intValue];
_numInterruptionsInCurrentSession = [[results objectForKey:FBSDKTimeSpentPersistKeySessionNumInterruptions] intValue];
_shouldLogActivateEvent = (_timeSinceLastSuspend > _numSecondsIdleToBeNewSession);
// Other than the first launch, we always log the last session's deactivate with this session's activate.
_shouldLogDeactivateEvent = _shouldLogActivateEvent;
if (!_shouldLogDeactivateEvent) {
// If we're not logging, then the time we spent deactivated is considered another interruption. But cap it
// so errant or test uses doesn't blow out the cardinality on the backend processing
_numInterruptionsInCurrentSession = MIN(_numInterruptionsInCurrentSession + 1, 200);
}
}
_lastRestoreTime = now;
_isCurrentlyLoaded = YES;
if (calledFromActivateApp) {
if (_shouldLogActivateEvent) {
[FBSDKAppEvents logEvent:FBSDKAppEventNameActivatedApp
parameters:@{
FBSDKAppEventParameterLaunchSource: [[self class] getSourceApplication]
}];
}
if (_shouldLogDeactivateEvent) {
int quantaIndex = 0;
while (_timeSinceLastSuspend > INACTIVE_SECONDS_QUANTA[quantaIndex]) {
quantaIndex++;
}
[FBSDKAppEvents logEvent:FBSDKAppEventNameDeactivatedApp
valueToSum:_secondsSpentInCurrentSession
parameters:
@{ FBSDKAppEventParameterNameSessionInterruptions : @(_numInterruptionsInCurrentSession),
FBSDKAppEventParameterNameTimeBetweenSessions : [NSString stringWithFormat:@"session_quanta_%d", quantaIndex],
FBSDKAppEventParameterLaunchSource: [[self class] getSourceApplication],
}
];
// We've logged the session stats, now reset.
_secondsSpentInCurrentSession = 0;
_numInterruptionsInCurrentSession = 0;
}
}
}
}
+ (void)setSourceApplication:(NSString *)sourceApplication openURL:(NSURL *)url
{
[self setSourceApplication:sourceApplication
isFromAppLink:[FBSDKInternalUtility dictionaryFromFBURL:url][@"al_applink_data"] != nil];
}
+ (void)setSourceApplication:(NSString *)sourceApplication isFromAppLink:(BOOL)isFromAppLink
{
g_isOpenedFromAppLink = isFromAppLink;
g_sourceApplication = sourceApplication;
}
+ (NSString *)getSourceApplication
{
NSString *openType = @"Unclassified";
if (g_isOpenedFromAppLink) {
openType = @"AppLink";
}
return (g_sourceApplication ?
[NSString stringWithFormat:@"%@(%@)", openType, g_sourceApplication]
: openType);
}
+ (void)resetSourceApplication
{
g_sourceApplication = nil;
g_isOpenedFromAppLink = NO;
}
+ (void)registerAutoResetSourceApplication
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(resetSourceApplication)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
}
@end

View File

@ -0,0 +1,23 @@
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import <Foundation/Foundation.h>
@interface FBSDKBoltsMeasurementEventListener : NSObject
+ (FBSDKBoltsMeasurementEventListener *)defaultListener;
@end

View File

@ -0,0 +1,78 @@
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import "FBSDKBoltsMeasurementEventListener.h"
#import "FBSDKAppEvents+Internal.h"
#import "FBSDKTimeSpentData.h"
static NSString *const BoltsMeasurementEventNotificationName = @"com.parse.bolts.measurement_event";
static NSString *const BoltsMeasurementEventName = @"event_name";
static NSString *const BoltsMeasurementEventArgs = @"event_args";
static NSString *const BoltsMeasurementEventPrefix = @"bf_";
@implementation FBSDKBoltsMeasurementEventListener
+ (instancetype)defaultListener
{
static dispatch_once_t dispatchOnceLocker = 0;
static FBSDKBoltsMeasurementEventListener *defaultListener = nil;
dispatch_once(&dispatchOnceLocker, ^{
defaultListener = [[self alloc] init];
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:defaultListener
selector:@selector(logFBAppEventForNotification:)
name:BoltsMeasurementEventNotificationName
object:nil];
});
return defaultListener;
}
- (void)logFBAppEventForNotification:(NSNotification *)note
{
// when catch al_nav_in event, we set source application for FBAppEvents.
if ([note.userInfo[BoltsMeasurementEventName] isEqualToString:@"al_nav_in"]) {
NSString *sourceApplication = note.userInfo[BoltsMeasurementEventArgs][@"sourceApplication"];
if (sourceApplication) {
[FBSDKTimeSpentData setSourceApplication:sourceApplication isFromAppLink:YES];
}
}
NSDictionary *eventArgs = note.userInfo[BoltsMeasurementEventArgs];
NSMutableDictionary *logData = [[NSMutableDictionary alloc] init];
for(NSString *key in eventArgs.allKeys) {
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[^0-9a-zA-Z _-]" options:0 error:&error];
NSString *safeKey = [regex stringByReplacingMatchesInString:key
options:0
range:NSMakeRange(0, [key length])
withTemplate:@"-"];
safeKey = [safeKey stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@" -"]];
logData[safeKey] = eventArgs[key];
}
[FBSDKAppEvents logImplicitEvent:[BoltsMeasurementEventPrefix stringByAppendingString:note.userInfo[BoltsMeasurementEventName]]
valueToSum:nil
parameters:logData
accessToken:nil];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end

View File

@ -0,0 +1,51 @@
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import <Foundation/Foundation.h>
@interface FBSDKBase64 : NSObject
/*!
@abstract Decodes a base-64 encoded string.
@param string The base-64 encoded string.
@return NSData containing the decoded bytes.
*/
+ (NSData *)decodeAsData:(NSString *)string;
/*!
@abstract Decodes a base-64 encoded string into a string.
@param string The base-64 encoded string.
@return NSString with the decoded UTF-8 value.
*/
+ (NSString *)decodeAsString:(NSString *)string;
/*!
@abstract Encodes data into a string.
@param data The data to be encoded.
@return The base-64 encoded string.
*/
+ (NSString *)encodeData:(NSData *)data;
/*!
@abstract Encodes string into a base-64 representation.
@param string The string to be encoded.
@return The base-64 encoded string.
*/
+ (NSString *)encodeString:(NSString *)string;
@end

View File

@ -0,0 +1,133 @@
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import "FBSDKBase64.h"
#import "FBSDKMacros.h"
@implementation FBSDKBase64
{
BOOL _optionsEnabled;
}
static FBSDKBase64 *_decoder;
static FBSDKBase64 *_encoder;
#pragma mark - Class Methods
+ (void)initialize
{
if (self == [FBSDKBase64 class]) {
BOOL optionsEnabled;
optionsEnabled = [NSData instancesRespondToSelector:@selector(initWithBase64EncodedString:options:)];
_decoder = [[FBSDKBase64 alloc] initWithOptionsEnabled:optionsEnabled];
optionsEnabled = [NSData instancesRespondToSelector:@selector(base64EncodedStringWithOptions:)];
_encoder = [[FBSDKBase64 alloc] initWithOptionsEnabled:optionsEnabled];
}
}
+ (NSData *)decodeAsData:(NSString *)string
{
return [_decoder decodeAsData:string];
}
+ (NSString *)decodeAsString:(NSString *)string
{
return [_decoder decodeAsString:string];
}
+ (NSString *)encodeData:(NSData *)data
{
return [_encoder encodeData:data];
}
+ (NSString *)encodeString:(NSString *)string
{
return [_encoder encodeString:string];
}
#pragma mark - Object Lifecycle
- (instancetype)init
{
FBSDK_NOT_DESIGNATED_INITIALIZER(initWithOptionsEnabled:);
return nil;
}
- (instancetype)initWithOptionsEnabled:(BOOL)optionsEnabled
{
if ((self = [super init])) {
_optionsEnabled = optionsEnabled;
}
return self;
}
#pragma mark - Implementation Methods
- (NSData *)decodeAsData:(NSString *)string
{
if (!string) {
return nil;
}
// This padding will be appended before stripping unknown characters, so if there are unknown characters of count % 4
// it will not be able to decode. Since we assume valid base64 data, we will take this as is.
int needPadding = string.length % 4;
if (needPadding > 0) {
needPadding = 4 - needPadding;
string = [string stringByPaddingToLength:string.length+needPadding withString:@"=" startingAtIndex:0];
}
if (_optionsEnabled) {
return [[NSData alloc] initWithBase64EncodedString:string options:NSDataBase64DecodingIgnoreUnknownCharacters];
} else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
return [[NSData alloc] initWithBase64Encoding:string];
#pragma clang diagnostic pop
}
}
- (NSString *)decodeAsString:(NSString *)string
{
NSData *data = [self decodeAsData:string];
if (!data) {
return nil;
}
return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}
- (NSString *)encodeData:(NSData *)data
{
if (!data) {
return nil;
}
if (_optionsEnabled) {
return [data base64EncodedStringWithOptions:0];
} else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
return [data base64Encoding];
#pragma clang diagnostic pop
}
}
- (NSString *)encodeString:(NSString *)string
{
return [self encodeData:[string dataUsingEncoding:NSUTF8StringEncoding]];
}
@end

View File

@ -0,0 +1,31 @@
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import <Foundation/Foundation.h>
#import "FBSDKBridgeAPIRequest.h"
@interface FBSDKBridgeAPICrypto : NSObject
+ (void)addCipherKeyToQueryParameters:(NSMutableDictionary *)queryParameters;
+ (NSDictionary *)decryptResponseForRequest:(FBSDKBridgeAPIRequest *)request
queryParameters:(NSDictionary *)queryParameters
error:(NSError *__autoreleasing *)errorRef;
+ (void)reset;
@end

View File

@ -0,0 +1,139 @@
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import "FBSDKBridgeAPICrypto.h"
#import "FBSDKBridgeAPIProtocol.h"
#import "FBSDKConstants.h"
#import "FBSDKCrypto.h"
#import "FBSDKError.h"
#import "FBSDKInternalUtility.h"
#import "FBSDKMacros.h"
#import "FBSDKSettings.h"
#import "FBSDKUtility.h"
static NSString *const FBSDKBridgeAPICryptoCipherKey = @"cipher";
static NSString *const FBSDKBridgeAPICryptoCipherKeyKey = @"cipher_key";
static NSString *g_cipherKey = nil;
@implementation FBSDKBridgeAPICrypto
#pragma mark - Class Methods
+ (void)addCipherKeyToQueryParameters:(NSMutableDictionary *)queryParameters
{
[FBSDKInternalUtility dictionary:queryParameters setObject:[self _cipherKey] forKey:FBSDKBridgeAPICryptoCipherKeyKey];
}
+ (NSDictionary *)decryptResponseForRequest:(FBSDKBridgeAPIRequest *)request
queryParameters:(NSDictionary *)queryParameters
error:(NSError *__autoreleasing *)errorRef
{
if (errorRef != NULL) {
*errorRef = nil;
}
NSString *cipher = queryParameters[FBSDKBridgeAPICryptoCipherKey];
if (!cipher) {
return queryParameters ?: @{};
}
NSString *version = queryParameters[FBSDKBridgeAPIVersionKey];
NSString *cipherKey = [self _cipherKey];
if (!version || !cipherKey) {
if (errorRef != NULL) {
NSDictionary *userInfo = @{
FBSDKErrorArgumentValueKey: queryParameters,
};
*errorRef = [FBSDKError errorWithCode:FBSDKEncryptionErrorCode
userInfo:userInfo
message:@"Error decrypting incoming query parameters."
underlyingError:nil];
}
return nil;
}
NSArray *additionalSignedDataArray = @[
[[NSBundle mainBundle] bundleIdentifier],
[FBSDKSettings appID] ?: @"",
@"bridge",
request.methodName ?: @"",
version,
];
NSString *additionalSignedDataString = [additionalSignedDataArray componentsJoinedByString:@":"];
NSData *additionalSignedData = [additionalSignedDataString dataUsingEncoding:NSUTF8StringEncoding];
FBSDKCrypto *crypto = [[FBSDKCrypto alloc] initWithMasterKey:cipherKey];
NSData *decryptedData = [crypto decrypt:cipher additionalSignedData:additionalSignedData];
if (!decryptedData) {
if (errorRef != NULL) {
NSDictionary *userInfo = @{
FBSDKErrorArgumentValueKey: @{
@"cipher": cipher,
@"bundleIdentifier": additionalSignedDataArray[0],
@"appID": additionalSignedDataArray[1],
@"host": additionalSignedDataArray[2],
@"methodName": additionalSignedDataArray[3],
@"version": additionalSignedDataArray[4],
},
};
*errorRef = [FBSDKError errorWithCode:FBSDKEncryptionErrorCode
userInfo:userInfo
message:@"Error decrypting incoming query parameters."
underlyingError:nil];
}
return nil;
}
NSString *decryptedString = [[NSString alloc] initWithData:decryptedData encoding:NSUTF8StringEncoding];
NSDictionary *decryptedDictionary = [FBSDKUtility dictionaryWithQueryString:decryptedString];
NSMutableDictionary *decryptedQueryParameters = [[NSMutableDictionary alloc] initWithDictionary:decryptedDictionary];
decryptedQueryParameters[FBSDKBridgeAPIVersionKey] = version;
return [decryptedQueryParameters copy];
}
+ (void)reset
{
[self _resetCipherKey];
}
#pragma mark - Object Lifecycle
- (instancetype)init
{
FBSDK_NO_DESIGNATED_INITIALIZER();
return nil;
}
#pragma mark - Helper Methods
+ (NSString *)_cipherKey
{
if (g_cipherKey) {
return g_cipherKey;
}
g_cipherKey = [[[NSUserDefaults standardUserDefaults] stringForKey:FBSDKBridgeAPICryptoCipherKeyKey] copy];
if (g_cipherKey) {
return g_cipherKey;
}
return [self _resetCipherKey];
}
+ (NSString *)_resetCipherKey
{
g_cipherKey = [[FBSDKCrypto makeMasterKey] copy];
[[NSUserDefaults standardUserDefaults] setObject:g_cipherKey forKey:FBSDKBridgeAPICryptoCipherKeyKey];
return g_cipherKey;
}
@end

View File

@ -0,0 +1,44 @@
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
//
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
// copy, modify, and distribute this software in source code or binary form for use
// in connection with the web services and APIs provided by Facebook.
//
// As with any software that integrates with the Facebook platform, your use of
// this software is subject to the Facebook Developer Principles and Policies
// [http://developers.facebook.com/policy/]. This copyright notice shall be
// included in all copies or substantial portions of the software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import <Foundation/Foundation.h>
#import <FBSDKCoreKit/FBSDKMacros.h>
#import "FBSDKBridgeAPIProtocolType.h"
@class FBSDKBridgeAPIRequest;
FBSDK_EXTERN NSString *const FBSDKBridgeAPIAppIDKey;
FBSDK_EXTERN NSString *const FBSDKBridgeAPISchemeSuffixKey;
FBSDK_EXTERN NSString *const FBSDKBridgeAPIVersionKey;
@protocol FBSDKBridgeAPIProtocol <NSObject>
- (NSURL *)requestURLWithActionID:(NSString *)actionID
scheme:(NSString *)scheme
methodName:(NSString *)methodName
methodVersion:(NSString *)methodVersion
parameters:(NSDictionary *)parameters
error:(NSError *__autoreleasing *)errorRef;
- (NSDictionary *)responseParametersForActionID:(NSString *)actionID
queryParameters:(NSDictionary *)queryParameters
cancelled:(BOOL *)cancelledRef
error:(NSError *__autoreleasing *)errorRef;
@end

Some files were not shown because too many files have changed in this diff Show More