NativeScript / ios-jsc

NativeScript for iOS using JavaScriptCore
http://docs.nativescript.org/runtimes/ios
Apache License 2.0
299 stars 59 forks source link

Consuming third-party libraries written in Swift #257

Open jasssonpet opened 9 years ago

jasssonpet commented 9 years ago

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

KristinaKoeva commented 9 years ago

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.

jasssonpet commented 9 years ago

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

x4080 commented 9 years ago

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

Thanks

fealebenpae commented 8 years ago

@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.

x4080 commented 8 years ago

Thanks

ligaz commented 8 years ago

@fealebenpae Maybe we should mention this in the docs?

jasssonpet commented 8 years ago

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.

sitefinitysteve commented 8 years ago

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?

fealebenpae commented 8 years ago

@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.

x4080 commented 8 years ago

Can we generate ts declaration from android library too?

fealebenpae commented 8 years ago

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

slavchev commented 8 years ago

@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

x4080 commented 8 years ago

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

Thanks

NathanWalker commented 8 years ago

@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 commented 8 years ago

@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

fealebenpae commented 8 years ago

@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.

NathanWalker commented 8 years ago

@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 commented 8 years ago

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.

x4080 commented 8 years ago

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?

RoryShively commented 8 years ago

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

ivanbuhov commented 8 years ago

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

PBartrina commented 8 years ago

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.

KristinaKoeva commented 8 years ago

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

PBartrina commented 8 years ago

Thank you @KristinaKoeva , I found it very helpful

Mardaneus86 commented 8 years ago

@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
sitefinitysteve commented 7 years ago

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

NathanWalker commented 7 years ago

@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
sitefinitysteve commented 7 years ago

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 notifications@github.com 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 commented 7 years ago

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

sitefinitysteve commented 7 years ago

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?