Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom map view crashing in initWithCoder in swift

I have a UITableViewCell, which has a map view as subview. Created a Custom class which handles all the actions like adding annotations, delegates etc and named it as CustomeMap.swift.

After I changed class name of map view to CustomeMap in Cell Xib, app crashes some times at initWithCoder method of CustomeMap because I have not implemented that method. Later I added the method as below:

import UIKit
import MapKit


protocol CustomeMapDelegate{
    func annotationClickEvent(info:NSDictionary?)
}

class CustomeMap: MKMapView,MKMapViewDelegate,CLLocationManagerDelegate {

    var locationManager:CLLocationManager?
    var delegate1:CustomeMapDelegate?
    var tap:UITapGestureRecognizer?

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func awakeFromNib()
    {
        if CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedAlways {
            self.showsUserLocation = true;
        }
        else
        {
            locationManager = CLLocationManager()
            locationManager?.delegate = self;
            if locationManager!.respondsToSelector("requestAlwaysAuthorization")
            {
                locationManager?.requestAlwaysAuthorization()
            }
        }
        self.delegate = self
        self.userInteractionEnabled = true
        tap = UITapGestureRecognizer(target: self, action: "calloutAction:")
    }
    func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus)
    {
        if(status == CLAuthorizationStatus.AuthorizedAlways)
        {
            self.showsUserLocation = true;
        }
    }

    func skipTouch()
    {

    }

    class func createAnnotationForDict(temp:NSDictionary)->MyPointAnnotation
    {
        var venue = temp.valueForKey("venue") as? NSDictionary
        var lat = venue!.valueForKey("latitude") as! Double
        var lon = venue!.valueForKey("longitude") as! Double
        var venueName = venue!.valueForKey("name") as! String
        var dealName = temp.valueForKey("title") as? String

        var coord:CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 0, longitude: 0)
        coord.latitude = lat
        coord.longitude = lon
        var point = MyPointAnnotation()
        point.userInfo = temp
        point.coordinate = coord
        point.title = dealName
        point.subtitle = venueName
        return point
    }

    func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView!
    {
        if annotation is MKUserLocation {
            return nil
        }

        let reuseId = "pin"

        var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
        if pinView == nil {
            pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
            pinView!.canShowCallout = true
            pinView!.animatesDrop = true
            pinView!.pinColor = .Red

            var assImg = UIImageView(image: UIImage(named: "r_arrow_g"))
            assImg.frame = CGRectMake(0, 0, 16, 16)
            assImg.contentMode = UIViewContentMode.ScaleAspectFit
            pinView!.rightCalloutAccessoryView = assImg

        }
        else {
            pinView!.annotation = annotation
        }
        return pinView
    }
    func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKAnnotationView!)
    {
        if view.annotation.isKindOfClass(MKUserLocation){ return }
        if let t1 = tap
        {

        }
        else
        {
            tap = UITapGestureRecognizer(target: self, action: "calloutAction:")
        }
        view.addGestureRecognizer(tap!)
    }
    func mapView(mapView: MKMapView!, didDeselectAnnotationView view: MKAnnotationView!)
    {
        if view.annotation.isKindOfClass(MKUserLocation){ return }
        view.removeGestureRecognizer(tap!)
    }
    func calloutAction(sender:UITapGestureRecognizer)
    {
        if(delegate1 != nil)
        {
            var view = sender.view as! MKAnnotationView
            var ann = view.annotation as! MyPointAnnotation
            delegate1!.annotationClickEvent(ann.userInfo)
        }
    }

}

but still same issue.

Crash log

Crashed: com.apple.main-thread
EXC_BAD_ACCESS KERN_INVALID_ADDRESS at 0x00000001 


    Thread : Crashed: com.apple.main-thread
