Marketo Mobile SDK for iOS 0.0.1
问题
如果您在使用或集成此插件时遇到问题,请在support.marketo.com创建支持工单
Marketo iOS SDK安装指南
先决条件
1. 在Marketo管理门户中注册一个应用程序,获取应用程序密钥和Munchkin ID。
2. 配置安卓推送访问,在此学习
(此处为链接).
3. 配置iOS推送访问,在此学习通过 Cocoapods 安装框架
暴露 removeDevicePushToken() 方法
1. 安装 CocoaPods。
sudo gem install cocoapods
2. 将目录更改为您的项目目录并创建一个带有智能默认设置的 Podfile。
pod init
3. 打开您的Podfile。
open -a Xcode Podfile
4. 在您的Podfile中添加以下行。
pod 'Marketo-iOS-SDK'
5. 保存并关闭您的Podfile。
6. 下载并安装Marketo iOS SDK。
pod install
7. 在Xcode中打开工作空间。打开App.xcworkspace
设置 Swift 桥接头
2. 命名文件 -Bridging-Header
3. 转到 Project -> Target -> Build Phases -> Swift Compiler - Code Generation -> 添加以下路径到 Objective-Bridging
##### 头文件: $(PODS_ROOT)/-Bridging-Header.h
SDK 初始化
在您可以使用 Marketo iOS SDK 之前,您必须使用您的 Munchkin 账户 ID 和 App 秘密键对其进行初始化。您可以在 Marketo Admin 区下方移动应用中找到这些信息。
1. 打开您的 AppDelegate.m 或 Bridging 文件 (Swift) 并导入 Marketo.h 头文件。
import <Marketo/Marketo.h>
2. 在 application:didFinishLaunchingWithOptions: 函数内粘贴以下代码。
Objective-C
Marketo *sharedInstance = [Marketo sharedInstance];
[sharedInstance initializeWithMunchkinID:@"munchkinAccountId" appSecret:@"secretKey" launchOptions:launchOptions];
Swift
let sharedInstance: Marketo = Marketo.sharedInstance()
sharedInstance.initializeWithMunchkinID("munchkinAccountId", appSecret: "secretKey", launchOptions: launchOptions)
3. 使用您的 Munchkin 账号 ID 和密钥替换上面的 munkinAccountId 和 secretKey,这些可以在 Marketo 管理员移动应用部分找到。
在苹果开发者账号上配置推送通知
1. 登录到苹果开发者会员中心。
2. 点击“Certificates, Identifiers & Profiles”。
3. 在“iOS, tvOS, watchOS”下点击“Certificates->All”文件夹。
4. 选择屏幕右上角的“+”按钮。
5. 启用“Apple Push Notification service SSL (Sandbox & Production)”复选框,然后点击“继续”。
6. 选择您用于构建应用的application identifier。
7. 创建并上传CSR以生成推送证书。
8. 将证书下载到本地计算机,并双击安装。
9. 打开“钥匙串访问”,鼠标右键点击证书,并将两项导出到.p12文件。
10. 通过Marketo管理控制台上传此文件以配置通知。
11. 更新应用程序配置文件。
#在xCode中启用推送通知
1. 在xCode项目中打开推送通知功能。
#在带有Marketo SDK的应用中启用推送通知
1. 在AppDelegate.h中导入以下内容。
Objective-C
#import <UserNotifications/UserNotifications.h>
Swift
import UserNotifications
2. 如下所示将 UNUserNotificationCenterDelegate 添加到 AppDelegate。
Objective-C
@interface AppDelegate : UIResponder <UIApplicationDelegate, UNUserNotificationCenterDelegate>
Swift
class AppDelegate: UIResponder, UIApplicationDelegate , UNUserNotificationCenterDelegate
3. 启动推送通知服务:要启用推送通知,请添加以下代码。
Objective-C
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
// ...
if ([UNUserNotificationCenter class])
{
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!error) {
NSLog(@"request authorization succeeded!");
}
}];
}
else if ([application respondsToSelector:@selector (registerUserNotificationSettings:)])
{
UIUserNotificationSettings *settings =
[UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert) categories:nil];
[application registerUserNotificationSettings:settings];
}
else
{
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
[application registerForRemoteNotificationTypes:myTypes];
}
[application registerForRemoteNotifications];
// ...
}
Swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// ...
if #available(iOS 10, *) {
let center = UNUserNotificationCenter.current()
center.delegate = self;
center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
// Enable or disable features based on authorization.
}
}
else
{
application.registerUserNotificationSettings(UIUserNotificationSettings(types: [.sound, .alert, .badge], categories: nil))
}
application.registerForRemoteNotifications()
// ...
}
4. 注册推送令牌
Objective-C
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
// Register the push token with Marketo
[[Marketo sharedInstance] registerPushDeviceToken:deviceToken];
}
Swift
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
// Register the push token with Marketo
Marketo.sharedInstance().registerPushDeviceToken(deviceToken)
}
令牌也可以取消注册
Objective-C
[[Marketo sharedInstance] unregisterPushDeviceToken];
Swift
Marketo.sharedInstance().unregisterPushDeviceToken
5. 处理推送通知:为了处理从Marketo接收的推送通知,请在AppDelegate中将以下代码进行放置。
Objective-C
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
[[Marketo sharedInstance] handlePushNotification:userInfo];
}
Swift
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
Marketo.sharedInstance().handlePushNotification(userInfo)
}
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
[[Marketo sharedInstance] application:application didReceiveLocalNotification:notification];
}
Swiftfunc application(_ application: UIApplication, didReceive notification: UILocalNotification) {
Marketo.sharedInstance().application(application, didReceive: notification)
}
7. 在AppDelegate中添加以下方法:使用此方法,您可以在应用在前台时显示警报、声音或增加角标。从iOS 10开始,您必须在此方法中调用您选择的completionHandler。
Objective-C#ifdef __IPHONE_10_0
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
NSLog(@"Notification is triggered");
completionHandler(UNNotificationPresentationOptionAlert); // OR
// completionHandler(UNNotificationPresentationOptionBadge); OR
// completionHandler(UNNotificationPresentationOptionSound);
}
#endif
Swift@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler(.alert) // OR
// completionHandler(.badge) OR
//completionHandler(.sound)
}
8. 在 AppDelegate iOS 10 中处理新接收到的推送通知:当用户通过打开应用、移除通知或选择一个 UNNotificationAction 对通知做出响应时,此方法将在代理上被调用。在应用从 applicationDidFinishLaunching: 返回之前,必须设置代理。
Objective-C#ifdef __IPHONE_10_0
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
withCompletionHandler:(void(^)())completionHandler {
[[Marketo sharedInstance] userNotificationCenter:center didReceiveNotificationResponse:response withCompletionHandler:completionHandler];
}
#endif
Swift@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
Marketo.sharedInstance().userNotificationCenter(center, didReceive: response, withCompletionHandler: completionHandler)
}
Objective-C
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
[[Marketo sharedInstance] application:application didReceiveLocalNotification:notification];
}
func application(_ application: UIApplication, didReceive notification: UILocalNotification) {
Marketo.sharedInstance().application(application, didReceive: notification)
}
#ifdef __IPHONE_10_0
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
NSLog(@"Notification is triggered");
completionHandler(UNNotificationPresentationOptionAlert); // OR
// completionHandler(UNNotificationPresentationOptionBadge); OR
// completionHandler(UNNotificationPresentationOptionSound);
}
#endif
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler(.alert) // OR
// completionHandler(.badge) OR
//completionHandler(.sound)
}
#ifdef __IPHONE_10_0
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
withCompletionHandler:(void(^)())completionHandler {
[[Marketo sharedInstance] userNotificationCenter:center didReceiveNotificationResponse:response withCompletionHandler:completionHandler];
}
#endif
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
Marketo.sharedInstance().userNotificationCenter(center, didReceive: response, withCompletionHandler: completionHandler)
}
#iOS 测试设备
1. 选择 Project -> Target -> Info -> URL 类型
2. 添加标识符:${PRODUCT_NAME}
3. 设置URL模式:mkto-
5. 包含应用:openURL:sourceApplication:annotation: 至 AppDelegate.m
在AppDelegate中处理自定义URL类型
Objective-C
#ifdef __IPHONE_10_0
-(BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
options:(NSDictionary<NSString *,id> *)options{
return [[Marketo sharedInstance] application:application
openURL:url
sourceApplication:nil
annotation:nil];
}
#elif
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
return [[Marketo sharedInstance] application:application
openURL:url
sourceApplication:nil
annotation:nil];
}
#endif
Swift
private func application(app: UIApplication, openURL url: URL, options: [String : AnyObject]) -> Bool {
return Marketo.sharedInstance().application(app, open: url, sourceApplication: nil, annotation: nil)
}
如何创建iOS用户资料
1. 创建用户资料
您可以通过发送如下所示的用户字段来创建丰富的个人资料。
Objective-C
MarketoLead *profile = [[MarketoLead alloc] init];
// Get user profile from network and populate
[profile setEmail:@"[email protected]"];
[profile setFirstName:@"John"];
[profile setLastName:@"Doe"];
[profile setAddress:@"1234KingFishSt"];
[profile setCity:@"SouthPadreIsland"];
[profile setState:@"CA"];
[profile setPostalCode:@"78596"];
[profile setCountry:@"USA"];
[profile setGender:@"male"];
[profile setLeadSource:@"_facebook_ads"];
[profile setBirthDay:@"01/01/1985"];
[profile setFacebookId:@"facebookid"];
[profile setFacebookProfileURL:@"facebook.com/profile"];
[profile setFacebookProfilePicURL:@"faceboook.com/profile/pic"];
[profile setLinkedInId:@"linkedinid"];
[profile setTwitterId:@"twitterid"];
Swift
let profile = MarketoLead()
// Get user profile from network and populate
profile.setEmail("[email protected]")
profile.setFirstName("John")
profile.setLastName("Doe")
profile.setAddress("1234KingFishSt")
profile.setCity("SouthPadreIsland")
profile.setState("CA")
profile.setPostalCode("78596")
profile.setCountry("USA")
profile.setGender("male")
profile.setLeadSource("_facebook_ads")
profile.setBirthDay("01/01/1985")
profile.setFacebookId("facebookid")
profile.setFacebookProfileURL("facebook.com/profile")
profile.setFacebookProfilePicURL("faceboook.com/profile/pic")
profile.setLinkedInId("linkedinid")
profile.setTwitterId("twitterid")
2. 添加更多标准字段
Objective-C
// Add other custom fields
[profile setFieldName:@"mobilePhone"withValue:@"123.456.7890"];
[profile setFieldName:@"numberOfEmployees"withValue:@"10"];
[profile setFieldName:@"phone"withValue:@"123.456.7890"];
Swift
profile.setFieldName("mobilePhone" , withValue :"123.456.7890");
profile.setFieldName("numberOfEmployees", withValue: "10");
profile.setFieldName("phone", withValue:"123.456.7890");
3. 报告用户资料
Objective-C
Marketo *sharedInstance = [Marketo sharedInstance];
// This method will update user profile
[sharedInstance associateLead:profile];
Swift
let marketo = Marketo.sharedInstance()
// This method will update user profile
marketo.associateLead(profile)
如何发送iOS上的自定义行为
您可以通过发送自定义行为来跟踪用户交互。
1. 发送自定义行为。
Objective-C
Marketo *sharedInstance = [Marketo sharedInstance];
[sharedInstance reportAction:@"Login" withMetaData:nil];
2. 添加自定义行为元数据。
Objective-C
MarketoActionMetaData *meta = [[MarketoActionMetaData alloc] init];
[meta setType:@"Shopping"];
[meta setDetails:@"RedShirt"];
[meta setLength:20];
[meta setMetric:30];
[sharedInstance reportAction:@"Bought Shirt" withMetaData:meta];
Swift
let meta = MarketoActionMetaData()
meta.setType("Shopping");
meta.setDetails("RedShirt");
meta.setLength(20);
meta.setMetric(30);
sharedInstance.reportAction("Bought Shirt", withMetaData:meta);
高级安全访问模式
(此处链接)。
Marketo SDK 提供了设置和删除安全密钥的方法。还有一个实用方法可以检索设备 ID。在登录客户服务器时,应将设备 ID 与电子邮件一起传递,用于计算安全密钥。SDK 需要击中新端点,指向上列算法,以检索实例化密钥对象的必要字段。如果已启用 Marketo 移动管理员中的安全访问模式,则必须在该 SDK 中设置此密钥,作为必要步骤。有关高级安全访问模式的更多信息,请在此处了解Objective-C
Marketo * sharedInstance =[Marketo sharedInstance];
// set secure signature
MKTSecuritySignature *signature =
[[MKTSecuritySignature alloc] initWithAccessKey:<ACCESS_KEY> signature:<SIGNATURE_TOKEN> timestamp:<EXPIRY_TIMESTAMP> email:<EMAIL>];
[sharedInstance setSecureSignature:signature];
[sharedInstance removeSecureSignature];
[sharedInstance getDeviceId];
Swift
let sharedInstance = Marketo.sharedInstance()
// set secure signature
let signature = MKTSecuritySignature(accessKey: <ACCESS_KEY>, signature: <SIGNATURE_TOKEN> , timestamp: <EXPIRY_TIMESTAMP>, email: <EMAIL>)
sharedInstance.setSecureSignature(signature)
[sharedInstance removeSecureSignature];
sharedInstance.getDeviceId()