ios-jsc icon indicating copy to clipboard operation
ios-jsc copied to clipboard

Consuming third-party libraries written in Swift

Open jasssonpet opened this issue 8 years ago • 30 comments

For example: https://github.com/danielgindi/ios-charts

  • [x] Respect objc_runtime_name attribute
  • [ ] https://github.com/NativeScript/ios-runtime/issues/95 - Swift supports namespaces, so there could be collisions
  • [x] https://github.com/NativeScript/ios-runtime/pull/274 - Support Swift name mangling

jasssonpet avatar Aug 06 '15 15:08 jasssonpet

The following would enable support for all Swift exposed code https://github.com/NativeScript/ios-runtime/pull/274 https://github.com/NativeScript/ios-metadata-generator/pull/15

Those would enable support for all Swift exposed code. According to Apple's documentation

A Swift class or protocol must be marked with the @objc attribute to be accessible and usable in Objective-C. This attribute tells the compiler that this piece of Swift code can be accessed from Objective-C. If your Swift class is a descendant of an Objective-C class, the compiler automatically adds the @objc attribute for you.

All others would not be available in our runtime.

KristinaKoeva avatar Aug 21 '15 14:08 KristinaKoeva

Using Swift Class Names with Objective-C APIs: https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/WritingSwiftClassesWithObjective-CBehavior.html#//apple_ref/doc/uid/TP40014216-CH5-ID107

jasssonpet avatar Aug 24 '15 11:08 jasssonpet

Hi is it possible now to use Swift based pods? If so what is the step required ? Or just Like regular pods?

Thanks

x4080 avatar Nov 12 '15 05:11 x4080

@x4080: Yes, you can consume a Swift-based pod just like a regular pod, provided the Swift code is exposed to Objective-C, like in the ios-charts pod.

fealebenpae avatar Nov 12 '15 11:11 fealebenpae

Thanks

x4080 avatar Nov 12 '15 22:11 x4080

@fealebenpae Maybe we should mention this in the docs?

ligaz avatar Nov 13 '15 09:11 ligaz

The Swift limitation isn't limited to CocoaPods only, so maybe we should add it to a broader article about using third-party native code.

jasssonpet avatar Nov 13 '15 09:11 jasssonpet

I am having a heck of a time trying to get this to load https://cocoapods.org/?q=TTG ...same deal, because it was generated in swift?

Where does one see if the code is exposed to OBJC?

sitefinitysteve avatar Dec 14 '15 20:12 sitefinitysteve

@sitefinitysteve: it is exposed to Objective-C - the TTGSnackbar class derives from UIView which is an Objective-C class and all Swift classes that derive from an Objective-C class are implicitly exposed. I've just confirmed that the metadata generator sees the class in the pod. You can do the same by following the instructions in this blog post: http://jasssonpet.github.io/generating-typescript-declarations-in-nativescript-for-javascript-coffeescript-code-completion.

If you like, you can open a new issue about your problem and attach the relevant error messages and build logs.

fealebenpae avatar Dec 15 '15 12:12 fealebenpae

Can we generate ts declaration from android library too?

x4080 avatar Dec 15 '15 12:12 x4080

@x4080: I believe you can, but @slavchev can explain the process.

fealebenpae avatar Dec 15 '15 13:12 fealebenpae

@x4080 You can find the dts generator at https://github.com/NativeScript/android-dts-generator. A big warning: the current implementation has some limitations and it works with Java source code files. The good news is that we are working on a new implementation that will generate *.d.ts files from *.jar libraries. See https://github.com/NativeScript/android-runtime/issues/104 and https://github.com/NativeScript/android-runtime/issues/105

slavchev avatar Dec 15 '15 13:12 slavchev

Allright i ll just wait for it and assume it will work with aar too?

Thanks

x4080 avatar Dec 15 '15 22:12 x4080

@fealebenpae In ios-charts, is it simply the fact that they made the swift classes public which exposes them to Objective-C? I'm trying to update this Pod https://github.com/cemolcay/MaterialCardView to be consumed by {N} and the MaterialCardView is not seen by {N} at moment... Just trying to understand exactly what is needed to expose it to Obj-C? Thanks!

NathanWalker avatar Feb 03 '16 18:02 NathanWalker

@fealebenpae Or does it have something to do with this: https://github.com/danielgindi/ios-charts/blob/master/Charts.podspec#L13-L21 ... If so, I'm not 100% sure how to modify, but maybe has something to do with this: https://github.com/cemolcay/MaterialCardView/blob/master/MaterialCardView.podspec#L125-L127

NathanWalker avatar Feb 03 '16 18:02 NathanWalker

@NathanWalker: I really can't say, I don't understand the relationship between CocoaPods and Swift very well yet. But the rule of thumb is that if an Objective-C app can use a Swift CocoaPod then so too should NativeScript.

fealebenpae avatar Feb 03 '16 19:02 fealebenpae

@fealebenpae I will say the documentation around this stuff from Apple is not 100% accurate. https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html#//apple_ref/doc/uid/TP40014216-CH10-ID122 Appears to suggest a number of things that seem to be incorrect. Simply using @objc does not expose swift classes to Objective C. Only marking a class as public appears to make them truly accessible from Objective C. :/

NathanWalker avatar Feb 04 '16 00:02 NathanWalker

Private declarations are not exposed to Objective-C unless they are explicitly marked with @IBAction, @IBOutlet, or @objc as well.

Ok, so simply adding @objc would seem to make them available. Apparently not. They must be marked public it seems.

NathanWalker avatar Feb 04 '16 00:02 NathanWalker

if the source of swift dont have @objc and public, can we just change the source that already downloaded ? or it will change after pod install?

x4080 avatar Feb 04 '16 11:02 x4080

Has this been figured out yet? I need to use this swift sdk https://github.com/uber/rides-ios-sdk/ and I'm trying to figure out if nativescript is a good fit for the project

RoryShively avatar Apr 06 '16 03:04 RoryShively

@RoryShively Since the APIs in the Uber SDK are consumable from Objective C, you can consume them in NativeScript application, too.

ivanbuhov avatar Apr 06 '16 07:04 ivanbuhov

Someone manages to load the https://github.com/danielgindi/ios-charts library in the OP via pods? Can't access the exposed API, although it looks like the library is correctly loaded.

PBartrina avatar Apr 18 '16 12:04 PBartrina

Here is a little dated example of using ios-charts in NativeScript

KristinaKoeva avatar Apr 19 '16 07:04 KristinaKoeva

Thank you @KristinaKoeva , I found it very helpful

PBartrina avatar Apr 19 '16 12:04 PBartrina

@KristinaKoeva your example seems to no longer work. Might have something to do with Xcode 8 and Swift 3.0. Haven't been able to test with Xcode 7 and Swift 2.3 yet.

Add the following to the podfile (from KristinaKoeva's example) to make it work in Xcode 8:

post_install do |installer|
  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |config|
      config.build_settings['SWIFT_VERSION'] = '2.3'
    end
  end
end

Mardaneus86 avatar Sep 27 '16 20:09 Mardaneus86

Whats the status of this? Auth0 updated to swift and I'm not sure what to do here :/ https://github.com/sitefinitysteve/nativescript-auth0/blob/master/platforms/ios/Podfile

sitefinitysteve avatar May 08 '17 03:05 sitefinitysteve

@sitefinitysteve if you update to latest version of Auth0 swift cocoapod, modify your Podfile to following:

pod "Lock", "~> 2.2"
pod "Auth0", "~> 1.5"

post_install do |installer|
  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |config|
      config.build_settings['SWIFT_VERSION'] = '3.0'
    end
  end
end

And you may need to add a build.xcconfig file with the following single line:

EMBEDDED_CONTENT_CONTAINS_SWIFT = YES

NathanWalker avatar May 08 '17 05:05 NathanWalker

Tried, I do have it set like that per (I think) enchevs swift plugin sample, but still doesn’t build :/

Well scratch that, had it as 2.0 and 1.2, let me try version bump!

On May 8, 2017, at 1:13 AM, Nathan Walker [email protected] wrote:

@sitefinitysteve https://github.com/sitefinitysteve if you update to latest version of Auth0 swift cocoapod, modify your Podfile to following:

pod "Lock", "~> 2.2" pod "Auth0", "~> 1.5"

post_install do |installer| installer.pods_project.targets.each do |target| target.build_configurations.each do |config| config.build_settings['SWIFT_VERSION'] = '3.0' end end end And you may need to add a build.xcconfig file with the following single line:

EMBEDDED_CONTENT_CONTAINS_SWIFT = YES — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/NativeScript/ios-runtime/issues/257#issuecomment-299774584, or mute the thread https://github.com/notifications/unsubscribe-auth/ABeI6OY4JF0ukcLsAu2cpEKJmeo8JGARks5r3qRcgaJpZM4Fm7JH.

sitefinitysteve avatar May 08 '17 05:05 sitefinitysteve

Yeah solid, working again thx @NathanWalker ...man killing me all night thats all she was

sitefinitysteve avatar May 08 '17 05:05 sitefinitysteve

What about consuming closures? I'm not seeing "withOptions" anywhere in the generated Meta https://auth0.com/docs/libraries/lock-ios/v2/configuration#configuring-lock-s-behavior https://github.com/auth0/Lock.swift/blob/71a04034848ed45f9a85d4725cb4b2625685f4c8/Lock/Lock.swift#L167

Should I open a separate issue for this?

sitefinitysteve avatar May 10 '17 19:05 sitefinitysteve