How to create and use an iOS Framework for sharing code with Extensions - ios

How does the minimal setup and usage of a custom iOS framework look like? I starting to look into this in order to share code with a Today Extension.
Here's what I did so far
Added a new Target choosing the Cocoa Touch Framework template and called it TesterKit
Inside the framework I created a new class TestClass.swift
Inside TestClass.swift I created a simple class method class fund tester() printing a string for testing
Looking at my app I can see that TesterKit has been added as Embedded Binaries and Linked Frameworks and Libraries, however it is red
In order to use this new Framework in the app, I added import TesterKit to the top of my AppDelegate
Then I tried to call the class method from the Framework using TestClass.tester(). But instead of showing the log message I get a …
"Use of unresolved identifier"
→ What am I doing wrong? Any wrong assumptions here?
Note: I already watched the WWDC sessions 416 "Building Modern Frameworks" and found the Framework Programmign Guide. If there are any example projects showing how to use such new custom iOS Frameworks, ideally using Swift + integrating this with Today Extensions, that might be helpful, too.

It seems like your import is not failing which means the framework is actually set up correctly...try making the function public.
public func tester() { print("tester()" }

Related

Embedding a Swift Framework into another Framework Xcode

I have basically two frameworks running on Xcode. "ResearchKit" and "AppMethods". While all works fine, the "AppMethods" framework utilizes code within "ResearchKit". In fact it is there to abstract out more methods into framework. A superclass of sorts.
When using them in code, I have to import both frameworks
import ResearchKit
import AppMethods
Is there a way to embed ResearchKit within AppMethods such that I only need to import AppMethods. Without ResearchKit, AppMethods would not exist.
It really depends on the way you implement and use these frameworks within the app.
Generally speaking, iOS doesn't support nested frameworks - please read the follwoing.
But imports can be done within each framework - i.e you can import ResearchKit directly from AppMethods and just "tell" Xcode that the code will be there on compile time, you will have to link the frameworks within the App level. Another useful guide I found
It is quite simple to achieve with CoacoaPods and SPM
Your "stack" is this:
App
|
AppMethods (AM for short)
|
ResearchKit (RK for short).
Within your classes in the app, where you dont explicitly call a RK API (class or method) you dont have to import RK. Otherwise, you have to import RK.
Please note you can embed one library inside another (in this case RK inside AM), and maybe make sure that nobody outside AM knows anything about RK. But once you start explicitly calling RK api within your app, there is no other way than importing RK explicitly along AM.

Add custom framework to Standard Library in Xcode

I've looked everywhere and no article gives me exactly the solution I'm looking for.
Is there a way to "install" a custom framework into Xcode?
For example, let's say I've created a framework called 'MyAwesomeFramework' which is really reusable (for example contains a lot of useful UIView extensions). Now what I want to do is be able to just create any new project and type import MyAwesomeFramework to use it, instead of having to add the respective Xcode Project to the project I want to use it in.
Apple Documentation
There are a few different options. You can manually add them via drag and drop or use one of many framework managers such as Carthage or Cocoapods.
Whatever method you choose has a few steps and can be a bit daunting initially so I’d recommend following a tutorial of some sort(I used YouTube). If done correctly you will be able to just import like you would do any other library.

importing native iOS framework to nativescript plugin

I'm trying to access an iOS framework class from my NativeScript plugin.
Right now, this plugin only has a SampleClass with a test method that returns a number, but it will change soon. For now, what I'm trying to do is to call the class method from that native framework.
I have seen some examples where the plugin seed extends an iOS base class and creates everything there, but in this case, I want to use a .framework file.
I'm assuming this would be equivalent to use some .aar Android file. In that case, I've seen the call would be made with something like this
var c = java.lang.Class.forName("org.test.plugin.name.MyActivity");
So, I have copied the .framework file to platforms/ios, my question is about how to get this SampleClass method.
Also, I've seen some documentation that recommends the use of CocoaPods. I'm new to iOS development and still don't get this at all, but if my application won't use any native dependency, would this be necessary? Should I need another files? (got a default Info.plist and build.xcconfig)
Thank you for your answers.
Since you are using TypeScript, you have to either declare the classes like,
// iOS
declare var SampleClass;
// Android
declare var org;
then you may access all available packages / classes / methods directly.
// Android
const MyActivity = org.test.plugin.name.MyActivity;
// iOS
SampleClass.new()
If you like intellisense support for these libraries, then you may even generate declaration files for both iOS & Android libraries with the given steps in the docs.

How to wrap existing iOS code in a new Appcelerator module?

This seems like a basic request, but I can't find the answer to it anywhere. I want to wrap some existing iOS code that I wrote, in a Appcelerator module. That's it. Important points:
I am NOT wrapping a pre-existing 3rd party iOS SDK.
I wrote the iOS code being wrapped.
Code is verified as working within xcode.
There are no .a files. There are 2x .h files and 2x .m files though.
There are no UI elements in the iOS code as it is only designed to connect the native bluetooth hardware to the app.
I have created a generic appcelerator iOS module project, built it, and successfully called the generic ID function within my app.
I cannot figure out how to successfully edit the generic module so that it utilizes my code. Every attempt results in it refusing to compile, and it's maddening.
I do not have access to Hyperloop.
Once I can successfully build the wrapped module, I would call an initialization function which triggers a native bluetooth hardware search. Once connected, there are functions within the module to send commands to the hardware and receive data back. This is the official documentation I've followed so far:
http://docs.appcelerator.com/platform/latest/#!/guide/iOS_Module_Quick_Start
That helped me build the blank module, include it in the app, and ensure that it worked by calling the built in test property. From there it stops short of actually telling me what I need to know. These are the closest things I've found so far, while still not being what I need:
http://docs.appcelerator.com/platform/latest/#!/guide/iOS_Module_Project-section-43288810_iOSModuleProject-AddaThird-PartyFramework
appcelerator module for existing ios project sdk
Heck, I still don't even know if I can do this within studio or if I have to edit the generic module in Xcode. Help! :) Many thanks in advance.
so first of all, this is not best practice and will cause possible problems in the future when the SDK changes and your module still relies on outdated core API's.
Regarding your question, you could either create a new component that subclasses the existing class, e.g.
class TiMyModuleListViewProxy : TiUiListViewProxy {
}
and call it with
var myList = MyModule.createListView();
or you write a category to extend the existing API with your own logic, e.g.
#interface TiUIListViewProxy (MyListView)
- (void)setSomethingElse:(id)value;
#end
#implementation TiUIListViewProxy (MyListView)
- (void)setSomethingElse:(id)value
{
// Set the value of "somethingElse" now
}
#end
I would prefer the second option since it matches a better Objective-C code-style, but please still be aware of the possible core-changes that might effect your implementation in the feature. Thanks!

iOS How to use private API?

I don't want to submit this app to AppStore. I've tried for many times but met so many problems :(
I use class-dump to get all the header files of UIKit.framework. In the UIApplication.h generated by class-dump, I saw the method I want to use----launchApplicationWithIdentifier.
Then I put UIApplication.h in my project and import it. Compile, I got a lot of "Redefinition of enumerator...." error because in the UIKit.framework I use previous, there's another UIApplication.h. But this file doesn't have the method launchApplicationWithIdentifier.
If I delete the previous UIKit.framework and import the folder generated by class-dump. Then it appears like a framework but if I unfold it, it's empty.
Then I want to make all generated header files a framework file ant replace the previous UIKit.framework. But I don't know how. As we can see, under the system framework directory, there's a file which has the same name as the framework and has a 'executed shell script' icon. How can I made this file?
I really got confused. Someone can give me a hand? Thank you.
Just specify the private methods in a category interface above the class implementation where you want to use it, like this:
#interface UIApplication (Private)
- (BOOL)launchApplicationWithIdentifier:(id)identifier suspended:(BOOL)suspended;
#end
Don't import the whole class-dump file and link with the original UIKit framework.
You must be very careful when using private API. These methods can change or be removed in future iOS versions!
Check if the method really exists with respondsToSelector: at runtime and be prepared for the case that it does not exist.
I used a secret MapKit feature in my own application and I knew that the private methods only exist in iOS 5. So my app still works in all iOS versions but this feature is only available in iOS 5 (Apple removed or changed it in iOS 6 beta 1).

Resources