Showing posts with label system. Show all posts
Showing posts with label system. Show all posts

Friday, January 1, 2016

Simple clock app with current date and time

This post shows how to display the current date and time, which can be obtained with NSDate().

The code below not only prints date and time in Short, Medium, Long, and Full styles in the debugger console, but also shows a clock with a custom format 
"yyyy/MM/dd\nEEEE\nhh:mm:ss a\nzzzz", which includes date, day of week, time, and timezone. For more information about the meaning of characters within the custom format string, please refer to Date Format Patterns.

1. Modify ViewController.swift as:


import UIKit

class ViewController: UIViewController {
    
    var format : NSDateFormatter!
    var labelClock : UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //Light pink color with RGB values
        view.backgroundColor = UIColor(red: 255.0/255.0, green: 200.0/255.0, blue: 210.0/255.0, alpha: 1.0)
        
        let label = UILabel(frame: CGRectMake(0,0,200,100))
        label.center = CGPoint(x: view.center.x, y: 150)
        label.text = "Now is"
        label.textAlignment = NSTextAlignment.Center //Align to center
        view.addSubview(label)
        
        labelClock = UILabel(frame: CGRectMake(0,0,250,100))
        labelClock.center = view.center
        labelClock.textAlignment = NSTextAlignment.Center
        labelClock.numberOfLines = 0 //Multi-lines
        labelClock.font = UIFont(name: "Helvetica-Bold", size: 20)
        view.addSubview(labelClock)
        
        format = NSDateFormatter()
        
        //Print different date styles.
        printDateStyles()
        
        //Custom Format for clock
        format.dateFormat = "yyyy/MM/dd\nEEEE\nhh:mm:ss a\nzzzz"
        
        //Update the date and time of the clock periodically.
        NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "updateClock", userInfo: nil, repeats: true)
        
    }
    
    //Print different styles once in debugger console
    func printDateStyles() {
        
        let formatArray : [NSDateFormatterStyle] = [NSDateFormatterStyle.ShortStyle, NSDateFormatterStyle.MediumStyle, NSDateFormatterStyle.LongStyle, NSDateFormatterStyle.FullStyle]
        let formatStringArray : [String] = ["No","Short","Medium","Long","Full"]
        
        let now = NSDate()
        var outputString : String!
        
        //Show Date Styles
        print("---Date Styles---")
        for style in formatArray {
            format.dateStyle = style
            outputString = "\(formatStringArray[style.hashValue]) Style:\t\(format.stringFromDate(now))"
            
            print(outputString)
        }
        
        //Remove Date String
        format.dateStyle = NSDateFormatterStyle.NoStyle
        
        //Show Time Styles
        print("\n---Time Styles---")
        for style in formatArray {
            format.timeStyle = style
            outputString = "\(formatStringArray[style.hashValue]) Style:\t\(format.stringFromDate(now))"
            
            print(outputString)
        }
    }
    
    //Update clock every second
    func updateClock() {
        let now = NSDate()
        
        labelClock.text = format.stringFromDate(now)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}

2. The print result with various styles:

---Date Styles---
Short Style: 12/31/15
Medium Style: Dec 31, 2015
Long Style: December 31, 2015
Full Style: Thursday, December 31, 2015

---Time Styles---
Short Style: 11:46 PM
Medium Style: 11:46:21 PM
Long Style: 11:46:21 PM GMT+8
Full Style: 11:46:21 PM Taipei Standard Time

3. The clock result:


A few moments later... Happy New Year!!


4. Change the language setting of iPhone. The date, day of week, time, and timezone data are also changed automatically.
The results below are with Traditional Chinese. (當iPhone的語言設定改變時,日期、星期時間和時區等資料也會跟著語言設定改變。以下為繁體中文的結果)

5. The print result in Chinese:

---Date Styles---
Short Style: 2016/1/1
Medium Style: 2016年1月1日
Long Style: 2016年1月1日
Full Style: 2016年1月1日 星期五

---Time Styles---
Short Style: 下午10:29
Medium Style: 下午10:29:04
Long Style: GMT+8 下午10:29:04
Full Style: 台北標準時間 下午10:29:04

6. The clock result in Chinese:

Wednesday, December 30, 2015

How to get UUID (Universally Unique Identifier), IDFV (Vendor Identifier) or IDFA (Advertising Identifier)

Update:
January 29, 2016
UUID (Universally Unique Identifier) may be generated with:


let uuid = NSUUID().UUIDString
print(uuid)

Result:


19B7xxxx-xxxx-xxxx-xxxx-xxxxxxxx0CFA

===============
Original post:
December 30, 2015

