0

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];
}

3 Answers3

1

// 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];
9to5ios
  • 5,319
  • 2
  • 37
  • 65
  • Yeah, I know, but I cannot add it from the `UIActivity` subclass because it isn't a viewcontroller (it has no knowledge of the `[self view]`. If I add it in my viewcontroller, then I don't know how to reference it from the `UIActivity` subclass when I need to tell it to fire. –  Aug 09 '13 at 13:16
0

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?

Community
  • 1
  • 1
allprog
  • 16,540
  • 9
  • 56
  • 97
  • I also tried notifications, but it fails with: `Attempt to present on which is already presenting `. Unless I can figure out a way to delay the presenting of the `ViewController` containing the `webView` I cannot do this either. –  Aug 09 '13 at 13:42
  • I believe view did appear is going to be called after the activity view controller was dismissed. You may put the web view opener functionality there instead of the handler method. – allprog Aug 09 '13 at 13:51
  • Have you been able to proceed? – allprog Aug 12 '13 at 09:00
0

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;

}