NSUrl读取网络资源

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSURL *url = [NSURL URLWithString:@"https://www.baidu.com"];
    NSLog(@"url的scheme为:%@",[url scheme]);
    NSLog(@"url的host%@",[url host]);
    NSLog(@"url的port%@",[url port]);
    NSLog(@"url的path为%@",[url path]);
    //使用URL对应的资源初始化NSString
    NSString *baidu = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];//输出NSString 即可看到页面的源代码
    NSLog(@"%@",baidu);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

输出为:

2016-02-03 22:02:05.527 NSUrl[838:26688] url的scheme为:https
2016-02-03 22:02:05.528 NSUrl[838:26688] url的hostwww.baidu.com
2016-02-03 22:02:05.528 NSUrl[838:26688] url的port(null)
2016-02-03 22:02:05.528 NSUrl[838:26688] url的path为
2016-02-03 22:02:17.546 NSUrl[838:26688] <html>
<head>
	<script>
		location.replace(location.href.replace("https://","http://"));
	</script>
</head>
<body>
	<noscript><meta http-equiv="refresh" content="0;url=http://www.baidu.com/"></noscript>
</body>
</html>


你可能感兴趣的:(ios,url,appdelegate)