Search code examples
iosobjective-cxcodeflutterflutterwebviewplugin

Flutter flutter_webview_plugin error [NSNull length]: unrecognized selector sent to instance on ios


I am using flutter_webview_plugin: ^0.4.0 to load a website. The app works fine on Android. On IOs I am getting the error

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull length]: unrecognized selector sent to instance 0x1fb513aa0'

for the line NSURL *scopeUrl = [NSURL fileURLWithPath:localUrlScope]; in file FlutterWebviewPlugin in Xcode.

I've added

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
    <key>NSAllowsArbitraryLoadsInWebContent</key>
    <true/>
</dict>

from the docs in my Info.plist.

I tried adding

<key>io.flutter.embedded_views_preview</key>
<string>YES</string>

in my Info.plist but it didn't work. I also tried replacing <string>YES</string> with <true/> which didn't work too.

I've tried setting the target IOs versions from 9.0 to 13.0 and the issue still exists.

I am using the file picker and alert dialog features provided by this package so I cannot use any another.

I've wrapped the URL with Uri.encodeFull().

The block of code where the error occurs is as follows:

if (@available(iOS 9.0, *)) {
                    if(localUrlScope == nil) {
                        [self.webview loadFileURL:htmlUrl allowingReadAccessToURL:htmlUrl];
                    }
                    else {
                        NSURL *scopeUrl = [NSURL fileURLWithPath:localUrlScope]; //ERROR OCCURING HERE
                        [self.webview loadFileURL:htmlUrl allowingReadAccessToURL:scopeUrl];
                    }
                } else {
                    @throw @"not available on version earlier than ios 9.0";
                }

I ran flutter doctor and it didn't show any issues.

Everytime I change something in the code, I delete Podfile.lock, build folder, run flutter clean so the changes made in some previous attempts aren't messing anything.

I am using an M1 Mac with Dart 2.13.4, Flutter 2.5.0, XCode 13.0 (13A233)


Solution

  • I have had this issue earlier when I was working on a similar thing with this package, After long hours of bashing head around, I found this from package documentation,

    Focus on this portion where it says this enter image description here

    Thing is when you set withLocalUrl to true, you have to Add localUrlScope option to be set to a path to a directory in your flutter code or you just remove withLocalUrl:true from it.

    As this option is ignored in Android so it works with no issues in it but for iOS we need to be specific with this