0

In my application, a viewController manages a UIWebView. Is is created with the main Storyboard of the application. This app is able to open a rtf email attachment and display it to its UIWebView.

When application is in background, everything is OK. The application delegate method

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation

is called and there a method

[viewController loadImportedRTFDocument:url];

is called. The controller loads the document, and displays it.

When application is not in background when user select the attachment in Mail app, its launches correctly. The App delegate openURL is called, and the viewController receives the message to load imported document. But it is not able to display it, since the UIWebView has not been allocated (returns nil pointer).

What I did observe is that the method called by the app delegate is made before the controller received the viewDidLoad message...

How can I be sure that the viewController has been correctly initialized in this situation, and where should I ask the UIWebView to load the NSData object associated with the rtf attachment?

Denis
  • 775
  • 7
  • 22
  • Are you going to use the same viewController to display all the documents or you want to reallocate it for each ? – A-Live Jun 24 '12 at 21:21
  • Yes, for saving memory, there is only one viewController that deals with one UIWebView. The controller loads a document html content in the view when necessary. The view is supposed to be alloc-init'ed while loading the storyboard file that describes the main view interface. – Denis Jun 25 '12 at 07:40

2 Answers2

0

You can use NSObject's performSelector to put the call to loadImportedRTFDocument at the end of the run loop (ie. after the app has fully loaded and is idle):

// Will be called after app has loaded
[viewController performSelector:@selector(loadImportedRTFDocument:) withObject:url afterDelay:0];
jjv360
  • 4,120
  • 3
  • 23
  • 37
  • Sorry, I'm probably an idiot, I don't understand what you mean... I tried that but unsuccessfuly... with no effect – Denis Jun 25 '12 at 23:13
  • Did you replace `[viewController loadImportedRTFDocument:url` with `[viewController performSelector:@selector(loadImportedRTFDocument:) withObject:url afterDelay:0];`? If you did, can you check if viewController isn't nil... – jjv360 Jun 26 '12 at 16:14
  • viewController is never nil. This is its UIWebView property that returns nil in any case tested, including the one you described... I cannot discuss further here, since solution should be dependent on iOS 6 and that there is a NDA on it until its official release. – Denis Jun 26 '12 at 20:20
  • In which method do you initialize your web view? Or is it done from a nib? – jjv360 Jun 27 '12 at 06:32
  • Ok, I don't have any experience with storyboards, but with nibs you need to link views to properties... Have you linked your `UIWebView` in the editor to your class's webView property? (Assuming storyboards work the same way as interface builder...) – jjv360 Jun 27 '12 at 14:05
  • For sure I did! The application works perfectly, and no problem occurs when it was in background when user choose to open an attachment with it. It arises only when application was not in memory when user opens the attachment. – Denis Jun 27 '12 at 16:21
0

So loadImportedRTFDocument is called before the view is loaded. To solve it, store the URL, then at viewDidLoad load it into the web-view. To avoid double-loading when the app was in the background, make a switch at loadImportedRTFDocument which will load the URL only if the webView is initialized. This way the webView should be loaded at viewDidLoad or at loadImportedRTFDocument depending on whether the view is loaded.

This solution will also (if the URL is still valid and the resource still exists) make your webView showing the data even when the webView was unloaded with the memoryWarning.

A-Live
  • 8,904
  • 2
  • 39
  • 74