If developer want to show a web page in an application, the class UIWebView is a good choice. First, we can define a property of UIWebView in view controller's implementation file:
@interface XXXViewController()
...
@property UIWebView *webview;
@end
Then initialize the UIWebView object in viewDidLoad delegate function of that view controller:
- (void)viewDidLoad
{
[super viewDidLoad];
...
_webview = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.size.width, self.size.height)];
_webview.tag = 55;
NSString *fullURL = @"https://www.facebook.com/BricKiller";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_webview loadRequest:requestObj];
}
Above code initiate the UIWebView object by assigning a frame of it. The code give it a view of full screen size. The tag property of UIWebView identify the view object. It is a number and we will refer this tag number later. I also established an object of NSURL and request the content of this URL in above code.
[self.view addSubview:_webview];
You may find an issue, if you opened the web view in your application, there is no place to close this web view and return to your original application UI. So you can add a close button on this web view by putting below code before showing the web view:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.backgroundColor = [SKColor colorWithWhite:1.0 alpha:0.5];
[button setTitle:@"Close" forState:UIControlStateNormal];
button.frame = CGRectMake(0, _webview.frame.size.height - 100, self.size.width, 100);
[button addTarget:self action:@selector(close:) forControlEvents:UIControlEventTouchUpInside];
- (IBAction)close:(id)sender {
[[self.view viewWithTag:55] removeFromSuperview];
}
Now I use the tag value to find the object of UIWebView, and then remove it from super view in close function. Here is what looks like of the running UIWebView:
No comments:
Post a Comment