AppsFlyerSDK / appsflyer-flutter-plugin

Flutter Plugin for AppsFlyer SDK
MIT License
144 stars 114 forks source link

Swift and Kotlin Native Code Setup #117

Closed supposedlysam-bb closed 3 years ago

supposedlysam-bb commented 3 years ago

Documentation Enhancement

Swift and Kotlin setup alternatives severely needed

As myself and my team come from a web background, none of us know how to convert the Objective-C and Java code provided in your guides to the appropriate Swift and Kotlin alternative; effectively blocking us from integrating with the appsflyer sdk plugin.

Please update your guide to include alternatives for both Swift and Kotlin!

GM-appsflyer commented 3 years ago

Hey @supposedlysam-bb Thanks for the feedback.

I've updated the guides doc for Kotlin/swift support, please find it here: https://github.com/AppsFlyerSDK/appsflyer-flutter-plugin/blob/master/doc/Guides.md

When creating a kotlin/swift app the only thing needed to be added is the deeplink handling: Life-cycles for iOS and the onNewIntent method for android.

Please let us know if you've encountered any specific issue on the matter.

supposedlysam-bb commented 3 years ago

Thank you! I really appreciate the help!

I also noticed there is some additional setup in the github readme that isn't in the Flutter guides. Does the additional setup for iOS 14 also need to be done for the Flutter app?

iOS 14 Plugin Setup

supposedlysam-bb commented 3 years ago

Also, is there a different import for Swift? Looks like the only Module it can find is the AppsFlyerLib module. I would think I should switch out all AppsFlyerAttribution references with AppsFlyerLib; is that correct?

supposedlysam-bb commented 3 years ago

Based on the docs, this is what I've added but it doesn't know what AppsFlyerAttribution is. Replacing it with AppsFlyerLib results in an error saying, Cannot force unwrap value of non-optional type 'AppsFlyerLib'. Removing the exclamation point results in many more errors. Would you be able to create an example project with Swift and Kotlin so I can pull from there?

import UIKit
import Flutter
import AppsFlyerLib

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        if #available(iOS 10.0, *) {
            UNUserNotificationCenter.current().delegate = self
        }
        GeneratedPluginRegistrant.register(with: self)
        return super.application(application, didFinishLaunchingWithOptions: launchOptions)
    }

    override func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
        AppsFlyerAttribution.shared()!.handleOpenUrl(url, sourceApplication: sourceApplication, annotation: annotation);
        return true
    }

    override func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
        AppsFlyerAttribution.shared()!.handleOpenUrl(url, options: options)
        return true
    }

     private func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
        AppsFlyerAttribution.shared()!.continueUserActivity(userActivity, restorationHandler: nil)
        return true
    }

    override func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
        AppsFlyerAttribution.shared()!.continueUserActivity(userActivity, restorationHandler: nil)
        return true
     }
}

Errors after replacing with Lib and removing exclamations image

GM-appsflyer commented 3 years ago

Hey @supposedlysam-bb,

Because of the plugins limitation, the flutter iOS is "waking up" later then the native iOS SDK. Because of that we've provided a workaround and implemented a different import in IOS (It's the reason we've implemented the "AppsFlyerAttribution"). Please make sure to use "#import "AppsflyerSdkPlugin.h" in the "Runner-Bridging-Header.h" file and the "AppsFlyerAttribution" should be recognized.

Regarding iOS 14, yes, it should be implemented in swift as well. This is Apple API (not appsflyer) that is in charge of calling the consent dialog for the user. I will make sure to add the swift version to our flutter docs as well. For now, find the swift implementation in the following article under the section "3.3 Configuring App Tracking Transparency (ATT) supportCopy direct link": https://support.appsflyer.com/hc/en-us/articles/207032066#integration-33-configuring-app-tracking-transparency-att-support

supposedlysam-bb commented 3 years ago

After adding the import to the Runner-Bridging-Header.h file and removing the import to AppsFlyerLib from the AppDelegate.swift I was able to get it to build successfully; only to immediately crash with an error:

-[NSNull doubleValue]: unrecognized selector sent to instance 0x1e2c10a00

That backtraces to the line 428 of the AppsFlyerSdkPlugin

timeToWaitForATTUserAuthorization = [(id)call.arguments[afTimeToWaitForATTUserAuthorization] doubleValue];

More setup I'm missing maybe?

supposedlysam-bb commented 3 years ago

In addition to the above changes, setting the timeToWaitForATTUserAuthorization on the AppsFlyerOptions I pass to the AppsFlyerSdk seemed to get me past this error!