Help with SFSafariViewController and CGContext... warnings

533 views
Skip to first unread message

itsa...@icloud.com

unread,
Aug 10, 2015, 12:10:07 PM8/10/15
to Brighton iPhone Creators
Hello people,

I feel embarrassed to be asking this but I’m totally stuck!

I’d like to use the swanky new Safari View Controller in my Objective-C app.

I’m using Xcode 7 Beta 5 and I’ve added the SafariServices.framework to my (otherwise fine) project, but if I type in SFSafariViewController, code completion offers no match and the I get a red line and ! in the margin. Examples I’ve seen in Swift show “import SafariServices” but #import <SafariServices.h> and variants thereof yield more errors.

Apple’s documentation for Safari says:

"Consider replacing your WKWebView or UIWebView-based browsers with SFSafariViewController if your app displays web content but does not customize that content.” However, WKWebView and UIWebView are links whereas is simply grey text.

A search for SFSafariViewController in Apple’s documentation yields nothing useful so I can’t even see how to use it! Devs are clearly using this yet, with several Swift snippets, reports and “How do I tint it?" questions on Stack Overflow, but I have no idea how to include it in my app. I must be doing something stupidly wrong but have no idea what it is.


My other question is about an app which I wrote initially for iOS 6 and has been rebuilt it each time a new iOS was released and it was fine up to and including iOS 8, however, building it for iOS 9 and using Xcode 7 Beta 5 outputs the following before I even reach -didFinishLaunchingWithOptions:

<Error>: CGContextSaveGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
<Error>: CGContextTranslateCTM: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
<Error>: CGContextRestoreGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

I have set the CG_CONTEXT_SHOW_BACKTRACE which shows a lot of backtrace but talks about nav bars and status bars and the like and as I’ve not changed anything since iOS 8 I don’t know which part of this very long backtrace is of any relevance. I suspect Apple have tightened something with iOS 9 but I don’t know what it is. All my web searches on this come back with people talking about their calls to CGContext with 0 parameters but these warnings appear before any of my code is executed so it must be something to do with my XIB file but wondered if anyone had any pointers to where my issue might be.


Any help with either or both would be much appreciated.

Cheers,


Stephen (in rural Essex!)



"To guess what UK government will do in any situation just ask yourself: What is the most mean-spirited, non-evidence-based and ineffective option?” ~ Marcus Chown

Adam Martin

unread,
Aug 10, 2015, 12:30:17 PM8/10/15
to brighton-iph...@googlegroups.com
"invalid context 0x0." is literally saying "a CGContext variable was null when it shouldn't have been".

I've had it a few times when trying to use Apple CG* calls in code that got invoked as a side-effect of a view loading or displaying (e.g. in viewDidLoad), but that Apple on some versions of iOS would call before they had finished initialising the system graphics.

Solution was usually to check all my code and look for anything making CG calls and have it check that the CGContext (sometimes passed-in by Apple!) wasn't null, and if it was ... return early, and assume the method would get re-called later once Apple had finished the setup.

A few times it was because my own code was trying to do something like find out the background colour so it could chose the right foreground colour, and the API calls I was using were technically correct, but under the hood required that "this UIView has been displayed on screen at least once" because they were using CGContext calls.

...so I'd also look for anything like that, including 3rd party UI code that might be trying to be clever.




--
You received this message because you are subscribed to the Google Groups "Brighton iPhone Creators" group.
To unsubscribe from this group and stop receiving emails from it, send an email to brighton-iphone-cr...@googlegroups.com.
To post to this group, send email to brighton-iph...@googlegroups.com.
Visit this group at http://groups.google.com/group/brighton-iphone-creators.
For more options, visit https://groups.google.com/d/optout.

itsa...@icloud.com

unread,
Aug 11, 2015, 5:53:50 AM8/11/15
to Brighton iPhone Creators
Thanks very much for the reply Adam.

The problem though is that when the messages appear, none of my own code has been executed and I’m not using 3rd party libraries. The issue seems to be something in my mainWindow.xib that is being loaded to start my app. This is what I get with the backtrace enabled:

