Help on iOS app
See original GitHub issueI am working on a capacitor app for iOS and I am running into some issues and have some general questions:
My goal is to have an app that has a few custom native UIViewControllers to handle some native things. The code I want to create is using OpenCV, which is C++ so right now all my custom code is Objective-C since Swift doesn’t easily support C++ integration yet.
I have much of it working with Capacitor in general but now I am running into roadblocks due to my ignorance of how it all works in XCode. I have the native code in the “App” project
App (Project)
App (Group)
MyUIViewController.mm (and .h)
Main.storyboard
and the Capacitor stuff in the “Pods” project
Pods (Project)
Development Pods (some kind of group but I am unfamiliar with the symbol)
Capacitor (Group)
Plugins (Group)
MyCustomPlugin.swift (and .m for bridging)
MyCustomPlugin.swift has the following code:
@objc(MyCustomPlugin)
public class MyCustomPlugin: CAPPlugin {
@objc func capture(_ call: CAPPluginCall) {
let value = call.getString("value") ?? "" // left in for testing
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "myUIViewController")
DispatchQueue.main.async {
self.bridge.viewController.present(controller, animated: true, completion: nil)
}
// left in for testing
call.success([
"value": value
])
}
}
When I run this on my iPhone it all works. I have a button in an Angular Component and when I click it I get my UIViewController view. All the Objective-C code works and all the OpenCV magic also happens just fine. BTW - This is COOL AS HELL and I really like Capacitor.
Now I need to real return data back to Angular after a user does something in MyUIViewController. From my plugin (in the Pod project) I need to get a concrete handle to MyUIViewController (not just the Storyboard’s UIViewController interface). But the Pod project does not see MyUIViewController. If I try to import it in the plugin class I get “No such module”. I though maybe it was a Swift/Objective-C thing but I tried a dummy Swift class in the App project and the plugin still could not see it. I do not know how to share the UIViewControllers (and related classes) with the plugin.
I tried to copy the references of the classes I need from App to Pods in the same Plugin group as the MyCustomPlugin class and Xcode has no issue with the classes, but then Xcode complained about the OpenCV framework (which BTW I added to the Podfile with the ‘App’ target as the documentation recommends). I am guessing I can get everything working by adding the OpenCV framework to the ‘Pods’ target, but it feels like I am doing something wrong. So I want to reach out and find out the “correct” way to do this.
Thank you, Steve
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (2 by maintainers)
Great issue and glad the native integration is working well!
The plugin is isolated to its own module, so unless you have dependencies in that plugin pointing to your custom native code, you won’t be able to access that code through static means. This is different from how Cordova worked where all plugin code was copied to your project, but that has other pitfalls.
However, you should be able to get that view controller dynamically using objective-c dynamism.
For example, try NSClassFromString to get MyUIViewController and instantiate it, probably can even replace the storyboard code with this: https://stackoverflow.com/a/32265287
Let me know how that goes! Closing as this is not a big but rather a conceptual question. Cheers
Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Capacitor, please create a new issue and ensure the template is fully filled out.