0  libGPUSupportMercury.dylib     0x2b7228fe gpus_ReturnNotPermittedKillClient
1  libGPUSupportMercury.dylib     0x2b7233cb gpusSubmitDataBuffers
2  libGPUSupportMercury.dylib     0x2b723249 gldCreateContext
3  GLEngine                       0x2717191b gliCreateContextWithShared
4  OpenGLES                       0x2724dab3 -[EAGLContext initWithAPI:properties:] + 406
5  OpenGLES                       0x2724d86f -[EAGLContext initWithAPI:sharedWithCompute:] + 142
6  VectorKit                      0x2fc2d58b ggl::OESContext::OESContext(ggl::GLDevice*, std::__1::shared_ptr<ggl::OESSharegroup>) + 530
7  VectorKit                      0x2fc2663f ggl::GLDevice::createRenderer() + 110
8  VectorKit                      0x2fb46c0f -[MDDisplayLayer _createGLLayer] + 166
9  VectorKit                      0x2fb469af -[MDDisplayLayer init] + 70
10 VectorKit                      0x2f8763b7 -[VKMapView initWithGlobe:shouldRasterize:inBackground:] + 486
11 MapKit                         0x25f4937f -[MKBasicMapView initWithFrame:andGlobe:shouldRasterize:] + 362
12 MapKit                         0x25f7b14b -[MKMapView _commonInitFromIB:gestureRecognizerHostView:showsAttribution:] + 982
13 MapKit                         0x25f7bc4d -[MKMapView initWithCoder:] + 128
14 AppName                        0x000ce410 @objc AppName.CustomeMap.init (AppName.CustomeMap.Type)(coder : ObjectiveC.NSCoder) -> AppName.CustomeMap (CustomeMap.swift)
15 UIKit                          0x280f6611 -[UIClassSwapper initWithCoder:] + 192
16 UIKit                          0x281bd6ef UINibDecoderDecodeObjectForValue + 850
17 UIKit                          0x281bd38f -[UINibDecoder decodeObjectForKey:] + 334
18 UIKit                          0x280f6253 -[UIRuntimeConnection initWithCoder:] + 150
19 UIKit                          0x281bd6ef UINibDecoderDecodeObjectForValue + 850
20 UIKit                          0x281bd645 UINibDecoderDecodeObjectForValue + 680
21 UIKit                          0x281bd38f -[UINibDecoder decodeObjectForKey:] + 334
22 UIKit                          0x280f593f -[UINib instantiateWithOwner:options:] + 958
23 UIKit                          0x28014077 -[UIViewController _loadViewFromNibNamed:bundle:] + 238
24 UIKit                          0x27e52e99 -[UIViewController loadView] + 92
25 UIKit                          0x27d370ed -[UIViewController loadViewIfRequired] + 68
26 UIKit                          0x27de0ed5 -[UINavigationController _layoutViewController:] + 32
27 UIKit                          0x27de0dfd -[UINavigationController _updateScrollViewFromViewController:toViewController:] + 228
28 UIKit                          0x27de0393 -[UINavigationController _startTransition:fromViewController:toViewController:] + 74
29 UIKit                          0x27de00c3 -[UINavigationController _startDeferredTransitionIfNeeded:] + 578
30 UIKit                          0x27ddfe2d -[UINavigationController __viewWillLayoutSubviews] + 44
31 UIKit                          0x27ddfdc1 -[UILayoutContainerView layoutSubviews] + 184
32 UIKit                          0x27d347ff -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 514
33 QuartzCore                     0x2775a835 -[CALayer layoutSublayers] + 136
34 QuartzCore                     0x2775620d CA::Layer::layout_if_needed(CA::Transaction*) + 360
35 QuartzCore                     0x27756095 CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 16
36 QuartzCore                     0x27755a71 CA::Context::commit_transaction(CA::Transaction*) + 224
37 QuartzCore                     0x27755875 CA::Transaction::commit() + 324
38 UIKit                          0x27d2cc91 _afterCACommitHandler + 132
39 CoreFoundation                 0x247e0ffd __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 20
40 CoreFoundation                 0x247de6bb __CFRunLoopDoObservers + 278
41 CoreFoundation                 0x247deac3 __CFRunLoopRun + 914
42 CoreFoundation                 0x2472c3b1 CFRunLoopRunSpecific + 476
43 CoreFoundation                 0x2472c1c3 CFRunLoopRunInMode + 106
44 GraphicsServices               0x2bd59201 GSEventRunModal + 136
45 UIKit                          0x27d9643d UIApplicationMain + 1440
46 AppName                        0x000c1d68 main (AppDelegate.swift:17)

Removed Subclassing concept, just created one view and added MKMapView as sub view. but now i am seeing following crash

Crash log

Thread : Crashed: com.apple.main-thread
 0  libGPUSupportMercury.dylib     0x2fc468fe gpus_ReturnNotPermittedKillClient
 1  libGPUSupportMercury.dylib     0x2fc473cb gpusSubmitDataBuffers
 2  libGPUSupportMercury.dylib     0x2fc47249 gldCreateContext
 3  GLEngine                       0x2b87393b gliCreateContextWithShared
 4  OpenGLES                       0x2b94fab3 -[EAGLContext initWithAPI:properties:] + 406
 5  OpenGLES                       0x2b94f86f -[EAGLContext initWithAPI:sharedWithCompute:] + 142
 6  VectorKit                      0x33ffde8b ggl::OESContext::OESContext(ggl::GLDevice*, std::__1::shared_ptr<ggl::OESSharegroup>) + 530
 7  VectorKit                      0x33ff6e77 ggl::GLDevice::createRenderer() + 110
 8  VectorKit                      0x33f1399b -[MDDisplayLayer _createGLLayer] + 166
 9  VectorKit                      0x33f1373b -[MDDisplayLayer init] + 70
 10 VectorKit                      0x33c3eae3 -[VKMapView initWithGlobe:shouldRasterize:inBackground:] + 486
 11 MapKit                         0x2a6586d7 -[MKBasicMapView initWithFrame:andGlobe:shouldRasterize:] + 362
