您的位置:首页 > 移动开发 > Objective-C

objective - c 简易的浏览器制作

2014-07-06 00:00 99 查看
摘要: objectiv-c 简易浏览器

由于看了https://developer.apple.com/library/mac/referencelibrary/GettingStarted/RoadMapOSX/books/RM_YourFirstApp_Mac/Articles/Introduction.html

这是翻译过的:http://www.cnblogs.com/patientAndPersist/archive/2013/05/29/3106041.html

这个,制作第一个APP之后,然后尝试去制作一个简易的浏览器。

不过有一些不同,首先,我们还是创建一个UI的简单界面


然后是在菜单Editor里面,Simulate Document进行模拟,并调解下窗口大小,如果没有跟随窗口移动,那么进行以下操作



这是按住control键拖动text field到web view上面后,可以出现的窗口,里面可以选择对齐方式等等。

之后拖动创建IBOutlet和action就不解释了,参考最上面网址,那个比较详细。

创建完成之后,我查了下,web view不能直接这样用,要添加WebKid.framework,如图下:


这个的添加方法是:

选中Framework,右键选择 Add File to"xxx",然后路径为System->Library->Framework->WebKit.framework 选完后确认

这下就准备的差不多了,以下就是代码:

//
//  AppDelegate.h
//  NewWebView
//
//  Created by Roeru on 6/7/14.
//  Copyright (c) 2014 Shinoi. All rights reserved.
//

#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
#import "NeedData.h"
@class NeedData;

@interface AppDelegate : NSObject <NSApplicationDelegate>

@property (assign) IBOutlet NSWindow *window;
@property (weak) IBOutlet NSTextField *textHttp;
@property (weak) IBOutlet WebView *webView;
@property NeedData *web;

- (IBAction)putInWebID:(id)sender;
- (void) webViewUpdata;

@end

//
//  AppDelegate.m
//  NewWebView
//
//  Created by Roeru on 6/7/14.
//  Copyright (c) 2014 Shinoi. All rights reserved.
//

#import "AppDelegate.h"

@implementation AppDelegate {

}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[self webViewUpdata];
}

- (IBAction)putInWebID:(id)sender {
NeedData *aNeedData = [[NeedData alloc]init];
[self setWeb:aNeedData];
NSString *webPutIn = [sender stringValue];
[self.web setWebAddress:webPutIn];
[self webViewUpdata];
}

- (void) webViewUpdata {
NSString *webData = [self.web webAddress];
NSURL *url = [NSURL URLWithString:webData];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[[[self webView] mainFrame] loadRequest:urlRequest];
}
@end

//
//  NeedData.h
//  NewWebView
//
//  Created by Roeru on 6/7/14.
//  Copyright (c) 2014 Shinoi. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface NeedData : NSObject

@property (assign) NSString *webAddress;

@end

//
//  NeedData.m
//  NewWebView
//
//  Created by Roeru on 6/7/14.
//  Copyright (c) 2014 Shinoi. All rights reserved.
//

#import "NeedData.h"

@implementation NeedData

@end

然后运行,输入苹果官网测试



简易的就做成功了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: