PureLayout in Swift with Cocoapods - ios

Using cocoapods-0.37.2 Here's my Podfile:
platform :ios, "8.0"
use_frameworks!
pod "GoogleMaps"
pod 'FontAwesomeKit'
pod 'PureLayout'
I'm trying to import PureLayout in my .swift file like this:
import PureLayout
class ViewController: UIViewController {
...
But I get error:
No such module 'PureLayout'
Why??? I thought that Cocoapods creates -Bridging-Header.h by itself when using use_frameworks! ?

I did not use the use_framework option but I used PureLayout in Swift with CocoaPods without issues.
My Podfile only has one line pod 'PureLayout'.
I created the <Your Module>-Bridging-Header.h file myself as instructed by Apple here. I added a line #import "PureLayout.h". I've also updated my target build settings to enter the bridging header file name as per instructions.
In the .swift file where I want to use PureLayout, I did not need to have import PureLayout. It would give me an error as you mentioned. The auto layout calls would just work, since it's defined through bridging header.
One side point - the test target would fail to compile if you add bridging header file to its build settings. So make sure the bridging header update is done on target not project level.
I guess the current README in PureLayout github page is a bit outdated, since it currently asks you to do import PureLayout in the .swift file. I added a comment in this README enhancement issue too.

install the podfile
open your .xcworkspace
put your import PureLayout statement in, get the error
delete the statement
clean your product
build your product
put your import PureLayout statement back in
This has been common occurence and fix for me when using outside frameworks.

spacemonkey's method work for me. In that case, you don't even need a -Bridging-Header.h file.
As for FontAwesomeKit, since it contains some resource file(xx.ttf, xx.otf, etc), you should add this file to project to make it work. Like following shows:
Clean your project and recompile, then you are done.
Following is my demo for this:
import FontAwesomeKit
import ChameleonFramework
import SnapKit
class ViewController: UIViewController {
#IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let iconSize : CGFloat = 128;
let codeIcon = FAKFontAwesome.codepenIconWithSize(iconSize);
imageView.image = codeIcon.imageWithSize(CGSizeMake(iconSize, iconSize));
view.backgroundColor = UIColor.flatGreenColorDark();
}
}
PS. Since you use swift, maybe the SnapKit Layout framework will much suitable than PureLayout.

Related

Using iosMath in Swift

how can I use iosMath in Swift on iOS, because iosMath is written in Objective C? Or is there another way to display mathematical formulas with Swift?
I will make this as fast as possible:
1.) install cocoaPods (open up terminal and write sudo gem install cocoapods)
2.) From terminal change directory to your project and init cocoaPods and edit the Podfile like this:
cd /ProjectFolder
pod init
nano podFile
(opens the podfile, where you paste the pod repo) , should look like this:
# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'
target 'mathExample' do
# Comment the next line if you're not using Swift and don't want to use dynam$
use_frameworks!
# Pods for mathExample
pod 'iosMath'
end
save podfile and install via:
pod install
from now, use the workspace file instead of the project file... We are done installing the pod you need right now...
Now it gets a little bit tricky -> You need to add new Objective-C file into the project (so just New File -> Objective-C file and select create bridging header) from now, you can just delete the created file (BUT NOT THE BRIDGING HEADER)
from now, you can use the Objective-C framework just as it was swift framework,don't forget to import that framework into classes you will use it:
import UIKit
//Importing our module
import iosMath
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
print(iosMathVersionNumber)
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Hope this helps, wish happy coding! :)
The general question has already been answered. Check this question out.
All you have to do is add iosMath in your podfile and as long as you have use_frameworks! in the podfile, you won't need any headers. Simply, import iosMath in your swift file and you are good to go.

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

Unable to integrate ZXingObjC in a iOS Swift Project

