Bridge Google Drive API to Swift

Google maps Swift 2 cocoapods

I found the answer, in Build Settings in other linking flags I have -objC, I change that with $(inherited) like this the first answer of this question Undefined symbols for architecture armv7 while using Google-Maps-iOS-SDK (1.8.1)

Adding Google Analytics to my podfile breaks the build (library not found)

I have created a bug report here: https://github.com/CocoaPods/CocoaPods/issues/3908

They agreed it was a bug so the best solution is to wait until it is fixed.

I believe I could hack to add "$(inherited)" to get it working but personally I'll pass on this.

Edit: Bug was fixed and released. No more problems.

Duplicate interface definition for class 'GTMHTTPUploadFetcher'

I ran into this same problem. My solution was to not install the Google API Client for iOS using CocoaPods because I was using Swift-based pods and I, therefore, could not remove use_frameworks! to attempt to get around the duplicate header issue.

I instead installed the library manually by following the detailed instructions at https://developers.google.com/drive/ios/quickstart?ver=swift for Steps 2, 3, and 4. I followed the instructions but applied them to my existing workspace instead of creating a new workspace.

It's important to note that I had to adjust the paths in User Header Search Paths to match the locations where I actually copied the source code from Google.

I'm copying the instructions here for reference.

Step 2: Download the Google Client Library

Run the following commands to download the library using git:

  • git clone https://github.com/google/google-api-objectivec-client.git
  • git clone https://github.com/google/gtm-oauth2.git
  • git clone https://github.com/google/gtm-session-fetcher.git
  • git clone https://github.com/stig/json-framework.git -b v2.3
  • cp -R gtm-oauth2/Source google-api-objectivec-client/Source/OAuth2
  • cp -R json-framework/Classes google-api-objectivec-client/Source/JSON

Step 3: Prepare the workspace

Open Xcode and create a new workspace named "Quickstart".
Using File > Add Files to "Quickstart"..., add the following projects to the workspace from the libraries you cloned in the previous step:

  • google-api-objectivec-client/Source/GTL.xcodeproj
  • gtm-session-fetcher/Source/GTMSessionFetcher.xcodeproj

Select the "GTMSessionFetcher" project and make the following changes:

  • Add a new target of the type iOS > Framework & Library> Cocoa Touch Static Library and name it "GTMSessionFetcherLib".
  • Add all of the .m files in the project's GTMSessionFetcher group to the target's Build Phases > Compile Sources section.

Select the "GTL" project's "GTLTouchStaticLib" target and make the following changes:

  • Add the library GTMSessionFetcher/libGTMSessionFetcherLib.a to Build Phases > Link Binary with Libraries.
  • Add the absolute path to gtm-session-fetcher/Source/ to Build Settings > User Header Search Paths.
  • Add the flag GTM_USE_SESSION_FETCHER=1 to Build Settings > Preprocessor Macros.
  • Delete the "GTL" project's "GTLFramework" target.
  • In the Project navigator, delete GTL project's GTL Source > Common > HTTPFetcher group.

