Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
F
Flutter Inappwebview
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
李增强
Flutter Inappwebview
Commits
4ebdba09
Commit
4ebdba09
authored
Sep 22, 2018
by
pichillilorenzo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
removed download method, injectScriptCode now returns result of the code injected #2, code cleanup
parent
d89cd5df
Changes
17
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
17 changed files
with
363 additions
and
264 deletions
+363
-264
.idea/workspace.xml
.idea/workspace.xml
+158
-128
android/build.gradle
android/build.gradle
+0
-1
android/src/main/AndroidManifest.xml
android/src/main/AndroidManifest.xml
+0
-1
android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppBrowserFlutterPlugin.java
...renzo/flutter_inappbrowser/InAppBrowserFlutterPlugin.java
+60
-28
android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppBrowserWebViewClient.java
...renzo/flutter_inappbrowser/InAppBrowserWebViewClient.java
+4
-4
android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/RequestPermissionHandler.java
...orenzo/flutter_inappbrowser/RequestPermissionHandler.java
+0
-2
android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/WebViewActivity.java
...pichillilorenzo/flutter_inappbrowser/WebViewActivity.java
+34
-62
example/ios/Podfile
example/ios/Podfile
+1
-0
example/ios/Runner.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings
...ner.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings
+8
-0
example/lib/main.dart
example/lib/main.dart
+17
-2
flutter_inappbrowser.iml
flutter_inappbrowser.iml
+1
-0
ios/Classes/InAppBrowserFlutterPlugin.h
ios/Classes/InAppBrowserFlutterPlugin.h
+1
-1
ios/Classes/InAppBrowserFlutterPlugin.m
ios/Classes/InAppBrowserFlutterPlugin.m
+2
-2
ios/Classes/InAppBrowserWebViewController.swift
ios/Classes/InAppBrowserWebViewController.swift
+34
-0
ios/Classes/SwiftFlutterPlugin.swift
ios/Classes/SwiftFlutterPlugin.swift
+41
-26
lib/flutter_inappbrowser.dart
lib/flutter_inappbrowser.dart
+1
-6
pubspec.yaml
pubspec.yaml
+1
-1
No files found.
.idea/workspace.xml
View file @
4ebdba09
This diff is collapsed.
Click to expand it.
android/build.gradle
View file @
4ebdba09
...
...
@@ -38,5 +38,4 @@ android {
dependencies
{
implementation
'com.android.support:customtabs:27.1.1'
implementation
'com.android.support:appcompat-v7:27.1.1'
implementation
'com.android.support.constraint:constraint-layout:1.1.3'
}
android/src/main/AndroidManifest.xml
View file @
4ebdba09
...
...
@@ -3,7 +3,6 @@
package=
"com.pichillilorenzo.flutter_inappbrowser"
>
<uses-permission
android:name=
"android.permission.INTERNET"
/>
<uses-permission
android:name=
"android.permission.WRITE_EXTERNAL_STORAGE"
/>
<application
android:theme=
"@style/AppTheme"
>
...
...
android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppBrowser.java
→
android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppBrowser
FlutterPlugin
.java
View file @
4ebdba09
...
...
@@ -30,13 +30,16 @@ import android.net.Uri;
import
android.os.Build
;
import
android.os.Bundle
;
import
android.support.annotation.RequiresApi
;
import
android.util.JsonReader
;
import
android.util.JsonToken
;
import
android.webkit.MimeTypeMap
;
import
android.webkit.ValueCallback
;
import
android.webkit.WebView
;
import
android.webkit.WebViewClient
;
import
android.util.Log
;
import
android.widget.Toast
;
import
java.time.Duration
;
import
java.io.IOException
;
import
java.io.StringReader
;
import
java.util.HashMap
;
import
java.util.Map
;
...
...
@@ -44,11 +47,10 @@ import io.flutter.plugin.common.MethodCall;
import
io.flutter.plugin.common.MethodChannel
;
import
io.flutter.plugin.common.MethodChannel.MethodCallHandler
;
import
io.flutter.plugin.common.MethodChannel.Result
;
import
io.flutter.plugin.common.PluginRegistry
;
import
io.flutter.plugin.common.PluginRegistry.Registrar
;
/** InAppBrowser */
public
class
InAppBrowser
implements
MethodCallHandler
{
/** InAppBrowser
FlutterPlugin
*/
public
class
InAppBrowser
FlutterPlugin
implements
MethodCallHandler
{
public
static
Registrar
registrar
;
public
Activity
activity
;
...
...
@@ -56,10 +58,10 @@ public class InAppBrowser implements MethodCallHandler {
public
static
WebViewActivity
webViewActivity
;
private
static
final
String
NULL
=
"null"
;
protected
static
final
String
LOG_TAG
=
"InAppBrowser"
;
protected
static
final
String
LOG_TAG
=
"InAppBrowser
FlutterP
"
;
public
InAppBrowser
(
Registrar
r
,
Activity
activity
)
{
public
InAppBrowser
FlutterPlugin
(
Registrar
r
,
Activity
activity
)
{
registrar
=
r
;
this
.
activity
=
activity
;
channel
=
new
MethodChannel
(
registrar
.
messenger
(),
"com.pichillilorenzo/flutter_inappbrowser"
);
...
...
@@ -68,7 +70,7 @@ public class InAppBrowser implements MethodCallHandler {
/** Plugin registration. */
public
static
void
registerWith
(
Registrar
registrar
)
{
final
MethodChannel
channel
=
new
MethodChannel
(
registrar
.
messenger
(),
"com.pichillilorenzo/flutter_inappbrowser"
);
channel
.
setMethodCallHandler
(
new
InAppBrowser
(
registrar
,
registrar
.
activity
()));
channel
.
setMethodCallHandler
(
new
InAppBrowser
FlutterPlugin
(
registrar
,
registrar
.
activity
()));
}
@RequiresApi
(
api
=
Build
.
VERSION_CODES
.
KITKAT
)
...
...
@@ -111,9 +113,9 @@ public class InAppBrowser implements MethodCallHandler {
Log
.
e
(
LOG_TAG
,
"Error dialing "
+
url
+
": "
+
e
.
toString
());
}
}
// load in InAppBrowser
// load in InAppBrowser
FlutterPlugin
else
{
Log
.
d
(
LOG_TAG
,
"loading in InAppBrowser"
);
Log
.
d
(
LOG_TAG
,
"loading in InAppBrowser
FlutterPlugin
"
);
open
(
url
,
options
);
}
}
...
...
@@ -141,26 +143,25 @@ public class InAppBrowser implements MethodCallHandler {
break
;
case
"injectScriptCode"
:
source
=
call
.
argument
(
"source"
).
toString
();
jsWrapper
=
"(function(){JSON.stringify([eval(%s)])})()"
;
injectDeferredObject
(
source
,
jsWrapper
);
result
.
success
(
true
);
jsWrapper
=
"(function(){return JSON.stringify(eval(%s));})();"
;
injectDeferredObject
(
source
,
jsWrapper
,
result
);
break
;
case
"injectScriptFile"
:
urlFile
=
call
.
argument
(
"urlFile"
).
toString
();
jsWrapper
=
"(function(d) { var c = d.createElement('script'); c.src = %s; d.body.appendChild(c); })(document)"
;
injectDeferredObject
(
urlFile
,
jsWrapper
);
jsWrapper
=
"(function(d) { var c = d.createElement('script'); c.src = %s; d.body.appendChild(c); })(document)
;
"
;
injectDeferredObject
(
urlFile
,
jsWrapper
,
null
);
result
.
success
(
true
);
break
;
case
"injectStyleCode"
:
source
=
call
.
argument
(
"source"
).
toString
();
jsWrapper
=
"(function(d) { var c = d.createElement('style'); c.innerHTML = %s; d.body.appendChild(c); })(document)"
;
injectDeferredObject
(
source
,
jsWrapper
);
jsWrapper
=
"(function(d) { var c = d.createElement('style'); c.innerHTML = %s; d.body.appendChild(c); })(document)
;
"
;
injectDeferredObject
(
source
,
jsWrapper
,
null
);
result
.
success
(
true
);
break
;
case
"injectStyleFile"
:
urlFile
=
call
.
argument
(
"urlFile"
).
toString
();
jsWrapper
=
"(function(d) { var c = d.createElement('link'); c.rel='stylesheet'; c.type='text/css'; c.href = %s; d.head.appendChild(c); })(document)"
;
injectDeferredObject
(
urlFile
,
jsWrapper
);
jsWrapper
=
"(function(d) { var c = d.createElement('link'); c.rel='stylesheet'; c.type='text/css'; c.href = %s; d.head.appendChild(c); })(document)
;
"
;
injectDeferredObject
(
urlFile
,
jsWrapper
,
null
);
result
.
success
(
true
);
break
;
case
"show"
:
...
...
@@ -203,7 +204,7 @@ public class InAppBrowser implements MethodCallHandler {
}
/**
* Inject an object (script or style) into the InAppBrowser WebView.
* Inject an object (script or style) into the InAppBrowser
FlutterPlugin
WebView.
*
* This is a helper method for the inject{Script|Style}{Code|File} API calls, which
* provides a consistent method for injecting JavaScript code into the document.
...
...
@@ -211,15 +212,14 @@ public class InAppBrowser implements MethodCallHandler {
* If a wrapper string is supplied, then the source string will be JSON-encoded (adding
* quotes) and wrapped using string formatting. (The wrapper string should have a single
* '%s' marker)
*
* @param source The source object (filename or script/style text) to inject into
* the document.
* @param jsWrapper A JavaScript string to wrap the source string in, so that the object
* is properly injected, or null if the source string is JavaScript text
*
which should be executed directly.
*
@param result
*/
private
void
injectDeferredObject
(
String
source
,
String
jsWrapper
)
{
if
(
webViewActivity
!=
null
)
{
private
void
injectDeferredObject
(
String
source
,
String
jsWrapper
,
final
Result
result
)
{
if
(
webViewActivity
!=
null
)
{
String
scriptToInject
;
if
(
jsWrapper
!=
null
)
{
org
.
json
.
JSONArray
jsonEsc
=
new
org
.
json
.
JSONArray
();
...
...
@@ -232,14 +232,46 @@ public class InAppBrowser implements MethodCallHandler {
}
final
String
finalScriptToInject
=
scriptToInject
;
activity
.
runOnUiThread
(
new
Runnable
()
{
@SuppressLint
(
"NewApi"
)
@Override
public
void
run
()
{
if
(
Build
.
VERSION
.
SDK_INT
<
Build
.
VERSION_CODES
.
KITKAT
)
{
// This action will have the side-effect of blurring the currently focused element
webViewActivity
.
webView
.
loadUrl
(
"javascript:"
+
finalScriptToInject
);
}
else
{
webViewActivity
.
webView
.
evaluateJavascript
(
finalScriptToInject
,
null
);
webViewActivity
.
webView
.
evaluateJavascript
(
finalScriptToInject
,
new
ValueCallback
<
String
>()
{
@Override
public
void
onReceiveValue
(
String
s
)
{
if
(
result
==
null
)
return
;
JsonReader
reader
=
new
JsonReader
(
new
StringReader
(
s
));
// Must set lenient to parse single values
reader
.
setLenient
(
true
);
try
{
String
msg
;
msg
=
reader
.
nextString
();
JsonReader
reader2
=
new
JsonReader
(
new
StringReader
(
msg
));
reader2
.
setLenient
(
true
);
if
(
reader2
.
peek
()
==
JsonToken
.
STRING
)
msg
=
reader2
.
nextString
();
result
.
success
(
msg
);
}
catch
(
IOException
e
)
{
Log
.
e
(
LOG_TAG
,
"IOException"
,
e
);
}
finally
{
try
{
reader
.
close
();
}
catch
(
IOException
e
)
{
// NOOP
}
}
}
});
}
}
});
...
...
@@ -279,7 +311,7 @@ public class InAppBrowser implements MethodCallHandler {
activity
.
startActivity
(
intent
);
// not catching FileUriExposedException explicitly because buildtools<24 doesn't know about it
}
catch
(
java
.
lang
.
RuntimeException
e
)
{
Log
.
d
(
LOG_TAG
,
"InAppBrowser: Error loading url "
+
url
+
":"
+
e
.
toString
());
Log
.
d
(
LOG_TAG
,
"InAppBrowser
FlutterPlugin
: Error loading url "
+
url
+
":"
+
e
.
toString
());
}
}
...
...
android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppBrowserWebViewClient.java
View file @
4ebdba09
...
...
@@ -104,7 +104,7 @@ public class InAppBrowserWebViewClient extends WebViewClient {
Map
<
String
,
Object
>
obj
=
new
HashMap
<>();
obj
.
put
(
"url"
,
url
);
InAppBrowser
.
channel
.
invokeMethod
(
"loadstart"
,
obj
);
InAppBrowser
FlutterPlugin
.
channel
.
invokeMethod
(
"loadstart"
,
obj
);
}
...
...
@@ -114,7 +114,7 @@ public class InAppBrowserWebViewClient extends WebViewClient {
activity
.
isLoading
=
false
;
// CB-10395 InAppBrowser's WebView not storing cookies reliable to local device storage
// CB-10395 InAppBrowser
FlutterPlugin
's WebView not storing cookies reliable to local device storage
if
(
android
.
os
.
Build
.
VERSION
.
SDK_INT
>=
android
.
os
.
Build
.
VERSION_CODES
.
LOLLIPOP
)
{
CookieManager
.
getInstance
().
flush
();
}
else
{
...
...
@@ -127,7 +127,7 @@ public class InAppBrowserWebViewClient extends WebViewClient {
Map
<
String
,
Object
>
obj
=
new
HashMap
<>();
obj
.
put
(
"url"
,
url
);
InAppBrowser
.
channel
.
invokeMethod
(
"loadstop"
,
obj
);
InAppBrowser
FlutterPlugin
.
channel
.
invokeMethod
(
"loadstop"
,
obj
);
}
public
void
onReceivedError
(
WebView
view
,
int
errorCode
,
String
description
,
String
failingUrl
)
{
...
...
@@ -139,7 +139,7 @@ public class InAppBrowserWebViewClient extends WebViewClient {
obj
.
put
(
"url"
,
failingUrl
);
obj
.
put
(
"code"
,
errorCode
);
obj
.
put
(
"message"
,
description
);
InAppBrowser
.
channel
.
invokeMethod
(
"loaderror"
,
obj
);
InAppBrowser
FlutterPlugin
.
channel
.
invokeMethod
(
"loaderror"
,
obj
);
}
/**
...
...
android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/RequestPermissionHandler.java
View file @
4ebdba09
...
...
@@ -13,8 +13,6 @@ import java.util.Map;
public
abstract
class
RequestPermissionHandler
implements
ActivityCompat
.
OnRequestPermissionsResultCallback
{
public
static
int
REQUEST_CODE_WRITE_EXTERNAL_STORAGE
=
0
;
private
static
Map
<
Integer
,
List
<
Runnable
>>
actionDictionary
=
new
HashMap
<>();
public
static
void
checkAndRun
(
Activity
activity
,
String
permission
,
int
requestCode
,
Runnable
runnable
)
{
...
...
android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/WebViewActivity.java
View file @
4ebdba09
package
com.pichillilorenzo.flutter_inappbrowser
;
import
android.Manifest
;
import
android.app.Activity
;
import
android.app.DownloadManager
;
import
android.content.Context
;
import
android.content.Intent
;
import
android.content.pm.PackageManager
;
import
android.graphics.Color
;
import
android.graphics.drawable.ColorDrawable
;
import
android.net.Uri
;
import
android.os.Build
;
import
android.os.Environment
;
import
android.support.annotation.RequiresApi
;
import
android.support.v4.app.ActivityCompat
;
import
android.support.v4.content.ContextCompat
;
import
android.support.v7.app.ActionBar
;
import
android.support.v7.app.AppCompatActivity
;
import
android.os.Bundle
;
import
android.util.Log
;
import
android.view.KeyEvent
;
import
android.view.Menu
;
import
android.view.MenuInflater
;
import
android.view.MenuItem
;
import
android.view.View
;
import
android.webkit.CookieManager
;
import
android.webkit.DownloadListener
;
import
android.webkit.URLUtil
;
import
android.webkit.ValueCallback
;
import
android.webkit.WebSettings
;
import
android.webkit.WebView
;
import
android.widget.AutoCompleteTextView
;
import
android.widget.ImageView
;
import
android.widget.ProgressBar
;
import
android.widget.SearchView
;
import
android.widget.Toast
;
import
java.util.HashMap
;
import
java.util.Map
;
...
...
@@ -64,7 +48,7 @@ public class WebViewActivity extends AppCompatActivity {
options
=
new
InAppBrowserOptions
();
options
.
parse
((
HashMap
<
String
,
Object
>)
b
.
getSerializable
(
"options"
));
InAppBrowser
.
webViewActivity
=
this
;
InAppBrowser
FlutterPlugin
.
webViewActivity
=
this
;
actionBar
=
getSupportActionBar
();
...
...
@@ -82,39 +66,39 @@ public class WebViewActivity extends AppCompatActivity {
inAppBrowserWebViewClient
=
new
InAppBrowserWebViewClient
(
this
);
webView
.
setWebViewClient
(
inAppBrowserWebViewClient
);
final
Activity
activity
=
this
;
webView
.
setDownloadListener
(
new
DownloadListener
()
{
@Override
public
void
onDownloadStart
(
final
String
url
,
final
String
userAgent
,
final
String
contentDisposition
,
final
String
mimetype
,
final
long
contentLength
)
{
RequestPermissionHandler
.
checkAndRun
(
activity
,
Manifest
.
permission
.
WRITE_EXTERNAL_STORAGE
,
RequestPermissionHandler
.
REQUEST_CODE_WRITE_EXTERNAL_STORAGE
,
new
Runnable
(){
@Override
public
void
run
(){
DownloadManager
.
Request
request
=
new
DownloadManager
.
Request
(
Uri
.
parse
(
url
));
final
String
filename
=
URLUtil
.
guessFileName
(
url
,
contentDisposition
,
mimetype
);
request
.
allowScanningByMediaScanner
();
request
.
setNotificationVisibility
(
DownloadManager
.
Request
.
VISIBILITY_VISIBLE_NOTIFY_COMPLETED
);
//Notify client once download is completed!
request
.
setVisibleInDownloadsUi
(
true
);
request
.
setDestinationInExternalPublicDir
(
Environment
.
DIRECTORY_DOWNLOADS
,
filename
);
DownloadManager
dm
=
(
DownloadManager
)
getSystemService
(
DOWNLOAD_SERVICE
);
if
(
dm
!=
null
)
{
dm
.
enqueue
(
request
);
Toast
.
makeText
(
getApplicationContext
(),
"Downloading File: "
+
filename
,
//To notify the Client that the file is being downloaded
Toast
.
LENGTH_LONG
).
show
();
}
else
{
Toast
.
makeText
(
getApplicationContext
(),
"Cannot Download File: "
+
filename
,
//To notify the Client that the file cannot be downloaded
Toast
.
LENGTH_LONG
).
show
();
}
}
});
}
});
//
final Activity activity = this;
//
//
webView.setDownloadListener(new DownloadListener() {
//
@Override
//
public void onDownloadStart(final String url, final String userAgent,
//
final String contentDisposition, final String mimetype,
//
final long contentLength) {
//
//
RequestPermissionHandler.checkAndRun(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE, RequestPermissionHandler.REQUEST_CODE_WRITE_EXTERNAL_STORAGE, new Runnable(){
//
@Override
//
public void run(){
//
DownloadManager.Request request = new DownloadManager.Request(
//
Uri.parse(url));
//
//
final String filename = URLUtil.guessFileName(url, contentDisposition, mimetype);
//
request.allowScanningByMediaScanner();
//
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed!
//
request.setVisibleInDownloadsUi(true);
//
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename);
//
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
//
if (dm != null) {
//
dm.enqueue(request);
//
Toast.makeText(getApplicationContext(), "Downloading File: " + filename, //To notify the Client that the file is being downloaded
//
Toast.LENGTH_LONG).show();
//
}
//
else {
//
Toast.makeText(getApplicationContext(), "Cannot Download File: " + filename, //To notify the Client that the file cannot be downloaded
//
Toast.LENGTH_LONG).show();
//
}
//
}
//
});
//
}
//
});
WebSettings
settings
=
webView
.
getSettings
();
...
...
@@ -261,18 +245,6 @@ public class WebViewActivity extends AppCompatActivity {
return
super
.
onKeyDown
(
keyCode
,
event
);
}
// @TargetApi(Build.VERSION_CODES.KITKAT)
// void eval(MethodCall call, final MethodChannel.Result result) {
// String code = call.argument("code");
//
// webView.evaluateJavascript(code, new ValueCallback<String>() {
// @Override
// public void onReceiveValue(String value) {
// result.success(value);
// }
// });
// }
public
void
close
()
{
finish
();
}
...
...
example/ios/Podfile
View file @
4ebdba09
...
...
@@ -54,6 +54,7 @@ target 'Runner' do
File
.
symlink
(
p
[
:path
],
symlink
)
pod
p
[
:name
],
:path
=>
File
.
join
(
symlink
,
'ios'
)
}
end
post_install
do
|
installer
|
...
...
example/ios/Runner.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings
0 → 100644
View file @
4ebdba09
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist
version=
"1.0"
>
<dict>
<key>
BuildSystemType
</key>
<string>
Original
</string>
</dict>
</plist>
example/lib/main.dart
View file @
4ebdba09
...
...
@@ -11,9 +11,24 @@ class MyInAppBrowser extends InAppBrowser {
}
@override
void
onLoadStop
(
String
url
)
{
void
onLoadStop
(
String
url
)
async
{
super
.
onLoadStop
(
url
);
print
(
"
\n\n
Stopped
$url
\n\n
"
);
print
(
await
this
.
injectScriptCode
(
"document.body.innerHTML"
));
print
(
await
this
.
injectScriptCode
(
"3"
));
print
(
await
this
.
injectScriptCode
(
"""
function asd (a,b) {
return a+b;
};
asd(3,5);
"""
));
print
(
await
this
.
injectScriptCode
(
"""
["
3
",56,"
sdf
"];
"""
));
print
(
await
this
.
injectScriptCode
(
"""
var x = {"
as
":4, "
dfdfg
": 6};
x;
"""
));
/*this.injectScriptFile("https://code.jquery.com/jquery-3.3.1.min.js");
this.injectScriptCode("""
\$( "body" ).html( "Next Step..." )
...
...
@@ -68,7 +83,7 @@ class _MyAppState extends State<MyApp> {
child:
new
RaisedButton
(
onPressed:
()
{
inAppBrowser
.
open
(
"https://flutter.io/"
,
options:
{
//"toolbarTopFixedTitle": "Fixed title",
"hideUrlBar"
:
true
,
//
"hideUrlBar": true,
//"toolbarTop": false,
//"toolbarBottom": false
});
...
...
flutter_inappbrowser.iml
View file @
4ebdba09
...
...
@@ -14,6 +14,7 @@
<excludeFolder
url=
"file://$MODULE_DIR$/example/flutter_plugin/.dart_tool"
/>
<excludeFolder
url=
"file://$MODULE_DIR$/example/flutter_plugin/.pub"
/>
<excludeFolder
url=
"file://$MODULE_DIR$/example/flutter_plugin/build"
/>
<excludeFolder
url=
"file://$MODULE_DIR$/example/flutter_plugin/example/ios/Flutter/flutter_assets/packages"
/>
<excludeFolder
url=
"file://$MODULE_DIR$/example/ios/Flutter/flutter_assets/packages"
/>
</content>
<orderEntry
type=
"sourceFolder"
forTests=
"false"
/>
...
...
ios/Classes/InAppBrowser.h
→
ios/Classes/InAppBrowser
FlutterPlugin
.h
View file @
4ebdba09
...
...
@@ -17,7 +17,7 @@
#import <Flutter/Flutter.h>
@interface
InAppBrowser
:
NSObject
<
FlutterPlugin
>
@interface
InAppBrowser
FlutterPlugin
:
NSObject
<
FlutterPlugin
>
+
(
void
)
registerWithRegistrar
:(
nonnull
NSObject
<
FlutterPluginRegistrar
>
*
)
registrar
;
@end
ios/Classes/InAppBrowser.m
→
ios/Classes/InAppBrowser
FlutterPlugin
.m
View file @
4ebdba09
...
...
@@ -15,10 +15,10 @@
under the License.
*/
#import "InAppBrowser.h"
#import "InAppBrowser
FlutterPlugin
.h"
#import <flutter_inappbrowser/flutter_inappbrowser-Swift.h>
@implementation
InAppBrowser
:
NSObject
@implementation
InAppBrowser
FlutterPlugin
:
NSObject
+
(
void
)
registerWithRegistrar
:(
NSObject
<
FlutterPluginRegistrar
>*
)
registrar
{
[
SwiftFlutterPlugin
registerWithRegistrar
:
registrar
];
}
...
...
ios/Classes/InAppBrowserWebViewController.swift
View file @
4ebdba09
...
...
@@ -433,6 +433,40 @@ class InAppBrowserWebViewController: UIViewController, WKUIDelegate, WKNavigatio
decisionHandler
(
.
allow
)
}
// func webView(_ webView: WKWebView,
// decidePolicyFor navigationResponse: WKNavigationResponse,
// decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {
// let mimeType = navigationResponse.response.mimeType
// if mimeType != nil && !mimeType!.starts(with: "text/") {
// download(url: webView.url)
// decisionHandler(.cancel)
// return
// }
// decisionHandler(.allow)
// }
//
// func download (url: URL?) {
// let filename = url?.lastPathComponent
//
// let destination: DownloadRequest.DownloadFileDestination = { _, _ in
// let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
// let fileURL = documentsURL.appendingPathComponent(filename!)
//
// return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
// }
//
// Alamofire.download((url?.absoluteString)!, to: destination).downloadProgress { progress in
// print("Download Progress: \(progress.fractionCompleted)")
// }.response { response in
// if response.error == nil, let path = response.destinationURL?.path {
// UIAlertView(title: nil, message: "File saved to " + path, delegate: nil, cancelButtonTitle: nil).show()
// }
// else {
// UIAlertView(title: nil, message: "Cannot save " + filename!, delegate: nil, cancelButtonTitle: nil).show()
// }
// }
// }
func
webView
(
_
webView
:
WKWebView
,
didStartProvisionalNavigation
navigation
:
WKNavigation
!
)
{
// loading url, start spinner, update back/forward
backButton
.
isEnabled
=
webView
.
canGoBack
...
...
ios/Classes/SwiftFlutterPlugin.swift
View file @
4ebdba09
...
...
@@ -88,22 +88,22 @@ public class SwiftFlutterPlugin: NSObject, FlutterPlugin {
result
(
true
)
break
case
"injectScriptCode"
:
self
.
injectScriptCode
(
arguments
:
arguments
!
)
result
(
true
)
self
.
injectScriptCode
(
arguments
:
arguments
!
,
result
:
result
)
break
case
"injectScriptFile"
:
self
.
injectScriptFile
(
arguments
:
arguments
!
)
self
.
injectScriptFile
(
arguments
:
arguments
!
,
result
:
nil
)
result
(
true
)
break
case
"injectStyleCode"
:
self
.
injectStyleCode
(
arguments
:
arguments
!
)
self
.
injectStyleCode
(
arguments
:
arguments
!
,
result
:
nil
)
result
(
true
)
break
case
"injectStyleFile"
:
self
.
injectStyleFile
(
arguments
:
arguments
!
)
self
.
injectStyleFile
(
arguments
:
arguments
!
,
result
:
nil
)
result
(
true
)
break
default
:
result
(
FlutterMethodNotImplemented
)
break
}
}
...
...
@@ -151,7 +151,7 @@ public class SwiftFlutterPlugin: NSObject, FlutterPlugin {
}
else
{
print
(
"url is empty"
)
result
(
false
)
result
(
FlutterError
(
code
:
"InAppBrowserFlutterPlugin"
,
message
:
"url is empty"
,
details
:
nil
)
)
}
result
(
true
)
}
...
...
@@ -166,7 +166,7 @@ public class SwiftFlutterPlugin: NSObject, FlutterPlugin {
}
else
{
print
(
"url is empty"
)
result
(
false
)
result
(
FlutterError
(
code
:
"InAppBrowserFlutterPlugin"
,
message
:
"url is empty"
,
details
:
nil
)
)
}
result
(
true
)
}
...
...
@@ -265,39 +265,54 @@ public class SwiftFlutterPlugin: NSObject, FlutterPlugin {
// '%@' marker).
//
// If no wrapper is supplied, then the source string is executed directly.
func
injectDeferredObject
(
_
source
:
String
,
withWrapper
jsWrapper
:
String
)
{
//if jsWrapper != nil {
func
injectDeferredObject
(
_
source
:
String
,
withWrapper
jsWrapper
:
String
,
result
:
FlutterResult
?)
{
let
jsonData
:
Data
?
=
try
?
JSONSerialization
.
data
(
withJSONObject
:
[
source
],
options
:
[])
let
sourceArrayString
=
String
(
data
:
jsonData
!
,
encoding
:
String
.
Encoding
.
utf8
)
if
sourceArrayString
!=
nil
{
let
sourceString
:
String
?
=
(
sourceArrayString
!
as
NSString
)
.
substring
(
with
:
NSRange
(
location
:
1
,
length
:
(
sourceArrayString
?
.
characters
.
count
??
0
)
-
2
))
let
jsToInject
=
String
(
format
:
jsWrapper
,
sourceString
!
)
webViewController
?
.
webView
?
.
evaluateJavaScript
(
jsToInject
)
webViewController
?
.
webView
?
.
evaluateJavaScript
(
jsToInject
,
completionHandler
:
{(
value
,
error
)
in
if
result
==
nil
{
return
}
do
{
let
data
:
Data
=
(
"["
+
String
(
describing
:
value
!
)
+
"]"
)
.
data
(
using
:
String
.
Encoding
.
utf8
,
allowLossyConversion
:
false
)
!
let
json
:
Array
<
Any
>
=
try
JSONSerialization
.
jsonObject
(
with
:
data
,
options
:
[])
as!
Array
<
Any
>
if
json
[
0
]
is
String
{
result
!
(
json
[
0
])
}
else
{
result
!
(
value
)
}
}
catch
let
error
as
NSError
{
print
(
"Failed to load:
\(
error
.
localizedDescription
)
"
)
result
!
(
FlutterError
(
code
:
"InAppBrowserFlutterPlugin"
,
message
:
"Failed to load:
\(
error
.
localizedDescription
)
"
,
details
:
error
))
}
})
}
//}
//else {
// webViewController?.webView?.evaluateJavaScript(source)
//}
}
public
func
injectScriptCode
(
arguments
:
NSDictionary
)
{
let
jsWrapper
=
"(function(){
JSON.stringify([eval(%@)])})()
"
injectDeferredObject
(
arguments
[
"source"
]
as!
String
,
withWrapper
:
jsWrapper
)
public
func
injectScriptCode
(
arguments
:
NSDictionary
,
result
:
FlutterResult
?
)
{
let
jsWrapper
=
"(function(){
return JSON.stringify(eval(%@));})();
"
injectDeferredObject
(
arguments
[
"source"
]
as!
String
,
withWrapper
:
jsWrapper
,
result
:
result
)
}
public
func
injectScriptFile
(
arguments
:
NSDictionary
)
{
let
jsWrapper
=
"(function(d) { var c = d.createElement('script'); c.src = %@; d.body.appendChild(c); })(document)"
injectDeferredObject
(
arguments
[
"urlFile"
]
as!
String
,
withWrapper
:
jsWrapper
)
public
func
injectScriptFile
(
arguments
:
NSDictionary
,
result
:
FlutterResult
?
)
{
let
jsWrapper
=
"(function(d) { var c = d.createElement('script'); c.src = %@; d.body.appendChild(c); })(document)
;
"
injectDeferredObject
(
arguments
[
"urlFile"
]
as!
String
,
withWrapper
:
jsWrapper
,
result
:
result
)
}
public
func
injectStyleCode
(
arguments
:
NSDictionary
)
{
let
jsWrapper
=
"(function(d) { var c = d.createElement('style'); c.innerHTML = %@; d.body.appendChild(c); })(document)"
injectDeferredObject
(
arguments
[
"source"
]
as!
String
,
withWrapper
:
jsWrapper
)
public
func
injectStyleCode
(
arguments
:
NSDictionary
,
result
:
FlutterResult
?
)
{
let
jsWrapper
=
"(function(d) { var c = d.createElement('style'); c.innerHTML = %@; d.body.appendChild(c); })(document)
;
"
injectDeferredObject
(
arguments
[
"source"
]
as!
String
,
withWrapper
:
jsWrapper
,
result
:
result
)
}
public
func
injectStyleFile
(
arguments
:
NSDictionary
)
{
let
jsWrapper
=
"(function(d) { var c = d.createElement('link'); c.rel='stylesheet', c.type='text/css'; c.href = %@; d.body.appendChild(c); })(document)"
injectDeferredObject
(
arguments
[
"urlFile"
]
as!
String
,
withWrapper
:
jsWrapper
)
public
func
injectStyleFile
(
arguments
:
NSDictionary
,
result
:
FlutterResult
?
)
{
let
jsWrapper
=
"(function(d) { var c = d.createElement('link'); c.rel='stylesheet', c.type='text/css'; c.href = %@; d.body.appendChild(c); })(document)
;
"
injectDeferredObject
(
arguments
[
"urlFile"
]
as!
String
,
withWrapper
:
jsWrapper
,
result
:
result
)
}
func
webViewDidStartLoad
(
_
webView
:
WKWebView
)
{
...
...
lib/flutter_inappbrowser.dart
View file @
4ebdba09
...
...
@@ -174,7 +174,7 @@ class InAppBrowser {
}
///Injects JavaScript code into the [InAppBrowser] window. (Only available when the target is set to `_blank` or to `_self`)
Future
<
void
>
injectScriptCode
(
String
source
)
async
{
Future
<
dynamic
>
injectScriptCode
(
String
source
)
async
{
Map
<
String
,
dynamic
>
args
=
<
String
,
dynamic
>{};
args
.
putIfAbsent
(
'source'
,
()
=>
source
);
return
await
_channel
.
invokeMethod
(
'injectScriptCode'
,
args
);
...
...
@@ -221,9 +221,4 @@ class InAppBrowser {
}
///
void
onCustomScheme
(
String
url
)
{
}
}
pubspec.yaml
View file @
4ebdba09
...
...
@@ -18,7 +18,7 @@ dependencies:
flutter
:
plugin
:
androidPackage
:
com.pichillilorenzo.flutter_inappbrowser
pluginClass
:
InAppBrowser
pluginClass
:
InAppBrowser
FlutterPlugin
# To add assets to your plugin package, add an assets section, like this:
# assets:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment