iOS - Using Cocoapods objective-C within Swift Code - ios

I'm trying to add an objective-C/CocoaPods code (GBFlatButton) to an existing swift code. I read all about I found regarding Header settings and I did this, let me know if is well performed.
Header Bridging configuration
Also the Header file has the right import files added to it.
//
// Use this file to import your target's public headers that you would like to expose to Swift.
//
#import <UIKit/UIKit.h>
#import <GBFlatButton/GBFlatButton.h>
#import <GBFlatButton/UIColor+GBFlatButton.h>
On the other hand the CocoaPods project doesn't give me an error when I compile the code. The error occurs when I use the button, maybe is the way I'm trying to use this objective-c code:
let MyButton: GBFlatButton! = GBFlatButton(frame: CGRect(x: 0.0, y: 0.0, width: 200.0, height: 40.0))
Also I tried to use it like this: #IBOutlet var FButton: GBFlatButton!
This is the final error I have:
/Users//Documents/Projects/party/party/party/partyStepViewController.swift:18:19:
Use of undeclared type 'GBFlatButton'
/Users//Documents/Projects/party/party/party/partyStepViewController.swift:18:9:
Could not infer type for 'MyButton'
Any clue will help :)
Thanks!!, if you need more info don't hesitate to ask me.

you forgot to add the bridging header in the Build settings of the app
In ViewController:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let button: GBFlatButton! = GBFlatButton(frame: CGRectMake(0, 0, 200, 50))
self.view.addSubview(button)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
just do this and then it will work

Related

Inserting UIKit content into a SwiftUI view hierarchy

I'm sorry if the title is confusing, i'm kind of new to the whole thingy.
I'm trying to integrate PassBase ID verification to my app, which is built using SwiftUI, their documentation offers instructions using Swift and view Controllers.
My question is, is there a way to insert the Swift code part into my SwiftUI view?
The code example from their Documentation:
import Passbase
import UIKit
class ViewController: UIViewController, PassbaseDelegate {
override func viewDidLoad() {
super.viewDidLoad()
PassbaseSDK.delegate = self
// Optional - You can prefill the email to skip that step.
Passbase.prefillUserEmail = "testuser#yourproject.com"
let button = PassbaseButton(frame: CGRect(x: 40, y: 90, width: 300, height: 60))
self.view.addSubview(button)
}
func onFinish(identityAccessKey: String) {
print("onFinish with identityAccessKey \(identityAccessKey)")
}
func onSubmitted(identityAccessKey: String) {
print("onSubmitted with identityAccessKey \(identityAccessKey)")
}
func onError(errorCode: String) {
print("onError with code \(errorCode)")
}
func onStart() {
print("onStart")
}
}
As i understand this part of code should create a button in a VC.
My goal is to add this button with functionality to my SwiftUI view.
Full Documentation: https://docs.passbase.com/ios#general
Thank you all in advance for the help!
The basic strategy is to use a View that represents the content you want to bring in from a UIViewController. Your View is going to conform to UIViewCotrollerRepresentable and use the functions of that protocol to create and manage the UIKit content.
The UIViewControllerRepresentable documentation is here
And, as was commented on your original post by vadian, there is A tutorial with sample code
With the sample code above, I would rename "ViewController" to be something like PassBaseViewController or PBViewController, then you would create a View that derives from UIViewControllerRepresentable
You end up with a file called PBViewController.swift that has your code from above:
import Passbase
import UIKit
class PBViewController: UIViewController, PassbaseDelegate {
override func viewDidLoad() {
super.viewDidLoad()
PassbaseSDK.delegate = self
// Optional - You can prefill the email to skip that step.
Passbase.prefillUserEmail = "testuser#yourproject.com"
let button = PassbaseButton(frame: CGRect(x: 40, y: 90, width: 300, height: 60))
self.view.addSubview(button)
}
... and the rest of the code from your question here ...
Then (probably in another file, but not necessarily) you could create the SwiftUIView that uses that view controller:
struct PassBaseView : UIViewControllerRepresentable {
typealias UIViewControllerType = PBViewController
func makeUIViewController(context: Context) -> PBViewController {
return PBViewController()
}
func updateUIViewController(_ uiViewController: PBViewController, context: Context) {
/* code here to make changes to the view controller if necessary when this view is updated*/
}
}

Add imageview to all viewcontrollers programmatically

there is a requirement like add an imageview to all viewcontrollers but I have 150+ xib's and it is time consuming to put imageview in every single xib.
Is there a common way to do it? I googled but nothing useful found.
Any help would be appreciated.
It will be easy if you use base class like this
class BaseVC: UIViewController
{
var imageView:UIImageView = UIImageView.init()
override func viewDidLoad() {
super.viewDidLoad()
addImageView()
}
override func viewWillAppear(_ animated: Bool) {
//self.view.bringSubview(toFront: imageView) //To bring imageview infront of other views put this method as per your requirement
}
func addImageView(name:String = "default")
{
let image = UIImage(named: name)
imageView.image = image
imageView.frame = CGRect(x: 0, y: 0, width: 100, height: 200)
//view.addSubview(imageView)
view.insertSubview(imageView, at: 0) /*For put image view below all image*/
}
}
You need to derive all your view controller from this like this
class YourVC: BaseVC
also you can change the image with different viewcontrollers.
like
class YourVC: BaseVC
{
override func viewDidLoad() {
super.viewDidLoad()
addImageView(name:"xyz")
}
}
you can write an extension for UIView to add imageview into it and run it into every your viewcontrollers.
extension UIView {
func addImage() {
let imageView = UIImageView(frame: frame)
imageView.image = UIImage(named: "Your Image Name")
addSubview(imageView)
imageView.didMoveToSuperview()
}
}
Create New ViewController without storyboard.
import UIKit
class BaseViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
setImageView()
}
func setImageView() {
let thisImageView = UIImageView(frame: CGRect(x: 10, y: 10, width: 70, height: 70))
thisImageView.image = UIImage(named: "your image name")
view.addSubview(thisImageView)
}
}
now you can set this BaseViewController as delegate of your project's ViewControllers.
import UIKit
class MainViewController: BaseViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
}
now you have this ImageView in MainViewController and all ViewControllers have BaseViewContoller as delegate.
Hope to be useful. Also sorry about my English.
TL;DR No. Based on my understanding of what you are imagining, no you can't write a function to add a UIImageView to every one of your viewControllers.
Long answer:
You need to create a separate controller swift file for each View and set it up in that file. You could create a supporting file in which you setup up the the ImageView then call in in the viewDidLoad for each view controller. (You could even make this an extension of UIView so you can just call something like self.setUpImageView())
My personal recommendation would be to drop the xibs as soon as you can and recreate everything pragmatically, I know it's a headache to throw all your work away but it is really worth it in the end on top of just being good practice. I have a file that I found that makes autolayout a breeze that I can share with you if you'd like. I used to really enjoy storyboards and xibs myself but they are a hassle that just isn't worth it anymore and cause such a headache in situation like this.

Nothing is showing when using BCMeshTransformView

I wanted to use BCMeshTransformView library into my swift project. I've created an empty swift project and added the library via cocoa pods.
Here's my ViewController class:
import UIKit
import BCMeshTransformView
class ViewController: UIViewController {
var transformView:BCMeshTransformView!
var imageView:UIImageView!
var transform:BCMutableMeshTransform!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
transformView = BCMeshTransformView(frame: self.view.bounds)
transformView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
imageView = UIImageView(image: UIImage(named: "picture.jpg")!)
imageView.center = CGPoint(x: transformView.contentView.bounds.midX, y: transformView.contentView.bounds.midY)
transformView.contentView.addSubview(imageView)
transformView.diffuseLightFactor = 0.0
transform = BCMutableMeshTransform.identityMeshTransform(withNumberOfRows: 20, numberOfColumns: 20)
transform.mapVertices { (vertex, vertexIndex) -> BCMeshVertex in
return BCMeshVertex(from: vertex.from, to: vertex.to)
}
transformView.meshTransform = transform
self.view.addSubview(transformView)
}
}
When I'm running the app nothing is showing. It's entirely white.
Removing this code:
transform = BCMutableMeshTransform.identityMeshTransform(withNumberOfRows: 20, numberOfColumns: 20)
transform.mapVertices { (vertex, vertexIndex) -> BCMeshVertex in
return BCMeshVertex(from: vertex.from, to: vertex.to)
}
transformView.meshTransform = transform
doesn't change anything.
But when in xcode I'm switching to "Show UI hierarchy" I can see an image:
Here's a whole sample project:
http://www116.zippyshare.com/v/IUTXbKJg/file.html
Why I cannot see anything? I tried making an example as simple as possible.
Looks like that was a problem with that library codun't find shader files. I had to copy BCMeshShader.fsh and BCMeshShader.vsh to my project.

Issue when adding a Facebook like button to my iOS app

I'm experimenting adding a Facebook like button to my app using swift. This is my code and also the steps I exactly took until now. I am wondering if I am missing doing any steps or if there is something wrong with the code?
1- Created a new Xcode project using Swift
2-added the following code to viewDiDLoad
3-imported "import Parse" and "import Social" as seen below
4-Created a bridging header file and imported FBSDKCoreKit/FBSDKCoreKit.h and FBSDKShareKit/FBSDKShareKit.h into it.
5-Added FBSDKCoreKit and FBSDKShareKit to my frameworks
And then I'm getting an "expected declaration" error on this line:
likeButton.objectID = "https://www.facebook.com/JCVDonline/?fref=ts"
Here is the full code:
import UIKit
import Parse
import Social
class NewsPageViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
var likeButton:FBSDKLikeControl = FBSDKLikeControl()
likeButton.objectID = "https://www.facebook.com/JCVDonline/?fref=ts"
likeButton.likeControlStyle = FBSDKLikeControlStyle.BoxCount
likeButton.frame = CGRectMake(16,20, 290, 40)
self.view.addSubview(likeButton)
}
}
The issue was I wanted to push a button and then have the like button show up. So I added an IBAction and it's working like gold! next for me is to figure a way to change the button image :)
here is the working code by the way:
import UIKit
import Parse
import Social
class NewsPageViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func likeOnFacebook(sender: AnyObject) {
var likeButton:FBSDKLikeControl = FBSDKLikeControl()
likeButton.objectID = "https://www.facebook.com/JCVDonline/?fref=ts"
likeButton.likeControlStyle = FBSDKLikeControlStyle.BoxCount
likeButton.frame = CGRectMake(16,20, 290, 40)
self.view.addSubview(likeButton)
}
}

How to construct a ScrollView using Swift?

I'm constructing my first App for IOS and I'm struggling to find a way to do a simple ScrollView using the Swift code on the XCode6, please can someone help me to find the solution?
My problem is that I don't know how to make the scrollview work in my code. I already putted the code as you can see below in the ViewController.swift and I was expecting to be able to select the Outlet "scroller" in the Main.storyboard for a ViewController, instead of this I'm receiving the error *"fatal error: Can't unwrap Optional.None (lldb)"* EXC_BAD_INSTRUCTION (code=EXC_1386_INVOP, subcode=0x0)
I have some ViewController screens and in one of that I putted one ScrollView and I want to make it works using the Swift.
I'm stuck on this:
import UIKit
class ViewController: UIViewController {
#IBOutlet var scroller:UIScrollView
override func viewDidLoad() {
super.viewDidLoad()
scroller.scrollEnabled = true;
scroller.contentSize = CGSizeMake(320, 624);
// 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.
}
}
I think if someone can provide a simple example how to do a scrollview using swift it will solve my problem. Any help is appreciate.
Trying to do it in a old style I tried to do it using a .m and .h file:
ViewController.m
#import "Amigo-Bridging-Header.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[scroller setScrollEnabled:YES];
[scroller setContentSize:CGSizeMake(320, 624)];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Amigo-Bridging-Header.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController {
IBOutlet UIScrollView *scroller;
}
#end
Cheers
Let's give this a shot. The one thing to note is I have yet to find a way to downcast self.view as a UIScrollView, so you can't make calls like self.view.contentOffset.
import UIKit
class ScrollingViewController : UIViewController {
// Create a scrollView property that we'll set as our view in -loadView
let scrollView = UIScrollView(frame: UIScreen.mainScreen().bounds)
override func loadView() {
// calling self.view later on will return a UIView!, but we can simply call
// self.scrollView to adjust properties of the scroll view:
self.view = self.scrollView
// setup the scroll view
self.scrollView.contentSize = CGSize(width:1234, height: 5678)
// etc...
}
func example() {
let sampleSubView = UIView()
self.view.addSubview(sampleSubView) // adds to the scroll view
// cannot do this:
// self.view.contentOffset = CGPoint(x: 10, y: 20)
// so instead we do this:
self.scrollView.contentOffset = CGPoint(x: 10, y: 20)
}
}
Your outlet is not connected. From the Swift with objective-C book:
When you declare an outlet in Swift, the compiler automatically converts the type to a weak implicitly unwrapped optional and assigns it an initial value of nil. In effect, the compiler replaces #IBOutlet var name: Type with #IBOutlet weak var name: Type! = nil
If this value was not connected, it would remain as nil and you'd get a runtime error when accessing the value.
#IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// setup the scroll view
self.scrollView.contentInset = UIEdgeInsetsMake(0, 0, 200, 0);
}

Resources