The purpose of this document is to provide installation and usage instructions for the MyFitnessPal SDK for iOS.
- This SDK is intended to be used for MFP v1 API tokens.
- End users must have the MFP app installed on their device.
- The MyFitnessPalSDK may only be used by existing MFP partners. Please contact [email protected] for information on becoming a partner and obtaining a valid
clientId
.
The MyFitnessPalSDK supports installation via CocoaPods.
platform :ios, '8.0'
pod 'MyFitnessPalSDK', :git => 'https://github.com/myfitnesspal/ios-sdk-public.git'
Open the MyFitnessPalSDK workspace and build the MyFitnessPalSDK-Universal target. Then drag and drop the MyFitnessPalSDK.framework
into your Xcode project.
Update your app delegate to include creation of an MFPSession
object and a URL handler.
#import "AppDelegate.h"
#import <MyFitnessPalSDK/MyFitnessPalSDK.h>
@interface AppDelegate ()
@property (nonatomic, strong) MFPSession *mfpSession;
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.mfpSession = [[MFPSession alloc] initWithClientId:@"sampleapiclient"
urlSchemeSuffix:nil
responseType:MFPAuthorizationTypeAccessToken
cacheKey:nil];
return YES;
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
if ([[MFPSession activeSession] handleOpenURL:url]) {
if ([[MFPSession activeSession] isOpen]) {
NSLog(@"Session opened");
}
}
return TRUE;
}
In order for the native MyFitnessPal iOS app to call back into your app, you'll need to add a URL Type
to your Info.plist
.
Go to Targets -> "target name" -> Info -> URL Types. From there, you can set a target URL. The URL scheme must have a prefix of mfp
. Optionally, if you have multiple apps on the same device, you can have a suffix such as premium
.
Putting it all together, your URL scheme would be mfp-sampleapiclient-premium
. If you only have one app, then the URL scheme would be mfp-sampleapiclient
.
The SDK uses app to app deep linking which requires use of the canOpenUrl
method to see if the MyFitnessPal app is installed on your device. iOS 9 introduced security measures against this which require us to now whitelist our custom URL schemes.
To do this open up your Info.plist
file and add an array called LSApplicationQueriesSchemes
. Then add 2 strings as children called mfp
and mfphd
.
In order to make calls to the MyFitnessPal Partner API you need to obtain an accessToken
. The SDK wraps this up inside the MFPAccessTokenData
object.
See the following properties on MFPAccessTokenData
:
accessToken
refreshToken
expirationDate
- (void)openActiveSession:(id)sender {
[[MFPSession activeSession] openActiveSessionWithScope:@[MFPPermissionTypeDiary] onSuccess:^{
NSLog(@"Open active session success");
} onFailure:^(NSError *__autoreleasing *error) {
NSLog(@"Failed to open active session");
}];
}
- (void)refreshActiveToken:(id)sender {
[[MFPSession activeSession] refreshAccessTokenOnSuccess:^{
NSLog(@"Acess token refreshed");
} onFailure:^(NSError *__autoreleasing *error) {
NSLog(@"Failed to refresh access token");
}];
}
- (void)revokeAccess:(id)sender {
if ([[MFPSession activeSession] isOpen]) {
[[MFPSession activeSession] revokeAccessOnSuccess:^{
NSLog(@"Access revoked");
} onFailure:^(NSError *__autoreleasing *error) {
NSLog(@"Failed to revoke access");
}];
}
}
MFPSession *activeSession = [MFPSession activeSession];
if ([activeSession isOpen]) {
NSString *txt = [NSString stringWithFormat:@"Active Session:\nOPEN\n\naccessToken:\n%@\n\nrefreshToken:\n%@\n\nexpirationDate:\n%@",
activeSession.accessTokenData.accessToken,
activeSession.accessTokenData.refreshToken,
activeSession.accessTokenData.expirationDate];
self.textView.text = txt;
}
else {
self.textView.text = @"Active Session: CLOSED";
}
Doxygen documentation can be found in this repo under /Documentation/html/index.html
.
Once you've obtained an access token you can start making calls to the Partner API to add exercises, read diary data, etc...
Please direct all inbound support inquiries to: [email protected]