Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ios] Unexpectedly found nil while implicitly unwrapping an Optional value #56

Closed
furkansarihan opened this issue Jul 2, 2020 · 3 comments · Fixed by #58
Closed

[ios] Unexpectedly found nil while implicitly unwrapping an Optional value #56

furkansarihan opened this issue Jul 2, 2020 · 3 comments · Fixed by #58

Comments

@furkansarihan
Copy link

furkansarihan commented Jul 2, 2020

This error caused to app crash running on both emulator and real ios device with internet connection.
App doesn’t crash without internet. It happens after ad request.

nativeAd object isn’t nil.

What was happened?

Screen Shot 2020-07-02 at 15 32 53

flutter doctor -v
[✓] Flutter (Channel beta, 1.18.0-11.1.pre, on Mac OS X 10.15.4 19E287, locale
    en)
    • Flutter version 1.18.0-11.1.pre at /Users/fusarihan/dev/flutter
    • Framework revision 2738a1148b (7 weeks ago), 2020-05-13 15:24:36 -0700
    • Engine revision ef9215ceb2
    • Dart version 2.9.0 (build 2.9.0-8.2.beta)

 
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.0)
    • Android SDK at /Users/fusarihan/Library/Android/sdk
    • Platform android-30, build-tools 30.0.0
    • Java binary at: /Applications/Android
      Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build
      1.8.0_242-release-1644-b3-6222593)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 11.5)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 11.5, Build version 11E608c
    • CocoaPods version 1.9.3

[✓] Android Studio (version 4.0)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin version 47.0.2
    • Dart plugin version 193.7361
    • Java version OpenJDK Runtime Environment (build
      1.8.0_242-release-1644-b3-6222593)

[✓] VS Code (version 1.46.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.12.1

[✓] Connected device (1 available)
    • F • 1d3aa0502670fba6e59b70d4a46bcc67d4fa5c57 • ios • iOS 13.4.1

• No issues found!
@furkansarihan furkansarihan changed the title Unexpectedly found nil while implicitly unwrapping an Optional value [ios] Unexpectedly found nil while implicitly unwrapping an Optional value Jul 2, 2020
@ghost
Copy link

ghost commented Jul 10, 2020

I'm having the same issue.

Edit for FIX UPDATE:

I'm not too sure of the cause, but somehow headlineView and some of the other views are nulling out. It may have something to do with the fact that even though I've fixed the original issue, ads are still not loading. If my assumption is correct, I'll further update my reply.

Here's how you fix the "Unexpectedly found nil.." issue:

In that same file, UnifiedAdLayout.swift, on lines 24-28, you'll see each of the five lines have the "weak" property included. Remove that. I.e.:

    private weak var unifiedNativeAdView: GADUnifiedNativeAdView!
    private weak var headlineView: UILabel!
    private weak var bodyView: UILabel!
    private weak var callToActionView: UILabel!
    private weak var attributionView: UILabel!

becomes

    private var unifiedNativeAdView: GADUnifiedNativeAdView!
    private var headlineView: UILabel!
    private var bodyView: UILabel!
    private var callToActionView: UILabel!
    private var attributionView: UILabel!

Fixed!

@furkansarihan
Copy link
Author

@kevintesar Can you load ads? Maybe there is a mistake on configurations.

@melodysdreamj
Copy link

melodysdreamj commented Jul 23, 2020

just move Bundle.main.loadNibNamed part to init. then fixed this issue.

original

init(frame: CGRect, viewId: Int64, args: [String: Any], messeneger: FlutterBinaryMessenger) {
self.args = args
self.messeneger = messeneger
self.frame = frame
self.viewId = viewId
self.placementId = self.args["placement_id"] as! String
self.layoutName = self.args["layout_name"] as! String
self.attributionText = self.args["text_attribution"] as! String

    self.adLoader = GADAdLoader(adUnitID: placementId, rootViewController: nil,
                adTypes: [ .unifiedNative ], options: nil)
    channel = FlutterMethodChannel(name: "com.github.sakebook.ios/unified_ad_layout_\(viewId)", binaryMessenger: messeneger)
    super.init()
    fetchAd()
}

private func fetchAd() {
    adLoader.delegate = self
    let request = GADRequest()
    adLoader.load(request)
}

func view() -> UIView {
    guard let nibObjects = Bundle.main.loadNibNamed(layoutName, owner: nil, options: nil),
        let adView = nibObjects.first as? GADUnifiedNativeAdView else {
            fatalError("Could not load nib file for adView")
    }
    unifiedNativeAdView = adView
    headlineView = adView.headlineView as? UILabel
    bodyView = adView.bodyView as? UILabel
    callToActionView = adView.callToActionView as? UILabel
    mediaView = adView.mediaView
    guard let attributionLabel = (adView as UIView).subviews.first(where: { (v) -> Bool in
        v.restorationIdentifier == "flutter_native_ad_attribution_view_id"
    }) as? UILabel else {
        fatalError("Could not find Restoration ID 'flutter_native_ad_attribution_view_id'")
    }
    attributionView = attributionLabel
    
    iconView = adView.iconView as? UIImageView
    starRatingView = adView.starRatingView as? UILabel
    storeView = adView.storeView as? UILabel
    priceView = adView.priceView as? UILabel
    advertiserView = adView.advertiserView as? UILabel
    
    return unifiedNativeAdView
}

fixed

init(frame: CGRect, viewId: Int64, args: [String: Any], messeneger: FlutterBinaryMessenger) {
self.args = args
self.messeneger = messeneger
self.frame = frame
self.viewId = viewId
self.placementId = self.args["placement_id"] as! String
self.layoutName = self.args["layout_name"] as! String
self.attributionText = self.args["text_attribution"] as! String

    self.adLoader = GADAdLoader(adUnitID: placementId, rootViewController: nil,
                adTypes: [ .unifiedNative ], options: nil)
    channel = FlutterMethodChannel(name: "com.github.sakebook.ios/unified_ad_layout_\(viewId)", binaryMessenger: messeneger)
    super.init()
    
    guard let nibObjects = Bundle.main.loadNibNamed(layoutName, owner: nil, options: nil),
        let adView = nibObjects.first as? GADUnifiedNativeAdView else {
            fatalError("Could not load nib file for adView")
    }
    self.adView = adView;
    
    fetchAd()
}
var adView:GADUnifiedNativeAdView?

private func fetchAd() {
    adLoader.delegate = self
    let request = GADRequest()
    adLoader.load(request)
}

func view() -> UIView {
    
    unifiedNativeAdView = adView
    headlineView = adView?.headlineView as? UILabel
    bodyView = adView?.bodyView as? UILabel
    callToActionView = adView?.callToActionView as? UILabel
    mediaView = adView?.mediaView
    guard let attributionLabel = (adView! as UIView).subviews.first(where: { (v) -> Bool in
        v.restorationIdentifier == "flutter_native_ad_attribution_view_id"
    }) as? UILabel else {
        fatalError("Could not find Restoration ID 'flutter_native_ad_attribution_view_id'")
    }
    attributionView = attributionLabel
    
    iconView = adView?.iconView as? UIImageView
    starRatingView = adView?.starRatingView as? UILabel
    storeView = adView?.storeView as? UILabel
    priceView = adView?.priceView as? UILabel
    advertiserView = adView?.advertiserView as? UILabel
    
    return unifiedNativeAdView
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants