External links

This topic contains 8 replies, has 2 voices, and was last updated by  Mitchell Toy 4 months, 3 weeks ago.

  • April 28, 2016 at 12:12 am #148433

    Hi all. Firstly, thank you Justin for making these excellent kits. It has made the development process a lot easier for me.
    I’m wondering if there is a way in the current kit to link to external URLs or Facebook pages and, if not, which approach is best to add a custom event to the swift extension.

    Best,
    Mitchell

  • April 29, 2016 at 9:33 am #148572

    Sure there’s a function in the Page_Extension.swift file named…

    func yourCustomCodeWithValue(value:String ){
    if (debugMode){
    print(“we called some custom code that YOU wrote with a value of \(value)”)
    }

    }

    You can run that function by calling an event with the key…

    CustomEvent

    and a value of your choosing, which could the URL.

    In which case, change that function to….

    func yourCustomCodeWithValue(value:String ){

    UIApplication.sharedApplication().openURL(NSURL(string: value:String)!)

    }

    I’d say just tread carefully if its a kids app. If I downloaded a book app for my kids to my phone which had a link to Facebook, I’d delete it right away. =) Plus I think thats something you would need to clear with your app reviewer.

    • April 29, 2016 at 9:44 am #148574

      That’s super helpful, thank you. I’ve viewed the latest tute discussing parental locks and am forming some ideas 🙂

  • May 5, 2016 at 8:54 am #149085

    So I’ve given that a shot and it has some compiler errors, just after ‘value’ in the line:
    UIApplication.sharedApplication().openURL(NSURL(string: value:String)!)
    Namely expected ‘,’ separator and expected expression in list of expressions.
    Any clues?

  • May 5, 2016 at 10:17 am #149087

    Just tested this. Worked here…

    if let url = NSURL(string: “http://www.cartoonsmart.com”) {
    UIApplication.sharedApplication().openURL(url)
    }

  • May 5, 2016 at 1:25 pm #149100

    I updated the kit too, so if you download the latest version, just copy and replace the Page.swift file. (Zip a backup first of your entire project of course). Or you can find the //MARK: Events (line 2163) and amend it like so….

     //MARK: Events
        
        func doEvent(eventName:String, eventValue:String) {
            
             if ( eventName == "OpenURL") {
                
                //opens URL in Safari, URL can be changed, allowing unrestricted web access
                
                if let url = NSURL( string: eventValue) {
                    
                    UIApplication.sharedApplication().openURL(url)
                    
                }
                
                
                
            } else if ( eventName == "OpenSingleURL") {
                
                //opens URL that can't be changed. Restricted browser access, probably good for a kids app
                
                if let url = NSURL(string: eventValue) {
                    
                    let vc:UIViewController = self.view!.window!.rootViewController!
                    
                    let safariView = SFSafariViewController(URL: url, entersReaderIfAvailable: true)
                    vc.presentViewController(safariView, animated: true, completion: nil)
                    
                }
                
            }
            
            else if (eventName == "Restore"){
    
    // the rest of.... 
    
    

    So you’re just adding in two new if statements, and then be sure at the top of the file you also import SafariServices. So all your imports will now be…

    import SpriteKit
    import AVFoundation
    import StoreKit
    import SafariServices
    

    The two new properties are…

    • OpenURL – opens URL in Safari, URL can be changed, allowing unrestricted web access
    • OpenSingleURL – opens URL that can’t be changed. Restricted browser access, probably good for a kids app
  • May 5, 2016 at 5:53 pm #149142

    That certainly did the trick – thanks very much!

  • May 5, 2016 at 5:54 pm #149143

    Made a short video on that too =)

  • May 6, 2016 at 2:20 am #149215

    That’s a great insight, thanks Justin.

You must be logged in to reply to this topic.