Implementing an Obj-C class in my Swift project - ios

I'm trying to add a webshop to my app. The problem is that the webshop has provided me with an Obj-C framework and I'm only familiar with making iOS apps in Swift.
I've managed to setup an Obj-C bridging header and instantiate the webshop object.
My project looks like this:
When the shop button on my CodeDetailViewController gets tapped this function gets triggered:
#IBAction func shopButtonPressed(sender: UIButton){
let instanceOfShop : Shop = Shop()
instanceOfShop.showShop()
}
My Shop.m looks like this:
#import <Foundation/Foundation.h>
#import <FMShop/FMShop.h>
#import "Shop.h"
#implementation Shop
- (void) showShop {
//SANDBOX
FMShop* shop = [[FMShop alloc] initWithKey:#"4M7HDPAQMY68S"
storeKey:#"C8RSHBH710GK8"
buyerID:nil
sandbox:YES
controller:nil
delegate:nil];
[shop showStore:kShopViewHome ID:nil];
}
#end
The showShop function in this class gets triggered. Nothing however shows up.
The manual on how to implement this webshop is aimed at Obj-C apps. Concerning the delegate it states:
delegate: Please define a class or an interface that implements the delegates. Without such, the Store will not show. Please go to FMShopDelegate to see the supported list of methods and callbacks.
This is what I could find in the framework:
#protocol FMShopDelegate <NSObject>
#optional
-(void)shopWillAppear:(FMShop*)shop;
-(void)shopDidAppear:(FMShop*)shop;
-(void)shopWillDisappear:(FMShop*)shop;
-(void)shopDidDisappear:(FMShop*)shop;
-(void)shopProductInfo:(FMShop*)shop products:(NSArray*)productArray;
-(void)shopHistoryOrderInfo:(FMShop*)shop orders:(NSArray*)orderArray;
-(void)shopPlaceOrderSuccess:(FMShop*)shop orderID:(NSString*)orderID;
-(void)shopOrderUserCancelled:(FMShop*)shop orderID:(NSString*)orderID;
-(void)shopFailed:(FMShop*)shop error:(FMShopErrorType)error;
#end
Can anyone tell me how to connect this final method to the right delegate?

Sounds like if you don't provide a delegate when you instantiate your instance of FMShop, then the show method will not work. In your Shop.m file, you're instantiating the FMShop object with nil as the delegate parameter.
Try having an object conform to the FMShopDelegate protocol, and passing this object as the delegate parameter. You could even have your Shop object conform to the protocol itself, and pass self as the delegate if that would serve your purposes.
You could do this in Swift in a new Shop.swift file, or in the Shop.m file in Objective-C:
Swift:
class Shop: NSObject {
func showShop() {
// I'm guessing on the syntax here, Xcode will help autocomplete the correct functions
let shop = FMShop(key: KEY, storeKey: STOREKEY...)
shop.show(store: kShopViewHome, id: nil)
}
}
extension Shop: FMShopDelegate {
// Implement any of the protocol functions you want here
}
Objective-C:
#interface Shop() <FMShopDelegate> // Conform to the delegate here
#end
#implementation Shop
- (void) showShop {
//SANDBOX
FMShop* shop = [[FMShop alloc] initWithKey:#"4M7HDPAQMY68S"
storeKey:#"C8RSHBH710GK8"
buyerID:nil
sandbox:YES
controller:nil
delegate:self];
[shop showStore:kShopViewHome ID:nil];
}
// Implement any of the protocol methods here
#end

You have to use bridging header for accessing obj-c file in swift
Below is the apple doc to describe how to create & use bridging header
Apple Doc

Use bridging header in your project.
https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html

Related

How to access a method in a class written in swift using the app delegate

I am a newbie for objc. I have written a class exposing it to the JS side using swift. The content in that file looks like as follows,
import Foundation
#objc(AppLinkModule)
class AppLinkModule: NSObject{
#objc
static var appLink: String?
#objc
func setLink(link: String){
AppLinkModule.appLink = link;
}
#objc
func getLink(_ resolve: RCTPromiseResolveBlock, rejecter reject: RCTPromiseRejectBlock) -> Void {
resolve(AppLinkModule.appLink);
AppLinkModule.appLink = nil;
}
}
The AppLinkModule.m file is as follows,
#import "React/RCTBridgeModule.h"
#interface RCT_EXTERN_MODULE(AppLinkModule, NSObject)
RCT_EXTERN_METHOD(getLink)
#end
What I actually want is to call the setLink method from the AppDelegate.m file or else directly set to the static variable appLink. Some instructions to achieve this will be greatly appreciated.
This looks fine.
If you want to call getLink() from your App Delegate, you'll need to import the Auto-generated Swift Bridging Header File that Xcode creates when you compile your code. This will allow the Objective-c AppDelegate.m file to find your exposed swift code.
#import <ProjectName-Swift.h> will allow your Swift to be exposed to the AppDelegate, and you already have marked the class and functions #objc so they will be visible.
Then you just need to either create a singleton or shared instance of your AppLinkModule (React Native Modules are all run statically) or you need to instantiate your module class.
Once you've done so, you can call this function in the AppDelegate like so:
[[[AppLinkModule ] shared ] getLink];

Objective-C protocol method invisible in Swift

I'm currently working on some Swift classes in my ObjC project.
The problem I have is the following:
I have this protocol declared in ClassA.h:
#protocol MyProtocol <NSObject>
- (void)complexMethodWithArg1:(id)arg1 arg2:(id)arg2 arg3:(id)arg3;
- (Folder *)currentDestinationFolder;
- (Flow)currentFlow;
#end
Pretty standard stuff.
Now my goal is to have a swift class with a property that is an object implementing this protocol.
So naturally, I add my class to the swift bridging header:
//
// Use this file to import your target's public headers that you would like to expose to Swift.
//
#import "ClassA.h"
and declare my property in my swift file under ClassB which is a UIViewController that implement ANOTHER protocol
class ClassB : UIViewController, AnotherProtocol {
var delegate:MyProtocol?
}
Problem here is: I want to call a bunch of my delegate methods in viewDidLoad. It's working for all of them except ONE method that gets not autocompletion and errors the compilation if entered manually:
override func viewDidLoad() {
self.delegate?.currentDestinationFolder() // works great, no problem
self.delegate?.currentFlow() // works great, no problem
self.delegate?.complexMethodWithArg1(arg1: arg1, arg2: arg2, arg3: arg3) // PROBLEM : no autocompletion, error if entered manually !
super.viewDidLoad()
}
I have no idea what's going on, it's not related to optional or required protocol methods, not related to the fact that my delegate property is optional (tried unwrapped).
Has anybody face some similar issue? seems like some kind of bug?
I went ahead and tried to reproduce the problem on an empty project.
MyProtocol.h (taking the declaration from your question and comments)
#import Foundation;
#import UIKit;
#class CAPNavigationBar;
#protocol MyProtocol <NSObject>
- (void)setupNavigationItemInNavigationBar:(CAPNavigationBar *)navigationBar
navigationItem:(UINavigationItem *)navigationItem
inViewController:(UIViewController *)viewController;
#end
CAPNavigationBar.h (just a mock)
#import Foundation;
#interface CAPNavigationBar : NSObject
#end
ViewController.swift
import UIKit
class ViewController: UIViewController {
var delegate: MyProtocol?
override func viewDidLoad() {
super.viewDidLoad()
let capNavigationBar = CAPNavigationBar()
self.delegate?.setupNavigationItemInNavigationBar(capNavigationBar, navigationItem: nil, inViewController: self)
}
}
Bridging header
#import "MyProtocol.h"
#import "CAPNavigationBar.h"
Summary
Everything is working as expected.
You have either a simple typo somewhere or you are not importing correctly all the types into Swift. Especially make sure that you are not importing types only as forward declarations.

SWIFT: try to use delegate method of objective c class

I have an existing Objective-C SDK (AugmentedKit SDK). I am trying to use it with Swift. AugmentedKit SDK has a class called 'AKSurfaceView' that implements a delegate to 'AKViewDataSource' protocol.
#class AKSurfaceView;
#protocol AKViewDataSource;
#protocol AKViewDelegate<AKViewDataSource>
AKViewDataSource looks like this:
#protocol AKViewDataSource<NSObject>
#required
-(AKMarkerTemplate*) viewforMarker:(AKMarker*) marker;
I've created a briging file, implemented other methods from AKSurfaceView, etc. - everything works fine but when I try to add a reference of the AKViewDelegate to my UIViewControllerClass it ends in a compile error:
"Type POIViewController does not conform to protocol 'AKViewDataSource'"
My implementation looks like this:
class POIViewController: UIViewController, AKViewDelegate {
My implementation of 'viewforMarker' in SWIFT looks like this:
func viewforMarker(marker:AKMarker) -> AKMarkerTemplate{
var markerView:SimpleMarker = SimpleMarker(frame: CGRectMake(0, 0, 230, 80))
markerView.title.text = marker.markerName
return markerView
}
Could please anybody help my to figure out how I have to implement 'viewforMarker' so the delegate recognise it?
It looks like your AKViewDelegate is a subclass of AKViewDataSource, in this case, you will have to conform to AKViewDelegate AND AKViewDataSource, that means you will have to implement the required methods of both protocols.
Make sure you have imported your Obective-C class inside Bridging-Header.h, and added the following in your swift class:
markerView.delegate = self

File Scope? Swift Delegates and Protocols

I'm working on building a new Swift app roughly based off an old Obj-c app. I'm currently working on the delegates
Here is what my obj-c code looked like in the .h file
#interface MyAppViewController : CustomViewController
#property (nonatomic, weak) id<MyAppViewControllerDelegate> delegate;
#end
#protocol MyAppViewControllerDelegate <NSObject>
- (void)myAppViewController:(MyAppViewController *)controller loggedInStudent: (MYStudent *)student;
- (void)myAppViewControllerWantsSignUp:(MyAppViewController *)controller;
#end
In SWIFT I did:
class MyAppViewController: CustomViewController {
var delegate: MyAppViewControllerDelegate?
protocol MyAppViewControllerDelegate{
func myAppViewController(controller: MyAppViewController, loggedInStudent: MYStudent)
func myAppViewControllerWantsSignUp(controller: MyAppViewController)
I've done a lot of reading and study on this, so I thought I was doing it basically right (totally new to swift though... so)
I'm getting this error though, "Declaration is only valid in file scope" on the protocol MyAppViewControllerDelegate {
I assumed this had something to do with declaring it within the class, so I moved it out, only now my code within the class doesn't recognize the delegate variable I declared..
Any ideas?
Should be this:
protocol MyAppViewControllerDelegate {
func myAppViewController(controller: MyAppViewController, loggedInStudent: MYStudent)
func myAppViewControllerWantsSignUp(controller: MyAppViewController)
}
class MyAppViewController: CustomViewController {
var delegate: MyAppViewControllerDelegate?
}
Although, if you're following a common pattern where the object that owns MyAppViewController is also its delegate, this may cause memory issues. You can use class typing to allow weak delegation like so:
protocol MyAppViewControllerDelegate : class {
func myAppViewController(controller: MyAppViewController, loggedInStudent: MYStudent)
func myAppViewControllerWantsSignUp(controller: MyAppViewController)
}
class MyAppViewController: CustomViewController {
weak var delegate: MyAppViewControllerDelegate?
}
This is slightly limiting because it requires you to use a class for your delegate, but it will help avoid retain cycles :)
From what I see in your source code is that you've declared your protocol inside of your class.
Just declare the protocol outside of the class declaration and you'll be fine.
Update:
The default access level is set to internal which is defined as
Internal access enables entities to be used within any source file from their defining module, >but not in any source file outside of that module. You typically use internal access when >defining an app’s or a framework’s internal structure.
In contrast to Objective-C or C you don't need a forward declaration if the implementation haven't happened before the usage.
Source: The Swift Programming Language, Access Control

ObjectiveC: Need suggestion for my way of having protected method? [duplicate]

This question already has answers here:
Protected methods in Objective-C
(9 answers)
Closed 9 years ago.
Simply put, I need a way to have some private methods in a class that are only exposed for its subclasses, and it is difficult (maybe impossible) to do this in Objective-C.
What I did so far:
// MyClass.h
#protocol MyClassProtectedMethodsProtocol
- (void)__protectedMethod;
#end
#interface MyClass : NSObject
- (void)publicMethod;
- (id<MyClassProtectedMethodsProtocol>)protectedInstanceForSubclass:(id)subclass;
#end
Then:
// MyClass.m
#import "MyClass.h"
#interface MyClass() <MyClassProtectedMethodsProtocol>
#end
#implementation MyClass
- (void)publicMethod
{
// something
}
- (id<MyClassProtectedMethodsProtocol>)protectedInstanceForSubclass:(id)subclass
{
if ([subclass isKindOf:MyClass.class] && ![NSStringFromClass(subclass.class) isEqualToString:NSStringFromClass(MyClass.class)])
{
// the subclass instance is a kind of MyClass
// but it has different class name, thus we know it is a subclass of MyClass
return self;
}
return nil;
}
- (void)__protectedMethod
// something protected
{
}
#end
Then the subclass of MyClass can just:
id<MyClassProtectedMethodsProtocol> protectedMethodInstance = [self protectedMethodForSubclass:self];
if (protectedMethodInstance != nil)
{
[protectedMethodInstance protectedMethod];
}
This way does not break OO (compared to calling the private method and ignoring the compiler warning, or even guessing the private method name as only .h is known), but a protocol is needed for the available protected methods and once this is exposed, in a big project that we only deliver interface and static library to client, client can actually know the private methods and try to call them regardless of warning. And the bigest problem is from outside of the subclass, user can as well call this method to get the protectedInstance. Can anyone advice?
Thanks
Check this: Protected methods in Objective-C
Simply put, there is no way to prevent a method from being called in Objective-C, since ultimately, the client can still call performSelector on any object.
A standard way to handle this scenario is to include the internal methods in a separate header, like MySuperClass_Internal.h. Use a class extension: #interface MySuperClass (Internal). Do not install MySuperClass_Internal.h at /usr/local/include or in the framework, or however you're delivering the library to your clients.

Resources