How to export Flutter project as SDK (iOS dynamic framework) - ios

We have built an awesome Flutter project, which has great functionality we want to export as a framework, just like native libraries do, so that the source code is hidden (convert to dynamic framework).
We have followed the instructions: https://github.com/flutter/flutter/wiki/Add-Flutter-to-existing-apps
which allows us to include Flutter project in a Host iOS app, initializing FlutterEngine and use of FlutterViewController.
The question is, how do we create a dynamic framework, let's say SomeProductSDK.framework, which will expose a public methods to create our SomeProductSDK related modal screens?
// In any app
import SomeProductSDK
let controller = TransactionViewController() // SomeProductSDK.framework with partial implementation with flutter
self.present(controller, animated: true)

I've partially achieved what you want. All of this is very experimental and overall a bad idea for production ready SDK. But... it's possible.
Create flutter app as usual and run it once on iOS simulator.
Open Xcode workspace and add new framework. For my purposes I will name it RunnerLib.
Change deployment target of that framework to be the same as for Runner. Also disable bitcode.
Change target membership of App.framework and Flutter.framework to RunnerLib.
Create Launcher class with one static method: + (void)launchFrom:(UIViewController *)parent, this what should create a FlutterViewController and present it.
Rewrite the Runner to use Launcher class. Replace FlutterAppDelegate with standard AppDelegate, make ViewController, etc. It should look like standard native iOS project, so you could create one and copy over AppDelegate, storyboard and ViewController.
Call launchFrom method in your view controller, in viewDidAppear or as IBAction on a button.
You should be able to build the Runner and see that flutter screen appears.
Now, if you build the Runner app, you can open the crated Runner.app, and see that Frameworks directory contains 3 frameworks: App, Flutter and Runner.
You will need to have two sets of frameworks: one set for simulator, compiled in debug mode, and another set for devices - archived. Getting debug frameworks is pretty easy, just compile from Xcode and inspect the product. Archived frameworks are harder, I recommend doing xcodebuild archive with disabled signing.
Your users will have to configure their project to use correct frameworks depending on device. Possibly this step can be automated by Carthage and fat binaries, but I'm not sure. The problem is with App.framework which looks completely different on device than on simulator.
Source code: https://github.com/szotp/runner_lib

In order to export the framework as a native library you'll have to ensure that the person who'll use your framework (user) has Flutter SDK installed onto his computer as your framework's code will surely be dependent on Flutter SDK by default. So what your trying to do is equivalent of having your own package on pub.dev.
If you find some way to make the framework you want to export - independent of Flutter's framework, only then exporting it as an framework would make sense. (Doing that should be possible beyond doubt, but see the amount of work and time you'll put in just to get your framework there. You could have built your own Flutter framework in those same efforts and time)
Since you have already made that awesome Flutter Project, consider uploading it as an package on https://pub.dev/ if you wish to.
Suggestion: You can ask the users who want to try your package to install Flutter. This way your hardwork won't go in vains.
Conclusion: It is not practically feasible to achieve what your trying to do.

Related

Why Am I Not Able To Embed My Custom Framework?

I have a very simple framework project, MyFramework, that builds successfully. The framework defines a single, global function, myFunction.
The framework also defines 2 global variables: MyFrameworkVersionNumber and MyFrameworkVersionString (These 2 variables were created for me by Xcode)
I have a very simple application project, MyApplication. I am adding the framework to the application project by dragging and dropping the framework package (i.e. the MyFramework.framework in DerivedData that was produced by building the framework) onto the Xcode Navigator and then selecting the framework in the Embedded Binaries section of the project's General tab.
If I add code to MyApplication that references the variable MyFrameworkVersionNumber then I am able to successfully build and run MyApplication.
If I add code to MyApplication that references the function myFunction then I am not able to build MyApplication.
First Update
I suddenly remembered that Carthage does exactly what I am trying to do. So, I used Carthage to build MyFramework and Voila! - I can drag/drop the Carthage build of the framework into the application project and successfully use it. So now my inquiry has become: What does Carthage know that I do not?
Oh for heaven's sake! It turned out to be so simple. MyFramework was being built for a Generic iOS Device (arm64 architecture). MyApplication was being built for a simulator (x86_64 architecture). As soon as I matched those two up all was well. The reason that the Carthage build worked so well is that it produces a universal binary (i.e. one that contains both architectures).
I still do not understand why the MyFrameworkVersionNumber global variable was able to be accessed regardless of the architecture. But I am okay with deferring that little mystery as some arcane bit of information that will be revealed in the goodness of time (perhaps something such as that it is in a header that is structured the same for both architectures).

xCode - how to link dynamic framework from workspace

What is the proper way to link a dynamic framework which lives in the same workspace like the app?
I have a problem when adding the framework from the products folder to the App's "Embedded Binaries" section - because the added framework may be built for simulator and the app builds against iphoneos. I can not add both, can I?
I want to achieve that when my app builds, the framework is built too and I can work the changes immediately instead of doing some cumbersome update via Carthage or something link that.
EDIT: http://netsplit.com/xcode-two-apps-with-a-shared-private-framework-in-a-workspace This blog post describes exactly what i want to do - but this does not work since i want to develop on simulator and iphone (it would require a fat framework). Or did I miss something.
Although is possible to develop main app and framework and doing integration test in the same time from many workspaces, at same point becomes a slow-ish and not scalable development process.
So I suggest you to make a sample app in the workspace of your framework and develop and test your features directly from there. Once your framework is quite stable you release it and you integrate it with your main app.

Modifying Cordova iOS platform template -- linking frameworks?

I have been working on a Cordova project for both iOS and Android.
As part of the project as we will need to build a number of projects from a basic template.
I have done this by creating a template for the actual cordova project and also as part of it I pulled the cordova-ios-master code base for when we add the iOS platform. So we use our own cordova-ios-master due to some small modifications we needed to add.
With some of the plugins we have added / created we need to access SDK's / API's from some third party developers (this is mainly for some push notification services). Normally when we create the project and have added the platform we then need to add the linked frameworks using Xcode.
However I can see that within the cordova-ios-master there is the template Xcode project. I was hoping I could add the linked frameworks within this project so that they are already added when we first create the project and add the custom iOS platform.
What seems to be happening though is if I add the linked library into the template Xcode project and then save it. Once I then re-add that platform to the cordova project instead of the Xcode project being named after the cordova project name, it seems to have messed the re-naming of the Xcode project.
Below shows how the Xcode project normally appears when you add the libraries manually after adding the platform
So if i modify the Xcode template in cordova-ios-master, shown below....
You can see it already has a libCordova.a added.
I add one more and re-save the project.
But then when I add the platform to my project again from this source, i open the Xcode project for it and whilst my library is added, the project was named "myproject" but i can't choose to run it as the project selection seems to appear as "PROJECT_NAME" as opposed to being named and usable as "my project"....
Im fairly new to mac's and Xcode so maybe I'm doing something basic wrong.
Any suggestions or ideas would really help, I hope this post makes sense, it is a bit complex.
Thanks again
Rhys
Right I found a solution. I think I was looking at the problem the wrong way round. So if I need to add a framework for a particular plugin, rather than edit the Xcode project to add the framework. Cordova actually gives you the option to add certain frameworks from within the plugin.xml. Found this answer here....
How to copy a custom ios framework using plugin.xml on Phonegap 3
so I have added a required framework like so....
<framework src="src/ios/OtherLevels/OtherLevels.framework" custom="true" />
My bad!
-Rhys

How to create a Xcode 6 workspace that includes 2 iOS Projects and an iOS Dynamic Framework project?

I'm trying to create one Xcode workspace that includes 3 projects:
- 2 different iOS apps
- One dynamic framework project
The two iOS apps should use the framework.
Sounds very easy and reasonable thing to do, but I just can't seem to make it to work.
When I add the framework to the iOS app target as linked library, the build process works but when running I get an exception that the framework could not be found.
If I go to the Embed Binaries -> click add -> choose the framework from the framework project, nothing happens (the embed binaries section stays empty.
I tried to do first add the framework as linked library, then add it to the embed binaries. Now compilation doesn't work (can't find the framework).
What am I missing? Every single example in the documentation shows how to add a framework as a new target under the same project. But that's doesn't help. I want the framework to be a separate project that a team can work on separately.
Open Xcode, so you can see Xcode in the menu bar.
Then go File>New>Workspace.
I believe you just drag and drop other projects into the workspace.
I've never really used a workspace, although I have made one before.

How to reuse Swift code in other projects?

I wrote a class in Swift. I want to use this code in two separate iOS app projects that I wrote. Both the shared code and the apps are written in Swift. What is the best way of doing that?
I tried to create both a framework and a library in Swift and then add it as a sub-project to my app. In both cases I could not make the app see the module. I tried to add the shared module to "Target Dependencies" and "Link Binary With Libraries" of the main app's target. No luck - the app still can not see the classes of the shared module.
Using Xcode6 Beta6 at the moment.
Solution
As Konstantin Koval and Heliem pointed out, all I needed is to use public in my class and method declarations, in the shared module. Now all works, both if I use workspace and if I add the module as a subproject.
Update
I just found an excellent easy solution for reusing code between projects in Swift. It is called Carthage (https://github.com/Carthage/Carthage). This is not a plug as I am not affiliated with it in any way, I just really like it. It works in iOS 8+.
Create a new project (iOS Cocoa Touch Framework) for your reusable code
Move your classes to that Framework project
Mark your methods and classes, that should be visible to others as public
Create Workspace.
You can create a workspace on step 1. When you create new Framework project, Xcode will ask you if you want to create new workspace and add that project to workspace. This is the best approach
Add both your project and Framework to the workspace
Select you project target -> General tab. Add Framework and Libraries (add your library here)
When you want to use code from your Library in swift file, import it using import 'LibTargetName'
You can take a more programatic approach by using SWM (Swift Modules): https://github.com/jankuca/swm
It is very similar to npm (node.js package manager) or bower (client-side module manager); you declare your dependencies in a swiftmodule.json file like below. It does not have a central module registry like the two mentioned JS solutions, it only accepts git URLs.
{
"name": "ProjectName",
"dependencies": {
"Dependency": "git://github.com/…/….git"
}
}
…run swm install and have the Dependency module (compiled into a *.swiftmodule binary) ready for import in the .modules/ directory.
import Dependency
And if you prefer to skip Xcode for app development, you can also build your whole app using swm build (more info in the project's readme).
The project is still in early stages but it makes itself useful a lot for me at least. It provides the most clean (and clear) way of creating importable modules.
Here is a video which is very straightforward: http://eonil-observatory.tumblr.com/post/117205738262/a-proper-way-to-add-a-subproject-to-another-xcode
The video is for OS X instead of iOS. But you will get it and figure out the process for iOS.
Let's assume that AppA needs to reused code from SharedProject.
The following process works for me in Xcode 7 Beta:
Create SharedProject. Project type must be Framework instead of Application. You write common code in this project and mark the code as public.
Open AppA in Xcode, open the folder which contains SharedProject in Finder. Drag the .xcodeproj file of SharedProject from Finder and drop it into the root folder of AppA in Xcode Project Navigator.
AppA --> Build Phases --> Link Binary with Libraries. Add SharedProject.
import SharedProject and reuse code from SharedProject!
Edit:
Nowadays I suggest you use Carthage. It's better than the home made solution above.

Resources