Reputation:
I'm trying create a subclass of UIActivity
to add a custom button to a UIActivityViewController
. I want this custom button to be able to open a link without leaving the application. I've found numerous solutions that allow for opening things in safari, but I can't figure out if it is possible to just open a UIWebview
inside my app (Modal view perhaps?).
I've tried creating a `UIWebview in the delegate method to handle the click, but since this isn't a viewcontroller I can't add it to the view hierarchy.
- (void)prepareWithActivityItems:(NSArray *)activityItems{
UIWebView *webView=[[UIWebView alloc] initWithFrame:CGRectMake(0,0,320,480)];
NSURLRequest *urlRequest;
NSURL *urlforWebView;
urlforWebView=[NSURL URLWithString:@"http://www.google.com"];
urlRequest=[NSURLRequest requestWithURL:urlforWebView];
[webView loadRequest:urlRequest];
}
Upvotes: 0
Views: 267
Reputation: 57
Try this
UIPlainViewController here is just a custom UIViewController with a WebView added to it. Implement (UIViewController *)activityViewController in your derived UIActivity class as such:
(UIViewController *)activityViewController {
NSString *page = @"some url";
NSURL *url = [[NSURL alloc] initWithString:page];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
[((UIPlainViewController*)self.plainViewController).webView loadRequest:request];
return self.plainViewController;
}
Upvotes: 0
Reputation: 16790
I believe this project implements just what you would like to do but it opens in safari: https://github.com/davbeck/TUSafariActivity Good for starting point.
The activity itself cannot open the view as it is not connected to the controller view hierarchy. You need some way to tell the host controller that the user has selected your activity. The easiest way to do this is through notifications:
There are lots of examples for using notifications, like this Send and receive messages through NSNotificationCenter in Objective-C?
Upvotes: 0
Reputation: 5545
// example:Need to add your webview to your mainview first
UIWebView *webView = [[UIWebView alloc] init];
[webView setFrame:CGRectMake(0, 0, 320, 460)];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]]];
[[self view] addSubview:webView];
Upvotes: 1