class ViewModel : NSObject, ObservableObject, ASWebAuthenticationPresentationContextProviding {
private var authSession: ASWebAuthenticationSession?
  func signInWithOpenID(provider: OAuthProvider) {
        let url = getOIDCAuthenticationURL(provider: provider)
        
        authSession?.cancel()
        authSession = nil
        
        authSession = ASWebAuthenticationSession(url: url, callbackURLScheme: "com.ninjanutri") { callbackURL, error in
            if let error = error {
                print("Error: \(error.localizedDescription)")
                return
            }
            
            guard let callbackURL = callbackURL else { return }
            
            guard let idToken = callbackURL.valueOf("id_token") else { return }
            
            self.signInWithIdToken(provider: provider, idToken: idToken)
        }
        
        authSession?.prefersEphemeralWebBrowserSession = false
        authSession?.presentationContextProvider = self
        authSession?.start()
    }
  public func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {        
        return ASPresentationAnchor()
    }
}
struct ContentView: View {
  @StateObject private var viewModel = ViewModel()
  var body: some View {
     Button {
      viewModel.signInWithOpenID(provider: .github)
     } label: {
        Text("Test")
     }
  }
}
when the prefersEphemeralWebBrowserSession is false, the alert and webview is totally working fine in Simulator and Real device, but not XCode Preview. Is this behaviour expected or it's a bug?
                    
                  
                Xcode Previews
RSS for tagCreate an app UI and configure almost everything your users see using Xcode Previews.
Posts under Xcode Previews tag
            
              
                76 Posts
              
            
            
              
                
              
            
          
          
  
    
    Selecting any option will automatically load the page
  
  
  
  
    
  
  
              Post
Replies
Boosts
Views
Activity
                    
                      Hi,
I want to run the live preview on my iPhone, but I received the error message that the app failed to launch.
How can I fix this? Everything is made with SwiftUI
Thanks
                    
                  
                
                    
                      I recieve the error message "XOJITError: Could not create code file directory for session: Permission denied". I'm Using Xcode 16.2.
It is an AppKit project using SwiftUI and it also happens if I create a clean project.
Error log is attached.
previews_diagnostics_summary.txt
                    
                  
                
                    
                      When I attempt to preview my code, I encounter an error message stating, “Cannot preview in this file: Failed to launch (App Name).” This issue is specific to this particular app. I attempted creating a new project, and that works fine.
This is what it says in the diagnostics:
|  [Remote] JITError
|  
|  ==================================
|  
|  |  [Remote] LLVMError
|  |  
|  |  LLVMError: LLVMError(description: "The file was not recognized as a valid object file")
                    
                  
                
                    
                      SwiftUI preview hangs on "Preparing iPhone Simulator for Previews". Also simulator hangs when trying to build and run. This occurred after I installed iOS 17 simulator. I tried reverting back to an iOS 16 version of the simulator, but haven't figured out how to do that. Build and run to an iPhone device works fine. Any ideas would be appreciated. Thanks.
                    
                  
                
                    
                      Hiya,
I am having issues trying to use previews in the xcode 15 beta, i am using an M2 macbook air with 8gb of ram.
I am using MacOS Sonoma and iOS 17.0 Beta 1 and am struggling using previews, the base simulator works but no previews.
I am not receiving any error so am very confused.
Any help would be majorly appreciated.
Thanks,
Gus
                    
                  
                
                    
                      Hello everyone, I've encountered an issue with SwiftUI Preview and would appreciate your help.
Problem Description In a mixed Objective-C and Swift project, I'm experiencing the following situation with SwiftUI Preview:
Crashes when previewing on a physical device
The same code runs normally when launched directly through Xcode
Project uses CocoaPods for dependency management with static frameworks
Project Environment:
Project Type: Mixed (Objective-C + Swift)
Dependency Management: CocoaPods
Dependency Type: Static frameworks
Device: Physical device preview
Steps to Reproduce:
Write Preview code in SwiftUI file
Select physical device as Preview device
Click Preview button
Application crashes
                    
                  
                
                    
                      When working on my Xcode project and using Previews from time to time I find preview crashes and ens up with the image below. The UI is completely unresponsive and only way to bring it back I find is close and relaunch Xcode.
Anyone else experience this - is there a known fix or a better way to "kick" preview to restart that doesn't involve full relaunch of XCode
                    
                  
                
                    
                      Hi there, i just want to know, why my xcode is not showing any print in my Xcode, when i build it, it tell me "Ready to continue xx" but the result of my code doesnt appear in right panel
                    
                  
                
                    
                      Is there any way to stop previews from live updating? It's constantly running and that's nice when I'm developing, but I'd like it to pause updating when I'm not using the Xcode. It heats up my m1 air and drains unnecessary power reducing the battery levels significantly.
                    
                  
                
                    
                      My preview crashed because it could not access my font resources. I am working on a Framework, and it seems that for frameworks, the resources are not copied into the app package of the preview.
Investigation:
I verified this by examining the contents of the preview: /Users/YOUR_NAME/Library/Developer/Xcode/UserData/Previews/Simulator Devices/DEVICE_UUID/data/Containers/Bundle/Application/APPLICATION_UUID/APPLICATION_NAME.app
Make sure to replace "YOUR_NAME," "DEVICE_UUID," "APPLICATION_UUID," and "APPLICATION_NAME" with the actual names or UUIDs relevant to your specific situation.
Now right-click on the app and select Show Package Contents.
For previews that work correctly, the name of the associated app is used, and you will see all the resources in the package.
However, my framework was not using the framework name. Instead, it was named XCPreviewAgent.app and did not contain any resources from my framework.
Fix:
As of November 2024 (Xcode 16.0 and Xcode 16.1), the fix is to use Legacy Preview Execution.
In the menu, select Editor > Canvas > Legacy Preview Execution.
Please leave a comment if this is fixed in a newer Xcode version.
                    
                  
                
                    
                      I got a preview crash info as this:
xxx crashed due to fatalError in PersistentModel.swift at line 559.
This KeyPath does not appear to relate RecordInfo to anything - \RecordInfo.<computed 0x00000001921dfb4e (Array<SomeInfo>)>
If I change preview device to iPhoneSE, It is no crash.
I think the reason is I have some dirty data in iPhone16 Pro preview device.
If I'm right and how to delete it?
Model of RecordInfo like this:
@Model
class RecordInfo {
    @Relationship(deleteRule: .cascade) var someInfo:[SomeInfo]
}
@Model
class SomeInfo {
    var date: Date
    var info: String
}
                    
                  
                
                    
                      Views can be viewed on the canvas in XCode via the code below.
#Preview {
    ContentView()
}
But how do I preview an App class in XCode?
Background: I'd like to preview various settings done in the Scene of the App.
                    
                  
                
                    
                      I can't seem to access preview for any of my views in any of my swift projects at all (using Mac to code and using iPhone 16 pro as preview and simulation, simulation can work), even after restarting Mac and Xcode itself.
The preview section has a popup saying "Cannot preview in this file, unexpected error occured" (for every file), please tell me what is wrong and help me solve it to see the preview, thank you!
details
                    
                  
                
              
                
              
              
                
                Topic:
                  
	
		Developer Tools & Services
  	
                
                
                SubTopic:
                  
                    
	
		Xcode
		
  	
                  
                
              
              
                Tags:
              
              
  
  
    
      
      
      
        
          
            Swift Student Challenge
          
        
        
      
      
    
      
      
      
        
          
            iOS
          
        
        
      
      
    
      
      
      
        
          
            Xcode Previews
          
        
        
      
      
    
      
      
      
        
          
            SwiftUI
          
        
        
      
      
    
  
  
              
                
                
              
            
          
                    
                      Hello everyone,
I’m currently developing my first VisionOS app in Xcode, starting with the default "Hello World" code provided when creating a new VisionOS Mixed Reality App. However, I’m facing some issues with performance and previewing that I can’t seem to resolve.
When I load the preview, it takes an extremely long time, and sometimes it doesn’t load at all. Even when I try to run the app in the VisionOS Simulator, the simulator shows an endless black screen and never displays the intended view. I’ve made no changes to the code, so it’s purely the base setup.
Here are my system details:
Xcode version: 16.1, VisionOs 2.0;
macOS version: 15.0.1;
Hardware: MacBook Air 2020 M1
I’ve tried restarting Xcode and my machine, but the issue persists. Has anyone else faced similar problems or have any suggestions for fixing this? Or is my hardware simply too weak? Any help would be greatly appreciated!
Thank you in advance!
                    
                  
                
                    
                      I'm developing an app which supports for connect with an BLE device. There are some views for showing data from bluetooth. The code below for an example:
    @EnvironmentObject var bleManager: BLEManager // a class implemented CBCentralManagerDelegate
    var body: some View {
        // bleConnected is a CBPeripheral
        if let bleConnected = bleManager.bleConnected {
            if let services = bleConnected.services {
                ForEach(services, id:\.self) { service in
                    // UI for print uuid, descriptions ...
                    showServiceDescriptionView(services)
                    if let characteristics = service.characteristics {
                        ForEach(characteristics, id:\.self) { characteristic in
                            // UI for print uuid, data for each characteristic
                            showCharacteristicsDetailView(characteristic)
                        }
                    }
                }
            }
        }
    }
    
}
But it seems that Xcode Preview do not support connect to a real bluetooth device. So the preview window in Xcode will always be a empty view.
I only can see the data on the real device. But it's important for me to design UI on the preview. So are there any method to mock a fake CBPeripheral in the Previewer? Or are there any other way to achieve this?