线程 1:EXC_BAD_ACCESS (code=1, address=0x0) 在 swift 中工作时尝试登录到 spotify
Posted
技术标签:
【中文标题】线程 1:EXC_BAD_ACCESS (code=1, address=0x0) 在 swift 中工作时尝试登录到 spotify【英文标题】:Thread 1: EXC_BAD_ACCESS (code=1, address=0x0) When working in swift trying to login to spotify 【发布时间】:2018-04-15 17:17:16 【问题描述】:代码:
//
// AppDelegate.swift
// SplitterSwift3
//
// Created by VideoLabN on 4/8/18.
// Copyright © 2018 VideoLabN. All rights reserved.
//
import UIKit
import AWSAuthCore
import AWSMobileClient
import AWSCore
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate
var window: UIWindow?
var auth = SPTAuth()
// Add a AWSMobileClient call in application:open url
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool
// called when user signs into spotify. Session data saved into user defaults, then notification posted to call updateAfterFirstLogin in ViewController.swift. Modeled off recommneded auth flow suggested by Spotify documentation
if auth.canHandle(auth.redirectURL)
auth.handleAuthCallback(withTriggeredAuthURL: url, callback: (error, session) in
if error != nil
print("error!")
let userDefaults = UserDefaults.standard
let sessionData = NSKeyedArchiver.archivedData(withRootObject: session)
print(sessionData)
userDefaults.set(sessionData, forKey: "SpotifySession")
userDefaults.synchronize()
NotificationCenter.default.post(name: Notification.Name(rawValue: "loginSuccessfull"), object: nil)
)
return true
return false
//Add a AWSMobileClient call in application:didFinishLaunching
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions:
[UIApplicationLaunchOptionsKey: Any]?) -> Bool
return AWSMobileClient.sharedInstance().interceptApplication(
application, didFinishLaunchingWithOptions:
launchOptions)
//
// ViewController.swift
// SplitterSwift3
//
// Created by VideoLabN on 4/8/18.
// Copyright © 2018 VideoLabN. All rights reserved.
//
import UIKit
import SafariServices
import AVFoundation
import AWSAuthCore
import AWSAuthUI
class ViewController: UIViewController, SPTAudiostreamingPlaybackDelegate, SPTAudioStreamingDelegate
// Variables
var auth = SPTAuth.defaultInstance()!
var session:SPTSession!
// Initialzed in either updateAfterFirstLogin: (if first time login) or in viewDidLoad (when there is a check for a session object in User Defaults
var player: SPTAudioStreamingController?
var loginUrl: URL?
// Outlets
@IBOutlet weak var loginSpotify: UIButton!
@IBOutlet weak var loginSplitter: UIButton!
@IBOutlet weak var testLabel: UILabel!
override func viewDidLoad()
print("test")
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.setup()
//NotificationCenter.default.addObserver(self, selector: #selector(ViewController.updateAfterFirstLogin, name: NSNotification.Name(rawValue: "loginSuccessfull"), object: nil)
//self.updateAfterFirstLogin()
func setup ()
// insert redirect your url and client ID below
let redirectURL = "splitter-app://callback" // put your redirect URL here
let clientID = "207ce42c908f42e485c540be11720888" // put your client ID here
auth.redirectURL = URL(string: redirectURL)
auth.clientID = "client id goes here"
auth.requestedScopes = [SPTAuthStreamingScope, SPTAuthPlaylistReadPrivateScope, SPTAuthPlaylistModifyPublicScope, SPTAuthPlaylistModifyPrivateScope]
loginUrl = auth.spotifyWebAuthenticationURL()
//print("test")
func initializePlayer(authSession:SPTSession)
if self.player == nil
self.player = SPTAudioStreamingController.sharedInstance()
self.player!.playbackDelegate = self
self.player!.delegate = self
try! player!.start(withClientId: auth.clientID)
self.player!.login(withAccessToken: authSession.accessToken)
@objc func updateAfterFirstLogin ()
loginSpotify.isHidden = true
let userDefaults = UserDefaults.standard
if let sessionObj:AnyObject = userDefaults.object(forKey: "SpotifySession") as AnyObject?
let sessionDataObj = sessionObj as! Data
let firstTimeSession = NSKeyedUnarchiver.unarchiveObject(with: sessionDataObj) as! SPTSession
self.session = firstTimeSession
initializePlayer(authSession: session)
func audioStreamingDidLogin(_ audioStreaming: SPTAudioStreamingController!)
// after a user authenticates a session, the SPTAudioStreamingController is then initialized and this method called
print("logged in")
//
self.player?.playSpotifyURI("spotify:track:58s6EuEYJdlb0kO7awm3Vp",
startingWith: 0, startingWithPosition: 0, callback: (error) in
// if (error != nil)
// print("playing!")
//
//
// )
@IBAction func spotifyButtonPressed(_ sender: Any)
let svc = SFSafariViewController(url: loginUrl!)
self.present(svc, animated: true, completion: nil)
//UIApplication.shared.open(loginUrl!, options: [:])
应用程序编译得很好,并在模拟器 iPhone 上启动。登录 Spotify 的按钮有效,并打开一个 Safari 实例,提示用户登录 Spotify。
一旦用户登录,它就会请求权限。用户接受权限后,应用程序在 appDelegate 类的第 16 行崩溃并出现以下错误:
线程 1:EXC_BAD_ACCESS(代码=1,地址=0x0)。
我已经在网上阅读了这个,人们说它相当于一个空指针异常,但我找不到导致这个错误的原因。谁能找到错误?
编辑:感谢那些做出回应的人!这是我的控制台输出:
objc[19082]:VCWeakObjectHolder 类在两者中都实现 /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/AVConference.framework/Frameworks/ViceroyTrace .framework/ViceroyTrace (0x12b9174d0) 和 /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/AVConference.framework/AVConference (0x12aa65e38)。将使用两者之一。哪一个是未定义的。 测试 2018-04-15 13:33:50.341600-0400 SplitterSwift3 [19082:1059086] [AXRun-PID] 客户端请求取消暂停 PID:-1 名称: 2018-04-15 13:33:50.441522-0400 SplitterSwift3[19082:1058985] [MC] systemgroup.com.apple.configurationprofiles 的系统组容器 路径是 /Users/videolabn/Library/Developer/CoreSimulator/Devices/CEC32A65-63E0-4499-AB25-6BD13A7AE013/data/Containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles 2018-04-15 13:33:50.442782-0400 SplitterSwift3[19082:1058985] [MC] 从私人有效用户设置中读取。 2018-04-15 13:33:50.536744-0400 SplitterSwift3[19082:1058985] [App] 如果我们在 真正的预提交处理程序,我们实际上不能添加任何新的栅栏,因为 到 CA 限制 (lldb)
【问题讨论】:
如果设置exception breakpoint,它会在哪一行停止? 控制台中有完整的崩溃日志吗?因为我看到几个“!” (强制取消)可能导致此问题或其他任何问题。 感谢@Larme 的评论!我是菜鸟,如何找到完整的崩溃日志? 感谢@MichaelHulet 的评论!我设置了一个通用异常断点,但我仍然在同一行得到相同的错误!我做错了什么还是我只是得到同样的错误? 我们怎么知道第 16 行是什么。从文件顶部开始计数,从方法开始计数,包括空行,不包括空行,包括 cmets,计数时不包括 cmets....第 16 行是什么? 【参考方案1】:UIApplicationDelegate
函数 application(_:open:sourceApplication:annotation:)
在 iOS 10 中已弃用。
从 iOS 11.3 开始,使用此功能的应用程序在调用时似乎会崩溃。解决方案是使用替换application(_:open:options:)
。
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool
if auth.canHandle(auth.redirectURL)
auth.handleAuthCallback(withTriggeredAuthURL: url, callback: (error, session) in
if error != nil
print("error!")
let userDefaults = UserDefaults.standard
let sessionData = NSKeyedArchiver.archivedData(withRootObject: session)
print(sessionData)
userDefaults.set(sessionData, forKey: "SpotifySession")
NotificationCenter.default.post(name: Notification.Name(rawValue: "loginSuccessfull"), object: nil)
)
return true
return false
另外,对于UserDefaults
,无需致电synchronize
【讨论】:
感谢您的帮助!我实现了这个更改,当我在 spotify premission 屏幕上按 OK 时,没有发生任何错误,而不是出现错误。它只是停止。然而,该应用程序并没有崩溃,因此我实现 spotify api 的方式可能存在问题。 它会返回到您的应用程序吗?当loginSuccessfull
通知发布时,您的应用程序会做些什么吗? - 检查您正在观察的通知名称的拼写;您发布的通知末尾有一个双 l。最好在一个地方创建通知名称并引用它们,而不是在可能出现不一致的地方使用文字字符串。以上是关于线程 1:EXC_BAD_ACCESS (code=1, address=0x0) 在 swift 中工作时尝试登录到 spotify的主要内容,如果未能解决你的问题,请参考以下文章
如何修复线程 1:EXC_BAD_ACCESS (code=1, address=0x58) xcode
我的动态数组有问题 - 线程 1:EXC_BAD_ACCESS (code=1, address=0x0)
Swift 在访问 NSManagedObject 的属性时给出错误线程 1:EXC_BAD_ACCESS(code=1, address=0x0)
加载 AVPlayer 时出现错误线程 1:EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
线程 1:EXC_BAD_ACCESS (code=1, address=0x0) 在 swift 中工作时尝试登录到 spotify
GMSGeocoder 在通知服务扩展 iOS 中给出线程 5: EXC_BAD_ACCESS (code=1, address=0x0)