swift / appdeligateでページ越しで共通変数を使う
AppDelegate.swiftとは?
アプリをつくった段階でデフォルトでつくられるファイルのひとつ。
アプリ全体のライフタイムイベントを管理するためのクラス。
http://qiita.com/SoyaTakahashi/items/cc8f48af792c353cd9f3
共通関数やら変数をページ越しで管理するときとかに使うとよさげ
例
appdeligate.swift
class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? var stampArray:[Stamp] = []; var isNewStampAdded = false
viewController.swift
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate //配列stampArrayにstampを追加 appDelegate.stampArray.append(stamp) //新規スタンプ追加フラグをtrueに設定 appDelegate.isNewStampAdded = true
swift / modalで開いた画面を閉じる
@IBAction func closeTapped(){ self.dismissViewControllerAnimated(true, completion: nil); }
swift / URLエンコード
let query = "ニュース"; let encoded = query.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet()) let url = "https://ajax.googleapis.com/ajax/services/search/news?v=1.0&hl=ja&rsz=8&q=" + encoded!;
swift / 文字列置換
- <b>タグを抜く例
var title = newsDic["title"] as? String; title = title?.stringByReplacingOccurrencesOfString("<b>", withString: ""); title = title?.stringByReplacingOccurrencesOfString("</b>", withString: "");
xcode / httpサイトを見れるようにする設定
- プロジェクトアイコンをクリックして、設定画面から「Info」のタブ
- [+] App Transport Security Settings を追加
- その中に、[+] Allow Arbitrary Loads で Boolean を True
swift / JSON取得&リスト表示&タイトルクリックでURLに飛ぶ
- 世界一受けたいiphoneアプリの授業から転載
import UIKit import Alamofire class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate { @IBOutlet var table: UITableView! var newsDataArray = NSArray(); func tableView(tableView: UITableView, numberOfRowsInSection section:Int) -> Int{ return newsDataArray.count } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)->UITableViewCell{ let cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Cell") let newsDic = newsDataArray[indexPath.row] as! NSDictionary cell.textLabel?.text = newsDic["title"] as? String cell.textLabel?.numberOfLines = 3 cell.detailTextLabel?.text = newsDic["publishedDate"] as? String return cell } func tableView(tableView: UITableView,didSelectRowAtIndexPath indexPath: NSIndexPath){ let newsDic = newsDataArray[indexPath.row] as! NSDictionary; let newsUrl = newsDic["unescapedUrl"] as! String; let url = NSURL(string:newsUrl); let app = UIApplication.sharedApplication(); app.openURL(url!); } override func viewDidLoad() { table.dataSource = self; table.delegate = self; super.viewDidLoad() getRSS(); } func getRSS(){ let requestUrl = "https://ajax.googleapis.com/ajax/services/search/news?v=1.0&topic=p&hl=ja&rsz=8"; Alamofire.request(.GET, requestUrl, parameters: nil).responseJSON { response in if let json = response.result.value { let jsonDic = json as! NSDictionary let responseData = jsonDic["responseData"] as! NSDictionary self.newsDataArray = responseData["results"] as! NSArray self.table.reloadData() } } }
swift / jsonをhttp経由で取得
Download
- https://github.com/Alamofire/Alamofire
- Download後、[Alamofire.xcodeproj]をライブラリメニューにペースト
setting
- general > embed Binary で[+]で追加。
- [Alamofile iOS]を選択して追加
code
let requestUrl = "https://ajax.googleapis.com/ajax/services/search/news?v=1.0&topic=p&hl=ja&rsz=8"; Alamofire.request(.GET, requestUrl, parameters: nil).responseJSON { response in if let json = response.result.value { print(json) } }