I am currently working on a project in which I need to implement a graphical framework that has been written for Android and iOS; however, I am writing the application using Flutter/Dart, and so far I can not find any way for showing native iOS views in a Flutter application. I have found a module written for android that claims to do this (blog post), and I am curious if anyone knows of any modules or techniques that can achieve this for iOS.
I know that Flutter has the ChannelMethod feature, but that is not a solution-at least from my understanding, ChannelMethods can pass platform-specific messages, but this does not help me show a platform-specific plugin in a Flutter application.
It has also occurred to me to hard code an equivalent graphic interface in Flutter and then use ChannelMethods to pass the needed data, but this is not ideal because it appears that not all data I would need from the plugin is easily available. I'm really looking for a way to show the UIView in Flutter somehow.
Is there any way to show a native iOS view in a Flutter application?
Alternatively, is there any way to segue to a iOS viewController from a Flutter application?
You can use Flutter Platform Views to display platform native widgets in the Flutter app.
In the sample given in the docs, FLNativeViewFactory creates the platform view, and it provides a reference to the UIView. The iOS Views can be added using the provided UIView reference.
Using Swift
class FLNativeView: NSObject, FlutterPlatformView {
init(
frame: CGRect,
viewIdentifier viewId: Int64,
arguments args: Any?,
binaryMessenger messenger: FlutterBinaryMessenger?
){
_view = UIView()
super.init()
// iOS views can be added here
}
}
Using Objective-C
#implementation FLNativeView {
UIView *_view;
}
- (instancetype)initWithFrame:(CGRect)frame
viewIdentifier:(int64_t)viewId
arguments:(id _Nullable)args
binaryMessenger:(NSObject<FlutterBinaryMessenger>*)messenger {
if (self = [super init]) {
_view = [[UIView alloc] init];
}
// iOS views can be added here
return self;
}
#end
Related
Trying to learn React Native custom Native UI Components.
// FridgeCameraViewManager.swift
import UIKit
#objc(FridgeCameraViewManager)
class FridgeCameraViewManager: RCTViewManager {
override func view() -> UIView! {
let label = UILabel()
label.text = "Swift Component"
label.textAlignment = .center
return label
}
#objc static override func requiresMainQueueSetup() -> Bool {
return false
}
}
.
// FridgeCameraViewManager.h
#import <Foundation/Foundation.h>
#import "React/RCTViewManager.h"
#interface RCT_EXTERN_MODULE(FridgeCameraViewManager, RCTViewManager)
#end
.
// FridgeCameraView.js
import {requireNativeComponent} from 'react-native';
const FridgeCameraView = requireNativeComponent('FridgeCameraView', null);
export default FridgeCameraView;
When I try to use FridgeCameraView component somewhere in App.js, it works only If I build & run the project using Xcode. Otherwise, using hot reload when changing something, I get "Invariant Violation: tried to register two views with the same name FridgeCameraView".
Somehow, the error went away when I installed react-router-native. I think there was a package conflict or something that I was missing. Hopefully, this will be a fix for the ones who encounter this error in the future. I'm still waiting for explanations if somebody knows what's behind this weird error.
(Not a solution, but still want to contribute)
TL;DR:
Just press R in terminal to refresh app
Auto hot-reload will register FridgeCameraView twice, reason unknown
For those of you who are wondering what's going on, we're learning the React Native - iOS Native Component and following the tutorial:
Swift in React Native - The Ultimate Guide Part 1: Modules
Swift in React Native - The Ultimate Guide Part 2: UI Components
And this is part 2 UI Components that went wrong. As React / React Native are changing fast, this scared resource on native component soon became outdated.
I accidentally need to reinstall my macOS & node_module, no luck in getting rid of the error. Installing another package doesn't solve the issue either.
I suspect that during React Native bridging in Swift -> Objective C -> React-JS, React Native will register our customized module FridgeCameraView twice; but will need expect in Objective C to dig deeper on this issue. Any contribution is welcomed!
I have a certain task where i want to pass a UIView to one of the SDK's in my iOS project.
Now, my project is a simple 2D game build in Unity 3d. I want to send the Panel in my Canvas to that SDK as UIView.
I am aware of iOS and Unity communication. It happens using as Char*.
I am not sure how should i access that GameObject Panel in my iOS code as UIView. I will be very thankful if i receive any help on this.
Above is one of solution that you can access from ios interface but before see this, read https://docs.unity3d.com/Manual/PluginsForIOS.html. it will help to understand how unity and native code can communicate.
iOS: test.mm
#import "Unity/UnityInterface.h"
...
...
-(void)SendMessage
{
UnitySendMessage("MessageGameCommunicator", "Receiver", [message UTF8String]);
}
Unity: Commnicator.cs, MessageGameCommunicator (GameObject name)
public class Commnicator : MonoBehaviour
{
public static void Receiver(string message)
{
// ...
// Do something.
}
}
After Unity interface get message from ios, you can implement whatever you want.
I need to call methods in the custom view in iOS module from titanium project. I have followed the tutorials in Appcelarator documentation about creating iOS module. I could create the custom view in Titanium using below code.
var manage = require('com.test');
var manageView = manage.createView({
left:40,
right:40,
top:40,
height: 250,
backgroundColor:'blue' }); manageView.customMethodInView();
But i got error like "customMethodInView is not a function" when i run the app.
#import "TiViewProxy.h"
#import "CustomView.h"
#interface ComTestViewProxy : TiViewProxy {
CustomView *customView;
}
- (void) customMethodInView;
#end
This is the code in viewProxy class in iOS Module project.
Please help.
I know this is late, but for any wondering soul out there, it is my understanding that even if you don't intend to pass an argument for your method, the signature of the method in the native module should always take one:
your method
- (void) customMethodInView;
should look like this :
- (void)customMethodInView:(id)unused;
How does cordova trigger objective-c native method. For example- When a user taps on submit button (html button), app needs to invoke native objective c function called 'dataSubmitted'.
Does Cordova monitor webview navigation and based on URL tags call method internally?
Is there any way JavaScript can interact with Obj-c native methods except monitoring webview navigations?
There are two ways to communicate with native language from JS.
1. Go to MainViewController.m -> find a function named webViewDidFinishLoad
and add the follwing code snippet..
- (void)webViewDidFinishLoad:(UIWebView*)theWebView
{
NSString *pageUrl = [theWebView.request.URL absoluteString];
if ([pageUrl containsString:#"xyz"]) {
// xyz for xyz.html
}else if ([pageUrl containsString:#"abc"]) {
// abc for abc.html
}
self.webView = theWebView;
return [super webViewDidFinishLoad:theWebView];
}
Develop your own native plugin with cordova.exec ;
There are a few libraries available to communicate between Javascript and Objective-C it seems:
WebViewJavascriptBridge: https://github.com/marcuswestin/WebViewJavascriptBridge
GAJavaScript: https://github.com/newyankeecodeshop/GAJavaScript
I suggest you to check out this beautiful SO Post which explains Javascript & Objective-C interactions in detail.
I use a native module that works great for getting device info in React Native, in the JS code. I'd like to also make use of it's functionality in other native (Objective-C) code.
Is it possible to access functionality of React Native custom modules from other native code?
You can either access the functionality directly (using -[RNDeviceInfo deviceName] method) or using the way React Native is accessing it, that is:
RNDeviceInfo *rn = [[RNDeviceInfo alloc] init];
NSLog(#"Device Name: %#", [rn constantsToExport][#"model"]);
I found one solution, that is admittedly a bit of a kludge. It does work, but I would imagine this is far from ideal.
At the top of the class that utilizes this code:
#interface RNDeviceInfo ()
- (NSString*) deviceName;
#end
Then I can make use of it like so:
RNDeviceInfo *rn = [[RNDeviceInfo alloc] init];
NSLog(#"Device Name: %#", [rn deviceName]);