Im working on an iOS project, which shows the customer number in a barcode. I had installed the framework ZXingObjC with CocoaPods, described in GitHub.
I can compile my Project without errors. I can also use the classes of ZXingObjC in my Objective-C classes, without errors. After than, I have added the import Command #import <ZXingObjC/ZXingObjC.h> to my bridging header file, like my other custom objective-c classes, without compile errors. (I had testet the header file by destroying some import statements and got the expected file not found exception.)
But now, I can't use any class of ZXingObjC in my swift classes. I only got the following compile error: Use of undeclared type '...'. The Xcode autocomplete is not working, too.
e.g.
var test : ZXMultiFormatWriter?
>> Use of undeclared type 'ZXMultiFormatWriter'
I tried:
setup new project, same issue
checked header search path: $(SRCROOT)/Pods/Headers/Public/Adjust
reinstalled the ZXingObjC framework
checked build settings: Enable Modules: YES
checked build settings: Other Linker Flags: $(inherited) -ObjC
-framework "ZXingObjC"
checked linked binaries in the build phases: framework is added
checked import statement in the bridging header file (#import
<ZXingObjC/ZXingObjC.h> and #import "ZXingObjC/ZXingObjC.h" -- no
difference)
Windows style: restarting Xcode and Mac ;-)
I'm using:
Xcode: 6.3.2
CocoaPods: 0.37.2
Project Deployment target: iOS 8.0
SDK: 8.3
Does anyone know the problem? Can anyone help?
How can I make the ZXingObjC framework available in swift?
Actually it is an easy issue:
Podfile
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!
pod 'ZXingObjC', '~> 3.1'
So, on terminal:
cd workspace
pod install
Then, once opened project on Xcode, you have to edit bridging-header adding ZXingObj:
#import <ZXingObjC/ZXingObjC.h>
Finally, in your swift classes that uses ZXingObjC, you have to import ZXingObjC.
import ZXingObjC
class ZXingObjCWrapper {
func encode() {
let writer = ZXMultiFormatWriter.writer()
....
}
}
The rest of the code for when you need to set an UIImage with this bar code:
func generateDataMatrixQRCode(from string: String) -> UIImage? {
do {
let writer = ZXMultiFormatWriter()
let hints = ZXEncodeHints() as ZXEncodeHints
let result = try writer.encode(string, format: kBarcodeFormatDataMatrix, width: 1000, height: 1000, hints: hints)
if let imageRef = ZXImage.init(matrix: result) {
if let image = imageRef.cgimage {
return UIImage.init(cgImage: image)
}
}
}
catch {
print(error)
}
return nil
}
The header search path was not correct in my project. The right values are:
$(inherited)
"${PODS_ROOT}/Headers/Public"
"${PODS_ROOT}/Headers/Public/ZXingObjC"
The second and third lines were not added by installation with CocoaPods.
EDIT: The installed framework have to be added to "Embedded Binaries" in General tab of the project.
I tried everything on this page to add ZXingObjC as a Pod. My goal was to generate an Aztec barcode.
I checked my Header Search Path. As Reddas said, I had to manually add "${PODS_ROOT}/Headers/Public/ZXingObjC". I also added ZXingObjC as an Embedded Binary (in the General Tab).
I checked my bridging file & all was good. I checked my view controllers where I wanted to generate the barcode. The import ZXingObjC was there.
No compile errors. But I can't declare a variable of ZXingObjC.
No luck. Any more suggestions?
EDIT - I went into the Targets, Build Settings and searched for Header Search Paths. I added in BOTH "${PODS_ROOT}/Headers/Public/ZXingObjC" and "${PODS_ROOT}/Headers/Private/ZXingObjC"
This seemed to unclog whatever broke. It works now. Strangely, I can now even delete those entries and it works.

YouTube-Player-iOS-Helper can't use YTPlayerView class

I am trying to implement the youtube-ios-player-helper, found here: https://github.com/youtube/youtube-ios-player-helper
What I did:
Edited my podfile, pod update, everything fine without any errors, Alamofire - which was also added via cocoapods still working
I can see the Pod in my workspace, and I can even select YTPlayerView as Class for my UIView in Storyboard
But when it comes to adding the IBOutlet it doesn't recognize the YTPlayerView Class anymore?!
Shouldn't it be impossible to even select it in storyboard if the Pod wasnt properly added?
I also tried the "manual" method, that gave a slightly better result as it would let me define the Player, but wouldn't let me import the YTPlayerView via Bridging-Header
The YTPlayerView is in the separate Pods module which you need to import.
My Podfile:
platform :ios, '8.3'
target 'MyApp' do
use_frameworks!
pod 'youtube-ios-player-helper'
end
Your Swift file:
import UIKit
import youtube_ios_player_helper // You're missing this line
class MyView: UIView { // Some class
#IBOutlet var playerView: YTPlayerView!
// ...
}
If import youtube_ios_player_helper doesn't work for you, you can add #import "YTPlayerView.h" to your bridging header as stated in Fayza Nawaz's answer.
I faced the issue and fixed it by adding
"#import YTPlayerView.h"
in bridging header file in my project. To learn about bridging header, check this link: adding bridging header in swift ios. I hope it'll help you.

Mixed CocoaPods project: ObjC functions are visible from Swift, class variables are not

I have set up a Swift iOS project with CocoaPods. I want to use pods based on both Swift and Objective C. Let's say I want to use KeychainAccess (Swift-based) and CocoaLumberjack (Objective C-based). Here's the Podfile I use:
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!
pod 'KeychainAccess'
pod 'CocoaLumberjack'
Now, I try to use these pods:
import UIKit
import KeychainAccess
import CocoaLumberjack
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// this works fine
let keychain = Keychain(service: "access-token")
// this works as well
DDLog.addLogger(DDTTYLogger.sharedInstance())
// 'DDLog.Type' does not have a member named 'logLevel'
DDLog.logLevel = .Verbose
// 'DDLog.Type' does not have a member named 'logAsync'
DDLog.logAsync = false
}
}
Am I missing something? How do I make CocoaLumberjack work? (or any other ObjC pod, I assume I will have the same issue with other pods as well).
I have created a project which reproduces this issue.

Resources