UIWebView acts differnetly in app store version th

2019-08-14 07:11发布

问题:

I've got a problem with an app that works perfectly in the simulator as well as a physical iPhone 4 and an iPhone 3GS. The app was approved and is now in the App Store, but the distribution build downloaded from the App Store exhibits a bug not seen in the dev/release build.

This is a free app, but is supported by local advertising. When the app launches (or returns from background), the AppDelegate attempts to download some HTML from our ad server, and if successful, presents a modal view controller with a UIWebView and passes an NSData variable containing the HTML. In development/release builds, this works PERFECTLY; the app launches, and after a few seconds, a view slides up and shows the ad, which can be dismissed with a button.

However distribution build from the App Store is different. When the modal view controller slides up, the UIWebView never loads. Remember, I present the view controller ONLY if able to download the ad data -- otherwise, the view is never presented.

Thankfully I implemented a timer in the ad view controller which will cause the modal view to dismiss itself if the webViewDidFinishLoad never fires (in which the timer is invalidated), so at least app users aren't too annoyed. But it's still ugly to have an empty view controller slide up and then slide away for apparently no reason.

Here's the relevant methods in the AppDelegate:

- (void)launchAd
{
    [NetworkActivity showFor:@"ad"];

    if (!alreadyActive && [ServerCheck serverReachable:@"openx.freewave-wifi.com" hideAlert:YES])
    {  
        alreadyActive = YES;

        [self performSelectorInBackground:@selector(downloadAdData) withObject:nil];
    }

    [NetworkActivity hideFor:@"ad"];
}

- (void)downloadAdData
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSString *baseURL = @"http://appdata.freewave-wifi.com/ad/";

    NSString *file = (IS_IPAD) ? @"ipad.php" : @"iphone.php";

    NSURL *adURL = [NSURL URLWithString:[baseURL stringByAppendingString:file]];

    adData = [[NSData alloc] initWithContentsOfURL:adURL];

    [self performSelectorOnMainThread:@selector(presentAdModal) withObject:nil waitUntilDone:NO];

    [pool release];
}

- (void)presentAdModal
{
    if (adData)
    {
        AdViewController *adView = [[AdViewController alloc] initWithNibName:nil bundle:nil];
        [adView setAdData:adData];

        UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:adView];


        [navController setModalPresentationStyle:UIModalPresentationFormSheet];
        [navController setModalTransitionStyle:UIModalTransitionStyleCoverVertical];

        [tabBarController presentModalViewController:navController animated:YES];

        [navController release], navController = nil;
        [adView release], adView = nil;
    }
    else
        LogError(@"Not presenting ad; unable to create data object.");
}

By the way, adData is defined in header with NSData *adData;

The AdViewController simply contains a UIWebView, which is loaded with

[webView loadData:adData MIMEType:@"text/html" textEncodingName:@"utf-8" baseURL:nil];

Again, this all works PERFECTLY, EVERY TIME with dev/release builds in simulator and physical devices -- just not on distribution build from app store. I have even converted the NSData to an NSString and barfed it out with NSLog() just to prove that the HTML was downloaded before presenting the AdView modally.

[sigh...]

EDIT 1: In case my original post was not clear, the webViewDidFinishLoad never gets called in distribution build (but it does in dev/release build).

EDIT 2: Also, just before I call

[webView loadData:adData MIMEType:@"text/html" textEncodingName:@"utf-8" baseURL:nil];

in the AdViewController, I added a temporary NSLog() and converted adData to NSString and logged it to the console, and the HTML was there. So, the UIWebView just refuses to load the NSData?

回答1:

HOLY COW. I figure it out.

Okay, before I say what I found, I did want to correct my own original wording: the modal ad has never worked in the simulator, but always on devices. I know the simulator can have its quirks, so I never thought anything of it, especially since it always worked on the devices. I know this is an important detail that was missing for this discussion, but it's been a couple of weeks since I worked on this project, and I'd forgotten all about it until today.

Now then... While tinkering with things, I noticed the AdView.xib was not in my project file list. I expanded a few folders thinking maybe it was accidentally dragged into one of them, but it was not listed at all. This really has me puzzled, though -- Xcode NEVER complained about a missing resource (no warnings or errors; always a perfect compile).

So, I navigated to the physical location and added the AdView.xib into the project. Now, the modal ad is displayed in the simulator, which is a first. I figure that since now the app works correctly in the simulator, it should work fine in the distribution build (odd correlation to make, but it's all I got until my update hits the App Store).

Obviously, I'll be submitting an update, so I won't accept my own answer until after the update hits the App Store (assuming I have actually fixed it).



回答2:

Ok, this is an extremely long shot, but perhaps worth considering.

The docs for NSData state that with regards to initWithContentsOfURL "The returned object might be different than the original receiver." So, if it was a different object, and one which was in fact autoreleased, consider this line in your code:

adData = [[NSData alloc] initWithContentsOfURL:adURL];

This won't add a retain count for adData -- you didn't write self.adData = or similar. So, bearing in mind the scenario mentioned whereby the returned NSData was autoreleased: your method downloadAdData wraps its content in an NSAutoreleasePool. This is correct practice. However, that might result in adData being released BEFORE presentAdModal is called on the main thread. So...

In presentAdModal you just check that adData isn't nil -- but it can be not nil, and still have been deallocated from memory at that point by your NSAutoreleasePool -- hence, you would in this situation trigger the "show web view" code, but be attempting to load an NSData object that had been trashed. Which probably would contain complete garbage, hence no successful "web view loaded" call.

As I said, a long shot, but the ony thing that jumps out at me at this point.

UPDATE:

A completely different cause of your problem might be this:

Your test environment (i.e. non App-Store builds) is making requests from a certain part of the internet (i.e. your office) which has permission to access the web server containing ads, due to either IP blocking or whatever network setup there is, whereas your App Store release builds are attempting to access the ad server from parts of the internet which are forbidden. Again, probably not the case, but worth mentioning.