<Error>: CGContextSaveGState: invalid context 0x0. Backtrace:
  <-[UIStatusBarBatteryItemView contentsImage]+773>
  <-[UIStatusBarItemView updateContentsAndWidth]+36>
    <-[UIStatusBarItemView initWithItem:data:actions:style:]+503>
    <+[UIStatusBarItemView createViewForItem:withData:actions:foregroundStyle:]+159>
      <-[UIStatusBarLayoutManager _createViewForItem:withData:actions:]+168>
      <-[UIStatusBarLayoutManager _prepareEnabledItemType:withEnabledItems:withData:actions:itemAppearing:itemDisappearing:]+3
        <-[UIStatusBarLayoutManager prepareEnabledItems:withData:actions:]+108>
        <-[UIStatusBarForegroundView _setStatusBarData:actions:animated:]+951>
          <-[UIStatusBarForegroundView setStatusBarData:actions:animated:]+874>
          <__51-[UIStatusBar _prepareToSetStyle:animation:forced:]_block_invoke+443>
            <+[UIView(Animation) performWithoutAnimation:]+82>
            <-[UIStatusBar _prepareToSetStyle:animation:forced:]+935>
              <-[UIStatusBar _requestStyleAttributes:animationParameters:forced:]+404>
              <-[UIStatusBar requestStyle:animationParameters:forced:]+490>
                <-[UIStatusBar requestStyle:animated:forced:]+113>
                <-[UIStatusBar forceUpdateStyleOverrides:]+97>
                  <-[UIStatusBar _evaluateServerRegistration]+294>
                  <-[UIStatusBar didMoveToSuperview]+33>
                    <__45-[UIView(Hierarchy) _postMovedFromSuperview:]_block_invoke+683>
                    <-[UIView(Hierarchy) _postMovedFromSuperview:]+484>
                      <-[UIView(Internal) _addSubview:positioned:relativeTo:]+2305>
                      <-[UIView(Hierarchy) addSubview:]+56>
                        <-[UIStatusBarWindow setStatusBar:]+311>
                        <-[UIApplication _createStatusBarWithRequestedStyle:orientation:hidden:]+384>
                          <-[UIApplication _runWithMainScene:transitionContext:completion:]+1047>
                          <__84-[UIApplication _handleApplicationActivationWithScene:transitionContext:completion:]_block_invo
                            <-[UIApplication workspaceDidEndTransaction:]+163>
                            <__37-[FBSWorkspace clientEndTransaction:]_block_invoke_2+71>
                              <__40-[FBSWorkspace _performDelegateCallOut:]_block_invoke+54>
                              <-[FBSSerialQueue _performNext]+184>
                                <-[FBSSerialQueue _performNextFromRunLoopSource]+52>
                                <FBSSerialQueueRunLoopSourceHandler+33>
                                  <__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__+15>
                                  <__CFRunLoopDoSources0+523>
                                    <__CFRunLoopRun+1032>
                                    <CFRunLoopRunSpecific+470>
                                      <CFRunLoopRunInMode+123>
                                      <-[UIApplication _run]+540>
                                        <UIApplicationMain+160>
                                        <main+138>


My app ran with no errors in iOS 8, I simply took the same project and rebuilt with Xcode 7 for iOS 9 and these errors appear.

I’m assuming that there’s something I’ve done wrong with the design of my window objects that I got away with until the latest Xcode/iOS, because none of my other three apps exhibit this issue.

Thanks again for your suggestions. Maybe I need to redesign my mainWindow again from scratch.

Stephen


"To guess what UK government will do in any situation just ask yourself: What is the most mean-spirited, non-evidence-based and ineffective option?” ~ Marcus Chown

Adam Martin

unread,
Aug 11, 2015, 6:48:02 AM8/11/15
to brighton-iph...@googlegroups.com
For this to be crashing:

On 11 August 2015 at 10:53, <itsa...@icloud.com> wrote:
[UIStatusBarBatteryItemView contentsImage]

...in a beta, screams to me "this is why I avoid Apple betas ... like I do the plague".

I would report it to Apple as a bug. Unless you've knowingly customized your Battery icon for the status bar?

Stephen Watson

unread,
Aug 11, 2015, 5:30:52 PM8/11/15
to brighton-iph...@googlegroups.com
Thanks Martin - as I'm still so experienced compared to most of you I assume everything is my fault! I will send a bug report ...

I've fixed my SFSafariViewController issue - I was being stupid - I only needed to #import <SafariServcies/SafariServices.h> then I could use it. The clue was in the Swift code but having never used this call I wasn't able to "translate" initially.

Thanks again.

Cheers,

Stephen

We are Costa of Borg. We will add your vibrancy and distinctiveness to our own. Your town and your customers will service us. Resistance is futile.

Shaun Pryszlak

unread,
Sep 24, 2015, 11:20:53 AM9/24/15
to Brighton iPhone Creators
FWIW I get that with one of my apps.  It has a full screen UIWebView and no status bar.  It is still there in the full release of Xcode 7.  It appears before the first line of didFinishLaunchingWithOptions is executed,  so who knows what is causing it.  The app has been around since iOS 3 or 4, so it may be a legacy thing.

Shaun

Stephen Watson

unread,
Sep 25, 2015, 8:49:28 AM9/25/15
to Brighton iPhone Creators
Yea, thanks Shaun - I think it’s a legacy thing and they are not bothering much. I reported it to Apple as Adam suggested. The app has been successfully submitted to the App Store.

Hey ho …

Stephen
Reply all
Reply to author
Forward
0 new messages