Tag: swift3

HOW TO INTEGRATE DROPBOX IN YOUR SWIFT PROJECT

HOW TO INTEGRATE DROPBOX IN YOUR SWIFT PROJECT

SwiftyDropbox is a Swift SDK that allows easy integration of Dropbox in your project. The system requirements for incorporating SwiftyDropbox in your project are:

  • iOS 9.0+
  • macOS 10.11+
  • XCODE 8.0+

Before incorporating the SDK, we require to register our app in Dropbox App Console, which will create a record of our app that will be associated with the API calls we make.

STEP-By-STEP INSTALLATION GUIDE

USING COCOA PODS

Open Terminal, come to the root folder by typing cd ..

Navigate to the project directory using the following & press enter

cd <Path_To_Your_Project>

Then create a new `Podfile` either with `pod init` or `open -a Xcode Podfile`. Once you have the pod file opened, add podSwiftyDropbox’. Your podfile should look something like this:

use_frameworks!

target ‘<YOUR_PROJECT_NAME>’ do

pod ‘SwiftyDropbox’

end

SwiftyDropox is compatible with Swift 3 but it also has Swift 2.3 compatible branch. You can access it using the following

use_frameworks!

target ‘<YOUR_PROJECT_NAME>’ do

pod ‘SwiftyDropbox’, :git => ‘https://github.com/dropbox/SwiftyDropbox&#8217;, :branch => ‘swift_2_3’

end

Run the following commands to install the dependency, one after the another

$ pod install

$ pod update

Congratulations! You have successfully installed SwiftyDropbox SDK to your project.

CONFIGURATION

Now go to your project and open .plist. Add the following to handle Apple’s new security changes.

<key>LSApplicationQueriesSchemes</key>

<array>

<string>dbapi-8-emm</string>

<string>dbapi-2</string>

</array>

This allows the Dropbox SDK to check if official Dropbox app is installed on the device, if it is then the app can be used to obtain OAuth 2.0 access token programmatically.

Also add the following code to your .plist

<key>CFBundleURLTypes</key>

<array>

<dict>

<key>CFBundleURLSchemes</key>

<array>

<string>db-<APP_KEY></string>

</array>

<key>CFBundleURLName</key>

<string></string>

</dict>

</array>

Change the <APP_KEY> in the above code. You can obtain the app key from dropbox app console where you initially registered your app. Save the changes made.

AUTHORISATION FLOW

INITIALISING CLIENT

Add the following code in your project’s AppDelegate.swift. Don’t forget to replace <APP_KEY> with your app’s app key

import SwiftyDropbox

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

DropboxClientsManager.setupWithAppKey(“<APP_KEY>”)

return true

}

Go to your view controller and add the following code.

import SwiftyDropbox

func OpenAuthorizationFlow() {

DropboxClientsManager.authorizeFromController(UIApplication.shared,

controller: self,

openURL: { (url: URL) -> Void in

UIApplication.shared.openURL(url)

})

}

There are 3 methods to retrieve the access token

  • If the iOS official Dropbox app is installed on the device then authentication will be done via the app and then redirected back to the SDK. This method is called Direct Auth
  • The app will open an In-App WebView for authenticating using dropbox’s authentication page.
  • Launching of default browser is another method.

So if you want to authenticate using the in-app web view then set browserAuth as false which is the fourth parameter in authorizeFromController:controller:openURL:browserAuth method.

Compile and Run the project, you will see a window like this

imageedit_4_9261894986

Next we need to handle the redirection once the authorisation is complete. Add the following code to AppDelegate.swift

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {

if let authResult = DropboxClientsManager.handleRedirectURL(url) {
switch authResult {
case .success(let token):
print(“Success! User is logged into Dropbox with token: \(token)”)
case .cancel:
print(“User cancelled OAuth flow.”)
case .error(let error, let description):
print(“Error \(error): \(description)”)
}
}
return false
}

 

On tapping Allow, the db-<APP_KEY> redirect URL will be launched from the webview, and will be handled in your application delegate’s application:handleOpenURL method, from which the result of the authorization can be parsed.

Congratulations! you have successfully incorporated Dropbox in your app. To learn about handling the API calls, refer next post of SwiftOverflow.

Advertisements

HOW TO USE ARRAY OF STRUCT IN SWIFT

Swift provides a  number of features that makes struct better than classes in most of the cases. I had attended a workshop where it was majorly focussed on array of struct.

Let us wrap up on few of the benefits of array of structs:

  • Structs are relatively small and light.
  • Structs are copiable as copying is a better and safer rather than having multiple references to the same reference as in case of classes.
  • Less of memory leaks

DECLARE ARRAY OF STRUCT

class ViewController: UIViewController {

var arrayOfStruct:Array<datastruct> = Array<datastruct>()

}

Once we have declared an array of struct type, we need to define the struct i.e. datastruct in this case.

struct datastruct{

var id:String?

var title:String?

var description:String?

init(add: NSDictionary){

id = add[“id”] as? String

title = add[“title”] as? String

description = add[“description”] as? String

}

}

Here, we have initialised the struct with parameter of NSDictionary type. But we can take other parameters too of different data types unlike NSDictionary. For example,

init(id: String, title: String, description: String) {

id = id

title = title

description = description

}

This is a simple implementation of array of struct.