Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIWebView iOS 8 Action Extension crash in iOS 8 mobile safari

View of iOS 8 action extension app has UIWebView. When I open this action extension in Safari and then extension app shows UIWebView of mobile Safari's URL.

But extension app crashes sometimes during loading web page or scrolling it for some web pages like nytimes.com.

I know extension app's usable memory depends on mobile Safari.

But I found that 'Awesome Screenshot for Safari' does not crash. (https://itunes.apple.com/us/app/awesome-screenshot-for-safari/id918780145)

I am wondering how to prevent action extension app from crashing.

@interface ActionViewController ()

@property(strong,nonatomic) IBOutlet UIImageView *imageView;
@property (strong, nonatomic) IBOutlet UIWebView *webView;
@end

@implementation ActionViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    NSExtensionItem *item = self.extensionContext.inputItems.firstObject;
    NSItemProvider *itemProvider = item.attachments.firstObject;

    if ([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeURL]) {

        __weak typeof(self) weakSelf = self;
        [itemProvider loadItemForTypeIdentifier:(NSString *)kUTTypeURL options:nil completionHandler:^(id<NSSecureCoding> item, NSError *error) {
            if (error) {
                [weakSelf.extensionContext cancelRequestWithError:error];
                return;
            }

            if (![(NSObject *)item isKindOfClass:[NSURL class]]) {
                NSError *unexpectedError = [NSError errorWithDomain:NSItemProviderErrorDomain
                                                               code:NSItemProviderUnexpectedValueClassError
                                                           userInfo:nil];
                [weakSelf.extensionContext cancelRequestWithError:unexpectedError];
                return;
            }

            NSURL *url = (NSURL *)item;

            [weakSelf.webView loadRequest:[NSURLRequest requestWithURL:url]];
        }];
    } else {
        NSError *unavailableError = [NSError errorWithDomain:NSItemProviderErrorDomain
                                                        code:NSItemProviderItemUnavailableError
                                                    userInfo:nil];
        [self.extensionContext cancelRequestWithError:unavailableError];
    }
}
like image 495
dobiho Avatar asked Oct 08 '14 03:10

dobiho


1 Answers

I was experiencing the same issue, using a UIWebView in the background to scrape some content for my app. It worked fine when I was connected to the debugger. But running the same build without the debugger always crashed.

I resolved it by migrating to WKWebView, which was actually pretty easy. I guess UIWebView is just too old and inefficient to be running in an extension inside of Safari. Using WKWebView worked perfectly.

I then discovered a crash when I was done using the web view and was saving my data. I was saving it to NSUserDefaults (using my group container) to pass to the main app. This was always crashing too, so I removed that code. I ended up using CoreData instead, which worked fine too.

Now I have a crash free Safari extension! :)

like image 148
Richard Venable Avatar answered Nov 03 '22 21:11

Richard Venable