2014年12月9日 星期二

啟用Watch App Simulator

目前測試 apple 最新的 apple watch 可以透過XCode 6.2 beta版

apple watch的project是要綁在手機上的專案,

所以watch app其實只是手機app的延伸,

若你今天要啟用apple watch simulator的話,

只要在xcode的target選擇watch app




然後按下compile

模擬器就會跑出來囉!

若你發現只有手機模擬器出現的話,

你可以在模擬器的工具列

Hardware→External Displays→選到最下方的Apple Watch

這樣Apple watch的simulator就會跑出來囉!

   


2014年11月25日 星期二

git SSL certificate problem: self signed certificate

今天同事使用Git 發生了

Git Pull Failed: fatal: unable to access 'https://git.xxxx.xxx.xx.xxx.git/': SSL certificate problem: self signed certificate

的問題

去你的Repoistory的.git內執行

git config http.sslVerify false

詳情可以參考

http://stackoverflow.com/questions/11621768/how-can-i-make-git-accept-a-self-signed-certificate

2014年11月18日 星期二

Git "....java did not match any file(s) known to git..."問題

今天使用IntelliJ IDEA的Git pull 與 push時,

一直出現以下的錯誤

Error:error: pathspec 'src/com/aaaa/bbbb/cccc/dddd/DMF.java' did not match any file(s) known to git.
  during executing git commit --only -F /private/var/folders/6h/zv5mh4px15d6yjksw693q_s40000gp/T/git-commit-msg-5225757047314811222.txt -- src/com/aaaa/bbbb/cccc/dddd/DMF.java

苦惱了一個上午,

最後找到原因是因為IDEA的IDE透逗怪怪的,

只要把該檔案做commit 但不要用IDE去做

去terminal下指令


git commit -m commitMessage


再用IDE或指令去pull  or push

這樣就可以正常囉


2014年9月16日 星期二

Swift 筆記[ 2014.10.06更新]

1. 語句不強制加

2. 可不用宣告明確的型別,var用來宣告變數let用來宣告常數

若要宣告明確的型別可以下寫法:

 let explicitDouble:Double = 70

3. 變數無法直接做隱式轉換,若想要轉換變數到不同的型態,
需明確指定要轉什麼型態。

ex:
let label = "this is label"

 let width = 99

 let labelWidth = label + String(width)

4. 以上的寫法之外,可以用更簡易的方式來轉成字串

ex:
let apple = 3
let oranges = 5
let appleSum = "I have \(apple) apples."
let fruitSummary = "I have \(apple + oranges) pieces of fruit."

5. 建立array的方式與一般程式語言一樣,利用[] 與index來存取

ex:

var shoppingList =["fish", "water", "fruit"]
shoppingList[1] = "bottle of water"

ex:

var occupation = [
"Malcolm":"Captain",
"Kaylee":"Mechanic",
]

occupation["Jayne"] = "Public Relations"

6. 建立空的array與dictionary 利用以下初始化語法:

let emptyAry = [String]()

let emptyDictionary = [String: Float]()

7. 如果型態資訊被推論,你一個空的array如[] 和空的dictionary如[:]

利如你設立一個新的值給變數,或傳一個參數到function

ex:
shppingList = []
occupations=[:]