Some device identifiers are now impossible to be obtained from public APIs of iOS:
IMSI - International Mobile Subscriber Identity (SIM card number)
IMEI - International Mobile Equipment Identity (Device ID)
UDID - Unique Device Identifier for Apple iDevices
MAC address - Media Access Control Address (Network address)

IDFV and IDFA are the identifiers available for identification or for advertising:

IDFV (Vendor Identifier / Identifier for Vendor)
IDFV is the same for different apps provided by the same vendor running on the same device.

IDFA (Advertising Identifier / Identifier for Advertising)
IDFA of an iOS device remains the same unless the user make a reset in the Settings app.

IDFV and IDFA can be obtained easily in code. However, IDFA has to be used for advertising. Improper use of IDFA in an app will cause app rejection from the App Store.

How to obtain IDFV / IDFA
Modify ViewController.swift as:

import UIKit
import AdSupport

class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let button = UIButton(frame: CGRectMake(70, 100, 200, 20))
        button.setTitle("Get IDs", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.blueColor(), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.cyanColor(), forState: UIControlState.Highlighted)
        button.addTarget(self, action: "btnPressed:", forControlEvents: UIControlEvents.TouchUpInside)
        view.addSubview(button)
    }
    
    func btnPressed(sender: UIButton) {
        
        let strIDFV = UIDevice.currentDevice().identifierForVendor?.UUIDString
        
        print("Vendor = \(strIDFV!)")
        
        var strIDFA : String = "No IDFA"
        
        if ASIdentifierManager.sharedManager().advertisingTrackingEnabled {
            strIDFA = ASIdentifierManager.sharedManager().advertisingIdentifier.UUIDString
        }
        print("IDFA = \(strIDFA)")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}

Results

An iPad and an iPhone are tested. The two devices are with different IDFVs and IDFAs.
The IDFV and IDFA are the same for different apps on the same iPhone.
If the "Limit Ad Tracking" option is turned on, there is no IDFA.
If the "Limit Ad Tracking" option is turned off, the IDFA is changed.
If the Advertising Identifier is reset, the IDFA is also changed.

Here are the results of print logs:

iPad

IDFV = C9A9xxxx-xxxx-xxxx-xxxx-xxxxxxF155E6
IDFA = 2544xxxx-xxxx-xxxx-xxxx-xxxxxxFE4B56

iPhone
App #1

IDFV = 912Dxxxx-xxxx-xxxx-xxxx-xxxxxx2332C6
IDFA = CEF5xxxx-xxxx-xxxx-xxxx-xxxxxx8D9C15

App #2

IDFV = 912Dxxxx-xxxx-xxxx-xxxx-xxxxxx2332C6
IDFA = CEF5xxxx-xxxx-xxxx-xxxx-xxxxxx8D9C15

Limit Ad Tracking
Select Settings -> Privacy -> Advertising -> Limit Ad Tracking -> On

IDFV = 912Dxxxx-xxxx-xxxx-xxxx-xxxxxx2332C6
IDFA = No IDFA

Select Settings -> Privacy -> Advertising -> Limit Ad Tracking -> Off

IDFV = 912Dxxxx-xxxx-xxxx-xxxx-xxxxxx2332C6
IDFA = C9ECxxxx-xxxx-xxxx-xxxx-xxxxxxF86A1B

Reset Advertising Identifier
Select Settings -> Privacy -> Advertising -> Reset Advertising Identifier -> Reset Identifier

IDFV = 912Dxxxx-xxxx-xxxx-xxxx-xxxxxx2332C6
IDFA = E8D2xxxx-xxxx-xxxx-xxxx-xxxxxx4A625C

References:
Does this app use the Advertising Identifier (IDFA)? - AdMob 6.8.0
Apple Developers Must Now Agree To Ad Identifier Rules Or Risk App Store Rejection
NSUUID / CFUUIDRef / UIDevice -unique​Identifier / -identifier​For​Vendor
The Developer’s Guide to Unique Identifiers
Using CoreTelephony framework to get IMEI and IMSI on iOS 7

Monday, October 12, 2015

Get the device type and iOS version

Update: December 10, 2016 / September 23, 2017
Xcode 8.1 (Swift 3.0.1) / Xcode 9.0 (Swift 4)

Swift 3/Swift 4 Code:

let systemVersion = UIDevice.current.systemVersion
print("iOS\(systemVersion)")
        
//iPhone or iPad
let model = UIDevice.current.model

print("device type=\(model)")

Result:

iOS10.1.1
device type=iPhone

Original post: October 12, 2015

Swift 1 Code:

let systemVersion = UIDevice.currentDevice().systemVersion
println("iOS\(systemVersion)")
        
//iPhone or iPad
let model = UIDevice.currentDevice().model
println("device type=\(model)")


Result:

iOS8.4.1
device type=iPad


To get the device model name string:

For Swift 2.0:

For Swift 1.2: