在相关网站首页加载到手机上浏览网页
2013-12-30 20:25
381 查看
学了一段时间的IOS相信大家一定非常的想编写一个软件使自己的手机能够访问你指定的网站。其实呢这个实现起来非常的简单,下面我会为大家分享一下代码,另外还要实现网络的状态,就是那个白色的菊花,由于第一个实在是太简单了,我就写在一块了,大家一看就明白了。
具体的代码如下:
HHLAppDelegate.h
#import <UIKit/UIKit.h>
@class HHLViewController;
@interface HHLAppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) HHLViewController *viewController;
@property (strong,nonatomic) UINavigationController *myNaVC;
@end
HHLAppDelegate.m
#import "HHLAppDelegate.h"
#import "HHLViewController.h"
@implementation HHLAppDelegate
- (void)dealloc
{
[_window release];
[_viewController release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[HHLViewController alloc] initWithNibName:@"HHLViewController" bundle:nil] autorelease];
UINavigationController *pNaVC = [[UINavigationController alloc]initWithRootViewController:self.viewController];
self.myNaVC = pNaVC;
[pNaVC release];
self.window.rootViewController = self.myNaVC;
[self.window makeKeyAndVisible];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end
HHLViewController.h
#import <UIKit/UIKit.h>
@interface HHLViewController : UIViewController<UIWebViewDelegate>
{
@private
UIWebView *myWebView;
UIActivityIndicatorView *myIndicatorView;
}
@end
HHLViewController.m
#import "HHLViewController.h"
@interface HHLViewController ()
@end
@implementation HHLViewController
- (void)dealloc
{
[myIndicatorView release];
if (myWebView.loading) {
[myWebView stopLoading];
}
myWebView.delegate = nil;//苹果文档中推荐,release前需要如此编写
[myWebView release];
[super dealloc];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"明确显示通信状态";
myWebView = [[UIWebView alloc]init];
myWebView.delegate =self;
myWebView.frame = self.view.bounds;
myWebView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
myWebView.scalesPageToFit = YES;//将UIWebView的scalePageToFit属性设置为YES,这样Web页面就会根据屏幕大小自动伸缩。
[self.view addSubview:myWebView];
[self.navigationController setNavigationBarHidden:NO animated:YES];
[self.navigationController setToolbarHidden:NO animated:YES];
//在工具条中追加活动指示器
myIndicatorView = [[UIActivityIndicatorView alloc]init];
myIndicatorView.frame =CGRectMake(0, 0, 50, 50);
UIBarButtonItem *indicator = [[[UIBarButtonItem alloc] initWithCustomView:myIndicatorView]autorelease];
UIBarButtonItem *adjustment = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]autorelease];
NSArray *buttons = [NSArray arrayWithObjects:adjustment,indicator,adjustment, nil];
[self setToolbarItems:buttons animated:YES];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
//Web页面显示
NSURLRequest *myRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com/"]];
[myWebView loadRequest:myRequest];
[self updateViewConstraints];
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
[myIndicatorView startAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[myIndicatorView stopAnimating];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
[myIndicatorView stopAnimating];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
由于代码很简单,如果大家有哪些不太清楚的,可以到官方文档看看,或者百度一下就OK了,
运行后,具体的效果如下图:
怎么样哦,效果不赖吧
具体的代码如下:
HHLAppDelegate.h
#import <UIKit/UIKit.h>
@class HHLViewController;
@interface HHLAppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) HHLViewController *viewController;
@property (strong,nonatomic) UINavigationController *myNaVC;
@end
HHLAppDelegate.m
#import "HHLAppDelegate.h"
#import "HHLViewController.h"
@implementation HHLAppDelegate
- (void)dealloc
{
[_window release];
[_viewController release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[HHLViewController alloc] initWithNibName:@"HHLViewController" bundle:nil] autorelease];
UINavigationController *pNaVC = [[UINavigationController alloc]initWithRootViewController:self.viewController];
self.myNaVC = pNaVC;
[pNaVC release];
self.window.rootViewController = self.myNaVC;
[self.window makeKeyAndVisible];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end
HHLViewController.h
#import <UIKit/UIKit.h>
@interface HHLViewController : UIViewController<UIWebViewDelegate>
{
@private
UIWebView *myWebView;
UIActivityIndicatorView *myIndicatorView;
}
@end
HHLViewController.m
#import "HHLViewController.h"
@interface HHLViewController ()
@end
@implementation HHLViewController
- (void)dealloc
{
[myIndicatorView release];
if (myWebView.loading) {
[myWebView stopLoading];
}
myWebView.delegate = nil;//苹果文档中推荐,release前需要如此编写
[myWebView release];
[super dealloc];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"明确显示通信状态";
myWebView = [[UIWebView alloc]init];
myWebView.delegate =self;
myWebView.frame = self.view.bounds;
myWebView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
myWebView.scalesPageToFit = YES;//将UIWebView的scalePageToFit属性设置为YES,这样Web页面就会根据屏幕大小自动伸缩。
[self.view addSubview:myWebView];
[self.navigationController setNavigationBarHidden:NO animated:YES];
[self.navigationController setToolbarHidden:NO animated:YES];
//在工具条中追加活动指示器
myIndicatorView = [[UIActivityIndicatorView alloc]init];
myIndicatorView.frame =CGRectMake(0, 0, 50, 50);
UIBarButtonItem *indicator = [[[UIBarButtonItem alloc] initWithCustomView:myIndicatorView]autorelease];
UIBarButtonItem *adjustment = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]autorelease];
NSArray *buttons = [NSArray arrayWithObjects:adjustment,indicator,adjustment, nil];
[self setToolbarItems:buttons animated:YES];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
//Web页面显示
NSURLRequest *myRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com/"]];
[myWebView loadRequest:myRequest];
[self updateViewConstraints];
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
[myIndicatorView startAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[myIndicatorView stopAnimating];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
[myIndicatorView stopAnimating];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
由于代码很简单,如果大家有哪些不太清楚的,可以到官方文档看看,或者百度一下就OK了,
运行后,具体的效果如下图:
怎么样哦,效果不赖吧
相关文章推荐
- 浏览发布的网站首页的时候,不能加载某个项,磁盘空间不足的
- 在电脑上用chrome浏览器调试android手机里的网页代码时,无法看到本地加载的js文件
- APP源码 教程 网页转APP 安卓Andriod应用开发 手机APP 移动网站
- Linux中国网站支持手机浏览了
- 手机开发实战81——浏览网页
- 根据两个经纬度点调用百度地图应用查询路线 适用android或者ios中及网页浏览(手机网页同样适用)
- 国外十大网页加载速度分析网站
- 移动网站性能优化:网页加载技术概览
- 网站设计模版,自适应,手机网页模版
- CAD二次开发控件,dwg控件,网页DWG控件,手机浏览编辑DWG控件
- 延时加载技术-----仿照手机淘宝网站图片延时加载
- 网页配色相关网站
- pc版网站在手机上浏览右侧有一块空白
- 使自己做的asp.net网页适应手机浏览
- Thinkphp入口文件判断网页是电脑访问还是手机访问网站
- 利用压缩网页来提升网站浏览速度
- 易语言 修改IE协议头模拟手机浏览网页
- 特别推荐-提高网页浏览美观的26个精美的PSD网站导航菜单
- ipguard控制台加载不到数据(看不到文档操作、网页浏览记录不到问题)
- Firefox扩展IE Tab Plus内置功能导致浏览所有网页加载superfish.com脚本