user1260375
user1260375

Reputation:

UIActivity subclass open a webview

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

Answers (3)

WhiteWabbit
WhiteWabbit

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

allprog
allprog

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:

  1. The view controller registers for notifications with a given identifier.
  2. The activity posts this notification if performed.
  3. The notification is processed by the activity in the handler method and it opens the web view.
  4. Don't forget to unregister the view controller from the notification center if the controller is being removed from the navigation stack.

There are lots of examples for using notifications, like this Send and receive messages through NSNotificationCenter in Objective-C?

Upvotes: 0

9to5ios
9to5ios

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

Related Questions