宣告一個array變數:
ex: 
var myStringAry:[String] =   [“A","B","C","D"];

var 

myStringAryOther:Array=["A","B","C","D"];



8. 與一般程式語言不同,if else 與 for-in , for, while, do-while不用加左右括號()

ex:
let individualScores=[9,7,5,3,1,2,4,6,8]
var teamScore = 0
for score in individualScores{
    if score > 4{
          teamScore+=3
    }
   else
   {
         teamScore+=1
   }
}

teamScore

註:直接寫teamScore是在playground內直接看到內容值的方式


9. 在 if 敘述句內,這個敘對敘述句必需是Boolean描述,
像 if score {...}是錯誤的描述

10. swift整數分為8,16,32,64 位元的有號無號整數型別,如

UInt8、Int32

11.  取得整數最小值與最大值範圍,可用 min與max屬性取得

ex:
let minValue = UInt8.min
let maxValue = UInt8.max

12.  通常你不需特別去選擇整數的size在你的程式碼,swfit提供Int,這個型態
的常度會自已對應與你現在的平台整數長度相同。即使在32Bit的平台下,Int
也能夠儲存 -2,147,483,648 ~ 2,147,483,647的範圍。

13.  UInt 也另可分為UInt32、UInt64,長度也會依目前的平台對應。在swift非必要
盡量不要使用UInt,除非你真的需要目前平台的無號整數的長度。除了這個情況,
最好使用Int,即使知道這是非負整數,統一使用Int可提高程式碼的重用性,避免
不同型別轉換,與匹配int type的推斷。


14.  浮點數範圍比Int更大。浮點數分為:Double、Float。
Double:代表64 bit浮點數。當你需要儲存很大或很高精度的浮點數。
Float:代表32 bit浮點數。精度要求不高可以使用此型別。
Double至少有15位數,而Float至少有6位數,選擇哪個型別取決於你的程式碼處理
範圍。


15.  swift 是type safe的語言,會依你的變數型別去type checks。並且提供
type inference(型別推斷)。有了type inference就很少需要去宣告型別。
常數與變數雖需要明亮的型別,但大部份不需要由開發者自已完成。
例如

let meaningOfLife = 42。

這指meaningOfLife為Int型別

let pi = 3.14159

這指pi自已為Double型別。通常swift浮點數在推斷型別時,會選擇Double而非Float。

let anotherPi = 3 + 0.14159

此型別則為Double,因為3沒有明確指出是什麼型別。

16. 整數可以寫成

    十進位:沒有前綴。
    ex: let decimalInteger = 17

    二進位:前綴是0b
   ex: let binaryInteger = 0b1001   // 表示17

   八進位:前綴是0o
   ex: let octalInteger = 0o21     // 表示17

  十六進位:前綴是0x
  ex: let hexadecimalInteger = 0x11  // 表示17

17. 浮點字面可以是十進位或16進位(0x),小數點兩邊至少有一個十進位或十六進位。
浮點數有一個optional的指數(exponent),在十進位浮點數透過大寫或小寫的e來指定。
十六進位則透過p來指定。

ex:

1.25e2表示1.25x10^2等於125.0
1.25e-2表示

16進位指數表示:

ex:
0xFp2 表示 15x2^2  等於60.0
0xFp-2 表示 15x2^-2 等於3.75

18. 數值和浮點數可以添加額外的零底線,不會影響數值。

ex:

let paddedDouble = 000123.456
let oneMillion = 1_000_000
let justOverOneMillion=1_000_000.000_000_1


19. 通常即使知道整數變數或常數是已知的非負數,也請用Int型別。

20. 不同型別的整數可以儲存不同的範圍。

    Int8:可儲存範圍 -128~127 
    UInt8: 可儲存 0~255

ex:

let cannotBeNegative: UInt8 = -1
// 會報error 不能存負數
let tooBig:Int8 = Int8.max +1
// 超過最大數值會報錯


21. 每個整數可儲存不同範圍值,必須在程式依不同情況選擇數值型型別轉換,
可預防隱式轉換的錯誤。

22. 以下例子twoThousand是 UInt16 type,而one是 UInt8 type,兩者不能相加,

需將one轉換成UInt16才可相加。

ex:

let twoThousand: UInt16  = 2_000
let one: UInt8 = 1
let twoThousandAndOne = twoThousand + UInt16(one)

twoThousandAndOne則被推斷為UInt16的type,因為兩個UInt16 type值相加。

23. SomeType(ofInitialValue)是呼叫UInt16一個初始化的函式,可接受UInt8的型別值。
這個初始化可以讓現有的UInt8創建一個新的UInt16 type。

24. 整數與浮點數必須做顯示指定型別轉換。
ex:

let three = 3
let pointOneFourOneFiveNine = 0.14159
let pi = Double(three) + pointOneFourOneFiveNine

25. 浮點數轉整數。
ex:

let integerPi = Int(pi)

26. 可以利用typealias來定義型別別名

ex:
typealias AudioSample = UInt16
var maxAmplitudeFoud = AudioSample.min
// maxAmplitudeFoud 現在為0

AudioSample就是UInt16

27. Swift的布林型態為Bool ,供兩種值分別為 true , false。與Int , Double都有type inferred

28. Tuples 是多個值組合為一個複合值,tuple內可以任意type

ex:
let http404Error = (404, "Not Found")
這個tuple 型別是(Int String)

tuple可以是任意的type如(Int, Int,Int),也可以將一個tuple分解成單獨的常數和變數

let (statusCode, statusMessage) = http404Error

println("The status code is \(statusCode)" )
// output : The status code is 404

println("The status message is \(statusMessage)")
// output: The statusMessage is Not Found


29. 如果你只要tuple一部份的內容,你可以用 _ 來標記

ex:

let (justTheStatusCode, _ ) = http404Error
println("The status code is \(justTheStatusCode)")

30. 你也可以用索引值來取tuple的單個元素,索引從零開始

ex:

println("The status code is \(http404Error.0)")
println("The status message is \(http404Error.1)")

31. 你可以為tuple中的元素命名,透過名字來取得值

ex:

let http200Status = (statusCode:200, description:"OK")

取值

println("The status code is \(http200Status.statusCode)")
println("The status message is \(http200Status.description)")

tuple用來網路回傳值非常有用,一次可以包含兩種不同的type。

31. Optional 可以讓你表示任意型別的值不存在,不需要一個特殊的值。

ex:
String裡的toInt方法用來轉換型別至Int,但"hello world"無法轉成數字

let possibleNumber = "123"
let convertNumber = possibleNumber.toInt()
//回傳 Int?

Int? 代表是一個Optional Int,表示可能包含Int值也可能不包含值。


32. 你可以設置一個optiional變數為沒有值的狀,可以指派為nil.
ex:
var serverResponseCode:Int? = 404
serverResponseCode = nil

nil無法被使用在非optional的常數或變數,如果你的學數或變數在你的code可能會沒有值的
情況,通常宣告為optional值比較恰當。


如果你設置了optional的常數或變數沒有提供預設值 ,這個常數或變數會自動指派nil

ex:
var surveyAnswer:String?
// suveryAnswer會自動指派nil

33. swift的nil與objective-c 的nil不同,nil是指出不存在的物件,在swift nil不是pointer,是指不存的值或確切型態。任何型態的Optionals都可以設為nil,不是只有object型態。

34. 若確定optional有值的話,可以用 !來取值,代表確定這個optional有值做強制解析(forced unwrapping)
ex:
if convertedNumber != nil
{
 println("convertedNumber has an integer value of \(convertedNumber!) .")
}

要確保 optional有值不為nil再使用 !,不然會產生runtime error

35. options bindings可以用if與while來對optional判斷,把值傳給一個常數或變數。

ex:

if let constantName = someOptional
{
statements
}

ex:

if let actualNumber = possibleNumber.toInt()
{
  println("\(possibleNumber) has an integer value of \(actualNumber)")
}
else
{
  println("\(possibleNumber) could not be converted to an integer")
}
// prints "123 has an intger value of 123"

 這是指如果Optional Int回傳optional Int包含一個值則創一個新的常數叫actualNumber,並將optional的值給他。actualNumber被用來輸出轉換結果,這裡不用再用!來抓取他的值。

你也可以改用if var actualNumber取代 ,存成變數。


// Implicitly Unwrapped Optionals



[未完 續待]

2014年9月15日 星期一

Android 分享文字訊息至Line

Android 指定開Line App傳入文字訊息

通常Android要開啟第三方App需知道對方app的package name


 PackageManager pm = mContext.getPackageManager();
        List<applicationinfo>  appList =  pm.getInstalledApplications(0);
        for(ApplicationInfo app: appList )
        {
            Log.i("info","app:" +  app );
            if( app.packageName.equals(LINE_PACKAGE_NAME))
            {

                return true;
            }
        }

上面的方法 可以抓出手機內所有的App package name

而你會發現Line的App Package Name的名稱是 jp.naver.line.android

故當程式可用此方式來檢查是否裝此app

若要從程式碼傳送文字至 Line訊息的話


intent.setAction(Intent.ACTION_SEND);
intent = mContext.getPackageManager().getLaunchIntentForPackage(AppConfig.LINE_PACKAGE_NAME);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "this is test! oh ya!");
mContext.startActivity(intent);

上面是送文字訊息

而AndroidManifest.xml 記得要加接收到ACTION_SEND的intent filter


<intent-filter >
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter >


Xcode刪除暫存檔

XCode是吃資源的怪物

由於我只買得起macbook air 128GB的容量

久了發現無緣無故硬碟空間就沒了

查了發現XCode的暫存檔佔了不小的容量

可至下方路徑

 資源庫 ▸ Developer ▸ Xcode ▸ DerivedData

將DerivedData內容刪除暫存檔



2014年9月1日 星期一

[android]BaseAdapter 刷新資料避免重覆內容

這幾天在Android遇到了這個問題

就是在自訂Android的BaseAdapter的時候

getView重覆進去跑了

查到有人說

listview的layout 長寬要設定為match_parent


另一種是說ListView要更新

yourAdapter.notifyDataSetChanged();

但結果還是一樣

因為我在getview去動態產生內容

所以要去把之前的layout刪除 

才不會有重覆的內容產生在listview



 @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewTag viewTag;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.layout_item, null);
            viewTag = new ViewTag((TableLayout) convertView.findViewById(R.id.tableLL),
                    (TextView) convertView.findViewById(R.id.question));
            convertView.setTag(viewTag);
        }
        else
        {
            viewTag = (ViewTag) convertView.getTag();
            viewTag.tableLL.removeAllViews();
        }

2014年8月9日 星期六

Xcode trace 方法名稱

為了避免方法名稱修改

在NSLog內的字串也要修改會造成麻煩

可以直接寫

NSLog(@"%@", NSStringFromSelector(_cmd));
_cmd在objective-c都為一個SEL

這樣就可以trace method 的名稱

2014年7月28日 星期一

UIWebview 的javascript與ios objective-c互動傳參數(Ⅱ)

繼4個月前的一篇【UIWebview 的javascript與ios objective-c互動傳參數

有人問到要如何從objective-c呼叫網頁的function並回傳值

以下為沒有傳參數的方法,

傳參數給網頁的function請看

UIWebview 的javascript與ios objective-c互動傳參數】 此篇


若要從objective-c去呼叫網頁javascript並回傳值的話

有兩種作法

一種就是再用function去呼叫丟objective-c內的的function

此方法較複雜也較麻煩  此方法可用前篇 在網頁呼叫callObjFunction的function

這是其中一種作法。

另種作法就最單純簡單 

就直接在javascript使用return 就可以傳回到objective-c內

例:

webview的程式碼:


<!DOCTYPE html>
<html>
    <head>
        <script>
            
        function callObjFunction()
        {
        
         return "I will return from js web";   
        }
       </script>
    </head>

    <body>
        
    </body>
</html>

Objective-c程式關鍵部份:



 NSString *str = [_theWebview stringByEvaluatingJavaScriptFromString: [NSString stringWithFormat:@"callObjFunction()"]];
    NSLog(@"str:%@", str);


這樣就可以看見log有I will return from js web的字樣

發文附圖:


範例程式: GitHub



2014年7月24日 星期四

Facebook新增取得birthday, photo..等權限

最近同事在研究取得FB資訊

他使用PHP來抓取資訊時

始終抓不到birthday, photo等進一步的資訊

一開始心理一直想應該是permission問題

但是在Facebook 上面的APP 一直就找不到新增其它權限的選項

以前玩明明就有這項東西存在

後來和同事討論找解答後

發現要從

Graph API Explorer裡面去設定

先選到你facebook 的app

點選 Get Access token


選擇你要額外開的權限




選好後 可輸入你的token下語法submit會回傳取到的json格式與內容


2014年7月17日 星期四

Mac啟用apache 安裝mySQL與phpmyadmin

Mac OS X 10.9.4啟用apache

執行

sudo apachectl start

檔案根目錄位於

/Library/WebServer/Documents/

啟用php功能



/etc/apache2/httpd.conf

搜尋

LoadModule php5_module libexec/apache2/libphp5.so

去掉前面的註解

重啟apache



sudo apachectl restart


撰寫php程式於根目錄 至localhost/檔名.php 即可


安裝mySQL

下載mySQL對應的版本

執行安裝 三個都要安裝

再打開系統偏好設定MySQL 啟用MySQL

可修改mySQL的環境變數 以便之後下指令


cd ; vi .bash_profile

貼入此內容


export PATH="/usr/local/mysql/bin:$PATH"


點選:wq存檔

重載shell 執行指令


source ~/.bash_profile

可用 mysql -v來測試是否成功,

接下來為mysql的root帳號設定登入密碼

執行


mysqladmin -u root password '你要登入mysql的密碼'


可以參考此blog設定

再來安裝phpmyadmin

上篇blog另提到要修復2002 socket錯誤

加入


sudo mkdir /var/mysql

sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock




下載phpMyAdmin all language版

解壓縮完 資料夾改名phpmyadmin

網址輸入 http://localhost/phpmyadmin

即可看見登入頁面

預設帳號:root
密碼為您安裝mySQL設定的密碼

登入若看見 “設定檔案現在需要密碼(passphrase)(blowfish_secret).

就到phpmyadmin資料夾內

找到 config.sample.inc.php改成config.inc.php

確認


$cfg['blowfish_secret'] = ‘這是cookie的內容隨便打  ’; /* YOU MUST FILL IN THIS FOR COOKIE AUTH! */

重新登入phpmyadmin就會看不見此問題了

推薦XCode外掛套件 - ColorSense-for-Xcode

ColorSense-for-Xcode 為一款XCode的顏色選擇套件,

只要打入[UIColor 後面就會跳出顏色面版讓你選擇

不用再自行輸入色彩代碼

如圖



安裝使用方式

只要下載官網專案檔

使用xcode開啟專案  compile 成功後

程式會自動在你的

~/Library/Application Support/Developer/Shared/Xcode/Plug-ins


加入外掛


目前官網說明在 OS X 10.8 with Xcode 4.4.1 and 4.5.的環境測試過


而我是在OS X 10.9.4 withXCode 5.1 and Xcode 6 Beta3 測試皆可以使用。

2014年6月12日 星期四

navigation pushViewController返回方式

若目前有

A(Root) → B → C → D

四個UIViewController,

每個都是使用navigation 的 push的做action,

今天你在B、C或D 要點擊按鈕回到A(Root)

在按鈕事件下


[self.navigationController popToRootViewController]

就可以回到A的Root。

返回某個指定的UIViewController 採用index定位的話,

就下


[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:2] animated:YES];

若要用class name去指定跳到的UIViewController


for (UIViewController *controller in self.navigationController.viewControllers) {
    if ([controller isKindOfClass:[你要跳到的UIViewController的class名稱  class]]) {
        [self.navigationController popToViewController:controller animated:YES];
    }
}

這樣就可以返回你想要返回的地方囉~

2014年5月28日 星期三

Apple開發者帳號 統編申請方式

若公司要購買開發者帳號,但需公司統編附在發票上的話,

申請的流程如下:

1.  先刷卡附款。

2.  附款成功後,獲得訂單編號。

3.  打電話與台灣客服聯繫 0800-020-021 ,選擇國語 按4

     告知需要開的統一編號,與發票需打的公司名字,

    只要把需求跟台灣客服說他們就會幫我們處理。


所以不用擔心網頁上沒有可以輸入統編的問題~


2014年4月24日 星期四

一條MOD網路線同時上網與看MOD

記錄一下 新租屋處個問題

房東只提供一條網路孔來接MOD

要上網時需把MOD的網路線拔至電腦連線

