SatoshiPortal / boltz-dart

Dart boltz client
MIT License
7 stars 7 forks source link

Fix/ios build #10

Closed salvatoto closed 4 months ago

salvatoto commented 4 months ago

Main issues resolved:

Instructions when adding to Flutter project:

  1. In the flutter_rust_bridge Dart wrapper, we need to point to the pod framework, which will contain the rust library:

Add this helper in Dart code:

final DynamicLibrary _objcLib = DynamicLibrary.process();

class NSBundleHelper {
  static final Pointer<Utf8> Function() _getMainBundlePath = _objcLib
      .lookup<NativeFunction<Pointer<Utf8> Function()>>('getMainBundlePath')
      .asFunction();

  static String get mainBundlePath => _getMainBundlePath().toDartString();
}

And this obj-c helper in NSBundleHelper.h in iOS Runner project:

#define NSBundleHelper_h

// BundleHelper.h

#import <Foundation/Foundation.h>

// Declares the C function for external use.
__attribute__((visibility("default")))
const char* getMainBundlePath(void);

#endif /* NSBundleHelper_h */
#import <Foundation/Foundation.h>
#import "NSBundleHelper.h"

const char* getMainBundlePath() {
    return [[NSBundle mainBundle].bundlePath UTF8String];
}
  1. Then point to pod framework for iOS. Android will be able to find the bare rust library boltzclient, however, since for iOS we wrap it in a cocoapods xcframework, we can find it this way:

          ? ExternalLibrary.open(
              '${NSBundleHelper.mainBundlePath}/Frameworks/boltz_dart.framework/boltz_dart')
          : null;
      await BoltzCore.init(externalLibrary: externalLibrary);
  2. Add bundle helper and frb bindings to Runner-Bridging-Header.h in iOS Runner Project:

    #import <boltz_dart/frb_generated.h>

34 Add NSBundleHelper.m to Xcode > Build Phases > Compile Sources if not there already

  1. Add dummyMethodToEnforceBundling in ios/AppDelegate. Don't call it, just need to be in that file so those functions aren't stripped during linking
i5hi commented 4 months ago

Thanks for the PR @salvatoto !

The binaries are building correctly. I will merge this and make a release with the mac/ios binaries.

If there are any issues with using the library following the release we can address it in a new PR.

cuhte3 commented 4 months ago

Am trying to add it to my native iOS app through SPM dependency and seeking for advices. As far as I can see I could use the C libboltz framework and fed_generated.h interface to create SPM package. The missing part right now is translation of the rust interfaces/types/errors located in rust/src/api/ into swift api. Does that sounds correct flow? Any advice? should I use the dummyMethodToEnforceBundling in my app? Thanks for the awesome library!

i5hi commented 4 months ago

Hey @cuhte3!

I am not familiar with iOS development and would not be able to comment on whether this flow would work.

AFAIK flutter_rust_bridge (frb) is best suited to translating files from rust to flutter. The flutter code then calls into the shared library binary files compiled in rust for ios. For Swift code, you maybe better served by a library like uniffi.

This is a good reference project: https://github.com/thunderbiscuit/uniffi-bindings-template

You could create a library using the same rust files as boltz-dart/rust and some uniffi macros to generate swift code.

i5hi commented 4 months ago

Another good example of a rust project with Swift bindings: https://github.com/Blockstream/lwk/tree/master/lwk_bindings