Has anyone had experience calling a UiKit view fro...
# compose-ios
b
Has anyone had experience calling a UiKit view from Compose Multiplatform? I have a CMP app where Compose is used for iOS and Android and I'm using expect/actual in order to show banner ads from Google AdMob. I got the Android side working pretty easily by having the actual function provide a Composable with the AndroidView for the banner ad, having a hard time figuring out how to get the UiKitView inside of a compose function for iOS. Particularly I'm trying to get this view to show up. https://developers.google.com/admob/ios/banner
I've seen the example for MapKit, but I don't have any references to the GADBannerView in Kotlin code like I do from within the iosApp in XCode.
p
Abstract the whole thing. Implement the UiView in swift calling the library and everything. Then wrap the whole thing within a @Composable expect/actual function
When swift APIs are complex this is a good alternative. Because otherwise mapping each functionality from swift to kotlin is really tedious plus will require double maintenance.
b
Cool, do you have an example of that? Would I just make a brand new view controller for this specifically?
p
Unfortunately I don't have an example. I just remembered another thread with this same concern and some body smartly suggested this solution. I believe using a simple UiView is better than a UiViewController bc will allow more granularity. Maybe I am wrong. But try just that, make your own UiViewController with all your stuff in swift land. Then use it in compose with UiKitView if using uiKit or UiKitViewController if using swift-ui
b
Cool, the view isn't going to be a whole screen it's just a little section for a banner ad. So hopefully that works fine, wish there were more examples. It makes sense how they do the MapKit but thats because that package shows up in the kotlin code, doesn't seem to be the case for the Google ads stuff though.
💯 1
âž• 1
p
I agree with you
l
Granted you properly added the cocoapods dependency and synced Gradle successfully, you should be able to use the symbols referenced in the Obj-C snippets.
b
@louiscad I'm using Swift Package Manager from the File -> Add Package Dependencies for GoogleAdMob, do I need to add cocoapods anywhere in gradle or something?
l
Swift Package Manager, as its name might imply, is for Swift code. You'll need to add the cocoapods dependency as documented on the Kotlin website
b
@louiscad I see, is it possible to just write the entire viewController in Swift within iOSApp and then import that into Kotlin code in the actual fun and use it within a composable?
l
Unfortunately, no, Kotlin code cannot directly reference Swift code
b
I see, I just asked because for gitlive and the ios firebase stuff I only had to init on the Swift side of things so I never had to add a cocoapod for the firebase sdk on the kotlin side of things.
But obviously that didn't require me to write a view for it that was embedded within my compose app
l
I'd just add the cocoapods dependency into Gradle and use the API from Kotlin
b
Cool, would I even need the Swift Package for it at all, within the iOS project that is?
l
I don't think you would
First result when Googling "cocoapods dependencies kotlin" https://kotlinlang.org/docs/native-cocoapods-libraries.html
BTW, you'll need to expose your project as a Pod too as told in another ongoing thread: https://kotlinlang.slack.com/archives/C3PQML5NU/p1717517662644959?thread_ts=1717452229.704629&cid=C3PQML5NU
p
I am not an iOS developer but out of what I heard cocoapods is dying and most iOS developers prefer moving away from it. Still very used but the future is getting rid of it.
âž• 1
r
I will talk about it at DroidCon Berlin and have prepared something: https://github.com/rschattauer/compose_multiplatform It's full compose multiplatform, while the map is UiKit as there obviously is no implementation for mapbox on CMP. Hope this helps :) feedback appreciated
🙌 2
👀 1
b
Awesome, I used this setup to be able to set a viewController up. I had previously gotten it working through drilling the viewController all the way through the App composable, but that made it so I had to cast it as a Any?
this is what I previously had MainViewControllerKt.MainViewController(googleAdMobViewController: myGoogleAdMobViewController)
p
Yeah, something like bellow, where
myGoogleAdMobViewController
access whatever it wants in swift land. I think is better and easier than the
cocoapods
or
.def
or the
interface
routes.
Copy code
expect @Composable fun GoogleAdMobView()
with
Copy code
actual @Composable fun GoogleAdMobView() {
  UiKitView {
    factory = { myGoogleAdMobViewController }
  }
}
b
Exactly what I did! It seems to be working good 🙂 and yeah, I can just use SwiftPackageManager exclusively. I ran into a huge headache trying to get my project exported as a pod and all the other configuration required.
💯 1
p
Same experience here with pod.
b
Is there a way to know the height of a viewController from Compose? It seems I need to pass a height modifier to the UiKitViewController in order to get it to appear, but my view is a banner ad which has a dynamic size to it. I don't want to take up too much unnecessary space when the banner ad is only 50px for example, but don't want to clip the ad if it's 90px.
p
Check if the proposed solution below is possible to be called from kotlin: https://stackoverflow.com/questions/56272425/how-to-find-view-controller-height-programmatically If not, then you could do the height computation in your viewController and expose an easy getCurrentHeight() function in swift to access it from kotlin.
j
Sorry to revive an old thread, @Pablichjenkov Do you have an example project using strategy similar to the
myGoogleAdMobViewController
you mentioned above?
p
Hey Jonathan, sorry I don't.
j
Do you happen to have a link to the source thread you consulted when building your solution?
p
Unfortunately I don't either. I have read some posts about this same problem here and there. People have found ways around but I don't have the right place to point you right now. Have you checked the recent documentation in regards to Swiftui/Uikit interop. I would start there, maybe this is something that is covered in the docs already. Also check the GitHub issues or YouTrack
326 Views