此時有一個方式,但你要先知道中華電信數據機的PPPoE帳密

準備一台Switch Hub

將MOD的線先接到hub,hub的孔再用另條網路線接上到MOD

hub其他孔就可以直接接電腦上網

但如果你要接到無線網路的話

此時你要再準備一台 無線路由器

從hub的孔接到 無線路由器的輸入

無線路由器的輸出接到電腦  用電腦先連到 無線路由器裡(看說明書連到192.168.0.1)

輸入帳密後  設定PPPoE方式 選動態IP

再輸入PPPoE的帳密  再設定無線網路連線 即可囉~


2014年4月23日 星期三

GoogleMap API error - [animateToCameraPosition... ]


今天要再用map時一直出現下面錯誤


[GMSMapView animateToCameraPosition:]: unrecognized selector sent to instance 

Code與官網一樣

最後發現

原來是少加了

-ObjC



這樣就可以順利發佈了

2014年3月19日 星期三

Android Webview javascript與Java 互動傳參數

重點是 要先設定

webview.setWebChromeClient(new WebChromeClient());
webview.addJavascriptInterface(new JavaScriptInterface(), "android");

於 HTML 的javascript加入

function executeFromObjCall(str)
{
    alert("execute from objective c call params:" + str);
            
}


然後在你的java檔加入

webview.loadUrl("javascript:executeFromObjCall('passValue');");


這樣就可以呼叫webview javascript檔

並由java傳值到javascript

 若要從javascript呼叫java的code的話

 我們先在javascript去呼叫某個function

如call callJavaFunction:


function callJavaFunction()
{                
   var str = window.android.callJavaMethod();
   alert(str);
}


在java code去新增

 public class JavaScriptInterface {
      public String callJavaMethod() {
           
       Log.i("info", "called from javascript execute in java");
       return "this is return params from java";
   }
}

JavaScriptInterface 是在上面有new JavaScriptInterface()的關係~ 

這樣就可以透過javascript呼叫java並回傳java的值到javascript了


2014年3月17日 星期一

UIWebview 的javascript與ios objective-c互動傳參數

UIWebview與Javascript互傳資料 可以參考此篇

我覺得寫的還不錯

 我們先來說說 Javascript 去 呼叫Objective-c 的function

 在javascript網頁建立一個HTML檔,


<!DOCTYPE html>
<html>
    <head>
        <script>
            
        function callObjFunction()
        {
        var jsParam = "paramFromJs";
        window.location  = 'js-call:runObjMethod:';
            
        }
       </script>
    </head>

    <body>
        <button onclick="callObjFunction()">call obj function</button>
    </body>
</html>
然後在我們的xcode裡objective-c 建立被呼叫的function
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSString *urlString = request.URL.absoluteString;
    // 判斷點下的網址是否有js-call,更精確應該再比對切出來的字串是否有runObjMethod
    if( [urlString hasPrefix:@"js-call:"])
    {
        [self runObjMethod];
        
        return NO;
    }
    return YES;
    
}

-(void) runObjMethod
{
    NSLog(@"this is objective-c method");
}
p.s. 如果你要javascript帶傳參數到objective-c的話,就js-call:param1:param2 然後去parse出param1,param2的內容就可以~ 若要回傳參數的話(objective -> javascript) 我們先把原本的HTML的javascript 內新增
function jsFunction(str)
{
    alert("this is javascript alert! -> obj2 pass value to js show:"+str);
}

在objective-c內的runObjMethod function下加入
// 方法1
NSString *myval = @"my parameters from objective-c";
// javascript 的function 名稱 與obj變數組合成字串
    NSString  *jsMethod = [NSString stringWithFormat:@"%@%@%@", @"jsFunction('", myval, @"')"];

   [_myWebView stringByEvaluatingJavaScriptFromString:jsMethod];
// 或    
// 方法2
    [_myWebView stringByEvaluatingJavaScriptFromString: [NSString stringWithFormat:@"jsFunction('%@')",myval ]];
這樣就可以將objective-c的參數 傳到UIWebview裡面的網頁囉~