Search code examples
iosuiwebviewuialertviewaddsubview

How I can add UIWebview inside a UIAlertView?


I tried with the following code: But I am getting simply an alert view without webview. Where should I correct to get the proper output?

 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Next Action"message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

        UIWebView *webView = [[UIWebView alloc] init];
        [webView setFrame:CGRectMake(0,0,300,300)];
        [webView loadHTMLString:[@"https://app.acuityscheduling.com/schedule.php?owner=11673736" description] baseURL:nil];
        [alert addSubview:webView];
        [alert show];

Solution

  • You have to add UIWebView as subview of UIView and UIView set as accessoryView of UIAlertView.

    Try this way

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Next Action"message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    
    UIWebView *webView = [[UIWebView alloc] init];
    [webView setFrame:CGRectMake(0,0,300,300)];
    [webView loadHTMLString:[@"https://app.acuityscheduling.com/schedule.php?owner=11673736" description] baseURL:nil];
    
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 300, 300)];
    [view addSubview:webView];
    [alert setValue:view forKey:@"accessoryView"];
    [alert show];