Skip to content

Commit

Permalink
Merge pull request #11 from EbramTawfik/iOS
Browse files Browse the repository at this point in the history
Adding implementation for iOS
  • Loading branch information
VNAPNIC authored Aug 12, 2021
2 parents 6654372 + cffe2e8 commit 2ee2749
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 70 deletions.
76 changes: 15 additions & 61 deletions example/ios/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,78 +10,32 @@ project 'Runner', {
'Release' => :release,
}

def parse_KV_file(file, separator='=')
file_abs_path = File.expand_path(file)
if !File.exists? file_abs_path
return [];
def flutter_root
generated_xcode_build_settings_path = File.expand_path(File.join('..', 'Flutter', 'Generated.xcconfig'), __FILE__)
unless File.exist?(generated_xcode_build_settings_path)
raise "#{generated_xcode_build_settings_path} must exist. If you're running pod install manually, make sure flutter pub get is executed first"
end
generated_key_values = {}
skip_line_start_symbols = ["#", "/"]
File.foreach(file_abs_path) do |line|
next if skip_line_start_symbols.any? { |symbol| line =~ /^\s*#{symbol}/ }
plugin = line.split(pattern=separator)
if plugin.length == 2
podname = plugin[0].strip()
path = plugin[1].strip()
podpath = File.expand_path("#{path}", file_abs_path)
generated_key_values[podname] = podpath
else
puts "Invalid plugin specification: #{line}"
end

File.foreach(generated_xcode_build_settings_path) do |line|
matches = line.match(/FLUTTER_ROOT\=(.*)/)
return matches[1].strip if matches
end
generated_key_values
raise "FLUTTER_ROOT not found in #{generated_xcode_build_settings_path}. Try deleting Generated.xcconfig, then run flutter pub get"
end

require File.expand_path(File.join('packages', 'flutter_tools', 'bin', 'podhelper'), flutter_root)

flutter_ios_podfile_setup

target 'Runner' do
use_frameworks!
use_modular_headers!

# Flutter Pod

copied_flutter_dir = File.join(__dir__, 'Flutter')
copied_framework_path = File.join(copied_flutter_dir, 'Flutter.framework')
copied_podspec_path = File.join(copied_flutter_dir, 'Flutter.podspec')
unless File.exist?(copied_framework_path) && File.exist?(copied_podspec_path)
# Copy Flutter.framework and Flutter.podspec to Flutter/ to have something to link against if the xcode backend script has not run yet.
# That script will copy the correct debug/profile/release version of the framework based on the currently selected Xcode configuration.
# CocoaPods will not embed the framework on pod install (before any build phases can generate) if the dylib does not exist.

generated_xcode_build_settings_path = File.join(copied_flutter_dir, 'Generated.xcconfig')
unless File.exist?(generated_xcode_build_settings_path)
raise "Generated.xcconfig must exist. If you're running pod install manually, make sure flutter pub get is executed first"
end
generated_xcode_build_settings = parse_KV_file(generated_xcode_build_settings_path)
cached_framework_dir = generated_xcode_build_settings['FLUTTER_FRAMEWORK_DIR'];

unless File.exist?(copied_framework_path)
FileUtils.cp_r(File.join(cached_framework_dir, 'Flutter.framework'), copied_flutter_dir)
end
unless File.exist?(copied_podspec_path)
FileUtils.cp(File.join(cached_framework_dir, 'Flutter.podspec'), copied_flutter_dir)
end
end

# Keep pod path relative so it can be checked into Podfile.lock.
pod 'Flutter', :path => 'Flutter'

# Plugin Pods

# Prepare symlinks folder. We use symlinks to avoid having Podfile.lock
# referring to absolute paths on developers' machines.
system('rm -rf .symlinks')
system('mkdir -p .symlinks/plugins')
plugin_pods = parse_KV_file('../.flutter-plugins')
plugin_pods.each do |name, path|
symlink = File.join('.symlinks', 'plugins', name)
File.symlink(path, symlink)
pod name, :path => File.join(symlink, 'ios')
end
flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
end

post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings['ENABLE_BITCODE'] = 'NO'
end
flutter_additional_ios_build_settings(target)
end
end
4 changes: 3 additions & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ class _DisplayManagerScreenState extends State<DisplayManagerScreen> {
}),
Button("Show presentation", () async {
displayManager.showSecondaryDisplay(
displayId: displays[1]?.displayId ?? -1,
displayId: displays.length > 0
? displays[1]?.displayId ?? -1
: -1,
routerName: "presentation");
}),
Button("NameByDisplayId", () async {
Expand Down
7 changes: 3 additions & 4 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ description: Demonstrates how to use the presentation_displays plugin.
# The following line prevents the package from being accidentally published to
# pub.dev using `pub publish`. This is preferred for private packages.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev

version: 1.0.0+1
environment:
sdk: ">=2.12.0 <3.0.0"
sdk: '>=2.12.0 <3.0.0'

dependencies:
flutter:
Expand All @@ -17,7 +17,7 @@ dependencies:
# presentation_displays: ^x.y.z
# See https://dart.dev/tools/pub/dependencies#version-constraints
# The example app is bundled with the plugin so we use a path dependency on
# the parent directory to use the current plugin's version.
# the parent directory to use the current plugin's version.
path: ../

# The following adds the Cupertino Icons font to your application.
Expand All @@ -33,7 +33,6 @@ dev_dependencies:

# The following section is specific to Flutter.
flutter:

# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
Expand Down
36 changes: 34 additions & 2 deletions ios/Classes/SwiftPresentationDisplaysPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,45 @@ import Flutter
import UIKit

public class SwiftPresentationDisplaysPlugin: NSObject, FlutterPlugin {
static var additionalWindows = [UIWindow]()
var flutterEngineChannel:FlutterMethodChannel=FlutterMethodChannel()

public static func register(with registrar: FlutterPluginRegistrar) {
let channel = FlutterMethodChannel(name: "presentation_displays", binaryMessenger: registrar.messenger())
let channel = FlutterMethodChannel(name: "presentation_displays_plugin", binaryMessenger: registrar.messenger())
let instance = SwiftPresentationDisplaysPlugin()
registrar.addMethodCallDelegate(instance, channel: channel)

NotificationCenter.default.addObserver(forName: UIScreen.didConnectNotification,
object: nil, queue: nil) { notification in
// Get the new screen information.
let newScreen = notification.object as! UIScreen
let screenDimensions = newScreen.bounds

// Configure a window for the screen.
let newWindow = UIWindow(frame: screenDimensions)
newWindow.screen = newScreen

// You must show the window explicitly.
newWindow.isHidden = false
// Save a reference to the window in a local array.
self.additionalWindows.append(newWindow)
}
}

public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
result("iOS " + UIDevice.current.systemVersion)
// result("iOS " + UIDevice.current.systemVersion)
if call.method=="showPresentation"{
showPresentation()
}
else if call.method=="transferDataToPresentation"{
self.flutterEngineChannel.invokeMethod("DataTransfer", arguments: call.arguments)
}
}
private func showPresentation()
{
let extVC = FlutterViewController()
extVC.setInitialRoute("presentation")
SwiftPresentationDisplaysPlugin.additionalWindows[0].rootViewController = extVC
self.flutterEngineChannel = FlutterMethodChannel(name: "presentation_displays_plugin_engine", binaryMessenger: extVC.binaryMessenger)
}
}
4 changes: 2 additions & 2 deletions ios/presentation_displays.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
Pod::Spec.new do |s|
s.name = 'presentation_displays'
s.version = '0.0.1'
s.summary = 'Flutter plugin supports to run on two screens. It's basically a tablet connected to another screen via an HDMI or Wireless'
s.summary = 'Flutter plugin supports to run on two screens. It\'s basically a tablet connected to another screen via an HDMI or Wireless'
s.description = <<-DESC
Flutter plugin supports to run on two screens. It's basically a tablet connected to another screen via an HDMI or Wireless
Flutter plugin supports to run on two screens. It\'s basically a tablet connected to another screen via an HDMI or Wireless
DESC
s.homepage = 'http://example.com'
s.license = { :file => '../LICENSE' }
Expand Down

0 comments on commit 2ee2749

Please sign in to comment.