Rob van den Berg
Rob van den Berg

Reputation: 820

Is there a better way to deep copy a UIWebView?

I managed to deep copy a UIWebView by loading the NSURLRequest in a new UIWebView. I also managed to copy the "scrollTo" position using javascript. Now I am looking at copying the history and probably many other things.

Is there a documented way to do this properly or am I almost there?

Upvotes: 4

Views: 1387

Answers (1)

Gabriele Petronella
Gabriele Petronella

Reputation: 108101

Unfortunately there's not straightforward way to implement this.

Since UIWebView does not conform to the NSCopying protocol, I believe your approach is valid so far.

If you want to make this reusable you may think of subclassing UIWebView and implement your copying algorithm inside the copyWithZone: method of the NSCopying protocol's methods.

If you do so, you can subsequently use the standard copy method to deep copy your objects.

As an example

UICopyableWebView.h

@interface UICopyableWebView : UIWebView <NSCopying>
@end

UICopyableWebView.m

#import "UICopyableWebView.h"

@implementation UICopyableWebView
- (id)copyWithZone:(NSZone *)zone {
     id copy = [[[self class] alloc] init];
    
     if (copy) {
         // copy the relevant features of the current instance to the copy instance
     }
     return copy;
}
@end  

Upvotes: 1

Related Questions