Rewrite the company OC Project with Swift (Day2--swift How to call the OC Class Library, the integration of the Baidu map developed by Swift and how to turn off ATS)

Source: Internet
Author: User

Next to a blog post, we've created the bridging file. So how to call?

At the beginning of the project, I wrote this project using Cocoapod to manage dependent libraries, here, or more records.

1 in your already installed cocoapod computer, open the terminal, enter the pod search Baidumapapi, the results are as follows:

The haircut is what we need, and the pod ' Baidumapapi ' ~>2.8.1 is added and saved in the podfile of our project.

2 Add up the class library will of course need to be installed into the project, so enter the pod install and wait for the installation to complete

3 Reopen the project and find the project structure as follows:

Then open our bridge file, referring to the Baidu map header file

Command + B try to compile successfully, the next step is successful.

4 Originally the project should enter the first page, but since the above is written how to use the Baidu Map API, then directly written here, the following write to use Baidu Map page will not write.

The code is as follows:

① first in the Appdelegate Add red box code, inside the cutting head part needs you to Baidu website to take the project bundle identify to generate a key, do not follow my this write Ah

② then the code in the controller is as follows:

Import UIKit

Class Viewcontroller:uiviewcontroller, Bmkmapviewdelegate, bmklocationservicedelegate{

var mapview:bmkmapview!

var locservice:bmklocationservice!

Override Func Viewdidload () {

Super.viewdidload ()

Additional setup after loading the view, typically from a nib.

Mapview = Bmkmapview (frame:self.view.frame)//CREATE view of Map

Self.view = Mapview//Let Baidu map view become the Controller view

Set positioning accuracy

Bmklocationservice.setlocationdesiredaccuracy (Kcllocationaccuracybest)

Specify a minimum distance update in meters

Bmklocationservice.setlocationdistancefilter (10)

Initializing Location Services

Locservice = Bmklocationservice ()

Start Location Services

Locservice. Startuserlocationservice ()

Mapview.showsuserlocation = False

Setting the position tracking state

Mapview.usertrackingmode = Bmkusertrackingmodenone

Show Anchor Layer

Mapview.showsuserlocation = True

Set whether multipoint (zoom) is supported

Mapview. zoomenabled = True

Up to 19 levels can be displayed in the mobile scale

Mapview. zoomlevel = 19

}

Positioning agent

After the user direction changes

Func didupdateuserheading (userlocation:bmkuserlocation!) {

Mapview.updatelocationdata (userlocation)

}

After the location update

Func didupdatebmkuserlocation (userlocation:bmkuserlocation!) {

Mapview.updatelocationdata (userlocation)

Move location to the middle of the screen

Mapview setcentercoordinate (userLocation.location.coordinate, Animated:true)

}

Override func Viewwillappear (Animated:bool) {

Mapview.viewwillappear ()

Mapview.delegate = self//the agent for the Mapview and positioned Locservice is the controller when the controller view is going to be displayed

Locservice.delegate = Self

}

Override func Viewwilldisappear (Animated:bool) {

Mapview.viewwilldisappear ()

Mapview.delegate = nil//In the controller view will disappear when set Mapview and the location of the Locservice agent for nil, specific reasons to see Baidu Map API, there are instructions

Locservice.delegate = Nil

}

5 after the code is written, when you run the program may find that cannot be positioned, then this may be your plist file less added something, together to send up;

  

Here is the choice of one, I have chosen to run in the background, you can choose another property, use time to run, as for key I do not remember. I can find it in Baidu Map Forum.

Here incidentally, after the iOS9, Apple opened ATS, the service is actually need to support HTTPS, it is estimated that Apple's goal is to reverse force developers to enhance their security, but sometimes the company does not want to spend money to buy the HTTPS certificate, after all, people like free, HTTP is available, why spend one thousand or two thousand a year to buy a certificate? So here's a way to close the ATS and add them in the Plist file as well.

After setting this, some third-party libraries such as AFN can be used normally.

Today's content is complete. Continue tomorrow ...

Rewrite the company OC Project with Swift (Day2--swift How to call the OC Class Library, the integration of the Baidu map developed by Swift and how to turn off ATS)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.