iPhone 6�^6 Plus�A�v����Swift����ō쐬���Ă݂����Ƃ������S�Ҍ�����iOS 8�̐V�@�\���g�����A�v���̍������ꂩ�����������A�ځB����́A�O��̋L���Œlj������ʐ^�̏����uPhotos Framework�v�̋@�\���g�p���Ď��o���A�ʐ^�̏��ɂЂ��t�������W�Ƀs����\�����܂��B�܂��A���̉ߒ���Optional�Ȃ�Swift��Objective-C�̈Ⴂ�ɂ��ĉ�����܂��B
�@�O��́uSwift�ł̕ϐ��̒�`�AXcode�ł̃A�E�g���b�g�ڑ��AiOS�V�~�����[�^�[�ւ̉摜�f�ޒlj��v�ł́ASwift�ł̕ϐ���`�ɂ��ĉ�����AMapView��փs���̒lj��Ɖ摜�f�ނ̒lj����s���܂����B
�@����͑O��̋L���Œlj������ʐ^�̏����uPhotos Framework�v�̋@�\���g�p���Ď��o���A�ʐ^�̏��ɂЂ��t�������W�Ƀs����\�����܂��B
�@������A�O��Ɉ��������uViewController.swift�v�t�@�C���ɃR�[�h��lj����Ă����܂��B
�@�O��̋L���ŁuViewController.swift�v�t�@�C���́uviewDidLoad�v���\�b�h����MapView�̐ݒ�Ɋւ���R�[�h��lj����܂����B
override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. self.mapView.rotateEnabled = false self.mapView.pitchEnabled = false let centerCoordinate = CLLocationCoordinate2D(latitude: 35.681382, longitude: 139.766084) let initialSpan = MKCoordinateSpan(latitudeDelta: 0.4, longitudeDelta: 0.4) let initialRegion = MKCoordinateRegion(center: centerCoordinate, span: initialSpan) self.mapView.setRegion(initialRegion, animated: true) }
�@����A����ɃR�[�h��lj����Ă����̂ŁA5�`12�s�ڂ̏����̂ЂƂ܂Ƃ܂��ʂ̃��\�b�h�ɕ����Ă��܂��܂��傤�B
�@Swift�̃��\�b�h�̊�{�`�͈ȉ��̂悤�ɂȂ�܂��B
func ���\�b�h�� (������: �^) -> �߂�l�̌^ { // ���� }
�@MapView�̏����ݒ���s��5�`12�s�ڂ̏������uprepareMapView�v���\�b�h�Ƃ��āuViewController.swift�v�t�@�C���́udidReceiveMemoryWarning�v���\�b�h�̎��̍s�ɒlj����܂��傤�B
�@����lj����郁�\�b�h�͈����E�߂�l�Ƃ��Ɂg�Ȃ��h�Ȃ̂ŁA�ufunc prepareMapView()�v�Ƃ����L�q���\�ł��B�܂��A���̃��\�b�h��viewController.swift�����炵���g�p���Ȃ��̂ŁA�A�N�Z�X�C���q�uprivate�v���w�肵�Ă��܂��B
private func prepareMapView() { self.mapView.rotateEnabled = false self.mapView.pitchEnabled = false let centerCoordinate = CLLocationCoordinate2D(latitude: 35.681382, longitude: 139.766084) let initialSpan = MKCoordinateSpan(latitudeDelta: 0.4, longitudeDelta: 0.4) let initialRegion = MKCoordinateRegion(center: centerCoordinate, span: initialSpan) self.mapView.setRegion(initialRegion, animated: true) }
�@�ȉ��̂悤��viewDidLoad���\�b�h���C�����āuprepareMapView�v���\�b�h���Ăяo���悤�ɂ��܂��傤�B
override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. self.prepareMapView() }
�@���悢��uPhotos Framework�v�̋@�\���g���n�߂܂��B
�@�uViewController.swift�v�t�@�C���̐擪�́uimport MapKit�v�̎��̍s�Ɂuimport Photos�v�Ƃ���1�s��lj����܂��B
import UIKit import MapKit import Photos // �lj�
�@��قǍ쐬�����uprepareMapView�v���\�b�h�ɉ����ē�̃��\�b�h��lj����܂��B
�@�ucheckAuthorizationStatus�v���\�b�h���ł́A�t�H�g���C�u�����ւ̃A�N�Z�X�����`�F�b�N���A�����ł���������߂鏈�����s���܂��B�t�H�g���C�u�����ւ̃A�N�Z�X��������Ă��Ȃ��ƃA���o����ʐ^�̏����擾�ł��܂���̂ŁA�^����Ƀ`�F�b�N�������s���܂��B
�@�uprepareAnnotations�v���\�b�h��MapView�ɒ��߃I�u�W�F�N�g��lj����郁�\�b�h�ł��B�������e�͌�قǐ������܂��B
private func checkAuthorizationStatus() { let status = PHPhotoLibrary.authorizationStatus() switch status { case .Authorized: self.prepareAnnotations() default: PHPhotoLibrary.requestAuthorization{ status in if status == .Authorized { self.prepareAnnotations() } } } } private func prepareAnnotations() { // ���� }
�@�܂��A�ȉ��̂悤��viewDidLoad���\�b�h�C�����āucheckAuthorizationStatus�v���\�b�h���Ăяo���悤�ɂ��Ă����܂��B
override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. self.prepareMapView() self.checkAuthorizationStatus() }
�@�ucheckAuthorizationStatus�v���\�b�h���̃R�[�h�����Ă����܂��傤�B
�@�uPHPhotoLibrary�v�N���X�́uauthorizationStatus�v���\�b�h���g�p����ƁA�uPHAuthorizationStatus�v�^�̒l���擾�ł��܂��B
�@�uPHAuthorizationStatus�v�^�̊e�l�̓��e�͈ȉ��̒ʂ�ł��B
�X�e�[�^�X | ���e |
---|---|
NotDetermined | �A�v���ɗ^���錠�������[�U�[�͂܂��I�����Ă��Ȃ� |
Restricted | �t�H�g���C�u�����ւ̃A�N�Z�X��������Ă��Ȃ��i�y�A�����^���R���g���[���Ȃǁj |
Denied | �t�H�g���C�u�����ւ̃A�N�Z�X�����[�U�[�������I�ɋ��ۂ��Ă��� |
Authorized | �t�H�g���C�u�����ւ̃A�N�Z�X�����[�U�[�������Ă��� |
�@�܂��A�uPHAuthorizationStatus�v�^�̒�`�͈ȉ��̂悤�ɂȂ��Ă��܂��B
enum PHAuthorizationStatus : Int { case NotDetermined case Restricted case Denied case Authorized }
�@switch������case���ł́ucase .Authorized:�v�ƋL�q���Ă��܂����A����́ucase PHAuthorizationStatus.Authorized:�v�Ƃ����L�q�̊ȗ��\�L�ł��B�萔�ustatus�v�̌^���^���_�Ō��܂邽�߁A���̂悤�ȋL�q���\�ɂȂ��Ă��܂��B
Copyright © ITmedia, Inc. All Rights Reserved.