iOS(Swift)

This guide provides step-by-step instructions on how to include the Conscent.ai Plugin in your iOS app. The Conscent.ai Plugin is developed in swift Language.

Pre-requisites

Conscent.ai iOS SDK supports iOS 13.0 and above.

Installation Steps

You can download the CCPlugin.xcframework File from here and add it to your project.

Make sure you change the embed mode for CCPlugin.xcframework to "Embed & Sign".

Initialize the SDK
  1. Import the CCPlugin framework into your ViewController class.

import CCPlugin
  1. Configure the SDK in didFinishLaunchingWithOptions in AppDelegate class

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  // Configure the SDK
  CCPlugin.shared.configure(mode: .sandbox, clientID: "your-client-id")
  // Additional configurations if necessary
  return true
}
  • yourClientId - Pass your clientId received from Conscent.ai.

  • Mode - configuration testing of different environments available.

Api Mode can be set as :
   Mode.sandbox
   Mode.production
  1. You need to set the scrollDepth for the paywall by accessing the scrollDepth property of the CCPlugin.shared instance and modifying its value.

// Retrieve and set the scroll depth
let screenHeight = scrollView.bounds.height
let scrollDepth: Int = Int(scrollView.contentOffset.y)
CCplugin.shared.scrollDepth = scrollDepth
  1. You have to confirm UIScrollViewDelegate and you need to set scrollDepth and scrollDepthPercentage.

  func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
    let scrollDepth: Int = Int(scrollView.contentOffset.y)
    CCplugin.shared.scrollDepth = scrollDepth
    let contentHeight = scrollView.contentSize.height
    let scrollViewHeight = scrollView.bounds.height
    let scrollOffset = scrollView.contentOffset.y
    // Calculate the scroll percentage
    let scrollDepthPercentage = (scrollOffset / (contentHeight - scrollViewHeight)) * 100.0
    // Use the scrollPercentage as needed (e.g., update a label or send to analytics)
    print("Scroll Depth: \(scrollDepthPercentage)%")
    CCplugin.shared.scrollDepthPercentage = scrollDepthPercentage
  }
  func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
    let contentHeight = scrollView.contentSize.height
    let scrollViewHeight = scrollView.bounds.height
    let scrollOffset = scrollView.contentOffset.y
    // Calculate the scroll percentage
    let scrollDepthPercentage = (scrollOffset / (contentHeight - scrollViewHeight)) * 100.0
    // Use the scrollPercentage as needed (e.g., update a label or send to analytics)
    print("Scroll Depth: \(scrollDepthPercentage)%")
    CCplugin.shared.scrollDepth = Int(scrollOffset)
    CCplugin.shared.scrollDepthPercentage = scrollDepthPercentage
  }
  1. You need to set the pageLength for the paywall by accessing the pageLength property of the CCPlugin.shared instance and modifying its value.

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    debugPrint("pageLength:\(scrollView.contentSize.height)")
    CCplugin.shared.pageLength = Int(scrollView.contentSize.height)
  }

Enabling the debug mode:

The debugMode property of the CCPlugin.shared instance can be set to true or false to enable or disable debug mode. When debug mode is enabled, toasts will be shown if the content ID or client ID entered is incorrect. This is useful for development purposes.

CCplugin.shared.debugMode = false

Initialize the paywall

In order to ensure that the Conscent.ai Paywall appears on the targeted pages and the deep insights and analytics are collected optimally you need to implement the following method on all the content/article pages.

CCplugin.shared.showPayWall(contentID: contentID,
           title: contentID,
           categories: ["category1","category2","category3"] ,
           sections: ["section12","section14"],
           tags: ["premium"],
           contentUrl: "https://www.google.com/",
           authorName: "abc",
           parentView: view,
           navigationController: self.navigationController,
           publicationDate: "2024-07-17T11:57:27.312Z",
           eventParamsDelegate: self,
           completiondelegate: self
    )                                    

Google Login Process
  1. To use Google login functionality you need to install pod in your project.

pod 'GoogleSignIn'
  1. Add your OAuth client ID and custom URL scheme

Update your app's Info.plist file to add your OAuth client ID and a custom URL scheme based on the reversed client ID.

The reversed client ID is your client ID with the order of the dot-delimited fields reversed. This is also shown under "iOS URL scheme" when selecting an existing iOS OAuth client in the Cloud console. For example: com.googleusercontent.apps.1234567890-abcdefg

<key>GIDClientID</key>
<string>YOUR_IOS_CLIENT_ID</string>
<key>CFBundleURLTypes</key>
<array>
  <dict>
    <key>CFBundleURLSchemes</key>
    <array>
      <string>YOUR_DOT_REVERSED_IOS_CLIENT_ID</string>
    </array>
  </dict>
</array>
  1. To initiate a Google login

CCplugin.shared.configure(mode: .sandbox, clientID: clientID)
CCplugin.shared.googleUserLogIn(controller: self, googleUserLogInDelegate: self)
Parameters
Description

contentID (string)

This will be your article or content id for which detail needs to be checked.

parentView

Pass the view on which you are going to show your content.

completionDelegate

This will be used to handle the success and failure cases. Pass the class as the delegate where you want to handle success or failure. This delegate is of the protocol CCPluginCompletionHandlerDelegate, which has three methods: purchasedOrNot(accessTime), success(accessType), onPaywallVisible(paywallType, paywallDisplayType, paywallHeight),loginSuccess(message, userId, authToken), failure(message)

subscriberDelegate(optional)

This is an optional callback that will be called if you pass your class as its delegate. It will be triggered when the subscription button is tapped. If you don't pass it in your delegate, it will not show the subscription view.

subscriberDelegate, which has one method: subscribeBtnTap() which will be triggered whenever the user clicks the Subscribe Button.

signInDelegate(optional)

This is an optional callback that will be called if you pass your class as its delegate. It will be triggered when the sign-in button is tapped. If you don't pass it in your delegate, it will not show the sign-in view.


signInDelegate, which has one method: signInTap() that will be triggered when the user clicks the signin button.

eventParamsDelegate(optional)

This will be used to get the events params. Pass the class as the delegate where you want to handle success. This delegate is of the protocol CCPluginEventParamsDelegate, which has methods: success(paywallId: String, contentId: String, paywallType: String, clientId: String, anonId: String) that will be triggered in case of google login click.

googleLogInDelegate(optional)

This will be used to trigger your Google sign. This delegate is of the protocol CCPluginGoogleLogInDelegate, which has methods: startGoogleLogin() that will be triggered in case of google login click.

Mandatory Step

  • In your Project go to your target and in the URL types add a new one with URL schemes "conscent".

  • This is important to handle redirection or app launches from the browser.

  • call below function inside of openURLContexts scene delegate(inbuilt in iOS).

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
    if let url = URLContexts.first?.url {
       CCplugin.shared.handleRelaunchApp(url: url)
    }
}

You need to call CCplugin.shared.exitSDK() while leaving the scope of current controller.

override func willMove(toParent parent: UIViewController?) {
    super.willMove(toParent: parent)
    if parent == nil {
      // Back button action was triggered
      debugPrint("Back button pressed")
      CCplugin.shared.exitSDK()
    }
  }

Call the below function and pass the userId, after the user has logged in:

CCplugin.shared.setClientUserId(clientUserId: "Your_User_Id")
Login Functionality
CCplugin.shared.configure(mode: .sandbox, clientID: clientID)
CCplugin.shared.userLogIn(userLogInDelegate: self)

CCPluginUserLogInDelegate

extension AccountViewController: CCPluginUserLogInDelegate {
  func userLogInSuccess(message: String, userId: String, authToken: String)
   {
    debugPrint("message: \(message), userId: \(userId), authToken: \(authToken)")
    }  
  func userLogInFailure(message: String, errorCode: String) {
        debugPrint(" message: \(message), errorCode: \(errorCode)")
        
    }
}

To enable Apple login follow these steps:

Xcode Steps:

  1. Open Xcode project.

  2. Go to Target Settings → Signing & Capabilities.

  3. Add "Sign in with Apple" capability.

  4. Verify entitlements.

  5. Ensure proper signing.

App Store Connect Steps:

  1. Select the app under "My Apps."

  2. Go to "App Information."

  3. Add "Sign in with Apple" information (privacy policy and terms of service URLs).

  4. Enable "Sign in with Apple."

  5. Submit changes for review.

Fetch User-details
CCplugin.shared.openUserDetails(userProfileDelegate: self)
CCplugin.shared.getUserDetail(completiondelegate: self)

CCPluginUserDetailsDelegate

extension AccountViewController: CCPluginUserDetailsDelegate {
    func success(userDetails: CCPlugin.UserDetails) {
         debugPrint("PhoneNo.: \(userDetails.phoneNumber ?? "") Email: \(userDetails.email ?? "") Name: \(userDetails.name ?? "")")     
    }
    
    func failure(error: String) {
        debugPrint(error)
    }
}

CCPluginUserProfileDelegate

extension AccountViewController: CCPluginUserProfileDelegate{
    func success(message: String, statusCode: String) {
        debugPrint("Message: \(message) statusCode:\(statusCode)")
    }
    
    func failure(message: String, errorCode: String) {
        debugPrint("Message: \(message) errorCode:\(errorCode)")
    }
    
}
Logout Functionality

CCPluginUserLogOutDelegate:

Pass the class as the delegate where you want to handle success or failure.

This delegate is of the protocol CCPluginlogout, which has two methods:

success() and failure() that will be triggered in case of success and failure of the process.

CCplugin.shared.userLogout(userLogOutDelegate: self)
extension AccountViewController: CCPluginlogout {
    func userLogOutSuccess() {
        debugPrint("loginSuccess")
    }
    
 func userLogOutFailure(message: String, errorCode: String) {
        debugPrint(" \(message), \(errorCode)")
        
    }
}

To use only the Subscription Landing Page, call the below method:

CCplugin.shared.openCustomUrl(customUrl: String)

CustomUrl is URL of the Subscription Landing Page with clientId.

Last updated