1

I am developing an iOS app that allows user to save urls, similar to the Pocket app. In the app I have a share extension that basically just save the url into a NSUserDefaults based on the app group. For some reason the share extension causes the mobile safari to hang (being non responsive) after selecting the share extension. The code for the share extension is so simple, I am wondering what may have caused it. On debugging in Xcode, the function in the share extension is not being called at all too it seems. Any clues? This is running on iOS 9.3.

Here is the code:

//
//  ShareViewController.swift
//  intrafeedappShare
//
//  Created by Dicky Johan on 5/21/16.
//  Copyright © 2016 Dicky Johan. All rights reserved.
//

import UIKit
import Social
import MobileCoreServices

class ShareViewController: UIViewController {

    var selectedURL: String?

    override func viewDidLoad() {

        super.viewDidLoad()

        let contentType = kUTTypeURL as String

        guard let item = self.extensionContext?.inputItems.first as? NSExtensionItem else {
                fatalError()
        }

        for attachment in item.attachments as! [NSItemProvider] {
            if attachment.hasItemConformingToTypeIdentifier(contentType) {
                attachment.loadItemForTypeIdentifier(kUTTypeURL as String, options: nil) { url, error in
                    if error == nil {
                        guard let url = url as? NSURL else {
                            self.extensionContext?.cancelRequestWithError(NSError(domain:"Url is empty",code:-1,userInfo: nil))
                            return
                        }
                        self.selectedURL = url.absoluteString

                        let defaults = NSUserDefaults(suiteName: Constants.Settings.sharedAppGroup)
                        if let arrUrls = defaults!.objectForKey(Constants.Settings.sharedURLS) {
                            // append to the existing list
                            arrUrls.appendString(url.absoluteString)

                        } else {
                            let newArrUrl = [url.absoluteString]

                            defaults!.setObject(newArrUrl, forKey: Constants.Settings.sharedURLS)
                        }
                        defaults!.synchronize()

                        self.extensionContext?.completeRequestReturningItems(nil, completionHandler: nil)


                        let alert = UIAlertController(title: "Success", message: "Added url to intrafeed", preferredStyle: .Alert)

                        let action = UIAlertAction(title: "Done", style: .Default) { _ in
                            self.dismissViewControllerAnimated(true, completion: nil)
                        }

                        alert.addAction(action)
                        self.presentViewController(alert, animated: true, completion: nil)

                    } else {
                        self.extensionContext?.cancelRequestWithError(error)

                        let alert = UIAlertController(title: "Error", message: "Error loading url", preferredStyle: .Alert)

                        let action = UIAlertAction(title: "Error", style: .Cancel) { _ in
                            self.dismissViewControllerAnimated(true, completion: nil)
                        }

                        alert.addAction(action)
                        self.presentViewController(alert, animated: true, completion: nil)
                    }


                }
            }
        }


    }


}
dickyj
  • 1,830
  • 1
  • 24
  • 41

1 Answers1

0

Ok, apparently there was a crash in the code, thus causing the Safari to freeze. On debugging the extension in Xcode, I found the issue.

dickyj
  • 1,830
  • 1
  • 24
  • 41