The download link on the page you linked to doesn't work, so I'll keep my answer more general.
You don't need to keep multiple WebViews or manually generate snapshots if you're using NSPageController
; it takes care of that for you. In your case, you want to use NSPageController
in History Mode. To do that, wire your WebView
to pageController.view
. You will need to implement three NSPageControllerDelegate
methods:
- (void)pageControllerWillStartLiveTransition:(NSPageController *)pageController;
- (void)pageController:(NSPageController *)pageController didTransitionToObject:(id)object;
- (void)pageControllerDidEndLiveTransition:(NSPageController *)pageController;
Every time the WebView goes to a new page (not through a back/forward action), call navigateForwardToObject:(id)object
on the pageController
. I would make object
a custom object that stores user state for a navigated page (scroll positions, highlighted text, form contents, etc.) as well as the page's WebHistoryItem
. The user state can be undefined initially, but should get set in pageControllerWillStartLiveTransition:
. Here's an example:
- (void)pageControllerWillStartLiveTransition:(NSPageController *)pageController
{
// Remember user state
MyCustomObject *object = [self.pageController.arrangedObjects objectAtIndex:self.pageController.selectedIndex];
object.userState = someUserState;
}
When that method returns, the pageController
will hide its view (the WebView) and display snapshots of previous states of its view instead.
Once the swiping animation is complete, pageController:didTransitionToObject:
will get called. Cool. What you should do in that method is grab the WebHistoryItem
out of object
and have the WebView go back to that item using the goToBackForwardItem:
method. You should also hold onto the user state you stored in object
(say, in an instance variable), because you'll need to restore it once WebView
finishes loading.
Lastly, in pageControllerDidEndLiveTransition:
you do anything you want done before redisplaying the WebView. I expect that would be nothing, since the user state isn't restored until the WebView finishes loading, so all you would need in its implementation would be [pageController completeTransition]
.
One last detail is the back/forward buttons. You should implement them as you would normally, but also have them call navigateBack:
or navigateForward:
on the pageController.
That pretty much covers it. I haven't actually tried this specific example, so let me know if you run into any problems.
Edit
Here's how I'd modify your source to get basic NSPageController
functionality. Add an NSPageController IBOutlet
property in your AppDelegate header file. In your MainMenu.xib
file add the page controller, wire its view to your WebView, make your AppDelegate its delegate, and give it a referencing outlet to the property we just created in the AppDelegate. Also, make your AppDelegate your WebView's frameLoadDelegate
. Inside basicWebAppDelegate.m
add a private property:
@interface basicWebAppDelegate ()
@property (assign) id currentItem;
@end
Then add the following inside implementation:
#pragma mark - WebFrameLoadDelegate
- (void)webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame {
if (frame == [sender mainFrame]) {
id object = [sender.backForwardList currentItem];
BOOL isCurrentItem = self.currentItem && (object == self.currentItem) ? YES : NO;
if (!isCurrentItem) {
[self.pageController navigateForwardToObject:[sender.backForwardList currentItem]];
}
}
}
#pragma mark - NSPageControllerDelegate
- (void)pageControllerWillStartLiveTransition:(NSPageController *)pageController {
self.currentItem = [self.webView.backForwardList currentItem];
// Here is where you'll save any state for your pageController.arrangedObjects[pageController.selectedIndex] object
}
- (void)pageController:(NSPageController *)pageController didTransitionToObject:(id)object {
BOOL isCurrentItem = self.currentItem && (object == self.currentItem) ? YES : NO;
if (!isCurrentItem) {
self.currentItem = object;
[self.webView goToBackForwardItem:object];
}
}
- (void)pageControllerDidEndLiveTransition:(NSPageController *)pageController {
self.currentItem = nil;
[pageController completeTransition];
}
Finally, change your goBack:
and goForward:
actions to just call [self.pageController navigateBack:sender]
and [self.pageController navigateForward:sender]
, respectively.
Note that I didn't bother saving any user state here and instead used WebHistoryItem
s directly as the objects. You might need to do differently.
Let me know if you need more help.