var allIndexPath = [NSIndexPath]()
let nSections = tableView.numberOfSections()
for j in 0 ..< nSections {
let nRows = tableView.numberOfRowsInSection(j)
for i in 0 ..< nRows {
let indexPath = NSIndexPath(forRow: i, inSection: j)
allIndexPath.append(indexPath)
}
}
tableView.beginUpdates()
tableView.deleteRowsAtIndexPaths(allIndexPath, withRowAnimation: UITableViewRowAnimation.None)
tableView.endUpdates()
要获得所有当前可见的 Cell,使用:
let indexPaths = tableView.indexPathsForVisibleRows()
在O-C 文件中使用 import “项目名称-Swift.h” 语句。其中“项目名称”是你的项目名称,例如:#import “ProductName-Swift.h”。
这个文件是 Xcode 自动生成的,它把项目中所有的 Swift 类的接口文件都定义到这个文件中了。注意这些类必须是用 @objc 声明或者继承了 NSObject 的 swift 类。
在多线程操作中,经常要保护某一语句块让它同步执行,iOS 中也有类似 Java 的 Synchronized() 语句。要向同步执行语句,将要同步执行的语句包裹在 objc_sync_enter()/objc_sync_exit() 中即可:
// 同步块,防止打印异常
objc_sync_enter(self)
let progress: NSProgress = object as! NSProgress
println("progress:\(progress.fractionCompleted)")
objc_sync_exit(self)
在沙盒环境,我们可以将 device token 打印到控制台,从而获得 device token。但更换到生产环境(production)后就没有这么方便了。如果想获得生产环境的 device token,可以依照以下步骤:
判断 cer 格式的PUSH证书是否有效非常简单,使用 PushMeBaby\SmartPush 之类的 Mac 应用程序向设备推一条推送通知就可以了。
但是服务器使用的PUSH 证书往往是 .pem(PHP/GoLang服务端)或 .p12(Java/C#服务端)格式的,我们可以用以下 OpenSSL 命令判断服务器 PUSH 证书是否有效,然后再确定是否是服务端代码的问题:
将 PKCS#12 (.pfx .p12) 转成 PEM:
用以下命令转换成同时包含私钥和证书的 pem 文件:
openssl pkcs12 -in apns_production_privatekey.p12 -out apns_production_privatekey.pem -nodes
期间会提示输入密码,请输入 p12 文件导出时的密码。
注意:如果只导出私钥可以加上 -nocerts 参数,只导出证书则可以加上 -nokeys 参数。
测试生产证书:
openssl s_client -connect gateway.push.apple.com:2195 -cert apns_production_privatekey.pem
如果输出如下信息(),表明证书是正常的:
CONNECTED(00000003) depth=1 /C=US/O=Entrust,
Inc./OU=www.entrust.net/rpa is incorporated by reference/OU=(c) 2009
Entrust, Inc./CN=Entrust Certification Authority - L1C verify
error:num=20:unable to get local issuer certificate verify return:0
— Certificate chain 0 s:/C=US/ST=California/L=Cupertino/O=Apple Inc./CN=gateway.push.apple.com i:/C=US/O=Entrust,
Inc./OU=www.entrust.net/rpa is incorporated by reference/OU=(c) 2009
Entrust, Inc./CN=Entrust Certification Authority - L1C 1
s:/C=US/O=Entrust, Inc./OU=www.entrust.net/rpa is incorporated by
reference/OU=(c) 2009 Entrust, Inc./CN=Entrust Certification Authority
- L1C i:/O=Entrust.net/OU=www.entrust.net/CPS_2048 incorp. by ref. (limits liab.)/OU=(c) 1999 Entrust.net Limited/CN=Entrust.net
Certification Authority (2048)
— Server certificate
—–BEGIN CERTIFICATE—– MIIFIzCCBAugAwIBAgIETCMvvjANBgkqhkiG9w0BAQUFADCBsTELMAkGA1UEBhMC …
… de7tGJEmdBgmtX2Wn0r0Erg+9nnjOEQ=
—–END CERTIFICATE—– subject=/C=US/ST=California/L=Cupertino/O=Apple
Inc./CN=gateway.push.apple.com issuer=/C=US/O=Entrust,
Inc./OU=www.entrust.net/rpa is incorporated by reference/OU=(c) 2009
Entrust, Inc./CN=Entrust Certification Authority - L1C
— Acceptable client certificate CA names /C=US/O=Apple Inc./OU=Apple Certification Authority/CN=Apple Root CA /C=US/O=Apple Inc./OU=Apple
Worldwide Developer Relations/CN=Apple Worldwide Developer Relations
Certification Authority /C=US/O=Apple Inc./OU=Apple Certification
Authority/CN=Apple Application Integration Certification Authority
— SSL handshake has read 3144 bytes and written 2168 bytes
— New, TLSv1/SSLv3, Cipher is AES256-SHA Server public key is 2048 bit Secure Renegotiation IS supported Compression: NONE Expansion:
NONE SSL-Session:
Protocol : TLSv1
Cipher : AES256-SHA
Session-ID:
Session-ID-ctx:
Master-Key: 9E737F1BD1AC1D41A2D6486B9F2DD08EF3B87EC9A91EBB42396D9664B34FF534DA2DCF4A897688ECBEFE00B069C481BC
Key-Arg : None
Start Time: 1452222166
Timeout : 300 (sec)
Verify return code: 0 (ok)
在 Xcode 中调试 App 时,当我们使用 NSLog、printf 语句时,可以向控制台打印一些信息。
如果 App 是以 release 的方式安装(无论以何种方式:App Store、Ad-Hoc 或者 OTA)到设备上,则只有 NSLog 语句打印的信息能够通过 Xcode 的 Devices 进行查看。
如果没有安装 Xcode,则可以通过 iTunes 的文件共享功能查看控制台日子,方法如下(只能查看 NSLog 输出的日志)。
在 application(_, didFinishLaunchingWithOptions:) 方法中加入代码:
let documents = NSHomeDirectory().stringByAppendingPathComponent("Documents")
let logFileName = "\(NSDate()).log"
let logFilePath = documents.stringByAppendingPathComponent(logFileName)
freopen(logFilePath.cStringUsingEncoding(NSASCIIStringEncoding)!, "a+", stderr)
打开 iTunes,连上设备,通过“应用 / 文件共享”将日志文件下载到 Mac。
在 iOS 9.x 的“设置>定位服务”中,甚至看不到 App 的名字。奇怪的是,在 iOS 8 中却正常。这是因为代码中的 requestAlwaysAuthorization/requestWhenInUseAuthorization 方法和 Info.plist 中的 NSLocationAlwaysUsageDescription/NSLocationWhenInUseUsageDescription 不对应。例如,在代码中使用的是 requestAlwaysAuthorization(总是使用定位服务,哪怕在后台),而 Info.plist 中却只声明了 NSLocationWhenInUseUsageDescription(只有 App 在前台时才使用定位服务),没声明 NSLocationAlwaysUsageDescription 字串。
所以最好是在 Info.plist 文件中同时声明 NSLocationAlwaysUsageDescription 和 NSLocationWhenInUseUsageDescription 字串,这样无论调用的是 requestAlwaysAuthorization 还是 requestWhenInUseAuthorization 方法,都不会出错。
iOS 8下没有问题。在 iOS 9 中,用 presentOptionsMenuFromXXX 方法代替。
这种情况下,必须用 self.tabBarController?.title=”xxx” 来修改导航栏标题。
同理,要修改右按钮使用 :
self.tabBarController?.navigationItem.rightBarButtonItem=UIBarButtonItem(customView: btn)
Product -> Scheme -> Edit Scheme -> Options 中,勾选 Allow Location Simulation ,同时在这里提供一个模拟器坐标,不能是 “none”。
在要用到这个单元格的 ViewController 中,在 viewDidLoad 方法(或者别的什么地方)中加入:
self.tableView.registerNib(UINib(nibName: “CustomCell”, bundle: NSBundle.mainBundle()), forCellReuseIdentifier: “CustomCellIdentifier”)
然后在 tableView(_, cellForRowAtIndexPath:) 方法中:
let cell = tableView.dequeueReusableCellWithIdentifier(“CustomCellIdentifier”) as! CustomCell
Apple WWDR 证书过期(2016年2月15日过期)。
原文:http://blog.csdn.net/kmyhy/article/details/53991385