顯示具有 ios 標籤的文章。 顯示所有文章
顯示具有 ios 標籤的文章。 顯示所有文章

2014年1月27日 星期一

How to Change the Workgroup in Mac OS X for Easy Networking

When you are creating your home network and you need to add computers with different operating systems, one thing you must do, to make networking and sharing easier, is to change the workgroup. While we’ve shown how to do this in Windows, we did not show how its done in Mac OS X. In the future we plan to update our series on networking Mac OS X with Windows, so let’s begin by showing how to change the workgroup in Mac OS X.
NOTE: This tutorial was created using OS X Mountain Lion (10.8).

Set the Workgroup in the Network Pane

First, you need to open System Preferences. Then, look for the Internet & Wireless section and click Network.
Change the workgroup in Mac OS X
The Network pane displays a list with your network connections. Select the active network connection, the one for which you want to change the workgroup.
Click Advanced and you are shown the properties of the selected network connection.
Change the workgroup in Mac OS X
Go to the WINS tab. WINS comes from Windows Internet Name Service and it is Microsoft's implementation of NetBIOS Name Service (NBNS) - a name server and service for NetBIOS computer names. The settings you make in this tab impact your network connection with Windows based devices and computers.
In the Workgroup field, type the new workgroup name you want to use. It must be the same name used by other computers in the network, to facilitate easy sharing of folders and devices.
Change the workgroup in Mac OS X
When done, click OK and you are back to the Network pane. Then, click Apply and your workgroup settings are applied.

2014年1月23日 星期四

Apple Push Nofification service SSL Certificates

iOS App 有內建的推播可以用, 首先要設定App 允許推播. 前置準備是要弄一台 Apache + Php 來送資料到 Apple.

Step 1: 進入 iOS Developer Center, 點 Cerfificates


Step 2: 點 "Edit"


Step 3: Push Notifications 打勾


Step 4: 按下面的步驟可以產生 CSR, 產生好後按 "Continue"


Step 5: 上傳 CSR 檔案,並按下 Generate


Step 6: 下載certificate
說明: 這個 certificates 分 development 和 product, develop 的好像3個月會過期,要重新 generate 並 download 一次.


Q: Step 5: 上傳 CSR 檔案,並按下 Generate
development跟product需要從分別不同的CSR嗎?還是同一個CSR就可以了 A: 還是同一個CSR就可以了 .


接下來的步驟,只能在 Mac 上執行.接棒給Mac.
(其實: 從頭到尾通通在 Mac 上做就ok了.)
==========================

Step 7: 進入  keychain 匯出 Push Services key 為 .p12 的檔案
(有2種,Development & Production 的 certificates, 有要測 development  的話請匯出2次。 )



Step 8: Save as 輸入檔名,點  "Save"

輸入  password


Step 9: 產生 pem 檔案:
openssl pkcs12 -clcerts -nokeys -out cert.pem -in ios_push_cert.p12
openssl pkcs12 -nocerts -out key.pem -in ios_push_cert.p12
openssl rsa -in key.pem -out key.unencrypted.pem
cat cert.pem key.unencrypted.pem > ck.pem


production指令範例:
openssl pkcs12 -clcerts -nokeys -out cert_production.pem -in ios_push_cert_production.p12
openssl pkcs12 -nocerts -out key_production.pem -in ios_push_cert_production.p12
openssl rsa -in key_production.pem -out key.unencrypted_production.pem
cat cert_production.pem key.unencrypted_production.pem > ck_production.pem


測試 sandbox push server 是否可以被連到:
telnet gateway.sandbox.push.apple.com 2195

測試 sandbox push server 是否可以使用 SSL 憑證連到:
openssl s_client -connect gateway.sandbox.push.apple.com:2195 -cert pushtestcert.pem -key pushtestkey.pem


收到的推播測試訊息:



browser 執行畫面:

badge 數字顯示:


notification:




xcode 裡的程式碼:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSLog(@"Registeringfor push notifications...");
    [[UIApplication sharedApplication]
     registerForRemoteNotificationTypes:
     (UIRemoteNotificationTypeAlert |
      UIRemoteNotificationTypeBadge |
      UIRemoteNotificationTypeSound)];
    
    return YES;
}

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    NSString *str = [NSString
                     stringWithFormat:@"%@",deviceToken];
    NSLog(@"Device Token:%@",str);
}

- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)err
{
    NSLog(@"didFailToRegisterForRemoteNotificationsWithError:%@",[err description]);
}

- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    /*
    for (id key in userInfo) {
        NSLog(@"key: %@, value: %@", key, [userInfo objectForKey:key]);
    }
     */
    NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];
    
    NSString *alertMsg = @"";
    NSInteger badge = 0;
    NSString *sound = @"";
    NSString *custom = @"";
    
    if( [apsInfo objectForKey:@"alert"] != NULL)
    {
        alertMsg = [apsInfo objectForKey:@"alert"];
    }
    
    
    if( [apsInfo objectForKey:@"badge"] != NULL)
    {
        badge = [[apsInfo objectForKey:@"badge"] integerValue];
    }
    if(badge>0)
    {
        // reset badge counter.
        int currentBadgeNumber = [UIApplication sharedApplication].applicationIconBadgeNumber;
        currentBadgeNumber += badge;
        [UIApplication sharedApplication].applicationIconBadgeNumber = currentBadgeNumber;
    }
    
    
    if( [apsInfo objectForKey:@"sound"] != NULL)
    {
        sound = [apsInfo objectForKey:@"sound"];
    }
    
    if( [userInfo objectForKey:@"Custom"] != NULL)
    {
        custom = [userInfo objectForKey:@"Custom"];
    }

}


php 的範例:
<?php
// Put your device token here (without spaces):修改點1
$deviceToken = '740f4707bebcf74f9b7c25d48e3358945f6aa01da5ddb387462c7eaf61bb78ad';
// Put your private key's passphrase here:修改點2
$passphrase = 'passowrd';
// Put your alert message here:修改點3
$message = 'My first push notification!';
////////////////////////////////////////////////////////////////////////////////
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');修改點4 *.pem
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// Open a connection to the APNS server
$fp = stream_socket_client(
 'ssl://gateway.sandbox.push.apple.com:2195', $err,
 $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
 exit("Failed to connect: $err $errstr" . PHP_EOL);
echo 'Connected to APNS' . PHP_EOL;
// Create the payload body
$body['aps'] = array('alert' => $message,'sound' => 'default');
// Encode the payload as JSON
$payload = json_encode($body);
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
if (!$result)
 echo 'Message not delivered' . PHP_EOL;
else
 echo 'Message successfully delivered' . PHP_EOL;
// Close the connection to the server
fclose($fp);
?>

2014年1月20日 星期一

IOS中定时器NSTimer的开启与关闭

调用一次计时器方法:
  1. myTimer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(scrollTimer) userInfo:nil repeats:NO];  
  2. //不重复,只调用一次。timer运行一次就会自动停止运行  
重复调用计时器方法:
  1. timer =  [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(function:) userInfo:nil repeats:YES];  
  2. //每1秒运行一次function方法。  
注意:将计数器的repeats设置为YES的时候,self的引用计数会加1。因此可能会导致self(即viewController)不能release,所以,必须在viewWillAppear的时候,将计数器timer停止,否则可能会导致内存泄露。
停止timer的运行,但这个是永久的停止:
  1. //取消定时器  
  2. [timer invalidate];  

要想实现:先停止,然后再某种情况下再次开启运行timer,可以使用下面的方法:
首先关闭定时器不能使用上面的方法,应该使用下面的方法:
  1. //关闭定时器  
  2. [myTimer setFireDate:[NSDate distantFuture]];  

然后就可以使用下面的方法再此开启这个timer了:
[csharp] view plaincopy
  1. //开启定时器  
  2. [myTimer setFireDate:[NSDate distantPast]];  

例子:比如,在页面消失的时候关闭定时器,然后等页面再次打开的时候,又开启定时器。
(主要是为了防止它在后台运行,暂用CPU)可以使用下面的代码实现:
  1. //页面将要进入前台,开启定时器  
  2. -(void)viewWillAppear:(BOOL)animated  
  3. {  
  4.     //开启定时器  
  5.     [scrollView.myTimer setFireDate:[NSDate distantPast]];  
  6. }  
  7.   
  8. //页面消失,进入后台不显示该页面,关闭定时器  
  9. -(void)viewDidDisappear:(BOOL)animated  
  10. {  
  11.     //关闭定时器  
  12.     [scrollView.myTimer setFireDate:[NSDate distantFuture]];  
  13. }