12 MapKit                         0x2a68a67f -[MKMapView _commonInitFromIB:gestureRecognizerHostView:showsAttribution:] + 1018
13 MapKit                         0x2a676b83 -[MKMapView initWithFrame:] + 130
14 UIKit                          0x2c44df19 -[UIView init] + 44
15 AppName                        0x000f7598 @!objc ObjectiveC.MKMapView.init (ObjectiveC.MKMapView.Type)() -> ObjectiveC.MKMapView (CustomeMap.swift)
16 AppName                        0x000f4f1c ObjectiveC.MKMapView.__allocating_init (ObjectiveC.MKMapView.Type)() -> ObjectiveC.MKMapView (CustomeMap.swift)
17 AppName                        0x000ef614 AppName.CustomeMap.awakeFromNib (AppName.CustomeMap)() -> () (CustomeMap.swift:31)
18 AppName                        0x000f06ec @objc AppName.CustomeMap.awakeFromNib (AppName.CustomeMap)() -> () (CustomeMap.swift)
19 UIKit                          0x2c7ee561 -[UINib instantiateWithOwner:options:] + 1680
20 UIKit                          0x2c70d303 -[UIViewController _loadViewFromNibNamed:bundle:] + 238
21 UIKit                          0x2c54c805 -[UIViewController loadView] + 92
22 UIKit                          0x2c430af9 -[UIViewController loadViewIfRequired] + 68
23 UIKit                          0x2c4dab75 -[UINavigationController _layoutViewController:] + 32
24 UIKit                          0x2c4daa9d -[UINavigationController _updateScrollViewFromViewController:toViewController:] + 228
25 UIKit                          0x2c4da033 -[UINavigationController _startTransition:fromViewController:toViewController:] + 74
26 UIKit                          0x2c4d9d63 -[UINavigationController _startDeferredTransitionIfNeeded:] + 578
27 UIKit                          0x2c4d9acd -[UINavigationController __viewWillLayoutSubviews] + 44
28 UIKit                          0x2c4d9a61 -[UILayoutContainerView layoutSubviews] + 184
29 UIKit                          0x2c42e24f -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 514
30 QuartzCore                     0x2be56a0d -[CALayer layoutSublayers] + 136
31 QuartzCore                     0x2be523e5 CA::Layer::layout_if_needed(CA::Transaction*) + 360
32 QuartzCore                     0x2be5226d CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 16
33 QuartzCore                     0x2be51c51 CA::Context::commit_transaction(CA::Transaction*) + 224
34 QuartzCore                     0x2be51a55 CA::Transaction::commit() + 324
35 UIKit                          0x2c4266e5 _afterCACommitHandler + 132
36 CoreFoundation                 0x28f34d95 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 20
37 CoreFoundation                 0x28f32453 __CFRunLoopDoObservers + 278
38 CoreFoundation                 0x28f3285b __CFRunLoopRun + 914
39 CoreFoundation                 0x28e803c1 CFRunLoopRunSpecific + 476
40 CoreFoundation                 0x28e801d3 CFRunLoopRunInMode + 106
41 GraphicsServices               0x302550a9 GSEventRunModal + 136
42 UIKit                          0x2c48ffa1 UIApplicationMain + 1440
43 AppName                        0x000eb500 main (AppDelegate.swift:21)
44 libdyld.dylib                  0x36fa6aaf start + 2

Somebody please help to find what I'm missing here.

like image 412
naresh Avatar asked May 08 '15 05:05

naresh


1 Answers

It looks like the subclass is messing with the original initialization methods. The docs for MKMapView say that you should not subclass, but use the delegate for everything you need.

Although you should not subclass the MKMapView class itself, you can get information about the map view’s behavior by providing a delegate object.

I suggest you create a custom UIView subclass, which contains a MKMapView and acts as its delegate. This is the first step I'd take to try to resolve this.

like image 71
Jernej Strasner Avatar answered Oct 22 '22 19:10

Jernej Strasner