您的位置:首页 > 运维架构 > 网站架构

在相关网站首页加载到手机上浏览网页

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了,
运行后,具体的效果如下图:


     


怎么样哦,效果不赖吧
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息