Bridge Google Drive API to Swift - ios

From a previously removed post:
I am struggling to get the Google Drive API to work with Swift, and hoping someone has a suggestion. Here is where I am at so far: I have the Google Drive API installed and working in an Objective-C ...
I am trying to reproduce this example from Google in Swift, but import GTLDrive returns an error in Xcode:
No such module 'GTLDrive.
I am unable to use GTLServiceDrive from the Swift classes.
Which combination of CocoaPod + bridging header should I use?

You need 3 things:
(1) Well formed Podfile
platform :ios, '8.0'
target 'GoogleDrive' do
pod 'Google-API-Client/Drive', '~> 1.0'
end
(2) Expose Google API through the bridging headers
#import "GTMOAuth2ViewControllerTouch.h"
#import "GTLDrive.h"
(3) No reference GTLDrive required in the Swift client class
override func viewDidLoad() {
super.viewDidLoad()
// ...
let service:GTLServiceDrive = GTLServiceDrive()
service.authorizer = GTMOAuth2ViewControllerTouch.authForGoogleFromKeychainForName("Drive API",
clientID: "YOUR_CLIENT_ID_HERE",
clientSecret: "YOUR_CLIENT_SECRET_HERE")
// ...
}

I just ran into that now in XCode 7.3, and fixed it with the following in the bridging header:
#import <GoogleAPIClient/GTLDrive.h>
Also, if you need the authentication portion:
#import <GTMOAuth2/GTMOAuth2ViewControllerTouch.h>

I threw away Google API Objective C library from Swift altogether and created an interim solution: https://github.com/metaprgmr/GoogleApiProxyForSwift. Incidentally, I used Google Drive as a guinnea pig, where you can find experiments at close to the end of its YouTube presentation. Check it out.

Related

Redefinition of module 'Realm'

I working on an Objective c project, and trying to get it to work with Swift too.
I've allready have been throw this process in the past, but did'nt get this error.
After adding use_framworks to my podfile, and replacing all "" imports with <> or #import, I'm getting this error:
Redefinition of module Realm
In the module.modulemap file:
framework module Realm { //This is the line the error is on
umbrella header "Realm.h"
export *
module * { export * }
explicit module Private {
header "RLMAccessor.h"
header "RLMArray_Private.h"
header "RLMListBase.h"
header "RLMMigration_Private.h"
header "RLMObjectSchema_Private.h"
header "RLMObjectStore.h"
header "RLMObject_Private.h"
header "RLMOptionalBase.h"
header "RLMProperty_Private.h"
header "RLMRealmConfiguration_Private.h"
header "RLMRealm_Private.h"
header "RLMResults_Private.h"
header "RLMSchema_Private.h"
}
explicit module Dynamic {
header "RLMRealm_Dynamic.h"
header "RLMObjectBase_Dynamic.h"
}
}
Any help?
Thanks
You only need to use the use_frameworks! keyword in CocoaPods if you're bringing in a dependency that uses Swift code. Is that what you're doing?
If you're also starting to access Realm code from a Swift class, make sure you've included the Swift/RLMSupport.swift bridging header as well.
Aside from that, looking at the CocoaPods issue where this sort of issue was reported, you should also make sure that you've absolutely removed all traces of #import "" everywhere.
If that still doesn't work for you, then you might need to add some more information to your question about how your project is set up with regards to how it uses both Objective-C and Swift, and what your podfile looks like.

Implement Google Analytics in ios swift

I am following the Analytics for iOS (developers.google.com/analytics/devguides/collection/ios/v3/?ver=swift) guide and I've got errors in my Swift code Project that I can't fix.
I am working with XCode 6.4, Swift and the iOS Deployment Target 8.1.
Step 1
First I installed a Google SDK using CocoaPods.
This is the console result after running pod install command:
Updating local specs repositories
CocoaPods 1.0.0.beta.2 is available.
To update use: `gem install cocoapods --pre`
[!] This is a test version we'd love you to try.
For more information see http://blog.cocoapods.org
and the CHANGELOG for this version http://git.io/BaH8pQ.
Analyzing dependencies
Downloading dependencies
Using Google (1.0.7)
Using GoogleAnalytics (3.14.0)
Using GoogleNetworkingUtilities (1.0.0)
Using GoogleSymbolUtilities (1.0.3)
Using GoogleUtilities (1.1.0)
Generating Pods project
Integrating client project
Sending stats
Pod installation complete! There is 1 dependency from the
Podfile and 5 total pods installed.
Step 2
Then opened, as said in the guide, my app's Project .xcworkspace file.
My Podfile looks like this:
# Uncomment this line to define a global platform for your project
# platform :ios, '8.0'
# Uncomment this line if you're using Swift
# use_frameworks!
target 'XXXXXX' do
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.1'
pod 'Google/Analytics', '~> 1.0.0'
end
target 'XXXXXXTests' do
pod 'Google/Analytics', '~> 1.0.0'
end
Where XXXXXX is my Project's name.
Step 3
I got the configuration file GoogleService-Info.plist and included in my Project adding all the targets (2 targets in my project).
Step 4
I created a BridgingHeader by by choosing File > New > File > iOS > Source > Header File.
I named it BridgingHeader.h and is in the root of my Project.
The content is:
#ifndef XXXXX_BridgingHeader_h
#define XXXXX_BridgingHeader_h
#import "Google/Analytics.h"
#import <Google/Analytics.h>
#include "GAI.h"
#import <CoreData/CoreData.h>
#import <SystemConfiguration/SystemConfiguration.h>
#import "Libraries/GoogleAnalytics/GAI.h"
#import "Libraries/GoogleAnalytics/GAIFields.h"
#import "Libraries/GoogleAnalytics/GAILogger.h"
#import "Libraries/GoogleAnalytics/GAITracker.h"
#import "Libraries/GoogleAnalytics/GAIDictionaryBuilder.h"
#endif
Where "XXXXX" is my Project's name.
Step 5
Now the problems:
I tried to include/import the Google Analytics into my AppDelegate.swift but I can't. This is the error:
AppDelegate.swift import Google Analytics
I also tried import "Google/Analytics.h" but another error appears: Expected identifier in import declaration.
How can I fix this so XCode doesn't give me errors?
Is the BridgingHeader wrong? Do I have to point at this somehow to recognize its inner headers?
Do I have to configure something else for the Google Analytics that I am missing right now?
Thank you very much.
There are two options for implementation with Google Analytics using CocoaPods.
pod 'Google/Analytics'
pod 'GoogleAnalytics'
There are pros and cons between them.
pod 'Google/Analytics'
need google configuration file(GoogleService-Info.plist)
simple bridging header file. Just add #import <Google/Analytics.h> in bridging header file.
add import Googlein every file you want to implement google analytics.
pod 'GoogleAnalytics'
no google configuration file(GoogleService-Info.plist)
more complex bridging header file.
I prefer to use pod 'GoogleAnalytics' but I'll explain how to solve this issue using pod 'Google/Analytics'
because the google official site recommends pod 'Google/Analytics'.
bridging header
You just need one line of code for google analytics.
#import <Google/Analytics.h>
Don't forget to set target-build setting for Objective-C-Bridging-Header.
You have to provide correct path to enable Objective-C-Bridging-Header.
Set Target-Build Setting-Objective-C-Bridging-Header
$(SRCROOT)/$(PRODUCT_NAME)/projectName_Bridging_Header.h
AppDelegate.swift
import Google
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?)
-> Bool { self.setupGoogleAnalytics()
..
self.setupGoogleAnalytics()
..
}
func setupGoogleAnalytics() {
// Configure tracker from GoogleService-Info.plist.
let configureError:NSError?
GGLContext.sharedInstance().configureWithError(&configureError)
assert(configureError == nil, "Error configuring Google services: \(configureError)")
let gai = GAI.sharedInstance()
gai.trackUncaughtExceptions = true // report uncaught exceptions
gai.logger.logLevel = GAILogLevel.Verbose // remove before app release
}
SomeViewController.swift
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(true)
if let default_tracker = GAI.sharedInstance().defaultTracker {
#if DEBUG
print("default tracker")
#endif
}
// let tracker = GAI.sharedInstance().defaultTracker
let tracker = GAI.sharedInstance().trackerWithTrackingId("tracking_ID")
tracker.set(kGAIScreenName, value: screenName)
let builder = GAIDictionaryBuilder.createScreenView()
tracker.send(builder.build() as [NSObject : AnyObject])
}
Why do I use trackerWithTrackingId instead of defaultTracker property? You could got an error if you use defaultTracker :
fatal error: unexpectedly found nil while unwrapping an Optional value
defaultTracker property's initial value is nil, but it will be set after
trackerWithTrackingId method is called. But it doesn't work perfectly sometimes. To avoid this issue, I recommend that use trackerWithTrackingId method directly.
I make the sample project using pod 'GoogleAnalytics'. You can get an idea from it.
Good luck.
Test Env
GoogleAnalytics 3.14
Xcode 7.2.1
In Podfile
pod 'Google/Analytics'
In YourFantasticProjectName-Bridging-Header.h
#import "Google/Analytics.h"
You don't need this
GGLContext.sharedInstance().configureWithError(&configureError)
You need to have a proper tracker
let gai = GAI.sharedInstance()
let tracker = gai.tracker(withTrackingId: "UA-12345678-1")
In order for live view to work in GA dashboard, you must track screen using GAIDictionaryBuilder and correct key kGAIScreenName
tracker.set(kGAIScreenName, value: "this is my screen")
let event = GAIDictionaryBuilder.createScreenView()
tracker?.send(event!.build() as! [NSObject: Any])
In the same vein, to track events, you need to use GAIDictionaryBuilder as it will create dictionary with correct GA keys, and GA likes correct keys
let event = GAIDictionaryBuilder.createEvent(withCategory: "category", action: "action", label: "level", value: NSNumber(value: 120))
tracker?.send(event!.build() as! [NSObject: Any])
It seems to work in the simulator too
I faced the same problem. I could not import the "Google/Analytics.h" header as Xcode generate error. Because "Google/Analytics.h" header is not available in the 'GoogleAnalytics sdk' as mentioned in Google's Official page.
So, i just used following line
#import "GAI.h"
Hope it will work just fine.
Environment
Xcode: 8.2
iOS :10.2
For swift 3:
var configureError:NSError? = nil
GGLContext.sharedInstance().configureWithError(&configureError)
assert(configureError == nil, "Error configuring Google services: \(configureError)")
let gai = GAI.sharedInstance()
gai?.trackUncaughtExceptions = true
gai?.logger.logLevel = GAILogLevel.verbose
I think it's better to send error to crashlytics, but continue executing an app:
func configureGoogleAnalytics() {
var configureError: NSError? = nil
GGLContext.sharedInstance().configureWithError(&configureError)
if configureError != nil {
Crashlytics.sharedInstance().recordError(configureError!)
}
}
Also check this answer for newest way to add analytics.

google adwords conversion tracking with swift

I am trying to get google adwords working on a swift project I have followed https://developers.google.com/app-conversion-tracking/ios/ and have had no results. Keeps saying no such module "ACTReporter"
Does anyone have any info?
This is possible in Swift using the usual bridging header routine. You can add the SDK to your project manually or use the GoogleConversionTracking pod.
Just add the pod to your podfile:
pod 'GoogleConversionTracking'
and then add the following to your bridging header:
#import <GoogleConversionTracking/ACTReporter.h>
And finally, in your AppDelegate.swift file (with the correct ID and label):
// Google Conversion Tracking
ACTAutomatedUsageTracker.enableAutomatedUsageReportingWithConversionID("0123456789")
ACTConversionReporter.reportWithConversionID("0123456789", label: "XXXXXX", value: "0.00", isRepeatable: false)

Failed to recognize AWSDynamoDB with XCode 7.0 Beta, iOS 8.3, and aws-ios-sdk-2.2.0

I am matching guides for using AWSDynamoDB in a test project using Swift 2 in XCode 7.0 Beta. I am required to use that platform instead of the previous stable one and make it work.
I am using the next links:
https://docs.aws.amazon.com/mobile/sdkforios/developerguide/setup.html
http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/LoadData_Java.html
Best way to make Amazon AWS DynamoDB queries using Swift?
I already made it work for reading and writing text files and images to an S3 Bucket, but now that I am trying to use DynamoDB service something might be missing.
Here is my Podfile contents:
# Uncomment this line to define a global platform for your project
platform :ios, '8.3'
target 'AWSSDKTest' do
source 'https://github.com/CocoaPods/Specs.git'
pod 'AWSCore'
pod 'AWSAutoScaling'
pod 'AWSCloudWatch'
pod 'AWSDynamoDB'
pod 'AWSEC2'
pod 'AWSElasticLoadBalancing'
pod 'AWSKinesis'
pod 'AWSLambda'
pod 'AWSMachineLearning'
pod 'AWSMobileAnalytics'
pod 'AWSS3'
pod 'AWSSES'
pod 'AWSSimpleDB'
pod 'AWSSNS'
pod 'AWSSQS'
pod 'AWSCognito'
end
target 'AWSSDKTestTests' do
end
I had to delete Podfile.lock and Pods folder, deleted Configuration Sets in Project->Info->Configurations, and installed pods again.
Here is my bridging.h file:
#ifndef AWSSDKTest_bridging_h
#define AWSSDKTest_bridging_h
#endif
#import <AWSCore/AWSCore.h>
#import <AWSS3/AWSS3.h>
#import <AWSDynamoDB/AWSDynamoDB.h>
#import <AWSSQS/AWSSQS.h>
#import <AWSSNS/AWSSNS.h>
#import <AWSCognito/AWSCognito.h>
I downloaded the aws-ios-sdk-2.2.0.zip file, unzipped and added to Frameworks all the AWS frameworks.
I am trying to use AWSDynamoDBModel, here is a swift file for implementing the Upload Sample Items Example for using DynamoDB Mapper:
import Foundation
class Forum : AWSDynamoDBModel, AWSDynamoDBModeling {
var name : String = ""
var category : String = ""
var threads : Int = 0
var messages : Int = 0
var views : Int = 0
// override init!() { super.init() }
required init!(coder: NSCoder!) {
fatalError("init(coder:) has not been implemented")
}
class func dynamoDBTableName() -> String! {
return "Demo"
}
class func hashKeyAttribute() -> String! {
return "email"
}
class func rangeKeyAttribute() -> String! {
return "date"
}
/*
override init(dictionary dictionaryValue: [NSObject : AnyObject]!, error: NSErrorPointer) {
super.init(dictionary: dictionaryValue, error: error)
}
override func isEqual(anObject: AnyObject?) -> Bool {
return super.isEqual(anObject)
}
*/
}
I had to comment the lines that caused error because those might be fixed. The errors mentioned that those functions couldnt be overridden and that super.init couldnt be called inside a root method.
After cleaning and building again, the error is at the class declaration line
class Forum : AWSDynamoDBModel, AWSDynamoDBModeling {
The error says: Use of undeclared type 'AWSDynamoDBModel'
If I try writing other AWSDynamoDB classes they don't appear in the list of suggestions and then cause the same error.
Additionally I want to mention that in the developer's guide setup (first link above) the 4th point of the Getting Started with Swift section says: "Import the AWSCore header in the application delegate":
#import <AWSCore/AWSCore.h>
Than can't be done, only in the bridging.h file which is mentioned in the 2nd point.
My first request of assistance is in fixing the error mentioned and making the project recognize the AWSDynamoDB framework.
Then I request your help for any observation about this merge of different tutorials, also any other online tutorial or guide that is more clear than those mentioned.
In case you are using CocoaPods (0.36) with "use_frameworks!", this answer might solve your problem:
"Normally when you’re importing Objective-C code into Swift, you do so by including the header of the file containing that code in the “Bridging Header” for your project. And that is indeed how you include code from a static library (which your pods used to be.)
But it is not how your import Objective-C code from a Framework. To do that you simply type…
import Framework
…inside your Swift file that’s using the Objective-C class (where “Framework” is the name of the actual Framework containing the class.)"
Source here: http://rogueleaderr.com/post/115372471213/unresolved-identifier-in-swift-when-importing
I think the problem is not related to Xcode7Beta, but the installation process of your project. (My sample DynamoDB project runs fine under Xcode7Beta.)
If you installed the AWS Mobile SDK via cocoapods, you neither have to worry about the bridging file nor need to download aws-ios-sdk-2.2.0.zip file since cocoapods already did everything for you.
My suggestion would be:
clean your project, remove all aws related frameworks, bridging files that you manually added,delete Podfile.lock and Pods folder and AWSSDKTest.xcworkspace file, and the re integrate pods by running "pod install"
Open your project using Xcode 6.4, confirm it can be built and run successfully under Xcode 6.
If everything looks good via Xcode 6, reopen it via Xcode7Beta, If it failed to compile, please post the error output so I can take a look.
Thanks

How to send emails in Swift using Mailgun

I have the need to send automatic emails in a Swift project I'm working on. So far my best lead seems to be using Mailgun. (I'm open to better options if anyone has liked something else)
Swift is not listed in the Mailgun API Reference in their documentation, and I didn't see objective-c either. The only article speaking at all about his I've found is this one.
Update
I've been trying to piece together everything and this is where I've gotten so far.
I was able to get Mailgun installed via cocoapods. Using it in Swift has been kinda tricky.
I setup cocoapods with the following pod file:
target 'TestApp' do
pod 'mailgun', '~> 1.0.3'
end
target 'TestAppTests' do
end
With this podfile I was able to run pod install and set up the dependencies. Then I setup an Objective-C-Bridging Header in build settings. I used the following objective-C bridging header.
#ifndef Promises_Promises_Bridging_Header_h
#define Promises_Promises_Bridging_Header_h
#import <mailgun/Mailgun.h>
#import "testMail.h"
#endif
I was getting a linking error for awhile, but I needed to have the project opened via the workspace and I had to go to Product -> Schemes -> Edit Schemes and add the Pods-mailgun to the top of the list and then it would let me build.
Now I want to take advantage of the MailGun API. The docs say to do the following.
Mailgun *mailgun = [Mailgun clientWithDomain:#"samples.mailgun.org" apiKey:#"key-3ax6xnjp29jd6fds4gc373sgvjxteol0"];
[mailgun sendMessageTo:#"Jay Baird <jay.baird#rackspace.com>"
from:#"Excited User <someone#sample.org>"
subject:#"Mailgun is awesome!"
body:#"A unicode snowman for you! ☃"];
I think i am facing exactly the same problem with you. But I am a little confused on how many .h and .m files you have used and what each one contains. I am clueless on obj C so I try to follow you blindly.
Correct me if I am wrong.
• You have one .h bridging header file that contains:
//
// Use this file to import your target's public headers that you would like to expose to Swift.
//
#import "AFNetworking.h"
#import <mailgun/Mailgun.h>
• You have one .m file that contains:
#import <Foundation/Foundation.h>
#import <mailgun/Mailgun.h>
#interface mailTest: NSObject
- (void) sendMail: (NSString*)email;
#end
• And in your Viewcontroller.swift you have (lets say in a button) this:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
#IBAction func myButton(sender: UIButton) {
var test: mailTest = mailTest()
test.sendMail("testemail#testemail.com")
}
}
Is that correct?
There's another option that you could try if you wanted as much as possible to be on the Swift side:
If you don't have a bridging header yet, create a dummy objective-c file (add new file to project). Xcode will ask if you will like to add a header file (this will sort out the header for you automatically)
Add this to your bridging header (providing you already imported mail gun using cocoa pods):
#import "mailgun/Mailgun.h"
Import maligun in your swift class:
import mailgun
Simply use mailgun's API in swift as you would in Objective-C:
#IBAction func sendEmail(_ sender: Any) {
let mailgun = Mailgun.client(withDomain: "samples.mailgun.org", apiKey: "key-3ax6xnjp29jd6fds4gc373sgvjxteol0")
mailgun?.sendMessage(to: "Jay Baird <jay.baird#rackspace.com>", from: "Excited User <someone#sample.org>", subject: "Mailgun is awesome!", body: "A unicode snowman for you! ☃")
}
At this point I've answered my own question. The process isn't too terrible. Install mailgun using cocoapods.
Link the objective-c code that is needed using an objective-c bridging header.
Create an objective c file to house your method that will call the mailgun operation, and use it.
#import <Foundation/Foundation.h>
#import <mailgun/Mailgun.h>
#interface mailTest: NSObject
- (void) sendMail: (NSString*)email;
#end
Then in my swift code I just call:
var test: mailTest = mailTest()
test.sendMail("testemail#testemail.com")
Here is the mailTest header file. I created a .m file that implements the sendMail method and calls the code from the API and it works great. The only other thing that could be challenging is setting up the mailgun account and configuring your domain so that emails can be sent, but that isn't code related.

Resources