Last active
November 5, 2020 00:46
-
-
Save Mardaneus86/7edd9aa068a0eb4aab40254b3fc44e0c to your computer and use it in GitHub Desktop.
Flutter iOS methodchannel for http calls (workaround for internal MobileIron base apps) - https://github.com/flutter/flutter/issues/41500
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import Flutter | |
@UIApplicationMain | |
@objc class AppDelegate: FlutterAppDelegate { | |
override func application( | |
_ application: UIApplication, | |
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? | |
) -> Bool { | |
let controller : FlutterViewController = window?.rootViewController as! FlutterViewController | |
let httpChannel = FlutterMethodChannel(name: "com.example.flutter/http", | |
binaryMessenger: controller.binaryMessenger) | |
httpChannel.setMethodCallHandler({ | |
(call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in | |
// Note: this method is invoked on the UI thread. | |
guard call.method == "http-get" || call.method == "http-post" else { | |
result(FlutterMethodNotImplemented) | |
return | |
} | |
if (call.method == "http-get") { | |
let session = URLSession.shared | |
let args = call.arguments as! NSDictionary | |
let url = args["url"] as! String | |
let clientId = args["client_id"] as! String | |
let clientSecret = args["client_secret"] as! String | |
var request = URLRequest(url: URL(string: url)!) | |
request.setValue(clientId, forHTTPHeaderField: "client_id") | |
request.setValue(clientSecret, forHTTPHeaderField: "client_secret") | |
request.httpMethod = "GET" | |
let task = session.dataTask(with: request) { data, response, error in | |
result([ | |
"data": String(data: data!, encoding: .utf8)!, | |
"statusCode": (response as? HTTPURLResponse)?.statusCode | |
]); | |
} | |
task.resume() | |
} | |
if (call.method == "http-post") { | |
// Do any additional setup after loading the view. | |
let session = URLSession.shared | |
let args = call.arguments as! NSDictionary | |
let url = args["url"] as! String | |
let body = args["body"] as! String | |
let clientId = args["client_id"] as! String | |
let clientSecret = args["client_secret"] as! String | |
var request = URLRequest(url: URL(string: url)!) | |
request.setValue(clientId, forHTTPHeaderField: "client_id") | |
request.setValue(clientSecret, forHTTPHeaderField: "client_secret") | |
request.setValue("application/json", forHTTPHeaderField: "Content-Type") | |
request.httpBody = body.data(using: .utf8) | |
request.httpMethod = "POST" | |
let task = session.dataTask(with: request) { data, response, error in | |
result([ | |
"data": String(data: data!, encoding: .utf8)!, | |
"statusCode": (response as? HTTPURLResponse)?.statusCode | |
]); | |
} | |
task.resume() | |
} | |
}) | |
GeneratedPluginRegistrant.register(with: self) | |
return super.application(application, didFinishLaunchingWithOptions: launchOptions) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:dio/dio.dart'; | |
import 'package:flutter/services.dart'; | |
class MobileIronHttpChannel { | |
static const platform = const MethodChannel('com.example.flutter/http'); | |
Future<Response> get(String clientId, String clientSecret, String url) async { | |
Map data = await platform.invokeMethod('http-get', { | |
"url": url, | |
"client_id": clientId, | |
"client_secret": clientSecret, | |
}); | |
return Response( | |
data: data["data"], | |
statusCode: data["statusCode"], | |
); | |
} | |
Future<Response> post(String clientId, String clientSecret, String url, String jsonBody) async { | |
Map data = await platform.invokeMethod('http-post', { | |
"url": url, | |
"client_id": clientId, | |
"client_secret": clientSecret, | |
"body": jsonBody, | |
}); | |
return Response( | |
data: data["data"], | |
statusCode: data["statusCode"], | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment