LoginSignup
6
5

More than 5 years have passed since last update.

swift webページラッパーアプリ

Last updated at Posted at 2015-03-31

まず、ViewController側の実装内容
単純に下記内容の記述のみでアプリ単体でWEBサイトを操作できる。

//
//  ViewController.swift
//  Created by 1000_VICKY on 2015/03/31.
//  Copyright (c) 2015年 com.docgack. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    //土台となるUIWindowオブジェクト
    var win : UIWindow = UIWindow();

    //任意のWebサイトを表示させるためのUIWebViewオブジェクト
    var webObj : UIWebView = UIWebView();

    //実機端末の解像度用の変数
    var maxHeight : CGFloat = 0.0
    var maxWidth : CGFloat = 0.0

    //実機端末のステータスバーの高さ
    var statusHeight : CGFloat = 0;

    //実機端末の現在の位置
    var nowHeight :CGFloat = 0;

    //表示させたいWebサイトのURL
    var targetUrl : String = "http://docgack.com"

    var requestUrl : NSURL?
    var req : NSURLRequest?
    var tabObj: UITabBarController?
    var nav : UINavigationBar = UINavigationBar();

    //戻るボタン
    var backButton : UIButton = UIButton()
    //進むボタン
    var previousButton : UIButton = UIButton();
    //更新ボタン
    var reloadButton:UIButton = UIButton();



    //起動時実行されるメソッド
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        self.initMethod();
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    //WebViewを作成するメソッド
    func makeWebViewMethod(){

        self.requestUrl = NSURL(string: self.targetUrl);
        self.req = NSURLRequest(URL : self.requestUrl!);
        self.webObj.scalesPageToFit = false
        self.webObj.frame = CGRectMake(0, self.nowHeight, self.win.bounds.width , self.win.bounds.height - self.nowHeight)
        self.webObj.loadRequest(self.req!)

        self.win.addSubview(self.webObj)
        self.tabObj = UITabBarController()
    }

    //実行時直後メソッド
    func initMethod(){
        //実機端末の解像度を取得
        self.maxHeight = self.view.bounds.height
        self.maxWidth = self.view.bounds.width

        //スタータスバーの高さを取得
        self.statusHeight = UIApplication.sharedApplication().statusBarFrame.height

        //ナビゲーションバーを作成
        self.nav.backgroundColor = UIColor.whiteColor()
        self.nav.frame = CGRectMake(0, 0, self.maxWidth, 30)
        self.nowHeight += 30

        //戻るボタンの作成
        self.backButton.setTitle("<<" , forState:.Normal)
        self.backButton.frame = CGRectMake(0, 0, 100, 30)
        self.backButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        self.backButton.layer.masksToBounds = true
        self.backButton.addTarget(self, action: "backButtonMethod:", forControlEvents: UIControlEvents.TouchUpInside)

        //進むボタンの作成
        self.previousButton.setTitle(">>", forState:.Normal)
        self.previousButton.frame = CGRectMake(self.maxWidth - 100 , 0 , 100, 30)
        self.previousButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        self.previousButton.layer.masksToBounds = true
        self.previousButton.addTarget(self, action: "previousButtonMethod:", forControlEvents: UIControlEvents.TouchUpInside)

        //更新ボタンの作成
        self.reloadButton.setTitle("⚪︎", forState: .Normal)
        self.reloadButton.frame = CGRectMake(self.maxWidth / 2, 0, 50, 50)
        self.reloadButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        self.reloadButton.layer.masksToBounds = true;
        self.reloadButton.addTarget(self, action: "reloadButtonMethod:", forControlEvents:UIControlEvents.TouchUpInside)

        //ナビゲーションバーにadd
        self.nav.addSubview(self.backButton)
        self.nav.addSubview(self.previousButton)
        self.nav.addSubview(self.reloadButton)


        //UIWindowを作成
        self.win.frame = CGRectMake(0, self.statusHeight, self.maxWidth, self.maxHeight - self.statusHeight)
        self.win.addSubview(self.nav)
        self.win.makeKeyAndVisible()
        self.view.addSubview(self.win)

        self.makeWebViewMethod()
    }

    //戻るボタンを押下時メソッド
    func backButtonMethod(sender: AnyObject?){
        self.webObj.goBack();

    }
    //進むボタンを押下時メソッド
    func previousButtonMethod(sender: AnyObject?){
        self.webObj.goForward()
    }
    //更新メソッド
    func reloadButtonMethod(sender: AnyObject){
        self.webObj.reload()
    }
}

以下は、AppDelegate側の実装内容。
こちらについては、storyboardを使わずに、アプリ作成を行うための記述が
されている。

//
//  AppDelegate.swift
//  q
//
//  Created by 1000_VICKY on 2015/03/31.
//  Copyright (c) 2015年 com.docgack. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    var mainTabObj : UITabBarController  = UITabBarController()
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
        self.window?.backgroundColor = UIColor.whiteColor() // 背景白
        self.window?.rootViewController = ViewController() // ViewControllerを指定
        self.window?.makeKeyAndVisible()
        return true
    }

    func applicationWillResignActive(application: UIApplication) {
        // 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.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // 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.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // 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.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // 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.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

明日まとめる

6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5