Step 4: Prepare the project

  • Create a new iOS > Application > Single View Application project named "QuickstartApp". Set the Language to Swift , and when saving the project set the Add to and Group fields to "Quickstart".
  • Add the following frameworks and libraries to the project's Build Phases > Link Binary with Libraries section:
    libGTLTouchStaticLib.a

    • Security.framework
    • SystemConfiguration.framework
  • Change the following Build Settings:
  • Add -ObjC -all_load to Other Linker Flags.
  • Add the absolute path to the following directories to User Header Search Paths:

    • gtm-session-fetcher/Source/
    • google-api-objectivec-client/Source/**
  • Add the flag GTM_USE_SESSION_FETCHER=1 to Preprocessor Macros.
  • Add the file google-api-objectivec-client/Source/OAuth2/Touch/GTMOAuth2ViewTouch.xib to the project's Supporting Files group.
  • Add the following files to the QuickstartApp group:

    • google-api-objectivec-client/Source/Services/Drive/Generated/GTLDrive_Sources.m
    • google-api-objectivec-client/Source/Services/Drive/Generated/GTLDrive.h
  • If not created automatically, create a new header file Bridging-Header.h with the following content:

    • #import "GTMOAuth2ViewControllerTouch.h"
    • #import "GTLDrive.h"
  • Set Build Settings > Objective-C Bridging Header to the absolute path of the bridging header.

Import Objective-C Framework (CocoaPod) into Swift?

Bridge

1. Create a xxx-Bridging-Header

Add a bridging header to your project using the method of your choice, the easiest one being creating a single .m file and answering Create Bridging Header to this dialog:

Create Bridging Header

2. Reference your Pod in the bridging header

Include your files as so:

//
// Use this file to import your target's public headers that
// you would like to expose to Swift.

#import "RTCICEServer.h"

3. Objective-C exposed to Swift

Once in the bridging header, you need not import the Obj-C classes in Swift. Use these directly:

let uri = URL(fileURLWithPath: "")
let rtc:RTCICEServer = RTCICEServer(uri: uri, username: "", password: "")
print(rtc)

Another example is described here.


► Find this solution on GitHub and additional details on Swift Recipes.

How send Email using Gmail api in swift

I found the solution

class func sendEmail() {

var gtlMessage = GTLGmailMessage()
gtlMessage.raw = self.generateRawString()

let appd = UIApplication.sharedApplication().delegate as! AppDelegate
let query = GTLQueryGmail.queryForUsersMessagesSendWithUploadParameters(nil)
query.message = gtlMessage

appd.service.executeQuery(query, completionHandler: { (ticket, response, error) -> Void in
println("ticket \(ticket)")
println("response \(response)")
println("error \(error)")
})
}

class func generateRawString() -> String {

var dateFormatter:NSDateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss Z"; //RFC2822-Format
var todayString:String = dateFormatter.stringFromDate(NSDate())

var rawMessage = "" +
"Date: \(todayString)\r\n" +
"From: <mail>\r\n" +
"To: username <mail>\r\n" +
"Subject: Test send email\r\n\r\n" +
"Test body"

println("message \(rawMessage)")

return GTLEncodeWebSafeBase64(rawMessage.dataUsingEncoding(NSUTF8StringEncoding))
}

URL Scheme: How can I create a link, which will open a document in the Google Drive app

There does not seem to be a good way to do what you want (at least according to my testing with Android 4.0.4; maybe the situation is different with other versions).

Using http: or https: links intercepted by an app

In theory, just using the https://docs.google.com/... link for the document should work for you. According to this answer on StackOverflow, intercepting http: or https: URLs is the proper way to start an app when opening a link from the Android browser. The Google Drive app does exactly this — it registers intent filters for https://drive.google.com, https://docs.google.com, https://spreadsheets.google.com and a bunch of similar URLs (including http: with the same host names). And this actually works — when using the stock Android browser, attempting to open a link pointing to https://drive.google.com results in the chooser popup with the Google Drive app included in the list (together with all installed browsers); selecting Google Drive results in opening the document in the Google Drive editor, as you want.

But the problem is that such intercepted HTTP[S] URLs work only in the stock Android browser — I have not been able to find any third-party browser which could show the app chooser when following such links. I tested Chrome, Dolphin, Firefox, Light Browser, Opera (including Classic and Mini), UC Browser, and all of them just opened the link internally instead of offering to pass it to the Google Drive app.

Using the intent: URI scheme

There is another way to make a link which starts an Android app — use the intent: URI scheme. I have not been able to find proper documentation for the intent: URI format; of course, the source code for the function which generates such URIs is available.

For your test document:

https://docs.google.com/document/d/1zSzDnV-90Ke3dzCCJ2CZ6iQ3JQ3F1hL1udGDqbNwwbY/edit?usp=sharing

the corresponding intent: link which opens it in the Google Drive app will be:

intent://docs.google.com/document/d/1zSzDnV-90Ke3dzCCJ2CZ6iQ3JQ3F1hL1udGDqbNwwbY/edit?usp=sharing#Intent;scheme=https;action=android.intent.action.VIEW;category=android.intent.category.DEFAULT;category=android.intent.category.BROWSABLE;package=com.google.android.apps.docs;end

A test link with this URI is on a separate page (it is not possible to make an actual link pointing to such URI here).

The process of conversion is as follows:

  1. Replace starting https: with intent:.

  2. Append intent parameters:

    #Intent;scheme=https;action=android.intent.action.VIEW;category=android.intent.category.DEFAULT;category=android.intent.category.BROWSABLE;package=com.google.android.apps.docs;end

    Here scheme=https correspond to https: in the original URL, so if you want to convert a plainhttp: URL, this field should be scheme=http. And package=com.google.android.apps.docs is the package name of the app which should handle the link.

Now, when such link is followed, the browser should open the Google Drive app directly (without showing the app chooser). However, if the app is not installed, Android will open the Market app instead, and perform a search for the specified package name, so that the user could install the required app.

It is also possible to make the intent: link without the package parameter:

intent://docs.google.com/document/d/1zSzDnV-90Ke3dzCCJ2CZ6iQ3JQ3F1hL1udGDqbNwwbY/edit?usp=sharing#Intent;scheme=https;action=android.intent.action.VIEW;category=android.intent.category.DEFAULT;category=android.intent.category.BROWSABLE;end

In this case the behavior should be the same as when the intercepted https: link is followed in the stock Android browser — the app chooser with the Google Drive app and all browser apps will be displayed, and if the Google Drive app is not installed, the user will not be redirected to install it from Market.

Unlike intercepted http: and https: links, intent: links work in a wider range of Android browser apps; unfortunately, some browsers do not support them. Results of my testing:

  • Works: stock Android 4.0.4 browser, Chrome, Light Browser, Opera, Opera Classic.
  • Does not work: Dolphin, Firefox (feature request is pending), UC Browser.

And, obviously, non-Android browsers would not support such links at all, so you will need to use some kind of browser sniffing if your pages also must be usable for other clients.

Using a custom URI scheme

Some apps use completely nonstandard URI schemes, which might also work from third-party browsers. However, the Google Drive app does not do that, therefore this solution is not suitable for it (unless someone creates a “bridge” app which just passes requests to the Google Drive app).

Some browsers could also disallow nonstandard URI schemes except some whitelisted ones (such as market:) due to security concerns; I did not try to test this.



Related Topics



Leave a reply



Submit