iOS Build app extension base on existing app. How to avoid code duplication? - ios

I have an iOS app that add Sound FX to videos. Now I want to build app extension target to support stocked Photo App. However, I have to duplicate almost every codes from the iOS app. And my solution that I have so far is to put common functions in a shared files and use it for both targets, because some codes in the app linked to AppDelegate and I can't use it in App Extension. Is there any better way to avoid code duplication in this case?
One more question: most extensions that I saw on AppStore only have 1 screen, is there any rules for this?
In my app, I have to let users to choose sounds from folder & library, and it requires navigation controller. But Navigation Controller is not usable in App Extension, should I build my own Navigaion Controller or is there a better way?

As long as you don't need to target iOS 7 or earlier, you can put the common classes into a shared framework and load that framework from both targets. This avoids having two instances of the code on disk.
As an added bonus, because of the way the OS handles shared library loading, all of the actual code pages get shared between the app and the extension, so you aren't wasting RAM, either.

Related

What is the difference between an "app" project and a "framework" project in Xcode?

I am working on a project with SwiftUI and it originally started with creating a new project as an "App" (Xcode, clicked on file, new, project, click on "App") but was then later asked to put it into a pod as a framework. I did it successfully (Xcode, clicked on file, new project, click on "Framework"), however I am unsure what the differences are and I'm unsure why I would want to do that. To me they look very similar, except that I'm unable to launch my project as a framework in the simulator. Luckily SwiftUI offers the canvas preview window however it is a bit finicky when it comes to certain button interactions, which is why I am wanting to use the simulator.
Two places of confusion:
What is the difference between an app and a framework project?
Why is it more advantageous to have my project as a framework?
An App is a standalone application that can be launched and run. For example, all of the apps that you have on your phone are just that -- apps. You tap on them and they launch and run, presenting a user interface, accepting input, etc.
A framework is something else entirely. It's a collection of code that is bundled together into a package that is used by another framework or by an app. Some frameworks are provided by the system -- for example, SwiftUI is a framework that it sounds like you're using in your app. Other frameworks are provided by 3rd parties. For example, you can find many frameworks via CocoaPods or the Swift Package Manager -- Alamofire is a common example. Also, you can make your own frameworks and use them in your own code as a form of organization and separation of responsibilities.
Why is it more advantageous to have my project as a framework?
It is not -- they are two almost completely different concepts (besides both ultimately being collections of code and resources). If you intend to build an app that is launch-able on someone's device, your only choice is to make an app. If you intend to make a collection of reusable code for use in your or someone else's app, than you would make a framework.
Excellent answer (and upvoted) by #jnpdx. Let me give you a physical example:
(1) Create a project in Xcode that is a framework. Call it "MyAppKit". Inside it create, well, basically anything - a View, UIView, or more likely a function that will be shared by several views. (Let's go with that.)
public func setLoginName(_ login:String) -> String {
return ""Hello, " + login + "!";
}
Pretty simple. Call it, pass in something, and it returns a string saying hello. Please note the public piece. It matters. (And there's much more there. This is a simple example.)
(2) Now we get to your app or apps. Let's say you have two apps that need to use this (again, very simple) code. One is SwiftUI, one is UIKit. (It doesn't matter except for syntax.) Sine my forte is UIKit I'll use that. (And it can be several dozen apps too.)
import MyAppKit
let myLoginMessage = setLoginName("World").
Pretty much, it's "Hello, World!'
Again, this is really a nonsensical example. But it should get you started on what the difference in Xcode is between a Framework project and an App project is.

Can I package entire iOS app as a Framework?

I have an app implemented in native iOS (Swift). There is a web version of the app as well. A client wants to embed my app to its own app and suggested I use an iFrame and load the web version.
I understand this is a tricky solution as Apple might reject the app for not using native implementation.
What I want to ask is if there is a way to package my app entirely as a Framework and load it that way (app size is fairly big, with several viewControllers and functionality).
I understand that I won't have access to App-load functions like the AppDelegate.
Also what happens if my app has Library dependencies ? (such as Alamofire)
Any other things I should be concerned about ?
Thank you
There are obviously a lot of options around this as far as design/approach.
I've done this multiple times (with apps live on the app store) and really it's just like developing any Framework.
First: AppDelegate. The easy way around this is to have the app's AppDelegate subclass your Framework's AppDelegate:
#UIApplicationMain class ParentAppDelegate: FrameworkAppDelegate { }
Just make sure the App calls super on all the relevant methods.
Second: Dependencies. This is probably the most annoying part since Frameworks can't embed other frameworks. But you still have a few easy options:
Have the enclosing app embed the needed framework
Add the sources of the needed framework directly to your framework.
Use a dependency manager (e.g. cocoapods) that takes care of this for you.
Other Concerns: One trap you can easily run into is working with Bundles. Anytime you dynamically load images/strings/IB references/etc. you will need to specify you're using the Framework's bundle, as at times it can default to using the app's bundle. The easiest way to do this is with this init e.g. Bundle(for: self.self)
Also keep in mind that the settings in info.plist and entitlements your framework needs will need to be added by the parent app.
General Comments on Approach: My advice (take it or leave it ☺️) would be caution around simply adding your full application to a client's application. Aside from IP and App-Review concerns, it can result in adding a lot of complexity or a fork of your current application to support it, making future maintenance a hassle.
Instead I would recommend putting only the portions of the application your client requires into a separate framework that both you and your client use for your separate applications.

swift how to embed one app to another

I am intern student who working mobile applications on swift. I created an app for company and I need to embed my app to other partner firm's app. I look some solutions on stackoverflow but they were not clear, Should I use target file? How to embed one app to another app?
Thanks for all help!!
Embedding one app to another isn't quiet right definition. What you are looking for is a Dynamic Framework. You can add a new Dynamic Framework target to your partner's app and move all the functionality and resources you need from your app.
https://developer.apple.com/library/content/technotes/tn2435/_index.html
A framework is a hierarchical directory that encapsulates a dynamic library, header files, and resources, such as storyboards, image files, and localized strings, into a single package. Apps using frameworks need to embed the framework in the app's bundle.
In case if you only want to allow the user to navigate from one app to another, you should use iOS Deep Linking mechanism. There are many third party, ready to use solutions for that, like Branch.
https://branch.io/
This can be usefull as well:
https://developer.apple.com/ios/universal-links/
This can be accomplished by defining a URL schema for your app so that the partner's app can point to.
Here is a tutorial that you need
http://www.brianjcoleman.com/tutorial-deep-linking-in-swift/

Make multiple replica of single ios app

I have 10 ios apps which have same UI and almost same functionality except minor changes.
I have completed one app and want to make other 9 app and also used some 3rd party libraries and made changes to them and made some my custom functions for app functionality.
My concern is that if i will get change for even one functionality , i have to make that change to other 9 apps.
What is the the best and fastest way except changing code in each file ?
You should keep one codebase and add multiple targets to it.
This way you can write both target specific code, and generic code.
You can read up more on this here One Codebase Multiple Targets
and here Apple Xcode Targets
Hope it helps

Can i open an Xcode project on a button click from another project without custom URL schema?

I want to integrate two Xcode projects together and they both are full Xcode projects with UI and all. For example lets say i have an App 'A' which has a UIButton and on click it should open App 'B' which has its own UI.
I know we can open an App from another App through Custom URL schema but is there a way i can do this without custom URL schema and via connecting Xcode projects.
I know this a little vague question but i don't a better way to put this.
Thanks,
You're question doesn't make any sense. You're talking about two very different things. Yes, you may import one Xcode project into another Xcode project and setup a dependency between them. However, this in no way affects how the apps work with each other. If you're wanting to embed one app inside another, you have to do it at the view controller/code level. You cannot simply add one project to another and expect the two to intercommunicate. You'll have to move the functionality of one app to another. Apps are sandboxed on the device and can therefore have no communication with each other except for by using custom URL schemes as you've mentioned.

Resources