0

I work on a ios application that requires to use nearby places api from google. It worked very well with swift but now , when i updated my xcode to xcode 7 and it changed from swift to swift 2 it doesn't work anymore. I am stuck with this , any help will be apreciated .

Here is the request for the api :

func fetchPlacesNearCoordinate(coordinate: CLLocationCoordinate2D, radius: Double, name : String){
    var urlString = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=\(44.942149),\(26.02453)&radius=\(radius)&types=food"
    urlString += "&key=\(apiServerKey)"

    urlString = urlString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!

    if placesTask.taskIdentifier > 0 && placesTask.state == .Running {
        placesTask.cancel()
    }

    UIApplication.sharedApplication().networkActivityIndicatorVisible = true

    do{

        //******************Here's the line that displays error

        placesTask = session.dataTaskWithURL(NSURL(string: urlString)!) {
            (data, response, error) -> Void in
            UIApplication.sharedApplication().networkActivityIndicatorVisible = false
            //var placesArray = [GooglePlace]()
            do {
                if let json = try NSJSONSerialization.JSONObjectWithData(data!, options:[]) as? NSDictionary {
                    if let results = json["results"] as? NSArray {
                        for rawPlace:AnyObject in results {
                            print(rawPlace)
                            var placename = ""
                            if let name = rawPlace["name"] as? NSString {
                                placename = name as String
                            }
                        }
                    }
                }
            } catch {
                //handle error
            }
            /*dispatch_async(dispatch_get_main_queue()) {
            completion(placesArray)
            }*/
        }
    }catch{
    }

    placesTask.resume()
}

And the errors i get :

2015-10-02 16:57:54.586 QR Code[4331:67414] -[NSURLSessionDataTask taskIdentifier]: unrecognized selector sent to instance 0x7fe791c3b840
2015-10-02 16:57:54.591 QR Code[4331:67414] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSURLSessionDataTask taskIdentifier]: unrecognized selector sent to instance 0x7fe791c3b840'

*** First throw call stack:(
0   CoreFoundation                      0x0000000111a8ef65 __exceptionPreprocess + 165
1   libobjc.A.dylib                     0x0000000111506deb objc_exception_throw + 48
2   CoreFoundation                      0x0000000111a9758d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3   CoreFoundation                      0x00000001119e4f7a ___forwarding___ + 970
4   CoreFoundation                      0x00000001119e4b28 _CF_forwarding_prep_0 + 120
5   QR Code                             0x000000010f57b3ff _TFC7QR_Code17MapViewController25fetchPlacesNearCoordinatefS0_FTVSC22CLLocationCoordinate2D6radiusSd4nameSS_T_ + 1215
6   QR Code                             0x000000010f57bfd3 _TFC7QR_Code17MapViewController11viewDidLoadfS0_FT_T_ + 1555
7   QR Code                             0x000000010f57c552 _TToFC7QR_Code17MapViewController11viewDidLoadfS0_FT_T_ + 34
8   UIKit                               0x00000001122ae931 -[UIViewController loadViewIfRequired] + 1344
9   UIKit                               0x00000001122b4923 -[UIViewController __viewWillAppear:] + 120
10  UIKit                               0x00000001122e418a -[UINavigationController _startCustomTransition:] + 1177
11  UIKit                               0x00000001122f37c7 -[UINavigationController _startDeferredTransitionIfNeeded:] + 712
12  UIKit                               0x00000001122f467d -[UINavigationController __viewWillLayoutSubviews] + 57
13  UIKit                               0x000000011248c63d -[UILayoutContainerView layoutSubviews] + 248
14  UIKit                               0x00000001121d411c -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 710
15  QuartzCore                          0x000000010fe7536a -[CALayer layoutSublayers] + 146
16  QuartzCore                          0x000000010fe69bd0 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
17  QuartzCore                          0x000000010fe69a4e _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
18  QuartzCore                          0x000000010fe5e1d5 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277
19  QuartzCore                          0x000000010fe8b9f0 _ZN2CA11Transaction6commitEv + 508
20  UIKit                               0x000000011211e1b6 _UIApplicationHandleEventQueue + 7183
21  CoreFoundation                      0x00000001119bb0a1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
22  CoreFoundation                      0x00000001119b0fcc __CFRunLoopDoSources0 + 556
23  CoreFoundation                      0x00000001119b0483 __CFRunLoopRun + 867
24  CoreFoundation                      0x00000001119afe98 CFRunLoopRunSpecific + 488
25  GraphicsServices                    0x0000000115770ad2 GSEventRunModal + 161
26  UIKit                               0x0000000112123676 UIApplicationMain + 171
27  QR Code                             0x000000010f5782bd main + 109
28  libdyld.dylib                       0x0000000113dd192d start + 1)
libc++abi.dylib: terminating with uncaught exception of type NSException
Vlad Mihai
  • 31
  • 8

1 Answers1

0

Please do not call the Places API Web Services directly from a mobile device - it opens up the potential for external parties to steal your API key from your application. Please follow the Proxy Server pattern laid out in this tutorial by Ray Wenderlich's team.

Brett
  • 2,399
  • 1
  • 17
  • 23