Friday, August 28, 2015

How to go back to home screen on iOS Simulator

There's no home button on iOS simulator though it is super simple to go back to home screen.

just use: 

Cmd + Shift + H





Friday, August 14, 2015

How to add Core Data to existence project on swift

First, Open your APPDelegate.swift and import core data by writing as

import UIKit
import CoreData


Next, add Core data file by click "File" -> "New" -> "File" -> core data on your OS version.

Next, add  NSManagedObjectContext, NSManagedObjectModel and NSPersistentStoreCoordinator to your app delegate to AppDelegate.swift 
You need to change the name of your project name and Data model name.
This is the standard code which is generated when you start your new project.

    
    // MARK: - Core Data stack
    
    lazy var applicationDocumentsDirectory: NSURL = {
       
        let urls = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
        return urls[urls.count-1] as! NSURL
        }()
    
    lazy var managedObjectModel: NSManagedObjectModel = {
   
        let modelURL = NSBundle.mainBundle().URLForResource("DATAMODELNAME", withExtension: "momd")!
        return NSManagedObjectModel(contentsOfURL: modelURL)!
        }()
    
    lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator? = {
    
        var coordinator: NSPersistentStoreCoordinator? = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
        let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("PROJECTNAME.sqlite")
        var error: NSError? = nil
        var failureReason = "There was an error creating or loading the application's saved data."
        if coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil, error: &error) == nil {
            coordinator = nil
        
            var dict = [String: AnyObject]()
            dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data"
            dict[NSLocalizedFailureReasonErrorKey] = failureReason
            dict[NSUnderlyingErrorKey] = error
            error = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
       
            NSLog("Unresolved error \(error), \(error!.userInfo)")
            abort()
        }
        
        return coordinator
        }()
    
    lazy var managedObjectContext: NSManagedObjectContext? = {
   
        let coordinator = self.persistentStoreCoordinator
        if coordinator == nil {
            return nil
        }
        var managedObjectContext = NSManagedObjectContext()
        managedObjectContext.persistentStoreCoordinator = coordinator
        return managedObjectContext
        }()
    
    // MARK: - Core Data Saving support
    
    func saveContext () {
        if let moc = self.managedObjectContext {
            var error: NSError? = nil
            if moc.hasChanges && !moc.save(&error) {
    
                NSLog("Unresolved error \(error), \(error!.userInfo)")
                abort()
            }
        }
    }





Next, add "self.saveContext()" to applicationWillTerminate 
    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

        self.saveContext()

it's done!


I referred to this website for this work. please check 
http://craig24.com/2014/12/how-to-add-core-data-to-an-existing-swift-project-in-xcode/#comment-34489


Thursday, August 13, 2015

How to fix the error " Unsupported Configuration Scene is unreachable due to lack of entry points and does not have an identifier for runtime access via -instantiateViewControllerWithIdentifier"

"Unsupported Configuration     Scene is unreachable due to lack of entry points and does not have an identifier  for runtime access via -instantiateViewControllerWithIdentifier"

I encounter it and didn't know what's happen.
but i found this is very common error when we use screen transition.
Xcode complain that there are no initial screen has been assigned.

if you are using "Navigation Controller" , then assign it as a " initial View Controller"
for assign, just check the checkbox as below.

easy.





Wednesday, August 12, 2015

How to create transition on swift


prepare two view controller on storyboard.
set button on first one.
ctrl + drag from the button to second view controller
select "show"

that's all.
this is primitive  way but we don't need any code nor navigation controller.


How to show Map on swift.

very simple.

add "MapKit View" from object library
add "MapKit framework" from Build Phases, " Link Binary With Libraries" by click "+" and chose MapKit framework.
add "import MapKit" on ViewController.swift


that's all.
great is MapKit framework.

Tuesday, August 11, 2015

How to Drop the Pin to the Map on swift


write in the "override fun viewDidLoad(){

        let AAA = MKPointAnnotation()
        let centerCoordinate = CLLocationCoordinate2D(latitude: 10.79661075, longitude: 106.69064892)
            AAA.coordinate = centerCoordinate
            AAA.title = "test"
            AAA.subtitle = "abced"
        self.theMap.addAnnotation(AAA)


"addAnnotation" helps you to drop the pin on the map.

*don't forget to declare the Map = MKMapView with import MapKit.

Sunday, August 9, 2015

How to set Photo expand as much as possible on imagecell

1. select target image cell on document Outline
2. remove check from "Constrain to margins"
3. click four red constrains line beside of number.
4. set 0 to each
5. click Add 4 Constrains