问题经过一些研究后重新编辑,现在问题已在标题中说明(特别是这与the Swifter framework一起使用)。我有:
一个。单页XCode 6.3 Beta iPhone项目;
湾我在 Info.plist 中添加 URL类型到 CFBundleURLTypes (通过项目中的 Info 标签)设置),看起来像这样:
...
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLName</key>
<string>com.example.SwifterTest4</string>
<key>CFBundleURLSchemes</key>
<array>
<string>swifter</string>
</array>
</dict>
</array>
...
℃。因此AppDelegate.swift编码:
import SwifteriOS
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate
{
var window: UIWindow?
func application ( application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject? ) -> Bool
{
println( "application() openURL: " + url.absoluteString! );
Swifter.handleOpenURL( url )
return true
}
}
d。和ViewController.swift编码:
import SwifteriOS
import UIKit
class ViewController: UIViewController
{
var swifter: Swifter
required init ( coder aDecoder: NSCoder )
{
let oauthToken: String = NSUserDefaults.standardUserDefaults().valueForKey("oauth_token") as! String
let oauthTokenSecret: String = NSUserDefaults.standardUserDefaults().valueForKey("oauth_secret") as! String
// these two actually contain useful values
let consumerKey = "my-consumer-key"
let consumerSecret = "my-consumer-secret"
self.swifter = Swifter( consumerKey: consumerKey, consumerSecret: consumerSecret, oauthToken: oauthToken, oauthTokenSecret: oauthTokenSecret )
super.init( coder: aDecoder )
}
override func viewDidLoad ()
{
super.viewDidLoad()
swifter.authorizeWithCallbackURL( NSURL( string: "swifter://success" )!,
success:
{
( accessToken: SwifterCredential.OAuthAccessToken?, response: NSURLResponse ) in
println( "SUCCESS" )
},
failure:
{
( error: NSError ) in
println( "FAILURE" )
})
}
}
AppDelegate中的application( ... )
func未被调用,而this official Apple documentation page的{em>注册自定义网址方案部分和this SO post表示添加了CFBundleTypeRole
中的Info.plist
足以强制拨打电话。但似乎没有。
参考下面的@ rmaddy的评论,我也试过这个稳定版本的XCode 6.2也无济于事,虽然即使它确实有效也没那么有用,因为the Swifter framework已经更新到过去一周Swift 1.2。如果没有调用application( ... )
[推测],fatal error: unexpectedly found nil while unwrapping an Optional value
会在let oauthToken: String
ViewController.init()
投放oauthToken
。或者,如果省略oauthTokenSecret
和FAILURE
,则会在控制台上打印{{1}}。
我将不胜感激,谢谢。